From aa0a5dd0a9b9815f96794328bec70a3766706c2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Mon, 3 Apr 2017 13:32:45 +0200 Subject: [PATCH] use print library instead of stdio --- main.c | 59 ++++++++++++++++++++++++---------------------------------- 1 file changed, 24 insertions(+), 35 deletions(-) diff --git a/main.c b/main.c index 6ad5b33..77e3449 100644 --- a/main.c +++ b/main.c @@ -20,11 +20,8 @@ /* standard libraries */ #include // standard integer types -#include // standard I/O facilities #include // standard utilities -#include // standard streams #include // string utilities -#include // mathematical utilities /* STM32 (including CM3) libraries */ #include // Cortex M3 utilities @@ -40,6 +37,7 @@ /* own libraries */ #include "global.h" // board definitions +#include "print.h" // printing utilities #include "usart.h" // USART utilities #include "usb_cdcacm.h" // USB CDC ACM utilities @@ -51,33 +49,31 @@ volatile bool rtc_internal_tick_flag = false; /**< flag set when internal RTC ticked */ /** @} */ -int _write(int file, char *ptr, int len) -{ - int i; // how much data has been sent - static char newline = 0; // what newline has been sent - if (file == STDOUT_FILENO || file == STDERR_FILENO) { - for (i = 0; i < len; i++) { - if (ptr[i] == '\r' || ptr[i] == '\n') { // send CR+LF newline for most carriage return and line feed combination - if (newline==0 || (newline==ptr[i])) { // newline has already been detected - usart_putchar_nonblocking('\r'); // send newline over USART - usart_putchar_nonblocking('\n'); // send newline over USART - cdcacm_putchar('\r'); // send newline over USB - cdcacm_putchar('\n'); // send newline over USB - newline = ptr[i]; // remember the newline - } - if (ptr[i] == '\n') { // line feed are always considered to end a line (the LF+CR combination is not supported to better support the others) - newline = 0; // clear new line - } - } else { // non-newline character - usart_putchar_nonblocking(ptr[i]); // send byte over USART - cdcacm_putchar(ptr[i]); // send byte over USB - newline = 0; // clear new line - } +size_t putc(char c) +{ + size_t length = 0; // number of characters printed + static char newline = 0; // to remember on which character we sent the newline + if (0==c) { + length = 0; // don't print string termination character + } else if ('\r' == c || '\n' == c) { // send CR+LF newline for most carriage return and line feed combination + if (0==newline || c==newline) { // send newline only if not already send (and only once on \r\n or \n\r) + usart_putchar_nonblocking('\r'); // send CR over USART + cdcacm_putchar('\r'); // send CR over USB + usart_putchar_nonblocking('\n'); // send LF over USART + cdcacm_putchar('\n'); // send LF over USB + length += 2; // remember we printed 2 characters + newline = c; // remember on which character we sent the newline + } else { + length = 0; // the \r or \n of \n\r or \r\n has already been printed } - return i; + } else { + usart_putchar_nonblocking(c); // send byte over USART + cdcacm_putchar(c); // send byte over USB + newline = 0; // clear new line + length++; // remember we printed 1 character } - return -1; + return length; // return number of characters printed } /** user input command */ @@ -149,16 +145,9 @@ void main(void) iwdg_start(); // start independent watchdog #endif - // setup board - board_setup(); - - // setup USART and USB for user communication + board_setup(); // setup board usart_setup(); // setup USART (for printing) cdcacm_setup(); // setup USB CDC ACM (for printing) - setbuf(stdout, NULL); // set standard out buffer to NULL to immediately print - setbuf(stderr, NULL); // set standard error buffer to NULL to immediately print - - // minimal setup ready printf("welcome to the STM32F1 CuVoodoo example code\n"); // print welcome message #if !(DEBUG)