sta-core/include/sta/spi/device.hpp
2023-01-19 23:22:35 +01:00

116 lines
2.2 KiB
C++

/**
* @file
* @brief SPI device interface.
*/
#ifndef STA_SPI_DEVICE_HPP
#define STA_SPI_DEVICE_HPP
#include <sta/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