2022-05-09 21:19:06 +02:00

134 lines
2.8 KiB
C++

/**
* @file
* @brief Implementations for SpiInterface and SpiDevice using STM32 HAL.
*/
#ifndef STA_STM32_SPI_HPP
#define STA_STM32_SPI_HPP
/**
* @defgroup stm32SPI SPI
* @ingroup stm32
* @brief STM32 SPI module.
*/
#ifdef DOXYGEN
/**
* @def STA_STM32_SPI_ENABLE
* @brief Enable module.
*
* Requires **STM_GPIO** module.
*
* @ingroup stm32BuildConfig
*/
# define STA_STM32_SPI_ENABLE
#endif // DOXYGEN
#include <sta/config.hpp>
#ifdef STA_STM32_SPI_ENABLE
#ifndef STA_STM32_GPIO_ENABLE
#error "STM32 GPIO module required"
#endif // !STA_STM32_GPIO_ENABLE
#include <sta/intf/spi_interface.hpp>
#include <sta/spi_device.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_ENABLE
#endif // STA_STM32_SPI_HPP