Add SPI device interface

This commit is contained in:
Henrik Stickann 2022-04-09 21:22:14 +02:00
parent 4140a07307
commit 3abe36ec81
2 changed files with 150 additions and 0 deletions

View File

@ -0,0 +1,94 @@
#ifndef STA_SPI_DEVICE_HPP
#define STA_SPI_DEVICE_HPP
#include <sta/spi_interface.hpp>
#include <stdint.h>
#include <stddef.h>
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

56
src/spi_device.cpp Normal file
View File

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