Skip to content

Instantly share code, notes, and snippets.

@vgonisanz
Created April 20, 2018 05:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vgonisanz/91d862e5b7950d1b833a504e87d37d84 to your computer and use it in GitHub Desktop.
Save vgonisanz/91d862e5b7950d1b833a504e87d37d84 to your computer and use it in GitHub Desktop.
/*
* How configure UART to use printf using avr-libc, reference: http://www.nongnu.org/avr-libc/user-manual/index.html
* This code was compiled using arduino-cmake: https://github.com/arduino-cmake/arduino-cmake.git
* cmake -DINPUT_BOARD=pro -DINPUT_PORT=/dev/ttyUSB0 -DBOARD_CPU=16MHzatmega328 ..
* make
* make upload # push reset button before
*/
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <util/delay.h>
#include <stdio.h>
/* UART buffering */
static FILE uartout = { 0 }; // FILE struct
static int uart_putchar(char c, FILE *stream);
// uart_putchar - char output function for printf
static int uart_putchar(char c, FILE *stream)
{
if( c == '\n' )
Serial.write('\r');
Serial.write(c) ;
return 0 ;
}
void configure_uart()
{
fdev_setup_stream(&uartout, uart_putchar, NULL, _FDEV_SETUP_WRITE);
stdout = &uartout;
printf("setup stdout\n");
}
void setup()
{
Serial.begin(9600);
configure_uart();
Serial.println("setup println");
}
void loop()
{
while(true)
{
printf("loop printf\n");
Serial.println("loop println");
_delay_ms(1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment