sta-core/src/bus/device.cpp

101 lines
3.3 KiB
C++

#include <sta/bus/device.hpp>
#include <sta/debug/assert.hpp>
namespace sta
{
Device::Device(Interface * intf)
: intf_{intf}
{
STA_ASSERT(intf != nullptr);
}
void Device::beginTransmission()
{
intf_->acquire();
select();
selected_ = true;
}
void Device::endTransmission()
{
deselect();
selected_ = false;
intf_->release();
}
bool Device::transfer(uint8_t value, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
// If these asserts fail, the user either forgot to call _beginTransmission()_ or the mutex acquision failed.
STA_ASSERT(intf_->isAcquired());
STA_ASSERT(selected_);
return intf_->transfer(value, timeout);
}
bool Device::transfer16(uint16_t value, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
// If these asserts fail, the user either forgot to call _beginTransmission()_ or the mutex acquision failed.
STA_ASSERT(intf_->isAcquired());
STA_ASSERT(selected_);
return intf_->transfer16(value, timeout);
}
bool Device::transfer(const uint8_t * buffer, size_t size, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
// If these asserts fail, the user either forgot to call _beginTransmission()_ or the mutex acquision failed.
STA_ASSERT(intf_->isAcquired());
STA_ASSERT(selected_);
// Check if the user passed a null pointer as a buffer.
STA_ASSERT(buffer != nullptr);
return intf_->transfer(buffer, size, timeout);
}
bool Device::writeMemory(uint8_t regAddr, const uint8_t * buffer, size_t size, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
// If these asserts fail, the user either forgot to call _beginTransmission()_ or the mutex acquision failed.
STA_ASSERT(intf_->isAcquired());
STA_ASSERT(selected_);
// Check if the user passed a null pointer as a buffer.
STA_ASSERT(buffer != nullptr);
return intf_->writeMemory(regAddr, buffer, size, timeout);
}
bool Device::receive(uint8_t * buffer, size_t size, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
// If these asserts fail, the user either forgot to call _beginTransmission()_ or the mutex acquision failed.
STA_ASSERT(intf_->isAcquired());
STA_ASSERT(selected_);
// Check if the user passed a null pointer as a buffer.
STA_ASSERT(buffer != nullptr);
return intf_->receive(buffer, size, timeout);
}
bool Device::readMemory(uint8_t regAddr, uint8_t * buffer, size_t size, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
// If these asserts fail, the user either forgot to call _beginTransmission()_ or the mutex acquision failed.
STA_ASSERT(intf_->isAcquired());
STA_ASSERT(selected_);
// Check if the user passed a null pointer as a buffer.
STA_ASSERT(buffer != nullptr);
return intf_->readMemory(regAddr, buffer, size, timeout);
}
bool Device::fill(uint8_t value, size_t count, uint32_t timeout /* = STA_MAX_TIMEOUT */)
{
// If these asserts fail, the user either forgot to call _beginTransmission()_ or the mutex acquision failed.
STA_ASSERT(intf_->isAcquired());
STA_ASSERT(selected_);
intf_->fill(value, count, timeout);
}
} // namespace sta