Skip to content

Commit

Permalink
updated spi comm
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed Jul 2, 2020
1 parent cd286e4 commit 417ca91
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
18 changes: 9 additions & 9 deletions src/eez/modules/bp3c/flash_slave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,19 @@ bool waitForAck(int slotIndex) {
uint8_t txData = 0;
uint8_t rxData;

spi::select(slotIndex, spi::CHIP_SLAVE_MCU_BOOTLOADER);
spi::select(slotIndex, spi::CHIP_SLAVE_MCU_NO_CRC);
spi::transmit(slotIndex, &txData, 1);
spi::deselect(slotIndex);

spi::select(slotIndex, spi::CHIP_SLAVE_MCU_BOOTLOADER);
spi::select(slotIndex, spi::CHIP_SLAVE_MCU_NO_CRC);
spi::transfer1(slotIndex, &txData, &rxData);
spi::deselect(slotIndex);

if (rxData == ACK) {
// received ACK
txData = ACK;

spi::select(slotIndex, spi::CHIP_SLAVE_MCU_BOOTLOADER);
spi::select(slotIndex, spi::CHIP_SLAVE_MCU_NO_CRC);
spi::transmit(slotIndex, &txData, 1);
spi::deselect(slotIndex);

Expand All @@ -153,7 +153,7 @@ bool syncWithSlave(int slotIndex) {

txData = BL_SPI_SOF;

spi::select(slotIndex, spi::CHIP_SLAVE_MCU_BOOTLOADER);
spi::select(slotIndex, spi::CHIP_SLAVE_MCU_NO_CRC);
spi::transmit(slotIndex, &txData, 1);
spi::deselect(slotIndex);

Expand Down Expand Up @@ -190,15 +190,15 @@ bool eraseAll(int slotIndex) {
txData[1] = CMD_EXTENDED_ERASE;
txData[2] = CRC_MASK ^ CMD_EXTENDED_ERASE;

spi::select(slotIndex, spi::CHIP_SLAVE_MCU_BOOTLOADER);
spi::select(slotIndex, spi::CHIP_SLAVE_MCU_NO_CRC);
spi::transmit(slotIndex, txData, 3);
spi::deselect(slotIndex);

if (!waitForAck(slotIndex)) {
return false;
}

spi::select(slotIndex, spi::CHIP_SLAVE_MCU_BOOTLOADER);
spi::select(slotIndex, spi::CHIP_SLAVE_MCU_NO_CRC);
spi::transmit(slotIndex, buffer, 3);
spi::deselect(slotIndex);

Expand Down Expand Up @@ -259,23 +259,23 @@ bool writeMemory(int slotIndex, uint32_t address, const uint8_t *buffer, uint32_
txData[1] = CMD_WRITE_MEMORY;
txData[2] = CRC_MASK ^ CMD_WRITE_MEMORY;

spi::select(slotIndex, spi::CHIP_SLAVE_MCU_BOOTLOADER);
spi::select(slotIndex, spi::CHIP_SLAVE_MCU_NO_CRC);
spi::transmit(slotIndex, txData, 3);
spi::deselect(slotIndex);

if (!waitForAck(slotIndex)) {
return false;
}

spi::select(slotIndex, spi::CHIP_SLAVE_MCU_BOOTLOADER);
spi::select(slotIndex, spi::CHIP_SLAVE_MCU_NO_CRC);
spi::transmit(slotIndex, addressAndCrc, 5);
spi::deselect(slotIndex);

if (!waitForAck(slotIndex)) {
return false;
}

spi::select(slotIndex, spi::CHIP_SLAVE_MCU_BOOTLOADER);
spi::select(slotIndex, spi::CHIP_SLAVE_MCU_NO_CRC);
spi::transmit(slotIndex, &numBytes, 1);
spi::transmit(slotIndex, (uint8_t *)buffer, bufferSize);
spi::transmit(slotIndex, &crc, 1);
Expand Down
23 changes: 16 additions & 7 deletions src/eez/platform/stm32/spi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ void select(uint8_t slotIndex, int chip) {
uint32_t crcCalculation = (slot.moduleInfo->spiCrcCalculationEnable ? SPI_CRCCALCULATION_ENABLE : SPI_CRCCALCULATION_DISABLE);
WRITE_REG(handle[slotIndex]->Instance->CR1, SPI_MODE_MASTER | SPI_DIRECTION_2LINES | SPI_POLARITY_LOW | SPI_PHASE_1EDGE | (SPI_NSS_SOFT & SPI_CR1_SSM) | slot.moduleInfo->spiBaudRatePrescaler | SPI_FIRSTBIT_MSB | crcCalculation);
handle[slotIndex]->Init.CRCCalculation = crcCalculation;
} else if (chip == CHIP_SLAVE_MCU_BOOTLOADER) {
// CRC calculation should be always disabled for bootloader
} else if (chip == CHIP_SLAVE_MCU_NO_CRC) {
WRITE_REG(handle[slotIndex]->Instance->CR1, SPI_MODE_MASTER | SPI_DIRECTION_2LINES | SPI_POLARITY_LOW | SPI_PHASE_1EDGE | (SPI_NSS_SOFT & SPI_CR1_SSM) | slot.moduleInfo->spiBaudRatePrescaler | SPI_FIRSTBIT_MSB | SPI_CRCCALCULATION_DISABLE);
handle[slotIndex]->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
} else if (chip == CHIP_IOEXP || chip == CHIP_TEMP_SENSOR) {
Expand Down Expand Up @@ -136,7 +135,7 @@ HAL_StatusTypeDef SPI_TransmitReceive(SPI_HandleTypeDef *hspi, uint8_t *pTxData,
pTxBuffPtr += 2;
TxXferCount -= 2;
} else {
*(__IO uint8_t *)&hspi->Instance->DR = (*pTxBuffPtr);
*(__IO uint8_t *)&hspi->Instance->DR = *pTxBuffPtr;
pTxBuffPtr++;
TxXferCount--;
}
Expand All @@ -150,7 +149,7 @@ HAL_StatusTypeDef SPI_TransmitReceive(SPI_HandleTypeDef *hspi, uint8_t *pTxData,
pTxBuffPtr += 2;
TxXferCount -= 2;
} else {
*(__IO uint8_t *)&hspi->Instance->DR = (*pTxBuffPtr);
*(__IO uint8_t *)&hspi->Instance->DR = *pTxBuffPtr;
pTxBuffPtr++;
TxXferCount--;
}
Expand Down Expand Up @@ -184,13 +183,23 @@ HAL_StatusTypeDef SPI_TransmitReceive(SPI_HandleTypeDef *hspi, uint8_t *pTxData,
// Check the end of the transaction

// Control if the TX fifo is empty
while ((hspi->Instance->SR & SPI_FLAG_FTLVL) != SPI_FTLVL_EMPTY);
while ((hspi->Instance->SR & SPI_FLAG_FTLVL) != SPI_FTLVL_EMPTY) {
if (SPI_FLAG_FTLVL == SPI_SR_FRLVL) {
/* Read 8bit CRC to flush Data Register */
READ_REG(*((__IO uint8_t *)&hspi->Instance->DR));
}
}

// Control the BSY flag
while (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_BSY));

// Control if the RX fifo is empty
while ((hspi->Instance->SR & SPI_FLAG_FRLVL) != SPI_FTLVL_EMPTY);
while ((hspi->Instance->SR & SPI_FLAG_FRLVL) != SPI_FTLVL_EMPTY) {
if (SPI_FLAG_FRLVL == SPI_SR_FRLVL) {
/* Read 8bit CRC to flush Data Register */
READ_REG(*((__IO uint8_t *)&hspi->Instance->DR));
}
}

return HAL_OK;
}
Expand All @@ -216,7 +225,7 @@ HAL_StatusTypeDef transfer5(uint8_t slotIndex, uint8_t *input, uint8_t *output)
}

HAL_StatusTypeDef transfer(uint8_t slotIndex, uint8_t *input, uint8_t *output, uint16_t size) {
return SPI_TransmitReceive(handle[slotIndex], input, output, size);
return HAL_SPI_TransmitReceive(handle[slotIndex], input, output, size, 100);
}

HAL_StatusTypeDef transmit(uint8_t slotIndex, uint8_t *input, uint16_t size) {
Expand Down
2 changes: 1 addition & 1 deletion src/eez/platform/stm32/spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static const int CHIP_ADC = 1;
static const int CHIP_IOEXP = 2;
static const int CHIP_TEMP_SENSOR = 3;
static const int CHIP_SLAVE_MCU = 4;
static const int CHIP_SLAVE_MCU_BOOTLOADER = 5;
static const int CHIP_SLAVE_MCU_NO_CRC = 5;

void init(uint8_t slotIndex, int chip);

Expand Down

0 comments on commit 417ca91

Please sign in to comment.