2023-01-31 21:14:02 +01:00

128 lines
2.9 KiB
C++

/**
* @file
* @brief Implementations for SpiInterface and SpiDevice using STM32 HAL.
*/
#ifndef STA_CORE_STM32_SPI_HPP
#define STA_CORE_STM32_SPI_HPP
/**
* @defgroup stm32SPI SPI
* @ingroup stm32
* @brief STM32 SPI module.
*/
// Only enable module on STM32 platform w/ HAL SPI module enabled
#include <sta/config.hpp>
#ifdef STA_PLATFORM_STM32
# include <sta/stm32/hal.hpp>
# 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 <sta/spi/device.hpp>
#include <sta/spi/interface.hpp>
#include <sta/stm32/clocks.hpp>
#include <sta/stm32/gpio_pin.hpp>
namespace sta
{
/**
* @ingroup stm32SPI
* @{
*/
/**
* @brief Get peripheral clock frequency.
*
* @return Clock frequency
*/
using STM32SpiPCLKFreqFn = uint32_t (*)();
/**
* @brief Info related to STM SPI interface.
*/
struct STM32SpiInterfaceInfo
{
SPI_HandleTypeDef * handle; /**< Interface handle */
STM32SpiPCLKFreqFn getPCLKFreq; /**< Getter for peripheral clock used by interface */
};
/**
* @brief Implementation of SpiInterface interface using STM32 HAL.
*/
class STM32SpiInterface : public SpiInterface
{
public:
/**
* @param info SPI interface info
* @param mutex Mutex object for managing access. Pass nullptr for no access control
*/
STM32SpiInterface(const STM32SpiInterfaceInfo & 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;
const SpiSettings & settings() const override;
private:
STM32SpiInterfaceInfo info_; /**< SPI interface info */
};
/**
* @brief Implementation of SpiDevice interface using STM32 HAL.
*/
class STM32SpiDevice : public SpiDevice
{
public:
/**
* @param intf SPI interface
* @param csPin Device CS pin
*/
STM32SpiDevice(STM32SpiInterface * intf, STM32GpioPin csPin);
private:
STM32GpioPin csPin_; /**< Device CS pin */
};
/** @} */
} // namespace sta
/**
* @brief Get SPI interface info struct for STM32 HAL handle.
*
* Requires STA_STM32_<handle>_PCLK_IDX to be defined for the MCU.
* MCU mappings are found in `core` -> sta/mcu/.hpp files.
*
* Check the MCUs Reference Manual RCC register documentation to see which
* peripheral clock is used.
*
* @param handle SPI interface handle
*
* @ingroup halSPI
*/
#define STA_STM32_SPI_INFO(handle) sta::STM32SpiInterfaceInfo{&handle, STA_STM32_GET_HANDLE_PCLK_FREQ_FN(handle)}
#endif // STA_STM32_SPI_ENABLED
#endif // STA_CORE_STM32_SPI_HPP