From fdf5b4890deac9a8e86624fc5be3bbd23246c513 Mon Sep 17 00:00:00 2001 From: Henrik Stickann <4376396-Mithradir@users.noreply.gitlab.com> Date: Thu, 2 Feb 2023 22:23:44 +0100 Subject: [PATCH] Rename SPI classes --- include/sta/spi/device.hpp | 172 +++++++++++------------ include/sta/spi/interface.hpp | 103 -------------- include/sta/spi/settings.hpp | 165 +++++++++++----------- include/sta/spi/spi.hpp | 107 ++++++++++++++ include/sta/stm32/spi.hpp | 158 +++++++++++---------- src/spi/device.cpp | 122 ++++++++-------- src/spi/interface.cpp | 21 --- src/spi/settings.cpp | 114 +++++++-------- src/spi/spi.cpp | 26 ++++ src/stm32/spi.cpp | 257 +++++++++++++++++----------------- 10 files changed, 630 insertions(+), 615 deletions(-) delete mode 100644 include/sta/spi/interface.hpp create mode 100644 include/sta/spi/spi.hpp delete mode 100644 src/spi/interface.cpp create mode 100644 src/spi/spi.cpp diff --git a/include/sta/spi/device.hpp b/include/sta/spi/device.hpp index 80c0dce..7821bff 100644 --- a/include/sta/spi/device.hpp +++ b/include/sta/spi/device.hpp @@ -1,12 +1,12 @@ /** * @file - * @brief SPI device interface. + * @brief SPI bus peripheral device. */ #ifndef STA_CORE_SPI_DEVICE_HPP #define STA_CORE_SPI_DEVICE_HPP #include -#include +#include #include #include @@ -14,101 +14,101 @@ namespace sta { - /** - * @brief Interface for SPI devices. - * - * @ingroup staCoreSPI - */ - class SpiDevice - { - public: - /** - * @param intf SPI hardware interface - * @param csPin Chip select pin - */ - SpiDevice(SpiInterface * intf, GpioPin * csPin); + /** + * @brief Peripheral device connected via SPI. + * + * @ingroup sta_core_spi + */ + class SPIDevice + { + public: + /** + * @param intf %SPI hardware interface + * @param csPin Chip select pin + */ + SPIDevice(SPI * intf, GpioPin * csPin); - /** - * @brief Start transmission with device. - * - * Must be called before any I/O operations. - */ - void beginTransmission(); - /** - * @brief End transmission with device. - * - * Must be called after last I/O operation. - */ - void endTransmission(); + /** + * @brief Start transmission with device. + * + * Must be called before any I/O operations. + */ + void beginTransmission(); + /** + * @brief End transmission with device. + * + * Must be called after last I/O operation. + */ + void endTransmission(); - /** - * @brief Send single byte of data. - * - * @param value 8-bit value - */ - void transfer(uint8_t value); - /** - * @brief Send two bytes of data. - * - * @param value 16-bit value - */ - void transfer16(uint16_t value); - /** - * @brief Send data from buffer. - * - * @param buffer Source buffer - * @param size Number of bytes to transfer - */ - void transfer(const uint8_t * buffer, size_t size); - /** - * @brief Send and receive data simultaneously. - * - * @param txBuffer Send buffer - * @param rxBuffer Receive buffer - * @param size Number of bytes to transfer - */ - void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size); - /** - * @brief Read incoming data to buffer. - * - * @param buffer Destination buffer - * @param size Number of bytes to read - */ - void receive(uint8_t * buffer, size_t size); + /** + * @brief Send single byte of data. + * + * @param value 8-bit value + */ + void transfer(uint8_t value); + /** + * @brief Send two bytes of data. + * + * @param value 16-bit value + */ + void transfer16(uint16_t value); + /** + * @brief Send data from buffer. + * + * @param buffer Source buffer + * @param size Number of bytes to transfer + */ + void transfer(const uint8_t * buffer, size_t size); + /** + * @brief Send and receive data simultaneously. + * + * @param txBuffer Send buffer + * @param rxBuffer Receive buffer + * @param size Number of bytes to transfer + */ + void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size); + /** + * @brief Read incoming data to buffer. + * + * @param buffer Destination buffer + * @param size Number of bytes to read + */ + void receive(uint8_t * buffer, size_t size); - /** - * @brief Send byte value repeatedly. - * - * @param value 8-bit value to repeat - * @param count Number of repetitions - */ - void fill(uint8_t value, size_t count); + /** + * @brief Send byte value repeatedly. + * + * @param value 8-bit value to repeat + * @param count Number of repetitions + */ + void fill(uint8_t value, size_t count); - /** - * @brief Get SPI interface settings. - * - * @return SPI settings - */ - const SpiSettings & settings() const; + /** + * @brief Get %SPI interface settings. + * + * @return SPI settings + */ + const SpiSettings & settings() const; - /** - * @brief Activate device via CS pin. - */ - void select(); - /** - * @brief Deactivate device via CS pin. - */ - void deselect(); + /** + * @brief Activate device via CS pin. + */ + void select(); + /** + * @brief Deactivate device via CS pin. + */ + void deselect(); - private: - SpiInterface * intf_; /**< SPI hardware interface */ - GpioPin * csPin_; /**< Chip select pin */ - }; + private: + SPI * intf_; /**< %SPI hardware interface */ + GpioPin * csPin_; /**< Chip select pin */ + }; } // namespace sta diff --git a/include/sta/spi/interface.hpp b/include/sta/spi/interface.hpp deleted file mode 100644 index b830526..0000000 --- a/include/sta/spi/interface.hpp +++ /dev/null @@ -1,103 +0,0 @@ -/** - * @file - * @brief SPI interface definition. - */ -#ifndef STA_CORE_SPI_INTERFACE_HPP -#define STA_CORE_SPI_INTERFACE_HPP - -#include -#include - -#include -#include - - -namespace sta -{ - /** - * @brief Interface for SPI hardware. - * - * @ingroup staCoreSPI - */ - class SpiInterface - { - public: - /** - * @param mutex Mutex object for managing shared access. Pass nullptr for no access control - */ - SpiInterface(Mutex * mutex = nullptr); - - - /** - * @brief Send single byte of data. - * - * @param value 8-bit value - */ - virtual void transfer(uint8_t value) = 0; - /** - * @brief Send two bytes of data. - * - * @param value 16-bit value - */ - virtual void transfer16(uint16_t value) = 0; - /** - * @brief Send data from buffer. - * - * @param buffer Source buffer - * @param size Number of bytes to transfer - */ - virtual void transfer(const uint8_t * buffer, size_t size) = 0; - /** - * @brief Send and receive data simultaneously. - * - * @param txBuffer Send buffer - * @param rxBuffer Receive buffer - * @param size Number of bytes to transfer - */ - virtual void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) = 0; - /** - * @brief Read incoming data to buffer. - * - * @param buffer Destination buffer - * @param size Number of bytes to read - */ - virtual void receive(uint8_t * buffer, size_t size) = 0; - - - /** - * @brief Send byte value repeatedly. - * - * @param value 8-bit value to repeat - * @param count Number of repetitions - */ - virtual void fill(uint8_t value, size_t count) = 0; - - - /** - * @brief Get SPI interface settings. - * - * @return SPI settings - */ - virtual const SpiSettings & settings() const = 0; - - - /** - * @brief Acquire usage rights to use the interface. - * - * Must be called before any I/O operations are executed. - */ - virtual void acquire(); - /** - * @brief Release usage rights for interface. - * - * Must be called after last I/O operation. - */ - virtual void release(); - - private: - Mutex * mutex_; /**< Mutex object */ - }; -} // namespace sta - - -#endif // STA_CORE_SPI_INTERFACE_HPP diff --git a/include/sta/spi/settings.hpp b/include/sta/spi/settings.hpp index bbd0e35..35b7433 100644 --- a/include/sta/spi/settings.hpp +++ b/include/sta/spi/settings.hpp @@ -1,13 +1,14 @@ /** * @file - * @brief SPI settings. + * @brief SPI bus settings. */ #ifndef STA_CORE_SPI_SETTINGS_HPP #define STA_CORE_SPI_SETTINGS_HPP + /** - * @defgroup staCoreSPI SPI - * @ingroup staCore + * @defgroup sta_core_spi SPI + * @ingroup sta_core * @brief SPI interface. */ @@ -17,97 +18,97 @@ namespace sta { - /** - * @ingroup staCoreSPI - * @{ - */ + /** + * @ingroup sta_core_spi + * @{ + */ - /** - * @brief SPI clock polarity. - */ - enum class SpiClkPolarity - { - LOW, /**< Low idle clock */ - HIGH /**< High idle clock */ - }; + /** + * @brief %SPI clock polarity. + */ + enum class SPIClkPolarity + { + LOW, /**< Low idle clock */ + HIGH /**< High idle clock */ + }; - /** - * @brief SPI clock phase. - */ - enum class SpiClkPhase - { - EDGE_1, /**< Sample on first edge, shift out on second edge */ - EDGE_2 /**< Shift out on first edge, sample on second edge */ - }; + /** + * @brief %SPI clock phase. + */ + enum class SPIClkPhase + { + EDGE_1, /**< Sample on first edge, shift out on second edge */ + EDGE_2 /**< Shift out on first edge, sample on second edge */ + }; - /** - * @brief SPI clock mode. - */ - enum class SpiMode - { - MODE_0, /**< Low idle clock, sample on rising edge, shift out on falling edge */ - MODE_1, /**< Low idle clock, sample on falling edge, shift out on rising edge */ - MODE_2, /**< High idle clock, sample on rising edge, shift out on falling edge */ - MODE_3 /**< High idle clock, sample on falling edge, shift out on rising edge */ - }; + /** + * @brief %SPI clock mode. + */ + enum class SPIMode + { + MODE_0, /**< Low idle clock, sample on rising edge, shift out on falling edge */ + MODE_1, /**< Low idle clock, sample on falling edge, shift out on rising edge */ + MODE_2, /**< High idle clock, sample on rising edge, shift out on falling edge */ + MODE_3 /**< High idle clock, sample on falling edge, shift out on rising edge */ + }; - /** - * @brief SPI data size. - */ - enum class SpiDataSize - { - SIZE_8, /**< 8-bit data size */ - SIZE_16 /**< 16-bit data size */ - }; + /** + * @brief %SPI data size. + */ + enum class SPIDataSize + { + SIZE_8, /**< 8-bit data size */ + SIZE_16 /**< 16-bit data size */ + }; - /** - * @brief SPI bit order. - */ - enum class SpiBitOrder - { - MSB, /**< Send most significant bit first */ - LSB /**< Send least significant bit first */ - }; + /** + * @brief %SPI bit order. + */ + enum class SPIBitOrder + { + MSB, /**< Send most significant bit first */ + LSB /**< Send least significant bit first */ + }; - /** - * @brief SPI settings. - */ - struct SpiSettings - { - SpiMode mode; /**< SPI clock mode */ - SpiDataSize dataSize; /**< SPI data size */ - SpiBitOrder bitOrder; /**< SPI bit order */ - uint32_t clkSpeed; /**< SPI clock speed */ - }; + /** + * @brief %SPI settings. + */ + struct SPISettings + { + SPIMode mode; /**< %SPI clock mode */ + SPIDataSize dataSize; /**< %SPI data size */ + SPIBitOrder bitOrder; /**< %SPI bit order */ + uint32_t clkSpeed; /**< %SPI clock speed */ + }; - /** - * @brief Get SPI clock polarity from clock mode. - * - * @param mode SPI clock mode - * @return SPI clock polarity - */ - SpiClkPolarity getSpiClkPolarity(SpiMode mode); - /** - * @brief Get SPI clock phase from clock mode. - * - * @param mode SPI clock mode - * @return SPI clock phase - */ - SpiClkPhase getSpiClkPhase(SpiMode mode); - /** - * @brief Get SPI clock mode from clock phase and polarity. - * - * @param polarity SPI clock polarity - * @param phase SPI clock phase - * @return SPI clock mode - */ - SpiMode getSpiMode(SpiClkPolarity polarity, SpiClkPhase phase); + /** + * @brief Get %SPI clock polarity from clock mode. + * + * @param mode %SPI clock mode + * @return %SPI clock polarity + */ + SPIClkPolarity getSPIClkPolarity(SPIMode mode); + /** + * @brief Get %SPI clock phase from clock mode. + * + * @param mode %SPI clock mode + * @return %SPI clock phase + */ + SPIClkPhase getSPIClkPhase(SPIMode mode); + /** + * @brief Get %SPI clock mode from clock phase and polarity. + * + * @param polarity %SPI clock polarity + * @param phase %SPI clock phase + * @return %SPI clock mode + */ + SPIMode getSPIMode(SPIClkPolarity polarity, SPIClkPhase phase); - /** @} */ + /** @} */ } // namespace sta diff --git a/include/sta/spi/spi.hpp b/include/sta/spi/spi.hpp new file mode 100644 index 0000000..5186cbe --- /dev/null +++ b/include/sta/spi/spi.hpp @@ -0,0 +1,107 @@ +/** + * @file + * @brief SPI bus software interface. + */ +#ifndef STA_CORE_SPI_SPI_HPP +#define STA_CORE_SPI_SPI_HPP + +#include +#include + +#include +#include + + +namespace sta +{ + /** + * @brief Interface class for %SPI hardware. + * + * Represents a single %SPI bus that can be shared by multiple devices. + * + * @ingroup sta_core_spi + */ + class SPI + { + public: + /** + * @param settings %SPI bus settings + * @param mutex Mutex object for managing shared access. Pass nullptr for no access control + */ + SPI(const SPISettings & settings, Mutex * mutex = nullptr); + + + /** + * @brief Send single byte of data. + * + * @param value 8-bit value + */ + virtual void transfer(uint8_t value) = 0; + /** + * @brief Send two bytes of data. + * + * @param value 16-bit value + */ + virtual void transfer16(uint16_t value) = 0; + /** + * @brief Send data from buffer. + * + * @param buffer Source buffer + * @param size Number of bytes to transfer + */ + virtual void transfer(const uint8_t * buffer, size_t size) = 0; + /** + * @brief Send and receive data simultaneously. + * + * @param txBuffer Send buffer + * @param rxBuffer Receive buffer + * @param size Number of bytes to transfer + */ + virtual void transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) = 0; + /** + * @brief Read incoming data to buffer. + * + * @param buffer Destination buffer + * @param size Number of bytes to read + */ + virtual void receive(uint8_t * buffer, size_t size) = 0; + + + /** + * @brief Send byte value repeatedly. + * + * @param value 8-bit value to repeat + * @param count Number of repetitions + */ + virtual void fill(uint8_t value, size_t count) = 0; + + + /** + * @brief Get %SPI interface settings. + * + * @return %SPI settings + */ + const SPISettings & settings() const; + + + /** + * @brief Acquire usage rights to use the interface. + * + * Must be called before any I/O operations are executed. + */ + virtual void acquire(); + /** + * @brief Release usage rights for interface. + * + * Must be called after last I/O operation. + */ + virtual void release(); + + private: + SPISettings settings_; /**< %SPI settings */ + Mutex * mutex_; /**< Mutex object */ + }; +} // namespace sta + + +#endif // STA_CORE_SPI_SPI_HPP diff --git a/include/sta/stm32/spi.hpp b/include/sta/stm32/spi.hpp index 88515d0..0e477bf 100644 --- a/include/sta/stm32/spi.hpp +++ b/include/sta/stm32/spi.hpp @@ -1,16 +1,10 @@ /** * @file - * @brief Implementations for SpiInterface and SpiDevice using STM32 HAL. + * @brief SPI bus implementation 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 @@ -24,102 +18,120 @@ # endif // HAL_SPI_MODULE_ENABLED #endif // STA_PLATFORM_STM32 + #if defined(STA_STM32_SPI_ENABLED) || defined(DOXYGEN) #include -#include +#include #include #include +/** + * @defgroup sta_core_stm32_spi SPI + * @ingroup sta_core_stm32 + * @brief STM32 %SPI module. + */ + + namespace sta { - /** - * @ingroup stm32SPI - * @{ - */ + /** + * @addtogroup sta_core_stm32_spi + * @{ + */ - /** - * @brief Get peripheral clock frequency. - * - * @return Clock frequency - */ - using STM32SpiPCLKFreqFn = uint32_t (*)(); + /** + * @brief STM32 HAL implementation of the `SPI` interface class. + */ + class STM32SPI : public SPI + { + public: + struct Info + { + SPI_HandleTypeDef * handle; /**< STM32 HAL handle */ + uint32_t pclkFreq; /**< Peripheral clock frequency used by interface */ + }; - /** - * @brief Info related to STM SPI interface. - */ - struct STM32SpiInterfaceInfo - { - SPI_HandleTypeDef * handle; /**< Interface handle */ - STM32SpiPCLKFreqFn getPCLKFreq; /**< Getter for peripheral clock 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 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); + /** + * @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); - 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 */ - }; + private: + STM32GpioPin csPin_; /**< Device CS pin */ + }; - /** - * @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. + * @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 `core` -> sta/mcu/.hpp files. + * 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 SPI interface handle + * @param handle STM32 HAL %SPI handle * - * @ingroup halSPI + * @ingroup sta_core_stm32_spi */ -#define STA_STM32_SPI_INFO(handle) sta::STM32SpiInterfaceInfo{&handle, STA_STM32_GET_HANDLE_PCLK_FREQ_FN(handle)} +#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 diff --git a/src/spi/device.cpp b/src/spi/device.cpp index 1c3f95d..cced053 100644 --- a/src/spi/device.cpp +++ b/src/spi/device.cpp @@ -5,84 +5,84 @@ namespace sta { - SpiDevice::SpiDevice(SpiInterface * intf, GpioPin * csPin) - : intf_{intf}, csPin_{csPin} - { - STA_ASSERT(intf != nullptr); - STA_ASSERT(csPin != nullptr); - } + SPIDevice::SPIDevice(SPI * intf, GpioPin * csPin) + : intf_{intf}, csPin_{csPin} + { + STA_ASSERT(intf != nullptr); + STA_ASSERT(csPin != nullptr); + } - void SpiDevice::beginTransmission() - { - // Acquire SPI access and activate device - intf_->acquire(); - select(); - } + void SPIDevice::beginTransmission() + { + // Acquire SPI access and activate device + intf_->acquire(); + select(); + } - void SpiDevice::endTransmission() - { - // Deactivate device and release SPI access - deselect(); - intf_->release(); - } + void SPIDevice::endTransmission() + { + // Deactivate device and release SPI access + deselect(); + intf_->release(); + } - // Forward I/O operations to SPI interface + // Forward I/O operations to SPI interface - void SpiDevice::transfer(uint8_t data) - { - intf_->transfer(data); - } + void SPIDevice::transfer(uint8_t data) + { + intf_->transfer(data); + } - void SpiDevice::transfer16(uint16_t data) - { - intf_->transfer16(data); - } + void SPIDevice::transfer16(uint16_t data) + { + intf_->transfer16(data); + } - void SpiDevice::transfer(const uint8_t * buffer, size_t size) - { - STA_ASSERT(buffer != nullptr); + void SPIDevice::transfer(const uint8_t * buffer, size_t size) + { + STA_ASSERT(buffer != nullptr); - intf_->transfer(buffer, size); - } + intf_->transfer(buffer, size); + } - void SpiDevice::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) - { - STA_ASSERT(txBuffer != nullptr); - STA_ASSERT(rxBuffer != nullptr); - STA_ASSERT(size != 0); + void SPIDevice::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) + { + STA_ASSERT(txBuffer != nullptr); + STA_ASSERT(rxBuffer != nullptr); + STA_ASSERT(size != 0); - intf_->transfer(txBuffer, rxBuffer, size); - } + intf_->transfer(txBuffer, rxBuffer, size); + } - void SpiDevice::receive(uint8_t * buffer, size_t size) - { - STA_ASSERT(buffer != nullptr); + void SPIDevice::receive(uint8_t * buffer, size_t size) + { + STA_ASSERT(buffer != nullptr); - intf_->receive(buffer, size); - } + intf_->receive(buffer, size); + } - void SpiDevice::fill(uint8_t value, size_t count) - { - STA_ASSERT(count != 0); + void SPIDevice::fill(uint8_t value, size_t count) + { + STA_ASSERT(count != 0); - intf_->fill(value, count); - } + intf_->fill(value, count); + } - const SpiSettings & SpiDevice::settings() const - { - return intf_->settings(); - } + const SpiSettings & SPIDevice::settings() const + { + return intf_->settings(); + } - void SpiDevice::select() - { - csPin_->setState(GpioPinState::LOW); - } + void SPIDevice::select() + { + csPin_->setState(GpioPinState::LOW); + } - void SpiDevice::deselect() - { - csPin_->setState(GpioPinState::HIGH); - } + void SPIDevice::deselect() + { + csPin_->setState(GpioPinState::HIGH); + } } // namespace sta diff --git a/src/spi/interface.cpp b/src/spi/interface.cpp deleted file mode 100644 index a50da61..0000000 --- a/src/spi/interface.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include - - -namespace sta -{ - SpiInterface::SpiInterface(Mutex * mutex /* = nullptr */) - : mutex_{mutex} - {} - - void SpiInterface::acquire() - { - if (mutex_ != nullptr) - mutex_->acquire(); - } - - void SpiInterface::release() - { - if (mutex_ != nullptr) - mutex_->release(); - } -} // namespace sta diff --git a/src/spi/settings.cpp b/src/spi/settings.cpp index 5d29a0a..7d76aa0 100644 --- a/src/spi/settings.cpp +++ b/src/spi/settings.cpp @@ -6,67 +6,67 @@ namespace sta { - SpiClkPolarity getSpiClkPolarity(SpiMode mode) - { - switch (mode) - { - case SpiMode::MODE_0: - case SpiMode::MODE_1: - return SpiClkPolarity::LOW; + SPIClkPolarity getSPIClkPolarity(SPIMode mode) + { + switch (mode) + { + case SPIMode::MODE_0: + case SPIMode::MODE_1: + return SPIClkPolarity::LOW; - case SpiMode::MODE_2: - case SpiMode::MODE_3: - return SpiClkPolarity::HIGH; + case SPIMode::MODE_2: + case SPIMode::MODE_3: + return SPIClkPolarity::HIGH; - default: - // Unreachable case - STA_ASSERT_MSG(false, "Case for SpiMode enum not handled"); - STA_UNREACHABLE(); - } - } + default: + // Unreachable case + STA_ASSERT_MSG(false, "Case for SPIMode enum not handled"); + STA_UNREACHABLE(); + } + } - SpiClkPhase getSpiClkPhase(SpiMode mode) - { - switch (mode) - { - case SpiMode::MODE_0: - case SpiMode::MODE_2: - return SpiClkPhase::EDGE_1; + SPIClkPhase getSPIClkPhase(SPIMode mode) + { + switch (mode) + { + case SPIMode::MODE_0: + case SPIMode::MODE_2: + return SPIClkPhase::EDGE_1; - case SpiMode::MODE_1: - case SpiMode::MODE_3: - return SpiClkPhase::EDGE_2; + case SPIMode::MODE_1: + case SPIMode::MODE_3: + return SPIClkPhase::EDGE_2; - default: - // Unreachable case - STA_ASSERT_MSG(false, "Case for SpiMode enum not handled"); - STA_UNREACHABLE(); - } - } + default: + // Unreachable case + STA_ASSERT_MSG(false, "Case for SPIMode enum not handled"); + STA_UNREACHABLE(); + } + } - SpiMode getSpiMode(SpiClkPolarity polarity, SpiClkPhase phase) - { - if (polarity == SpiClkPolarity::LOW) - { - if (phase == SpiClkPhase::EDGE_1) - { - return SpiMode::MODE_0; - } - else - { - return SpiMode::MODE_1; - } - } - else - { - if (phase == SpiClkPhase::EDGE_1) - { - return SpiMode::MODE_2; - } - else - { - return SpiMode::MODE_3; - } - } - } + SPIMode getSPIMode(SPIClkPolarity polarity, SPIClkPhase phase) + { + if (polarity == SPIClkPolarity::LOW) + { + if (phase == SPIClkPhase::EDGE_1) + { + return SPIMode::MODE_0; + } + else + { + return SPIMode::MODE_1; + } + } + else + { + if (phase == SPIClkPhase::EDGE_1) + { + return SPIMode::MODE_2; + } + else + { + return SPIMode::MODE_3; + } + } + } } // namespace sta diff --git a/src/spi/spi.cpp b/src/spi/spi.cpp new file mode 100644 index 0000000..56d38db --- /dev/null +++ b/src/spi/spi.cpp @@ -0,0 +1,26 @@ +#include + + +namespace sta +{ + SPI::SPI(const SPISettings & settings, Mutex * mutex /* = nullptr */) + : settings_{settings}, mutex_{mutex} + {} + + const SPISettings & SPI::settings() const + { + return settings_; + } + + void SPI::acquire() + { + if (mutex_ != nullptr) + mutex_->acquire(); + } + + void SPI::release() + { + if (mutex_ != nullptr) + mutex_->release(); + } +} // namespace sta diff --git a/src/stm32/spi.cpp b/src/stm32/spi.cpp index 955be2c..ec5af2a 100644 --- a/src/stm32/spi.cpp +++ b/src/stm32/spi.cpp @@ -7,168 +7,161 @@ #ifdef STA_MCU_LITTLE_ENDIAN -# define STA_STM32_SPI_REVERSE_BIT_ORDER SpiBitOrder::MSB +# define STA_STM32_SPI_REVERSE_BIT_ORDER SPIBitOrder::MSB #elif STA_MCU_BIG_ENDIAN -# define STA_STM32_SPI_REVERSE_BIT_ORDER SpiBitOrder::LSB +# define STA_STM32_SPI_REVERSE_BIT_ORDER SPIBitOrder::LSB #endif namespace sta { - static SpiSettings getSpiSettings(SPI_HandleTypeDef * handle, uint32_t pclkFreq) - { - SpiSettings settings; + static SPISettings getSPISettings(SPI_HandleTypeDef * handle, uint32_t pclkFreq) + { + SPISettings settings; - settings.mode = getSpiMode( - (handle->Init.CLKPolarity == SPI_POLARITY_LOW) ? SpiClkPolarity::LOW : SpiClkPolarity::HIGH, - (handle->Init.CLKPhase == SPI_PHASE_1EDGE) ? SpiClkPhase::EDGE_1 : SpiClkPhase::EDGE_2 - ); - settings.dataSize = (handle->Init.DataSize == SPI_DATASIZE_8BIT) ? SpiDataSize::SIZE_8 : SpiDataSize::SIZE_16; - settings.bitOrder = (handle->Init.FirstBit == SPI_FIRSTBIT_MSB) ? SpiBitOrder::MSB : SpiBitOrder::LSB; + settings.mode = getSPIMode( + (handle->Init.CLKPolarity == SPI_POLARITY_LOW) ? SPIClkPolarity::LOW : SPIClkPolarity::HIGH, + (handle->Init.CLKPhase == SPI_PHASE_1EDGE) ? SPIClkPhase::EDGE_1 : SPIClkPhase::EDGE_2 + ); + settings.dataSize = (handle->Init.DataSize == SPI_DATASIZE_8BIT) ? SPIDataSize::SIZE_8 : SPIDataSize::SIZE_16; + settings.bitOrder = (handle->Init.FirstBit == SPI_FIRSTBIT_MSB) ? SPIBitOrder::MSB : SPIBitOrder::LSB; - uint32_t prescaler = 1; - switch (handle->Init.BaudRatePrescaler) - { - case SPI_BAUDRATEPRESCALER_2: - prescaler = 2; - break; - case SPI_BAUDRATEPRESCALER_4: - prescaler = 4; - break; - case SPI_BAUDRATEPRESCALER_8: - prescaler = 8; - break; - case SPI_BAUDRATEPRESCALER_16: - prescaler = 16; - break; - case SPI_BAUDRATEPRESCALER_32: - prescaler = 32; - break; - case SPI_BAUDRATEPRESCALER_64: - prescaler = 64; - break; - case SPI_BAUDRATEPRESCALER_128: - prescaler = 128; - break; - case SPI_BAUDRATEPRESCALER_256: - prescaler = 256; - break; - default: - // Unreachable case - STA_ASSERT_MSG(false, "Case for SPI_BAUDRATEPRESCALER not handled"); - STA_UNREACHABLE(); - } + uint32_t prescaler = 1; + switch (handle->Init.BaudRatePrescaler) + { + case SPI_BAUDRATEPRESCALER_2: + prescaler = 2; + break; + case SPI_BAUDRATEPRESCALER_4: + prescaler = 4; + break; + case SPI_BAUDRATEPRESCALER_8: + prescaler = 8; + break; + case SPI_BAUDRATEPRESCALER_16: + prescaler = 16; + break; + case SPI_BAUDRATEPRESCALER_32: + prescaler = 32; + break; + case SPI_BAUDRATEPRESCALER_64: + prescaler = 64; + break; + case SPI_BAUDRATEPRESCALER_128: + prescaler = 128; + break; + case SPI_BAUDRATEPRESCALER_256: + prescaler = 256; + break; + default: + // Unreachable case + STA_ASSERT_MSG(false, "Case for SPI_BAUDRATEPRESCALER not handled"); + STA_UNREACHABLE(); + } - // SPI clock speed is based of PCLK - settings.clkSpeed = pclkFreq / prescaler; + // SPI clock speed is based of PCLK + settings.clkSpeed = pclkFreq / prescaler; - return settings; - } + return settings; + } - STM32SpiInterface::STM32SpiInterface(const STM32SpiInterfaceInfo & info, Mutex * mutex /* = nullptr */) - : SpiInterface(mutex), info_{info} - { - STA_ASSERT(info.handle != nullptr); - STA_ASSERT(info.getPCLKFreq != nullptr); - } + STM32SPI::STM32SPI(SPI_HandleTypeDef * handle, uint32_t pclkFreq, Mutex * mutex = nullptr) + : SPI(getSPISettings(handle, pclkFreq), mutex), handle_{handle} + { + STA_ASSERT(handle != nullptr); + } + + STM32SPI::STM32SPI(const Info & info, Mutex * mutex /* = nullptr */) + : STM32SPI(info.handle, info.pclkFreq, mutex) + {} - void STM32SpiInterface::transfer(uint8_t value) - { - if (settings().dataSize == SpiDataSize::SIZE_8) - { - HAL_SPI_Transmit(info_.handle, &value, 1, HAL_MAX_DELAY); - } - else - { - // Required since tx buffer is cast to uint16_t * internally - uint16_t dummy = value; - HAL_SPI_Transmit(info_.handle, reinterpret_cast(&dummy), 1, HAL_MAX_DELAY); - } - } + void STM32SPI::transfer(uint8_t value) + { + if (settings().dataSize == SPIDataSize::SIZE_8) + { + HAL_SPI_Transmit(handle_, &value, 1, HAL_MAX_DELAY); + } + else + { + // Required since tx buffer is cast to uint16_t * internally + uint16_t dummy = value; + HAL_SPI_Transmit(handle_, reinterpret_cast(&dummy), 1, HAL_MAX_DELAY); + } + } - void STM32SpiInterface::transfer16(uint16_t value) - { - uint16_t size = 1; + void STM32SPI::transfer16(uint16_t value) + { + uint16_t size = 1; - // Send as two bytes if data size is 8-bit - if (settings().dataSize == SpiDataSize::SIZE_8) - { - size = 2; + // Send as two bytes if data size is 8-bit + if (settings().dataSize == SPIDataSize::SIZE_8) + { + size = 2; - if (settings().bitOrder == STA_STM32_SPI_REVERSE_BIT_ORDER) - { - // Reverse byte order from internal representation - value = STA_UINT16_SWAP_BYTE_ORDER(value); - } - } + if (settings().bitOrder == STA_STM32_SPI_REVERSE_BIT_ORDER) + { + // Reverse byte order from internal representation + value = STA_UINT16_SWAP_BYTE_ORDER(value); + } + } - HAL_SPI_Transmit(info_.handle, reinterpret_cast(&value), size, HAL_MAX_DELAY); - } + HAL_SPI_Transmit(handle_, reinterpret_cast(&value), size, HAL_MAX_DELAY); + } - void STM32SpiInterface::transfer(const uint8_t * buffer, size_t size) - { - STA_ASSERT(buffer != nullptr); - STA_ASSERT(size != 0); + void STM32SPI::transfer(const uint8_t * buffer, size_t size) + { + STA_ASSERT(buffer != nullptr); + STA_ASSERT(size != 0); - HAL_SPI_Transmit(info_.handle, const_cast(buffer), size, HAL_MAX_DELAY); - } + HAL_SPI_Transmit(handle_, const_cast(buffer), size, HAL_MAX_DELAY); + } - void STM32SpiInterface::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) - { - STA_ASSERT(txBuffer != nullptr); - STA_ASSERT(rxBuffer != nullptr); - STA_ASSERT(size != 0); + void STM32SPI::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) + { + STA_ASSERT(txBuffer != nullptr); + STA_ASSERT(rxBuffer != nullptr); + STA_ASSERT(size != 0); - HAL_SPI_TransmitReceive(info_.handle, const_cast(txBuffer), rxBuffer, size, HAL_MAX_DELAY); - } + HAL_SPI_TransmitReceive(handle_, const_cast(txBuffer), rxBuffer, size, HAL_MAX_DELAY); + } - void STM32SpiInterface::receive(uint8_t * buffer, size_t size) - { - STA_ASSERT(buffer != nullptr); + void STM32SPI::receive(uint8_t * buffer, size_t size) + { + STA_ASSERT(buffer != nullptr); - HAL_SPI_Receive(info_.handle, buffer, size, HAL_MAX_DELAY); - } + HAL_SPI_Receive(handle_, buffer, size, HAL_MAX_DELAY); + } - void STM32SpiInterface::fill(uint8_t value, size_t count) - { - STA_ASSERT(count != 0); + void STM32SPI::fill(uint8_t value, size_t count) + { + STA_ASSERT(count != 0); - if (settings().dataSize == SpiDataSize::SIZE_8) - { - for (size_t i = 0; i < count; ++i) - { - HAL_SPI_Transmit(info_.handle, &value, 1, HAL_MAX_DELAY); - } - } - else - { - // Required since tx buffer is cast to uint16_t * internally - uint16_t dummy = value; - for (size_t i = 0; i < count; ++i) - { - HAL_SPI_Transmit(info_.handle, reinterpret_cast(&dummy), 1, HAL_MAX_DELAY); - } - } - } - - - const SpiSettings & STM32SpiInterface::settings() const - { - // Cache settings - static SpiSettings settings = getSpiSettings(info_.handle, info_.getPCLKFreq()); - - return settings; - } + if (settings().dataSize == SPIDataSize::SIZE_8) + { + for (size_t i = 0; i < count; ++i) + { + HAL_SPI_Transmit(handle_, &value, 1, HAL_MAX_DELAY); + } + } + else + { + // Required since tx buffer is cast to uint16_t * internally + uint16_t dummy = value; + for (size_t i = 0; i < count; ++i) + { + HAL_SPI_Transmit(handle_, reinterpret_cast(&dummy), 1, HAL_MAX_DELAY); + } + } + } - - STM32SpiDevice::STM32SpiDevice(STM32SpiInterface * intf, STM32GpioPin csPin) - : SpiDevice(intf, &csPin_), csPin_{csPin} - {} + STM32SPIDevice::STM32SPIDevice(STM32SPI * intf, STM32GpioPin csPin) + : SPIDevice(intf, &csPin_), csPin_{csPin} + {} } // namespace sta