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 * @file
* @brief SPI device interface. * @brief SPI bus peripheral device.
*/ */
#ifndef STA_CORE_SPI_DEVICE_HPP #ifndef STA_CORE_SPI_DEVICE_HPP
#define STA_CORE_SPI_DEVICE_HPP #define STA_CORE_SPI_DEVICE_HPP
#include <sta/gpio_pin.hpp> #include <sta/gpio_pin.hpp>
#include <sta/spi/interface.hpp> #include <sta/spi/spi.hpp>
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
@ -15,18 +15,18 @@
namespace sta namespace sta
{ {
/** /**
* @brief Interface for SPI devices. * @brief Peripheral device connected via SPI.
* *
* @ingroup staCoreSPI * @ingroup sta_core_spi
*/ */
class SpiDevice class SPIDevice
{ {
public: public:
/** /**
* @param intf SPI hardware interface * @param intf %SPI hardware interface
* @param csPin Chip select pin * @param csPin Chip select pin
*/ */
SpiDevice(SpiInterface * intf, GpioPin * csPin); SPIDevice(SPI * intf, GpioPin * csPin);
/** /**
@ -89,7 +89,7 @@ namespace sta
/** /**
* @brief Get SPI interface settings. * @brief Get %SPI interface settings.
* *
* @return SPI settings * @return SPI settings
*/ */
@ -106,7 +106,7 @@ namespace sta
void deselect(); void deselect();
private: private:
SpiInterface * intf_; /**< SPI hardware interface */ SPI * intf_; /**< %SPI hardware interface */
GpioPin * csPin_; /**< Chip select pin */ GpioPin * csPin_; /**< Chip select pin */
}; };
} // namespace sta } // 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 * @file
* @brief SPI settings. * @brief SPI bus settings.
*/ */
#ifndef STA_CORE_SPI_SETTINGS_HPP #ifndef STA_CORE_SPI_SETTINGS_HPP
#define STA_CORE_SPI_SETTINGS_HPP #define STA_CORE_SPI_SETTINGS_HPP
/** /**
* @defgroup staCoreSPI SPI * @defgroup sta_core_spi SPI
* @ingroup staCore * @ingroup sta_core
* @brief SPI interface. * @brief SPI interface.
*/ */
@ -18,33 +19,33 @@
namespace sta namespace sta
{ {
/** /**
* @ingroup staCoreSPI * @ingroup sta_core_spi
* @{ * @{
*/ */
/** /**
* @brief SPI clock polarity. * @brief %SPI clock polarity.
*/ */
enum class SpiClkPolarity enum class SPIClkPolarity
{ {
LOW, /**< Low idle clock */ LOW, /**< Low idle clock */
HIGH /**< High idle clock */ HIGH /**< High idle clock */
}; };
/** /**
* @brief SPI clock phase. * @brief %SPI clock phase.
*/ */
enum class SpiClkPhase enum class SPIClkPhase
{ {
EDGE_1, /**< Sample on first edge, shift out on second edge */ EDGE_1, /**< Sample on first edge, shift out on second edge */
EDGE_2 /**< Shift out on first edge, sample on second edge */ EDGE_2 /**< Shift out on first edge, sample on second edge */
}; };
/** /**
* @brief SPI clock mode. * @brief %SPI clock mode.
*/ */
enum class SpiMode enum class SPIMode
{ {
MODE_0, /**< Low idle clock, sample on rising edge, shift out on falling edge */ 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_1, /**< Low idle clock, sample on falling edge, shift out on rising edge */
@ -53,18 +54,18 @@ namespace sta
}; };
/** /**
* @brief SPI data size. * @brief %SPI data size.
*/ */
enum class SpiDataSize enum class SPIDataSize
{ {
SIZE_8, /**< 8-bit data size */ SIZE_8, /**< 8-bit data size */
SIZE_16 /**< 16-bit data size */ SIZE_16 /**< 16-bit data size */
}; };
/** /**
* @brief SPI bit order. * @brief %SPI bit order.
*/ */
enum class SpiBitOrder enum class SPIBitOrder
{ {
MSB, /**< Send most significant bit first */ MSB, /**< Send most significant bit first */
LSB /**< Send least significant bit first */ LSB /**< Send least significant bit first */
@ -72,39 +73,39 @@ namespace sta
/** /**
* @brief SPI settings. * @brief %SPI settings.
*/ */
struct SpiSettings struct SPISettings
{ {
SpiMode mode; /**< SPI clock mode */ SPIMode mode; /**< %SPI clock mode */
SpiDataSize dataSize; /**< SPI data size */ SPIDataSize dataSize; /**< %SPI data size */
SpiBitOrder bitOrder; /**< SPI bit order */ SPIBitOrder bitOrder; /**< %SPI bit order */
uint32_t clkSpeed; /**< SPI clock speed */ uint32_t clkSpeed; /**< %SPI clock speed */
}; };
/** /**
* @brief Get SPI clock polarity from clock mode. * @brief Get %SPI clock polarity from clock mode.
* *
* @param mode SPI clock mode * @param mode %SPI clock mode
* @return SPI clock polarity * @return %SPI clock polarity
*/ */
SpiClkPolarity getSpiClkPolarity(SpiMode mode); SPIClkPolarity getSPIClkPolarity(SPIMode mode);
/** /**
* @brief Get SPI clock phase from clock mode. * @brief Get %SPI clock phase from clock mode.
* *
* @param mode SPI clock mode * @param mode %SPI clock mode
* @return SPI clock phase * @return %SPI clock phase
*/ */
SpiClkPhase getSpiClkPhase(SpiMode mode); SPIClkPhase getSPIClkPhase(SPIMode mode);
/** /**
* @brief Get SPI clock mode from clock phase and polarity. * @brief Get %SPI clock mode from clock phase and polarity.
* *
* @param polarity SPI clock polarity * @param polarity %SPI clock polarity
* @param phase SPI clock phase * @param phase %SPI clock phase
* @return SPI clock mode * @return %SPI clock mode
*/ */
SpiMode getSpiMode(SpiClkPolarity polarity, SpiClkPhase phase); SPIMode getSPIMode(SPIClkPolarity polarity, SPIClkPhase phase);
/** @} */ /** @} */

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 * @file
* @brief Implementations for SpiInterface and SpiDevice using STM32 HAL. * @brief SPI bus implementation using STM32 HAL.
*/ */
#ifndef STA_CORE_STM32_SPI_HPP #ifndef STA_CORE_STM32_SPI_HPP
#define 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 // Only enable module on STM32 platform w/ HAL SPI module enabled
#include <sta/config.hpp> #include <sta/config.hpp>
@ -24,51 +18,56 @@
# endif // HAL_SPI_MODULE_ENABLED # endif // HAL_SPI_MODULE_ENABLED
#endif // STA_PLATFORM_STM32 #endif // STA_PLATFORM_STM32
#if defined(STA_STM32_SPI_ENABLED) || defined(DOXYGEN) #if defined(STA_STM32_SPI_ENABLED) || defined(DOXYGEN)
#include <sta/spi/device.hpp> #include <sta/spi/device.hpp>
#include <sta/spi/interface.hpp> #include <sta/spi/spi.hpp>
#include <sta/stm32/clocks.hpp> #include <sta/stm32/clocks.hpp>
#include <sta/stm32/gpio_pin.hpp> #include <sta/stm32/gpio_pin.hpp>
/**
* @defgroup sta_core_stm32_spi SPI
* @ingroup sta_core_stm32
* @brief STM32 %SPI module.
*/
namespace sta namespace sta
{ {
/** /**
* @ingroup stm32SPI * @addtogroup sta_core_stm32_spi
* @{ * @{
*/ */
/** /**
* @brief Get peripheral clock frequency. * @brief STM32 HAL implementation of the `SPI` interface class.
*
* @return Clock frequency
*/ */
using STM32SpiPCLKFreqFn = uint32_t (*)(); class STM32SPI : public SPI
/**
* @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:
struct Info
{
SPI_HandleTypeDef * handle; /**< STM32 HAL handle */
uint32_t pclkFreq; /**< Peripheral clock frequency used by interface */
};
public: public:
/** /**
* @param info SPI interface info * @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 * @param mutex Mutex object for managing access. Pass nullptr for no access control
*/ */
STM32SpiInterface(const STM32SpiInterfaceInfo & info, Mutex * mutex = nullptr); 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 transfer(uint8_t value) override;
void transfer16(uint16_t value) override; void transfer16(uint16_t value) override;
@ -78,24 +77,22 @@ namespace sta
void fill(uint8_t value, size_t count) override; void fill(uint8_t value, size_t count) override;
const SpiSettings & settings() const override;
private: private:
STM32SpiInterfaceInfo info_; /**< SPI interface info */ SPI_HandleTypeDef * handle_; /**< STM32 HAL handle */
}; };
/** /**
* @brief Implementation of SpiDevice interface using STM32 HAL. * @brief STM32 HAL implementation of the `SPIDevice` class.
*/ */
class STM32SpiDevice : public SpiDevice class STM32SPIDevice : public SPIDevice
{ {
public: public:
/** /**
* @param intf SPI interface * @param intf %SPI interface
* @param csPin Device CS pin * @param csPin Device CS pin
*/ */
STM32SpiDevice(STM32SpiInterface * intf, STM32GpioPin csPin); STM32SPIDevice(STM32SPI * intf, STM32GpioPin csPin);
private: private:
STM32GpioPin csPin_; /**< Device CS pin */ STM32GpioPin csPin_; /**< Device CS pin */
@ -107,19 +104,34 @@ 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. * 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 * Check the MCUs Reference Manual RCC register documentation to see which
* peripheral clock is used. * 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 #endif // STA_STM32_SPI_ENABLED

View File

@ -5,21 +5,21 @@
namespace sta namespace sta
{ {
SpiDevice::SpiDevice(SpiInterface * intf, GpioPin * csPin) SPIDevice::SPIDevice(SPI * intf, GpioPin * csPin)
: intf_{intf}, csPin_{csPin} : intf_{intf}, csPin_{csPin}
{ {
STA_ASSERT(intf != nullptr); STA_ASSERT(intf != nullptr);
STA_ASSERT(csPin != nullptr); STA_ASSERT(csPin != nullptr);
} }
void SpiDevice::beginTransmission() void SPIDevice::beginTransmission()
{ {
// Acquire SPI access and activate device // Acquire SPI access and activate device
intf_->acquire(); intf_->acquire();
select(); select();
} }
void SpiDevice::endTransmission() void SPIDevice::endTransmission()
{ {
// Deactivate device and release SPI access // Deactivate device and release SPI access
deselect(); deselect();
@ -29,24 +29,24 @@ namespace sta
// Forward I/O operations to SPI interface // Forward I/O operations to SPI interface
void SpiDevice::transfer(uint8_t data) void SPIDevice::transfer(uint8_t data)
{ {
intf_->transfer(data); intf_->transfer(data);
} }
void SpiDevice::transfer16(uint16_t data) void SPIDevice::transfer16(uint16_t data)
{ {
intf_->transfer16(data); intf_->transfer16(data);
} }
void SpiDevice::transfer(const uint8_t * buffer, size_t size) void SPIDevice::transfer(const uint8_t * buffer, size_t size)
{ {
STA_ASSERT(buffer != nullptr); 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) void SPIDevice::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size)
{ {
STA_ASSERT(txBuffer != nullptr); STA_ASSERT(txBuffer != nullptr);
STA_ASSERT(rxBuffer != nullptr); STA_ASSERT(rxBuffer != nullptr);
@ -55,14 +55,14 @@ namespace sta
intf_->transfer(txBuffer, rxBuffer, size); intf_->transfer(txBuffer, rxBuffer, size);
} }
void SpiDevice::receive(uint8_t * buffer, size_t size) void SPIDevice::receive(uint8_t * buffer, size_t size)
{ {
STA_ASSERT(buffer != nullptr); STA_ASSERT(buffer != nullptr);
intf_->receive(buffer, size); intf_->receive(buffer, size);
} }
void SpiDevice::fill(uint8_t value, size_t count) void SPIDevice::fill(uint8_t value, size_t count)
{ {
STA_ASSERT(count != 0); STA_ASSERT(count != 0);
@ -70,18 +70,18 @@ namespace sta
} }
const SpiSettings & SpiDevice::settings() const const SpiSettings & SPIDevice::settings() const
{ {
return intf_->settings(); return intf_->settings();
} }
void SpiDevice::select() void SPIDevice::select()
{ {
csPin_->setState(GpioPinState::LOW); csPin_->setState(GpioPinState::LOW);
} }
void SpiDevice::deselect() void SPIDevice::deselect()
{ {
csPin_->setState(GpioPinState::HIGH); csPin_->setState(GpioPinState::HIGH);
} }

View File

@ -1,21 +0,0 @@
#include <sta/spi/interface.hpp>
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

View File

@ -6,66 +6,66 @@
namespace sta namespace sta
{ {
SpiClkPolarity getSpiClkPolarity(SpiMode mode) SPIClkPolarity getSPIClkPolarity(SPIMode mode)
{ {
switch (mode) switch (mode)
{ {
case SpiMode::MODE_0: case SPIMode::MODE_0:
case SpiMode::MODE_1: case SPIMode::MODE_1:
return SpiClkPolarity::LOW; return SPIClkPolarity::LOW;
case SpiMode::MODE_2: case SPIMode::MODE_2:
case SpiMode::MODE_3: case SPIMode::MODE_3:
return SpiClkPolarity::HIGH; return SPIClkPolarity::HIGH;
default: default:
// Unreachable case // Unreachable case
STA_ASSERT_MSG(false, "Case for SpiMode enum not handled"); STA_ASSERT_MSG(false, "Case for SPIMode enum not handled");
STA_UNREACHABLE(); STA_UNREACHABLE();
} }
} }
SpiClkPhase getSpiClkPhase(SpiMode mode) SPIClkPhase getSPIClkPhase(SPIMode mode)
{ {
switch (mode) switch (mode)
{ {
case SpiMode::MODE_0: case SPIMode::MODE_0:
case SpiMode::MODE_2: case SPIMode::MODE_2:
return SpiClkPhase::EDGE_1; return SPIClkPhase::EDGE_1;
case SpiMode::MODE_1: case SPIMode::MODE_1:
case SpiMode::MODE_3: case SPIMode::MODE_3:
return SpiClkPhase::EDGE_2; return SPIClkPhase::EDGE_2;
default: default:
// Unreachable case // Unreachable case
STA_ASSERT_MSG(false, "Case for SpiMode enum not handled"); STA_ASSERT_MSG(false, "Case for SPIMode enum not handled");
STA_UNREACHABLE(); STA_UNREACHABLE();
} }
} }
SpiMode getSpiMode(SpiClkPolarity polarity, SpiClkPhase phase) SPIMode getSPIMode(SPIClkPolarity polarity, SPIClkPhase phase)
{ {
if (polarity == SpiClkPolarity::LOW) if (polarity == SPIClkPolarity::LOW)
{ {
if (phase == SpiClkPhase::EDGE_1) if (phase == SPIClkPhase::EDGE_1)
{ {
return SpiMode::MODE_0; return SPIMode::MODE_0;
} }
else else
{ {
return SpiMode::MODE_1; return SPIMode::MODE_1;
} }
} }
else else
{ {
if (phase == SpiClkPhase::EDGE_1) if (phase == SPIClkPhase::EDGE_1)
{ {
return SpiMode::MODE_2; return SPIMode::MODE_2;
} }
else else
{ {
return SpiMode::MODE_3; return SPIMode::MODE_3;
} }
} }
} }

26
src/spi/spi.cpp Normal file
View File

@ -0,0 +1,26 @@
#include <sta/spi/spi.hpp>
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

View File

@ -7,24 +7,24 @@
#ifdef STA_MCU_LITTLE_ENDIAN #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 #elif STA_MCU_BIG_ENDIAN
# define STA_STM32_SPI_REVERSE_BIT_ORDER SpiBitOrder::LSB # define STA_STM32_SPI_REVERSE_BIT_ORDER SPIBitOrder::LSB
#endif #endif
namespace sta namespace sta
{ {
static SpiSettings getSpiSettings(SPI_HandleTypeDef * handle, uint32_t pclkFreq) static SPISettings getSPISettings(SPI_HandleTypeDef * handle, uint32_t pclkFreq)
{ {
SpiSettings settings; SPISettings settings;
settings.mode = getSpiMode( settings.mode = getSPIMode(
(handle->Init.CLKPolarity == SPI_POLARITY_LOW) ? SpiClkPolarity::LOW : SpiClkPolarity::HIGH, (handle->Init.CLKPolarity == SPI_POLARITY_LOW) ? SPIClkPolarity::LOW : SPIClkPolarity::HIGH,
(handle->Init.CLKPhase == SPI_PHASE_1EDGE) ? SpiClkPhase::EDGE_1 : SpiClkPhase::EDGE_2 (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.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.bitOrder = (handle->Init.FirstBit == SPI_FIRSTBIT_MSB) ? SPIBitOrder::MSB : SPIBitOrder::LSB;
uint32_t prescaler = 1; uint32_t prescaler = 1;
switch (handle->Init.BaudRatePrescaler) switch (handle->Init.BaudRatePrescaler)
@ -66,34 +66,37 @@ namespace sta
} }
STM32SpiInterface::STM32SpiInterface(const STM32SpiInterfaceInfo & info, Mutex * mutex /* = nullptr */) STM32SPI::STM32SPI(SPI_HandleTypeDef * handle, uint32_t pclkFreq, Mutex * mutex = nullptr)
: SpiInterface(mutex), info_{info} : SPI(getSPISettings(handle, pclkFreq), mutex), handle_{handle}
{ {
STA_ASSERT(info.handle != nullptr); STA_ASSERT(handle != nullptr);
STA_ASSERT(info.getPCLKFreq != nullptr);
} }
STM32SPI::STM32SPI(const Info & info, Mutex * mutex /* = nullptr */)
: STM32SPI(info.handle, info.pclkFreq, mutex)
{}
void STM32SpiInterface::transfer(uint8_t value)
void STM32SPI::transfer(uint8_t value)
{ {
if (settings().dataSize == SpiDataSize::SIZE_8) if (settings().dataSize == SPIDataSize::SIZE_8)
{ {
HAL_SPI_Transmit(info_.handle, &value, 1, HAL_MAX_DELAY); HAL_SPI_Transmit(handle_, &value, 1, HAL_MAX_DELAY);
} }
else else
{ {
// Required since tx buffer is cast to uint16_t * internally // Required since tx buffer is cast to uint16_t * internally
uint16_t dummy = value; uint16_t dummy = value;
HAL_SPI_Transmit(info_.handle, reinterpret_cast<uint8_t *>(&dummy), 1, HAL_MAX_DELAY); HAL_SPI_Transmit(handle_, reinterpret_cast<uint8_t *>(&dummy), 1, HAL_MAX_DELAY);
} }
} }
void STM32SpiInterface::transfer16(uint16_t value) void STM32SPI::transfer16(uint16_t value)
{ {
uint16_t size = 1; uint16_t size = 1;
// Send as two bytes if data size is 8-bit // Send as two bytes if data size is 8-bit
if (settings().dataSize == SpiDataSize::SIZE_8) if (settings().dataSize == SPIDataSize::SIZE_8)
{ {
size = 2; size = 2;
@ -104,43 +107,43 @@ namespace sta
} }
} }
HAL_SPI_Transmit(info_.handle, reinterpret_cast<uint8_t *>(&value), size, HAL_MAX_DELAY); HAL_SPI_Transmit(handle_, reinterpret_cast<uint8_t *>(&value), size, HAL_MAX_DELAY);
} }
void STM32SpiInterface::transfer(const uint8_t * buffer, size_t size) void STM32SPI::transfer(const uint8_t * buffer, size_t size)
{ {
STA_ASSERT(buffer != nullptr); STA_ASSERT(buffer != nullptr);
STA_ASSERT(size != 0); STA_ASSERT(size != 0);
HAL_SPI_Transmit(info_.handle, const_cast<uint8_t *>(buffer), size, HAL_MAX_DELAY); HAL_SPI_Transmit(handle_, const_cast<uint8_t *>(buffer), size, HAL_MAX_DELAY);
} }
void STM32SpiInterface::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size) void STM32SPI::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size)
{ {
STA_ASSERT(txBuffer != nullptr); STA_ASSERT(txBuffer != nullptr);
STA_ASSERT(rxBuffer != nullptr); STA_ASSERT(rxBuffer != nullptr);
STA_ASSERT(size != 0); STA_ASSERT(size != 0);
HAL_SPI_TransmitReceive(info_.handle, const_cast<uint8_t *>(txBuffer), rxBuffer, size, HAL_MAX_DELAY); HAL_SPI_TransmitReceive(handle_, const_cast<uint8_t *>(txBuffer), rxBuffer, size, HAL_MAX_DELAY);
} }
void STM32SpiInterface::receive(uint8_t * buffer, size_t size) void STM32SPI::receive(uint8_t * buffer, size_t size)
{ {
STA_ASSERT(buffer != nullptr); 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) void STM32SPI::fill(uint8_t value, size_t count)
{ {
STA_ASSERT(count != 0); STA_ASSERT(count != 0);
if (settings().dataSize == SpiDataSize::SIZE_8) if (settings().dataSize == SPIDataSize::SIZE_8)
{ {
for (size_t i = 0; i < count; ++i) for (size_t i = 0; i < count; ++i)
{ {
HAL_SPI_Transmit(info_.handle, &value, 1, HAL_MAX_DELAY); HAL_SPI_Transmit(handle_, &value, 1, HAL_MAX_DELAY);
} }
} }
else else
@ -149,25 +152,15 @@ namespace sta
uint16_t dummy = value; uint16_t dummy = value;
for (size_t i = 0; i < count; ++i) for (size_t i = 0; i < count; ++i)
{ {
HAL_SPI_Transmit(info_.handle, reinterpret_cast<uint8_t *>(&dummy), 1, HAL_MAX_DELAY); HAL_SPI_Transmit(handle_, reinterpret_cast<uint8_t *>(&dummy), 1, HAL_MAX_DELAY);
} }
} }
} }
const SpiSettings & STM32SpiInterface::settings() const
{
// Cache settings
static SpiSettings settings = getSpiSettings(info_.handle, info_.getPCLKFreq());
return settings; STM32SPIDevice::STM32SPIDevice(STM32SPI * intf, STM32GpioPin csPin)
} : SPIDevice(intf, &csPin_), csPin_{csPin}
STM32SpiDevice::STM32SpiDevice(STM32SpiInterface * intf, STM32GpioPin csPin)
: SpiDevice(intf, &csPin_), csPin_{csPin}
{} {}
} // namespace sta } // namespace sta