Skip to content

Commit

Permalink
[sw/lib] add nerov32-flavored vprintf funtion
Browse files Browse the repository at this point in the history
  • Loading branch information
stnolting committed Oct 23, 2023
1 parent acf4297 commit 688a429
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
1 change: 1 addition & 0 deletions sw/lib/include/neorv32_uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ char neorv32_uart_getc(neorv32_uart_t *UARTx);
int neorv32_uart_char_received(neorv32_uart_t *UARTx);
char neorv32_uart_char_received_get(neorv32_uart_t *UARTx);
void neorv32_uart_puts(neorv32_uart_t *UARTx, const char *s);
void neorv32_uart_vprintf(neorv32_uart_t *UARTx, const char *format, va_list args);
void neorv32_uart_printf(neorv32_uart_t *UARTx, const char *format, ...);
int neorv32_uart_scan(neorv32_uart_t *UARTx, char *buffer, int max_size, int echo);
/**@}*/
Expand Down
49 changes: 35 additions & 14 deletions sw/lib/source/neorv32_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,13 @@ void neorv32_uart_puts(neorv32_uart_t *UARTx, const char *s) {


/**********************************************************************//**
* Custom version of 'printf' function using UART.
* Custom version of 'vprintf'.
*
* @note This function is blocking.
*
* @param[in,out] UARTx Hardware handle to UART register struct, #neorv32_uart_t.
* @param[in] format Pointer to format string.
* @param[in] args A value identifying a variable arguments list.
*
* <TABLE>
* <TR><TD>%s</TD><TD>String (array of chars, zero-terminated)</TD></TR>
Expand All @@ -324,50 +325,54 @@ void neorv32_uart_puts(neorv32_uart_t *UARTx, const char *s) {
* <TR><TD>%p</TD><TD>32-bit pointer, printed as 8-char hexadecimal - lower-case</TD></TR>
* </TABLE>
**************************************************************************/
void neorv32_uart_printf(neorv32_uart_t *UARTx, const char *format, ...) {
void neorv32_uart_vprintf(neorv32_uart_t *UARTx, const char *format, va_list args) {

char c, string_buf[11];
char c, string_buf[12];
int32_t n;

va_list a;
va_start(a, format);

while ((c = *format++)) {
if (c == '%') {
c = *format++;
switch (c) {

case 's': // string
neorv32_uart_puts(UARTx, va_arg(a, char*));
neorv32_uart_puts(UARTx, va_arg(args, char*));
break;

case 'c': // char
neorv32_uart_putc(UARTx, (char)va_arg(a, int));
neorv32_uart_putc(UARTx, (char)va_arg(args, int));
break;

case 'i': // 32-bit signed
case 'd':
n = (int32_t)va_arg(a, int32_t);
n = (int32_t)va_arg(args, int32_t);
if (n < 0) {
n = -n;
neorv32_uart_putc(UARTx, '-');
}
__neorv32_uart_itoa((uint32_t)n, string_buf);
neorv32_uart_puts(UARTx, string_buf);
break;

case 'u': // 32-bit unsigned
__neorv32_uart_itoa(va_arg(a, uint32_t), string_buf);
__neorv32_uart_itoa(va_arg(args, uint32_t), string_buf);
neorv32_uart_puts(UARTx, string_buf);
break;

case 'x': // 32-bit hexadecimal
case 'p':
case 'X':
__neorv32_uart_tohex(va_arg(a, uint32_t), string_buf);
__neorv32_uart_tohex(va_arg(args, uint32_t), string_buf);
if (c == 'X') {
__neorv32_uart_touppercase(11, string_buf);
__neorv32_uart_touppercase(11, string_buf);
}
neorv32_uart_puts(UARTx, string_buf);
break;

case '%': // escaped percent sign
neorv32_uart_putc(UARTx, '%');
break;

default: // unsupported format
neorv32_uart_putc(UARTx, '%');
neorv32_uart_putc(UARTx, c);
Expand All @@ -381,12 +386,28 @@ void neorv32_uart_printf(neorv32_uart_t *UARTx, const char *format, ...) {
neorv32_uart_putc(UARTx, c);
}
}
va_end(a);
}


/**********************************************************************//**
* Simplified custom version of 'scanf' function for UART.
* Custom version of 'printf'.
*
* @note This function is blocking.
*
* @param[in,out] UARTx Hardware handle to UART register struct, #neorv32_uart_t.
* @param[in] format Pointer to format string. See neorv32_uart_vprintf.
**************************************************************************/
void neorv32_uart_printf(neorv32_uart_t *UARTx, const char *format, ...) {

va_list args;
va_start(args, format);
neorv32_uart_vprintf(UARTx, format, args);
va_end(args);
}


/**********************************************************************//**
* Simplified custom version of 'scanf'.
*
* @note This function is blocking.
*
Expand Down

0 comments on commit 688a429

Please sign in to comment.