Rename SPI classes

This commit is contained in:
Henrik Stickann
2023-02-02 22:23:44 +01:00
parent 59585b2ae5
commit fdf5b4890d
10 changed files with 630 additions and 615 deletions

View File

@@ -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 <sta/gpio_pin.hpp>
#include <sta/spi/interface.hpp>
#include <sta/spi/spi.hpp>
#include <cstddef>
#include <cstdint>
@@ -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

View File

@@ -1,103 +0,0 @@
/**
* @file
* @brief SPI interface definition.
*/
#ifndef STA_CORE_SPI_INTERFACE_HPP
#define STA_CORE_SPI_INTERFACE_HPP
#include <sta/mutex.hpp>
#include <sta/spi/settings.hpp>
#include <cstddef>
#include <cstdint>
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

View File

@@ -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

107
include/sta/spi/spi.hpp Normal file
View File

@@ -0,0 +1,107 @@
/**
* @file
* @brief SPI bus software interface.
*/
#ifndef STA_CORE_SPI_SPI_HPP
#define STA_CORE_SPI_SPI_HPP
#include <sta/mutex.hpp>
#include <sta/spi/settings.hpp>
#include <cstddef>
#include <cstdint>
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

View File

@@ -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 <sta/config.hpp>
@@ -24,102 +18,120 @@
# 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/spi/spi.hpp>
#include <sta/stm32/clocks.hpp>
#include <sta/stm32/gpio_pin.hpp>
/**
* @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_<handle>_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_<n>_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