mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/sta-core.git
synced 2025-09-28 21:17:33 +00:00
Merge io_interfaces repo into helpers
This commit is contained in:
23
src/atomic/mutex.cpp
Normal file
23
src/atomic/mutex.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <sta/atomic/mutex.hpp>
|
||||
#ifdef STA_ATOMIC_ENABLE
|
||||
|
||||
|
||||
namespace sta
|
||||
{
|
||||
AtomicMutex::AtomicMutex()
|
||||
: lock_{ATOMIC_FLAG_INIT}
|
||||
{}
|
||||
|
||||
void AtomicMutex::acquire()
|
||||
{
|
||||
while (lock_.test_and_set());
|
||||
}
|
||||
|
||||
void AtomicMutex::release()
|
||||
{
|
||||
lock_.clear();
|
||||
}
|
||||
} // namespace sta
|
||||
|
||||
|
||||
#endif // STA_ATOMIC_ENABLE
|
33
src/atomic/signal.cpp
Normal file
33
src/atomic/signal.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <sta/atomic/signal.hpp>
|
||||
#ifdef STA_ATOMIC_ENABLE
|
||||
|
||||
|
||||
namespace sta
|
||||
{
|
||||
AtomicSignal::AtomicSignal()
|
||||
: signal_{false}
|
||||
{}
|
||||
|
||||
void AtomicSignal::notify()
|
||||
{
|
||||
signal_.store(true);
|
||||
}
|
||||
|
||||
bool AtomicSignal::peek()
|
||||
{
|
||||
return signal_.load();
|
||||
}
|
||||
|
||||
bool AtomicSignal::test()
|
||||
{
|
||||
return signal_.exchange(false);
|
||||
}
|
||||
|
||||
void AtomicSignal::wait()
|
||||
{
|
||||
while (!signal_.exchange(false));
|
||||
}
|
||||
} // namespace sta
|
||||
|
||||
|
||||
#endif // STA_ATOMIC_ENABLE
|
38
src/hal/gpio_pin.cpp
Normal file
38
src/hal/gpio_pin.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <sta/hal/gpio_pin.hpp>
|
||||
|
||||
#ifdef STA_HAL_GPIO_ENABLE
|
||||
|
||||
#include <sta/assert.hpp>
|
||||
|
||||
|
||||
namespace sta
|
||||
{
|
||||
HalGpioPin::HalGpioPin(GPIO_TypeDef * port, uint16_t pin)
|
||||
: port_{port}, pin_{pin}
|
||||
{
|
||||
STA_ASSERT(port != nullptr);
|
||||
}
|
||||
|
||||
void HalGpioPin::setState(GpioPinState state)
|
||||
{
|
||||
HAL_GPIO_WritePin(port_, pin_, (state == GpioPinState::LOW) ? GPIO_PIN_RESET : GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
GPIO_TypeDef * HalGpioPin::getPort() const
|
||||
{
|
||||
return port_;
|
||||
}
|
||||
|
||||
uint16_t HalGpioPin::getPin() const
|
||||
{
|
||||
return pin_;
|
||||
}
|
||||
|
||||
uint8_t HalGpioPin::getIndex() const
|
||||
{
|
||||
return GPIO_GET_INDEX(port_);
|
||||
}
|
||||
} // namespace sta
|
||||
|
||||
|
||||
#endif // STA_HAL_GPIO_ENABLE
|
194
src/hal/spi.cpp
Normal file
194
src/hal/spi.cpp
Normal file
@@ -0,0 +1,194 @@
|
||||
#include <sta/hal/spi.hpp>
|
||||
|
||||
#ifdef STA_HAL_SPI_ENABLE
|
||||
|
||||
#include <sta/assert.hpp>
|
||||
#include <sta/endian.hpp>
|
||||
#include <sta/lang.hpp>
|
||||
|
||||
|
||||
#ifdef STA_MCU_LITTLE_ENDIAN
|
||||
# define STA_HAL_SPI_REVERSE_BIT_ORDER SpiBitOrder::MSB
|
||||
#elif STA_MCU_BIG_ENDIAN
|
||||
# define STA_HAL_SPI_REVERSE_BIT_ORDER SpiBitOrder::LSB
|
||||
#else // !STA_MCU_LITTLE_ENDIAN && !STA_MCU_BIG_ENDIAN
|
||||
# ifdef STA_HAL_SPI_REVERSE_BIT_ORDER
|
||||
# warning "Internal STA_HAL_SPI_REVERSE_BIT_ORDER macro manually defined! Better now what you are doing!!!"
|
||||
# else // !STA_HAL_SPI_REVERSE_BIT_ORDER
|
||||
# error "Unknown endian-ness. Define STA_MCU_LITTLE_ENDIAN or STA_MCU_BIG_ENDIAN in <sta/config.hpp>"
|
||||
# endif // !STA_HAL_SPI_REVERSE_BIT_ORDER
|
||||
#endif // !STA_MCU_LITTLE_ENDIAN && !STA_MCU_BIG_ENDIAN
|
||||
|
||||
|
||||
namespace sta
|
||||
{
|
||||
static SpiSettings getHalSpiSettings(SPI_HandleTypeDef * handle, uint32_t pclkFreq)
|
||||
{
|
||||
SpiSettings settings;
|
||||
|
||||
settings.mode = getSpiMode(
|
||||
(handle->Init.CLKPolarity == SPI_POLARITY_LOW) ? SpiClkPolarity::LOW : SpiClkPolarity::HIGH,
|
||||
(handle->Init.CLKPhase == SPI_PHASE_1EDGE) ? SpiClkPhase::EDGE_1 : SpiClkPhase::EDGE_2
|
||||
);
|
||||
settings.dataSize = (handle->Init.DataSize == SPI_DATASIZE_8BIT) ? SpiDataSize::SIZE_8 : SpiDataSize::SIZE_16;
|
||||
settings.bitOrder = (handle->Init.FirstBit == SPI_FIRSTBIT_MSB) ? SpiBitOrder::MSB : SpiBitOrder::LSB;
|
||||
|
||||
uint32_t prescaler = 1;
|
||||
switch (handle->Init.BaudRatePrescaler)
|
||||
{
|
||||
case SPI_BAUDRATEPRESCALER_2:
|
||||
prescaler = 2;
|
||||
break;
|
||||
case SPI_BAUDRATEPRESCALER_4:
|
||||
prescaler = 4;
|
||||
break;
|
||||
case SPI_BAUDRATEPRESCALER_8:
|
||||
prescaler = 8;
|
||||
break;
|
||||
case SPI_BAUDRATEPRESCALER_16:
|
||||
prescaler = 16;
|
||||
break;
|
||||
case SPI_BAUDRATEPRESCALER_32:
|
||||
prescaler = 32;
|
||||
break;
|
||||
case SPI_BAUDRATEPRESCALER_64:
|
||||
prescaler = 64;
|
||||
break;
|
||||
case SPI_BAUDRATEPRESCALER_128:
|
||||
prescaler = 128;
|
||||
break;
|
||||
case SPI_BAUDRATEPRESCALER_256:
|
||||
prescaler = 256;
|
||||
break;
|
||||
default:
|
||||
// Unreachable case
|
||||
STA_ASSERT_MSG(false, "Case for SPI_BAUDRATEPRESCALER not handled");
|
||||
STA_UNREACHABLE();
|
||||
}
|
||||
|
||||
// SPI clock speed is based of PCLK
|
||||
settings.clkSpeed = pclkFreq / prescaler;
|
||||
|
||||
return settings;
|
||||
}
|
||||
|
||||
|
||||
HalSpiInterface::HalSpiInterface(const HalSpiInterfaceInfo & info, Mutex * mutex /* = nullptr */)
|
||||
: SpiInterface(mutex), info_{info}
|
||||
{
|
||||
STA_ASSERT(info.handle != nullptr);
|
||||
STA_ASSERT(info.getPCLKFreq != nullptr);
|
||||
}
|
||||
|
||||
|
||||
void HalSpiInterface::transfer(uint8_t value)
|
||||
{
|
||||
if (settings().dataSize == SpiDataSize::SIZE_8)
|
||||
{
|
||||
HAL_SPI_Transmit(info_.handle, &value, 1, HAL_MAX_DELAY);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Required since tx buffer is cast to uint16_t * internally
|
||||
uint16_t dummy = value;
|
||||
HAL_SPI_Transmit(info_.handle, reinterpret_cast<uint8_t *>(&dummy), 1, HAL_MAX_DELAY);
|
||||
}
|
||||
}
|
||||
|
||||
void HalSpiInterface::transfer16(uint16_t value)
|
||||
{
|
||||
static_assert(sizeof(value) == 2, "Unexpected uint16_t size");
|
||||
|
||||
uint16_t size = 1;
|
||||
|
||||
// Send as two bytes if data size is 8-bit
|
||||
if (settings().dataSize == SpiDataSize::SIZE_8)
|
||||
{
|
||||
size = 2;
|
||||
|
||||
if (settings().bitOrder == STA_HAL_SPI_REVERSE_BIT_ORDER)
|
||||
{
|
||||
// Reverse byte order from internal representation
|
||||
value = STA_UINT16_SWAP_BYTE_ORDER(value);
|
||||
}
|
||||
}
|
||||
|
||||
HAL_SPI_Transmit(info_.handle, reinterpret_cast<uint8_t *>(&value), size, HAL_MAX_DELAY);
|
||||
}
|
||||
|
||||
void HalSpiInterface::transfer(const uint8_t * buffer, size_t size)
|
||||
{
|
||||
STA_ASSERT(buffer != nullptr);
|
||||
STA_ASSERT(size != 0);
|
||||
|
||||
HAL_SPI_Transmit(info_.handle, const_cast<uint8_t *>(buffer), size, HAL_MAX_DELAY);
|
||||
}
|
||||
|
||||
void HalSpiInterface::transfer(const uint8_t * txBuffer, uint8_t * rxBuffer, size_t size)
|
||||
{
|
||||
STA_ASSERT(txBuffer != nullptr);
|
||||
STA_ASSERT(rxBuffer != nullptr);
|
||||
STA_ASSERT(size != 0);
|
||||
|
||||
HAL_SPI_TransmitReceive(info_.handle, const_cast<uint8_t *>(txBuffer), rxBuffer, size, HAL_MAX_DELAY);
|
||||
}
|
||||
|
||||
void HalSpiInterface::receive(uint8_t * buffer, size_t size)
|
||||
{
|
||||
STA_ASSERT(buffer != nullptr);
|
||||
|
||||
HAL_SPI_Receive(info_.handle, buffer, size, HAL_MAX_DELAY);
|
||||
}
|
||||
|
||||
|
||||
void HalSpiInterface::fill(uint8_t value, size_t count)
|
||||
{
|
||||
STA_ASSERT(count != 0);
|
||||
|
||||
if (settings().dataSize == SpiDataSize::SIZE_8)
|
||||
{
|
||||
for (size_t i = 0; i < count; ++i)
|
||||
{
|
||||
HAL_SPI_Transmit(info_.handle, &value, 1, HAL_MAX_DELAY);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Required since tx buffer is cast to uint16_t * internally
|
||||
uint16_t dummy = value;
|
||||
for (size_t i = 0; i < count; ++i)
|
||||
{
|
||||
HAL_SPI_Transmit(info_.handle, reinterpret_cast<uint8_t *>(&dummy), 1, HAL_MAX_DELAY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const SpiSettings & HalSpiInterface::settings() const
|
||||
{
|
||||
// Cache settings
|
||||
static SpiSettings settings = getHalSpiSettings(info_.handle, info_.getPCLKFreq());
|
||||
|
||||
return settings;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
HalSpiDevice::HalSpiDevice(SpiInterface * intf, HalGpioPin csPin)
|
||||
: SpiDevice(intf), csPin_{csPin}
|
||||
{}
|
||||
|
||||
void HalSpiDevice::select()
|
||||
{
|
||||
csPin_.setState(GpioPinState::LOW);
|
||||
}
|
||||
|
||||
void HalSpiDevice::deselect()
|
||||
{
|
||||
csPin_.setState(GpioPinState::HIGH);
|
||||
}
|
||||
} // namespace sta
|
||||
|
||||
|
||||
#endif // STA_HAL_SPI_ENABLE
|
44
src/hal/uart.cpp
Normal file
44
src/hal/uart.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#include <sta/hal/uart.hpp>
|
||||
|
||||
#ifdef STA_HAL_UART_ENABLE
|
||||
|
||||
#include <sta/assert.hpp>
|
||||
|
||||
|
||||
namespace sta
|
||||
{
|
||||
HalUART::HalUART(UART_HandleTypeDef * handle)
|
||||
: handle_{handle}
|
||||
{
|
||||
STA_ASSERT(handle != nullptr);
|
||||
}
|
||||
|
||||
|
||||
void HalUART::write(const uint8_t * buffer, size_t size)
|
||||
{
|
||||
STA_ASSERT(buffer != nullptr);
|
||||
|
||||
HAL_UART_Transmit(handle_, const_cast<uint8_t *>(buffer), size, HAL_MAX_DELAY);
|
||||
}
|
||||
} // namespace sta
|
||||
|
||||
|
||||
#ifdef STA_HAL_UART_DEBUG_SERIAL
|
||||
|
||||
// Get extern declaration for DebugSerial because const namespace level variables have internal linkage by default
|
||||
#include <sta/debug_serial.hpp>
|
||||
|
||||
#include <usart.h>
|
||||
|
||||
namespace sta
|
||||
{
|
||||
HalUART gHalDebugSerial(&STA_HAL_UART_DEBUG_SERIAL);
|
||||
|
||||
// Used by <sta/debug.hpp>
|
||||
UART * const DebugSerial = &gHalDebugSerial;
|
||||
} // namespace sta
|
||||
|
||||
#endif // STA_HAL_UART_DEBUG_SERIAL
|
||||
|
||||
|
||||
#endif // STA_HAL_UART_ENABLE
|
20
src/mutex.cpp
Normal file
20
src/mutex.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#include <sta/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/spi_device.cpp
Normal file
76
src/spi_device.cpp
Normal file
@@ -0,0 +1,76 @@
|
||||
#include <sta/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
|
21
src/spi_interface.cpp
Normal file
21
src/spi_interface.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#include <sta/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
|
72
src/spi_settings.cpp
Normal file
72
src/spi_settings.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
#include <sta/spi_settings.hpp>
|
||||
|
||||
#include <sta/assert.hpp>
|
||||
#include <sta/lang.hpp>
|
||||
|
||||
|
||||
namespace sta
|
||||
{
|
||||
SpiClkPolarity getSpiClkPolarity(SpiMode mode)
|
||||
{
|
||||
switch (mode)
|
||||
{
|
||||
case SpiMode::MODE_0:
|
||||
case SpiMode::MODE_1:
|
||||
return SpiClkPolarity::LOW;
|
||||
|
||||
case SpiMode::MODE_2:
|
||||
case SpiMode::MODE_3:
|
||||
return SpiClkPolarity::HIGH;
|
||||
|
||||
default:
|
||||
// Unreachable case
|
||||
STA_ASSERT_MSG(false, "Case for SpiMode enum not handled");
|
||||
STA_UNREACHABLE();
|
||||
}
|
||||
}
|
||||
|
||||
SpiClkPhase getSpiClkPhase(SpiMode mode)
|
||||
{
|
||||
switch (mode)
|
||||
{
|
||||
case SpiMode::MODE_0:
|
||||
case SpiMode::MODE_2:
|
||||
return SpiClkPhase::EDGE_1;
|
||||
|
||||
case SpiMode::MODE_1:
|
||||
case SpiMode::MODE_3:
|
||||
return SpiClkPhase::EDGE_2;
|
||||
|
||||
default:
|
||||
// Unreachable case
|
||||
STA_ASSERT_MSG(false, "Case for SpiMode enum not handled");
|
||||
STA_UNREACHABLE();
|
||||
}
|
||||
}
|
||||
|
||||
SpiMode getSpiMode(SpiClkPolarity polarity, SpiClkPhase phase)
|
||||
{
|
||||
if (polarity == SpiClkPolarity::LOW)
|
||||
{
|
||||
if (phase == SpiClkPhase::EDGE_1)
|
||||
{
|
||||
return SpiMode::MODE_0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return SpiMode::MODE_1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (phase == SpiClkPhase::EDGE_1)
|
||||
{
|
||||
return SpiMode::MODE_2;
|
||||
}
|
||||
else
|
||||
{
|
||||
return SpiMode::MODE_3;
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace sta
|
203
src/uart.cpp
Normal file
203
src/uart.cpp
Normal file
@@ -0,0 +1,203 @@
|
||||
#include <sta/uart.hpp>
|
||||
|
||||
#include <sta/printf.hpp>
|
||||
|
||||
#include <cstring>
|
||||
#include <cinttypes>
|
||||
|
||||
|
||||
|
||||
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
|
Reference in New Issue
Block a user