From 3abe36ec810438d0bc193feb5ed1a51415fb4a94 Mon Sep 17 00:00:00 2001 From: Henrik Stickann <4376396-Mithradir@users.noreply.gitlab.com> Date: Sat, 9 Apr 2022 21:22:14 +0200 Subject: [PATCH] Add SPI device interface --- include/sta/spi_device.hpp | 94 ++++++++++++++++++++++++++++++++++++++ src/spi_device.cpp | 56 +++++++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 include/sta/spi_device.hpp create mode 100644 src/spi_device.cpp diff --git a/include/sta/spi_device.hpp b/include/sta/spi_device.hpp new file mode 100644 index 0000000..8fbbc8b --- /dev/null +++ b/include/sta/spi_device.hpp @@ -0,0 +1,94 @@ +#ifndef STA_SPI_DEVICE_HPP +#define STA_SPI_DEVICE_HPP + +#include + +#include +#include + + +namespace sta +{ + /** + * @brief Interface for SPI devices. + */ + class SpiDevice + { + public: + /** + * @param intf SPI hardware interface + */ + SpiDevice(SpiInterface * intf); + + /** + * @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 data from buffer. + * + * @param buffer Source buffer + * @param size Number of bytes in buffer + */ + void transfer(const uint8_t * buffer, size_t size); + /** + * @brief Send two bytes of data. + * + * @param value 16-bit value + */ + void transfer16(uint16_t data); + + /** + * @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 32-bit value repeatedly. + * + * @param value 32-bit value to repeat + * @param count Number of repetitions + */ + void fill32(uint32_t value, size_t count); + + /** + * @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 Activate device via CS pin. + */ + virtual void select() = 0; + /** + * @brief Deactivate device via CS pin. + */ + virtual void deselect() = 0; + + private: + SpiInterface * intf_; /**< SPI hardware interface */ + }; +} // namespace sta + + +#endif // STA_SPI_DEVICE_HPP diff --git a/src/spi_device.cpp b/src/spi_device.cpp new file mode 100644 index 0000000..50a961a --- /dev/null +++ b/src/spi_device.cpp @@ -0,0 +1,56 @@ +#include + + +namespace sta +{ + SpiDevice::SpiDevice(SpiInterface * intf) + : intf_{intf} + {} + + void SpiDevice::beginTransmission() + { + // Acquire SPI access and activate device + intf_->acquire(); + select(); + } + + void SpiDevice::endTransmission() + { + // Deactivate device and release SPI access + deselect(); + intf_->release(); + } + + + // Forward I/O operations to SPI interface + + void SpiDevice::transfer(uint8_t data) + { + intf_->transfer(data); + } + + void SpiDevice::transfer(const uint8_t * data, size_t size) + { + intf_->transfer(data, size); + } + + void SpiDevice::transfer16(uint16_t data) + { + intf_->transfer16(data); + } + + void SpiDevice::fill(uint8_t value, size_t count) + { + intf_->fill(value, count); + } + + void SpiDevice::fill32(uint32_t value, size_t count) + { + intf_->fill32(value, count); + } + + void SpiDevice::receive(uint8_t * buffer, size_t size) + { + intf_->receive(buffer, size); + } +} // namespace sta