Implement changes to SPI interfaces

This commit is contained in:
Henrik Stickann
2022-04-19 23:22:04 +02:00
parent a468133c97
commit 3041499ce0
2 changed files with 140 additions and 51 deletions

View File

@@ -23,6 +23,23 @@
namespace sta
{
/**
* @brief Get peripheral clock frequency.
*
* @return Clock frequency
*/
using HalSpiPCLKFreqFn = uint32_t (*)();
/**
* @brief Info related to HAL SPI interface.
*/
struct HalSpiInterfaceInfo
{
SPI_HandleTypeDef * handle; /**< Interface handle */
HalSpiPCLKFreqFn getPCLKFreq; /**< Getter for peripheral clock used by interface */
};
/**
* @brief Implementation of `SpiInterface` interface using HAL.
*/
@@ -30,24 +47,23 @@ namespace sta
{
public:
/**
* @param handle SPI handle
* @param info SPI interface info
* @param mutex Mutex object for managing access. Pass nullptr for no access control
*/
HalSpiInterface(SPI_HandleTypeDef * handle, Mutex * mutex = nullptr);
HalSpiInterface(const HalSpiInterfaceInfo & 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 transfer16(uint16_t value) override;
void fill(uint8_t value, size_t count) override;
void fill32(uint32_t value, size_t count) 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:
SPI_HandleTypeDef * handle_; /**< SPI handle */
HalSpiInterfaceInfo info_; /**< SPI interface info */
};
@@ -72,6 +88,38 @@ namespace sta
} // namespace sta
/**
* @brief Get function returning PCLK frequency.
*
* @param n Index of peripheral clock
*/
#define STA_HAL_GET_PCLK_FREQ_FN(n) HAL_RCC_GetPCLK ## n ## Freq
/**
* @brief Get SPI interface info struct.
*
* Check the MCUs Reference Manual RCC register documentation to see which
* peripheral clock is used.
*
* @param n Index of SPI interface (e.g. 2 for SPI2)
* @param pclk Index of peripheral clock used by SPI interface
*/
#define STA_HAL_SPI_INFO_MANUAL(n, pclk) sta::HalSpiInterfaceInfo{&hspi ## n, STA_HAL_GET_PCLK_FREQ_FN(pclk)}
/**
* @brief Get SPI interface info struct.
*
* Requires STA_HAL_SPI_n_PCLK_IDX set to idx of PCLK used by the interface.
*
* Check the MCUs Reference Manual RCC register documentation to see which
* peripheral clock is used.
*
* @param n Index of SPI interface (e.g. 2 for SPI2)
*/
#define STA_HAL_SPI_INFO(n) STA_HAL_SPI_INFO_MANUAL(n, STA_HAL_SPI_ ## n ## _PCLK_IDX)
#endif // STA_HAL_SPI_ENABLE
#endif // STA_HAL_SPI_HPP