diff --git a/meson.build b/meson.build index b3ec0582..16ff81fe 100644 --- a/meson.build +++ b/meson.build @@ -216,8 +216,8 @@ stm32f405_src = ['platform/mcu/STM32F4xx/boot/startup.cpp', 'platform/mcu/STM32F4xx/drivers/rng.c', 'platform/mcu/STM32F4xx/drivers/rcc.c', 'platform/mcu/STM32F4xx/drivers/i2c_stm32.c', - 'platform/mcu/STM32F4xx/drivers/spi_stm32.c', 'platform/mcu/STM32F4xx/drivers/adc_stm32.c', + 'platform/drivers/SPI/spi_stm32.c', 'platform/drivers/audio/stm32_dac.cpp', 'platform/drivers/audio/stm32_adc.cpp', 'platform/drivers/audio/stm32_pwm.cpp', diff --git a/platform/mcu/STM32F4xx/drivers/spi_stm32.c b/platform/drivers/SPI/spi_stm32.c similarity index 75% rename from platform/mcu/STM32F4xx/drivers/spi_stm32.c rename to platform/drivers/SPI/spi_stm32.c index 832f696c..f5ee5ffc 100644 --- a/platform/mcu/STM32F4xx/drivers/spi_stm32.c +++ b/platform/drivers/SPI/spi_stm32.c @@ -18,6 +18,7 @@ * along with this program; if not, see * ***************************************************************************/ +#include #include #include #include "spi_stm32.h" @@ -30,27 +31,27 @@ static inline uint8_t spi_sendRecv(SPI_TypeDef *spi, const uint8_t val) return spi->DR; } -int spi_init(const struct spiDevice *dev, const uint32_t speed, const uint8_t flags) +int spiStm32_init(const struct spiDevice *dev, const uint32_t speed, const uint8_t flags) { SPI_TypeDef *spi = (SPI_TypeDef *) dev->priv; - uint32_t clkDiv = 0; + uint8_t busId; switch((uint32_t) spi) { case SPI1_BASE: - clkDiv = (RCC->CFGR >> 13) & 0x07; + busId = PERIPH_BUS_APB2; RCC->APB2ENR |= RCC_APB2ENR_SPI1EN; __DSB(); break; case SPI2_BASE: - clkDiv = (RCC->CFGR >> 10) & 0x07; + busId = PERIPH_BUS_APB1; RCC->APB1ENR |= RCC_APB1ENR_SPI2EN; __DSB(); break; case SPI3_BASE: - clkDiv = (RCC->CFGR >> 10) & 0x07; + busId = PERIPH_BUS_APB1; RCC->APB1ENR |= RCC_APB1ENR_SPI3EN; __DSB(); break; @@ -60,15 +61,14 @@ int spi_init(const struct spiDevice *dev, const uint32_t speed, const uint8_t fl break; } - uint32_t apbClk = SystemCoreClock; - if((clkDiv & 0x04) != 0) - apbClk /= (1 << ((clkDiv & 0x03) + 1)); - uint8_t spiDiv; uint32_t spiClk; + uint32_t busClk = rcc_getBusClock(busId); + + // Find nearest clock frequency, round down for(spiDiv = 0; spiDiv < 7; spiDiv += 1) { - spiClk = apbClk / (1 << (spiDiv + 1)); + spiClk = busClk / (1 << (spiDiv + 1)); if(spiClk <= speed) break; } @@ -98,7 +98,7 @@ int spi_init(const struct spiDevice *dev, const uint32_t speed, const uint8_t fl return 0; } -void spi_terminate(const struct spiDevice *dev) +void spiStm32_terminate(const struct spiDevice *dev) { SPI_TypeDef *spi = (SPI_TypeDef *) dev->priv; @@ -125,55 +125,34 @@ void spi_terminate(const struct spiDevice *dev) } int spiStm32_transfer(const struct spiDevice *dev, const void *txBuf, - const size_t txSize, void *rxBuf, const size_t rxSize) + void *rxBuf, const size_t size) { SPI_TypeDef *spi = (SPI_TypeDef *) dev->priv; uint8_t *rxData = (uint8_t *) rxBuf; const uint8_t *txData = (const uint8_t *) txBuf; // Send only - if((rxBuf == NULL) || (rxSize == 0)) + if(rxBuf == NULL) { - for(size_t i = 0; i < txSize; i++) + for(size_t i = 0; i < size; i++) spi_sendRecv(spi, txData[i]); return 0; } // Receive only - if((txBuf == NULL) || (txSize == 0)) + if(txBuf == NULL) { - for(size_t i = 0; i < rxSize; i++) + for(size_t i = 0; i < size; i++) rxData[i] = spi_sendRecv(spi, 0x00); return 0; } // Transmit and receive - size_t txRxSize = (txSize < rxSize) ? txSize : rxSize; - for(size_t i = 0; i < txRxSize; i++) + for(size_t i = 0; i < size; i++) rxData[i] = spi_sendRecv(spi, txData[i]); - // Still something to send? - if(txSize > txRxSize) - { - for(size_t i = 0; i < (txSize - txRxSize); i++) - { - size_t pos = txRxSize + i; - spi_sendRecv(spi, txData[pos]); - } - } - - // Still something to receive? - if(rxSize > txRxSize) - { - for(size_t i = 0; i < (rxSize - txRxSize); i++) - { - size_t pos = txRxSize + i; - rxData[pos] = spi_sendRecv(spi, 0x00); - } - } - return 0; } diff --git a/platform/mcu/STM32F4xx/drivers/spi_stm32.h b/platform/drivers/SPI/spi_stm32.h similarity index 89% rename from platform/mcu/STM32F4xx/drivers/spi_stm32.h rename to platform/drivers/SPI/spi_stm32.h index 67d5c862..d50b96a0 100644 --- a/platform/mcu/STM32F4xx/drivers/spi_stm32.h +++ b/platform/drivers/SPI/spi_stm32.h @@ -23,8 +23,12 @@ #include +#ifdef __cplusplus +extern "C" { +#endif + /** - * Instantiate an STM32 I2C master device. + * Instantiate an STM32 SPI master device. * * @param name: device name. * @param peripheral: underlying MCU peripheral. @@ -32,8 +36,7 @@ */ #define SPI_STM32_DEVICE_DEFINE(name, peripheral, mutx) \ extern int spiStm32_transfer(const struct spiDevice *dev, const void *txBuf, \ - const size_t txSize, void *rxBuf, \ - const size_t rxSize); \ + void *rxBuf, const size_t size); \ const struct spiDevice name = \ { \ .transfer = &spiStm32_transfer, \ @@ -51,13 +54,18 @@ const struct spiDevice name = \ * @param flags: SPI configuration flags. * @return zero on success, a negative error code otherwise. */ -int spi_init(const struct spiDevice *dev, const uint32_t speed, const uint8_t flags); +int spiStm32_init(const struct spiDevice *dev, const uint32_t speed, const uint8_t flags); /** * Shut down an SPI peripheral and driver. * * @param dev: SPI device descriptor. */ -void spi_terminate(const struct spiDevice *dev); +void spiStm32_terminate(const struct spiDevice *dev); + + +#ifdef __cplusplus +} +#endif #endif /* SPI_STM32_H */