mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/sta-core.git
synced 2025-09-28 21:17:33 +00:00
Add SPI subdir
This commit is contained in:
115
include/sta/spi/device.hpp
Normal file
115
include/sta/spi/device.hpp
Normal file
@@ -0,0 +1,115 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief SPI device interface.
|
||||
*/
|
||||
#ifndef STA_SPI_DEVICE_HPP
|
||||
#define STA_SPI_DEVICE_HPP
|
||||
|
||||
#include <sta/intf/gpio_pin.hpp>
|
||||
#include <sta/spi/interface.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
|
||||
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 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 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 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 */
|
||||
};
|
||||
} // namespace sta
|
||||
|
||||
|
||||
#endif // STA_SPI_DEVICE_HPP
|
103
include/sta/spi/interface.hpp
Normal file
103
include/sta/spi/interface.hpp
Normal file
@@ -0,0 +1,103 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief SPI interface definition.
|
||||
*/
|
||||
#ifndef STA_SPI_INTERFACE_HPP
|
||||
#define STA_SPI_INTERFACE_HPP
|
||||
|
||||
#include <sta/intf/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_SPI_INTERFACE_HPP
|
114
include/sta/spi/settings.hpp
Normal file
114
include/sta/spi/settings.hpp
Normal file
@@ -0,0 +1,114 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief SPI settings.
|
||||
*/
|
||||
#ifndef STA_SPI_SETTINGS_HPP
|
||||
#define STA_SPI_SETTINGS_HPP
|
||||
|
||||
/**
|
||||
* @defgroup staCoreSPI SPI
|
||||
* @ingroup staCore
|
||||
* @brief SPI interface.
|
||||
*/
|
||||
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
|
||||
namespace sta
|
||||
{
|
||||
/**
|
||||
* @ingroup staCoreSPI
|
||||
* @{
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @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 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 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 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
|
||||
|
||||
|
||||
#endif // STA_SPI_SETTINGS_HPP
|
Reference in New Issue
Block a user