use print library instead of stdio

This commit is contained in:
King Kévin 2017-04-03 13:32:45 +02:00
parent 6531aac77b
commit aa0a5dd0a9
1 changed files with 24 additions and 35 deletions

59
main.c
View File

@ -20,11 +20,8 @@
/* standard libraries */
#include <stdint.h> // standard integer types
#include <stdio.h> // standard I/O facilities
#include <stdlib.h> // standard utilities
#include <unistd.h> // standard streams
#include <string.h> // string utilities
#include <math.h> // mathematical utilities
/* STM32 (including CM3) libraries */
#include <libopencmsis/core_cm3.h> // 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)