Big assert / debug clean up

This commit is contained in:
Dario
2023-07-12 14:05:51 +01:00
parent 27350f71b8
commit 6da6666559
26 changed files with 108 additions and 218 deletions

30
src/debug/assert.cpp Normal file
View File

@@ -0,0 +1,30 @@
#include <sta/debug/assert.hpp>
#ifdef STA_ASSERT_ENABLED
#include <sta/debug/debug.hpp>
#include <sta/lang.hpp>
#include <cstdio>
namespace sta
{
STA_WEAK
void assert_failed(const char * expr, const char * file, uint32_t line)
{
STA_DEBUG_PRINT(file);
STA_DEBUG_PRINT(':');
STA_DEBUG_PRINT(line);
STA_DEBUG_PRINT(": Assertion failed: ");
STA_DEBUG_PRINTLN(expr);
}
STA_WEAK
void assert_halt()
{
STA_BKPT();
while (true);
}
} // namespace sta
#endif // STA_ASSERT_ENABLED

View File

@@ -0,0 +1,185 @@
#include <sta/debug/printing/printable.hpp>
#include <cinttypes>
#include <cstring>
#include <cstdio>
#include <sta/debug/assert.hpp>
#include <sta/lang.hpp>
namespace sta
{
void Printable::print(char c)
{
print(&c, 1);
}
void Printable::print(bool b)
{
print(b ? "true" : "false");
}
void Printable::print(double d)
{
char buffer[64];
snprintf(buffer, sizeof(buffer), "%f", d);
print(buffer);
}
void Printable::print(uint8_t num, IntegerBase base)
{
printBase(num, base, "%" PRIu8, sizeof(num));
}
void Printable::print(uint16_t num, IntegerBase base)
{
printBase(num, base, "%" PRIu16, sizeof(num));
}
void Printable::print(uint32_t num, IntegerBase base)
{
printBase(num, base, "%" PRIu32, sizeof(num));
}
void Printable::print(const char * str)
{
print(str, strlen(str));
}
void Printable::println()
{
print("\r\n", 2);
}
void Printable::println(char c)
{
print(&c, 1);
println();
}
void Printable::println(bool b)
{
print(b);
println();
}
void Printable::println(double d)
{
print(d);
println();
}
void Printable::println(uint8_t num, IntegerBase base)
{
print(num, base);
println();
}
void Printable::println(uint16_t num, IntegerBase base)
{
print(num, base);
println();
}
void Printable::println(uint32_t num, IntegerBase base)
{
print(num, base);
println();
}
void Printable::println(const char * str)
{
println(str, strlen(str));
}
void Printable::println(const char * str, size_t length)
{
print(str, length);
println();
}
void Printable::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 Printable::printDec(uintmax_t num, const char * fmt)
{
char buffer[64];
snprintf(buffer, sizeof(buffer), fmt, static_cast<uint32_t>(num));
print(buffer);
}
void Printable::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 Printable::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

View File

@@ -0,0 +1,20 @@
#include <sta/debug/printing/printable_printf.hpp>
#include <sta/debug/assert.hpp>
#include <cstdio>
namespace sta
{
PrintablePrintf::PrintablePrintf()
{
}
void PrintablePrintf::print(const char * str, size_t length)
{
STA_ASSERT(str != nullptr);
STA_ASSERT(length > 0);
printf("%.*s", length, str);
}
} // namespace sta

View File

@@ -0,0 +1,25 @@
#include <sta/debug/printing/printable_uart.hpp>
#include <sta/debug/assert.hpp>
#include <sta/printf.hpp>
#include <cinttypes>
#include <cstring>
namespace sta
{
PrintableUART::PrintableUART(UART * intf)
: intf_{intf}
{
STA_ASSERT(intf != nullptr);
STA_ASSERT(intf->settings().mode == UARTMode::RX || intf->settings().mode == UARTMode::RX_TX);
}
void PrintableUART::print(const char * str, size_t length)
{
intf_->transfer(reinterpret_cast<const uint8_t *>(str), length);
}
} // namespace sta