sta-core/src/spi/device.cpp
2023-02-02 22:23:44 +01:00

89 lines
1.7 KiB
C++

#include <sta/spi/device.hpp>
#include <sta/assert.hpp>
namespace sta
{
SPIDevice::SPIDevice(SPI * intf, GpioPin * csPin)
: intf_{intf}, csPin_{csPin}
{
STA_ASSERT(intf != nullptr);
STA_ASSERT(csPin != nullptr);
}
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::transfer16(uint16_t data)
{
intf_->transfer16(data);
}
void SPIDevice::transfer(const uint8_t * buffer, size_t size)
{
STA_ASSERT(buffer != nullptr);
intf_->transfer(buffer, size);
}
void SPIDevice::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size)
{
STA_ASSERT(txBuffer != nullptr);
STA_ASSERT(rxBuffer != nullptr);
STA_ASSERT(size != 0);
intf_->transfer(txBuffer, rxBuffer, size);
}
void SPIDevice::receive(uint8_t * buffer, size_t size)
{
STA_ASSERT(buffer != nullptr);
intf_->receive(buffer, size);
}
void SPIDevice::fill(uint8_t value, size_t count)
{
STA_ASSERT(count != 0);
intf_->fill(value, count);
}
const SpiSettings & SPIDevice::settings() const
{
return intf_->settings();
}
void SPIDevice::select()
{
csPin_->setState(GpioPinState::LOW);
}
void SPIDevice::deselect()
{
csPin_->setState(GpioPinState::HIGH);
}
} // namespace sta