Compare commits

..

5 Commits

4 changed files with 276 additions and 20 deletions

View File

@ -8,20 +8,21 @@
//--includes--------------------------------------------------------------------
#include "debug.h"
#include "dma_mbuf.h"
#include "format.h"
//--local definitions-----------------------------------------------------------
#define BUFFER_SIZE 83 //80 char line + \n + 2 bytes for index
#define BUFFER_NB 3 //1 buffer in write, one in transfer and one waiting
static uint32_t write_debug(uint8_t c, void* arg);
#define BUFFER_SIZE 162 //(80 char line + \n) * 2
//--local variables-------------------------------------------------------------
static enum UsartPeriph usart_periph;
static uint8_t tx_buffer1[BUFFER_SIZE];
static uint8_t tx_buffer2[BUFFER_SIZE];
static uint8_t tx_buffer3[BUFFER_SIZE];
static uint8_t* tx_buffers[BUFFER_NB] = {tx_buffer1, tx_buffer2, tx_buffer3};
static struct DmaMultiBuffer mbuf;
static uint8_t tx_buffer[BUFFER_SIZE];
//--public functions------------------------------------------------------------
@ -31,30 +32,79 @@ static uint8_t* tx_buffers[BUFFER_NB] = {tx_buffer1, tx_buffer2, tx_buffer3};
void _debug_init(enum UsartPeriph usart, enum GpioPort tx_port,
enum GpioPin tx_pin)
{
usart_periph = usart;
gpio_configure(tx_port, tx_pin, GPIO_MODE_OUTPUT_FAST,
GPIO_CONFIG_OUT_ALT_PUSH_PULL);
usart_configure(usart_periph, USART_CONFIG_8N1, 1152000);
usart_set_tx_buffer(USART_PERIPH_3, tx_buffers, BUFFER_SIZE, BUFFER_NB,
DMA_CONFIG_PRIO_LOW);
usart_configure(usart, USART_CONFIG_8N1, 1000000);
dma_mbuf_configure(&mbuf,usart_configure_tx_dma(usart),
DMA_CONFIG_PRIO_LOW, tx_buffer, BUFFER_SIZE);
debug_trace("\n");
debug_trace("--------------------------------------------------------------------------------\n");
debug_trace("------------------------------------------------------------------------------\n");
debug_trace("starting debug software\n");
debug_trace("compiled on " __DATE__ " at " __TIME__ "\n");
debug_trace("--------------------------------------------------------------------------------\n");
debug_trace("------------------------------------------------------------------------------\n");
}
#endif
#if DEBUG_TRACE
void _debug_trace(char* format, ...)
{
while(*format != '\0') {
while(usart_write_byte(usart_periph, *format)) {}
++format;
write_debug('T', nullptr);
write_debug(':', nullptr);
va_list va;
va_start(va, format);
format_vfctprintf(write_debug, nullptr, format, va);
va_end(va);
write_debug('\n', nullptr);
//ensure everything is written when done
while (dma_mbuf_switch(&mbuf)) {}
}
#endif
#if DEBUG_WARN
void _debug_warn(char* format, ...)
{
print_debug("W:");
print_debug(format);
//ensure everything is written when done
while (dma_mbuf_switch(&mbuf)) {}
}
#endif
#if DEBUG_ERROR
void _debug_error(char* format, ...)
{
print_debug("E:");
print_debug(format);
//ensure everything is written when done
while (dma_mbuf_switch(&mbuf)) {}
}
#endif
//--local functions-------------------------------------------------------------
#if DEBUG_TRACE || DEBUG_WARN || DEBUG_ERROR
static uint32_t write_debug(uint8_t c, void* arg)
{
while (dma_mbuf_write_byte(&mbuf, c))
{
//buffer is full, wait until transfer started
while (dma_mbuf_switch(&mbuf)) {}
}
return 0;
}
#endif

View File

@ -13,12 +13,11 @@
#include "../drv/usart.h"
#include "../drv/gpio.h"
#include "stdint.h"
#define DEBUG_TRACE 1
#define DEBUG_WARN 0
#define DEBUG_ERROR 0
//--type definitions------------------------------------------------------------
#if DEBUG_TRACE || DEBUG_WARN || DEBUG_ERROR

174
srv/format.c Normal file
View File

@ -0,0 +1,174 @@
/** @file format.c
* Module handling various formating functions
*
* The module provides a lightweight printf function and the attached tools
*/
//--includes--------------------------------------------------------------------
#include "format.h"
//--local definitions-----------------------------------------------------------
struct BufferArg {
char* restrict buffer;
uint32_t buffer_size;
uint32_t byte_index;
};
static uint32_t buffer_write(uint8_t byte, void* arg);
static uint32_t render_format(FormatCallback callback, void* arg,
const char* restrict format, va_list va);
static uint32_t render_signed(FormatCallback callback, void* arg,
int32_t number, uint8_t base);
static uint32_t render_unsigned(FormatCallback callback, void* arg,
uint32_t number, uint8_t base);
//--local variables-------------------------------------------------------------
//--public functions------------------------------------------------------------
uint32_t format_snprintf(char* restrict buffer, uint32_t buffer_size,
const char* restrict format, ...)
{
va_list va;
va_start(va, format);
struct BufferArg arg = { buffer, buffer_size, 0 };
uint32_t ret = render_format(buffer_write, &arg, format, va);
va_end(va);
return ret;
}
uint32_t format_fctprintf(FormatCallback callback, void* arg,
const char* restrict format, ...)
{
va_list va;
va_start(va, format);
uint32_t ret = render_format(callback, arg, format, va);
va_end(va);
return ret;
}
uint32_t format_vfctprintf(FormatCallback callback, void* arg,
const char* restrict format, va_list va)
{
return render_format(callback, arg, format, va);
}
//--local functions-------------------------------------------------------------
static uint32_t buffer_write(uint8_t byte, void* arg)
{
struct BufferArg* buffer_arg = (struct BufferArg*)arg;
if (buffer_arg->byte_index >= buffer_arg->buffer_size) {
return 1;
}
buffer_arg->buffer[buffer_arg->byte_index] = byte;
++buffer_arg->byte_index;
return 0;
}
static uint32_t render_format(FormatCallback callback, void* arg,
const char* restrict format, va_list va)
{
bool in_format = false;
for (const char* c = format; *c != '\0'; ++c) {
if (in_format) {
switch (*c) {
case '%':
if (callback('%', arg)) return 1;
break;
case 'c':
if (callback((uint8_t)va_arg(va, uint32_t), arg)) {
return 1;
}
break;
case 's':
{
const char* str = va_arg(va, const char*);
for (const char* c2 = str; *c2 != '\0'; ++c2) {
if (callback(*c2, arg)) return 1;
}
}
break;
case 'i':
if (render_signed(callback, arg, va_arg(va, int32_t), 10)) {
return 1;
}
break;
case 'u':
if (render_unsigned(callback, arg,
va_arg(va, uint32_t), 10)) {
return 1;
}
break;
case 'x':
if (render_unsigned(callback, arg,
va_arg(va, uint32_t), 16)) {
return 1;
}
break;
default:
//TODO manage error
break;
}
} else {
switch (*c) {
case '%':
in_format = true;
continue;
default:
if (callback(*c, arg)) return 1;
break;
}
}
in_format = false;
}
return 0;
}
static uint32_t render_signed(FormatCallback callback, void* arg,
int32_t number, uint8_t base)
{
if (number < 0) {
callback('-', arg);
number = -number;
}
return render_unsigned(callback, arg, (uint32_t)number, base);
}
static uint32_t render_unsigned(FormatCallback callback, void* arg,
uint32_t number, uint8_t base)
{
uint32_t base_power = base;
while (base_power <= number) {
base_power *= base;
}
while (base_power > 1) {
base_power /= base;
uint8_t digit = 0;
while (number >= base_power) {
number -= base_power;
++digit;
}
if (digit < 10) {
if (callback('0' + digit, arg)) return 1;
} else {
if (callback('A' + digit - 10, arg)) return 1;
}
}
return 0;
}

33
srv/format.h Normal file
View File

@ -0,0 +1,33 @@
/** @file format.h
* Module handling various formating functions
*
* The module provides a lightweight printf function and the attached tools
*/
#ifndef _FORMAT_H_
#define _FORMAT_H_
//--includes--------------------------------------------------------------------
#include <stdint.h>
#include <stdarg.h>
//--type definitions------------------------------------------------------------
typedef uint32_t (*FormatCallback)(uint8_t byte, void* arg);
//--functions-------------------------------------------------------------------
uint32_t format_snprintf(char* restrict buffer, uint32_t buffer_size,
const char* restrict format, ...);
uint32_t format_fctprintf(FormatCallback callback, void* arg,
const char* restrict format, ...);
uint32_t format_vfctprintf(FormatCallback callback, void* arg,
const char* restrict format, va_list va);
#endif //_FORMAT_H_