From 417ca91352b8230c43a4d624c5046301d3d6d68e Mon Sep 17 00:00:00 2001 From: Martin Vladic Date: Thu, 2 Jul 2020 14:26:06 +0200 Subject: [PATCH] updated spi comm --- src/eez/modules/bp3c/flash_slave.cpp | 18 +++++++++--------- src/eez/platform/stm32/spi.cpp | 23 ++++++++++++++++------- src/eez/platform/stm32/spi.h | 2 +- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/eez/modules/bp3c/flash_slave.cpp b/src/eez/modules/bp3c/flash_slave.cpp index b7b3f7eb4..c415d4f35 100644 --- a/src/eez/modules/bp3c/flash_slave.cpp +++ b/src/eez/modules/bp3c/flash_slave.cpp @@ -116,11 +116,11 @@ 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); @@ -128,7 +128,7 @@ bool waitForAck(int slotIndex) { // 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); @@ -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); @@ -190,7 +190,7 @@ 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); @@ -198,7 +198,7 @@ bool eraseAll(int 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); @@ -259,7 +259,7 @@ 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); @@ -267,7 +267,7 @@ bool writeMemory(int slotIndex, uint32_t address, const uint8_t *buffer, uint32_ 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); @@ -275,7 +275,7 @@ bool writeMemory(int slotIndex, uint32_t address, const uint8_t *buffer, uint32_ 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); diff --git a/src/eez/platform/stm32/spi.cpp b/src/eez/platform/stm32/spi.cpp index fbe2a8230..fbad79ad5 100644 --- a/src/eez/platform/stm32/spi.cpp +++ b/src/eez/platform/stm32/spi.cpp @@ -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) { @@ -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--; } @@ -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--; } @@ -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; } @@ -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) { diff --git a/src/eez/platform/stm32/spi.h b/src/eez/platform/stm32/spi.h index c99a71171..ff3910863 100644 --- a/src/eez/platform/stm32/spi.h +++ b/src/eez/platform/stm32/spi.h @@ -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);