/** * @file * @brief SPI bus implementation using STM32 HAL. */ #ifndef STA_CORE_STM32_SPI_HPP #define STA_CORE_STM32_SPI_HPP // Only enable module on STM32 platform w/ HAL SPI module enabled #include #ifdef STA_PLATFORM_STM32 # include # ifndef HAL_GPIO_MODULE_ENABLED # error "STM32 GPIO module required!" # endif // !HAL_GPIO_MODULE_ENABLED # ifdef HAL_SPI_MODULE_ENABLED # define STA_STM32_SPI_ENABLED # endif // HAL_SPI_MODULE_ENABLED #endif // STA_PLATFORM_STM32 #if defined(STA_STM32_SPI_ENABLED) || defined(DOXYGEN) #include #include #include #include /** * @defgroup sta_core_stm32_spi SPI * @ingroup sta_core_stm32 * @brief STM32 %SPI module. */ namespace sta { /** * @addtogroup sta_core_stm32_spi * @{ */ /** * @brief STM32 HAL implementation of the `SPI` interface class. */ class STM32SPI : public SPI { public: /** * @brief Handle and corresponding peripheral clock frequency. */ struct Info { SPI_HandleTypeDef * handle; /**< STM32 HAL handle */ uint32_t pclkFreq; /**< Peripheral clock frequency used by interface */ }; public: /** * @param handle STM32 HAL handle * @param pclkFreq Peripheral clock frequency used by %SPI interface * @param mutex Mutex object for managing access. Pass nullptr for no access control */ STM32SPI(SPI_HandleTypeDef * handle, uint32_t pclkFreq, Mutex * mutex = nullptr); /** * @param info Interface info * @param mutex Mutex object for managing access. Pass nullptr for no access control */ STM32SPI(const Info & info, Mutex * mutex = nullptr); void transfer(uint8_t value) override; void transfer16(uint16_t value) override; void transfer(const uint8_t * buffer, size_t size) override; void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) override; void receive(uint8_t * buffer, size_t size) override; void fill(uint8_t value, size_t count) override; private: SPI_HandleTypeDef * handle_; /**< STM32 HAL handle */ }; /** * @brief STM32 HAL implementation of the `SPIDevice` class. */ class STM32SPIDevice : public SPIDevice { public: /** * @param intf %SPI interface * @param csPin Device CS pin */ STM32SPIDevice(STM32SPI * intf, STM32GpioPin * csPin); }; /** @} */ } // namespace sta /** * @brief Get bus info for STM32 %SPI interface via HAL handle. * * Requires STA_STM32__PCLK_IDX to be defined for the MCU. * MCU mappings are found in the sta/stm32/mcu/.hpp files. * * Check the MCUs Reference Manual RCC register documentation to see which * peripheral clock is used. * * @param handle STM32 HAL %SPI handle * * @ingroup sta_core_stm32_spi */ #define STA_STM32_SPI_INFO(handle) sta::STM32SPI::Info{&handle, STA_STM32_GET_HANDLE_PCLK_FREQ_FN(handle)()} /** * @brief Get bus info for STM32 %SPI interface via index. * * Requires STA_STM32_SPI__PCLK_IDX to be defined for the MCU. * MCU mappings are found in the sta/stm32/mcu/.hpp files. * * Check the MCUs Reference Manual RCC register documentation to see which * peripheral clock is used. * * @param n STM32 %SPI interface index * * @ingroup sta_core_stm32_spi */ #define STA_STM32_SPI_INFO_N(n) sta::STM32SPI::Info{&handle, STA_STM32_GET_SPI_PCLK_FREQ_FN(n)()} #endif // STA_STM32_SPI_ENABLED #endif // STA_CORE_STM32_SPI_HPP