Move interfaces to subdir

This commit is contained in:
Henrik Stickann
2022-05-02 13:37:03 +02:00
parent f8666b69c8
commit 01312ef97d
11 changed files with 15 additions and 12 deletions

20
src/intf/mutex.cpp Normal file
View File

@@ -0,0 +1,20 @@
#include <sta/intf/mutex.hpp>
namespace sta
{
/**
* @brief Dummy mutex implementation with no access control.
*/
class DummyMutex : public Mutex
{
public:
void acquire() override {}
void release() override {}
};
static DummyMutex dummyMutex;
Mutex * Mutex::ALWAYS_FREE = &dummyMutex;
} // namespace sta

76
src/intf/spi_device.cpp Normal file
View File

@@ -0,0 +1,76 @@
#include <sta/intf/spi_device.hpp>
#include <sta/assert.hpp>
namespace sta
{
SpiDevice::SpiDevice(SpiInterface * intf)
: intf_{intf}
{
STA_ASSERT(intf != 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();
}
} // namespace sta

View File

@@ -0,0 +1,21 @@
#include <sta/intf/spi_interface.hpp>
namespace sta
{
SpiInterface::SpiInterface(Mutex * mutex /* = nullptr */)
: mutex_{mutex}
{}
void SpiInterface::acquire()
{
if (mutex_ != nullptr)
mutex_->acquire();
}
void SpiInterface::release()
{
if (mutex_ != nullptr)
mutex_->release();
}
} // namespace sta

203
src/intf/uart.cpp Normal file
View File

@@ -0,0 +1,203 @@
#include <sta/intf/uart.hpp>
#include <sta/printf.hpp>
#include <cinttypes>
#include <cstring>
namespace sta
{
void UART::print(char c)
{
print(&c, 1);
}
void UART::print(bool b)
{
print(b ? "true" : "false");
}
void UART::print(double d)
{
char buffer[64];
snprintf(buffer, sizeof(buffer), "%f", d);
print(buffer);
}
void UART::print(uint8_t num, IntegerBase base /* = IntegerBase::DEC */)
{
printBase(num, base, "%" PRIu8, sizeof(num));
}
void UART::print(uint16_t num, IntegerBase base /* = IntegerBase::DEC */)
{
printBase(num, base, "%" PRIu16, sizeof(num));
}
void UART::print(uint32_t num, IntegerBase base /* = IntegerBase::DEC */)
{
printBase(num, base, "%" PRIu32, sizeof(num));
}
void UART::print(size_t num, IntegerBase base /* = IntegerBase::DEC */)
{
printBase(num, base, "%z", sizeof(num));
}
void UART::print(const char * str)
{
print(str, strlen(str));
}
void UART::print(const char * str, size_t length)
{
write(reinterpret_cast<const uint8_t *>(str), length);
}
void UART::println()
{
print("\r\n", 2);
}
void UART::println(char c)
{
print(&c, 1);
println();
}
void UART::println(bool b)
{
print(b);
println();
}
void UART::println(double d)
{
print(d);
println();
}
void UART::println(uint8_t num, IntegerBase base /* = IntegerBase::DEC */)
{
print(num, base);
println();
}
void UART::println(uint16_t num, IntegerBase base /* = IntegerBase::DEC */)
{
print(num, base);
println();
}
void UART::println(uint32_t num, IntegerBase base /* = IntegerBase::DEC */)
{
print(num, base);
println();
}
void UART::println(size_t num, IntegerBase base /* = IntegerBase::DEC */)
{
print(num, base);
println();
}
void UART::println(const char * str)
{
println(str, strlen(str));
}
void UART::println(const char * str, size_t length)
{
print(str, length);
println();
}
void UART::printBase(uintmax_t num, IntegerBase base, const char * fmt, size_t size)
{
switch (base)
{
case IntegerBase::DEC:
printDec(num, fmt);
break;
case IntegerBase::BIN:
// Digits in base 2 = size in bytes * 8
printBin(num, size * 8);
break;
case IntegerBase::HEX:
// Digits in base 16 = size in bytes * 2
printHex(num, size * 2);
break;
default:
print("<invalid_base>");
}
}
void UART::printDec(uintmax_t num, const char * fmt)
{
char buffer[64];
snprintf(buffer, sizeof(buffer), fmt, static_cast<uint32_t>(num));
print(buffer);
}
void UART::printBin(uintmax_t value, size_t digits)
{
// Need 8 digits for every byte
char buffer[sizeof(value) * 8];
// Check bounds
if (digits > sizeof(buffer))
{
print("<bin_value_too_big>");
return;
}
// Nothing to do
if (digits == 0)
return;
for (size_t i = 0; i < digits; ++i)
{
// Convert bit to '0' or '1'
// First digit in buffer is MSB in value, so shift from high to low
buffer[i] = '0' + ((value >> (digits - 1 - i)) & 0x1);
}
print(buffer, digits);
}
void UART::printHex(uintmax_t value, size_t digits)
{
// Need 2 digits for every byte
char buffer[sizeof(value) * 2];
// Check bounds
if (digits > sizeof(buffer))
{
print("<hex_value_too_big>");
return;
}
// Nothing to do
if (digits == 0)
return;
for (size_t i = 0; i < digits; ++i)
{
// Convert 4 bits to hex
// First digit in buffer is 4 MSBs in value, so shift from high to low
uint8_t hex = ((value >> ((digits - 1 - i) * 4)) & 0xF);
if (hex > 9)
buffer[i] = 'A' + (hex - 10);
else
buffer[i] = '0' + hex;
}
print(buffer, digits);
}
} // namespace sta