Author Topic: Arduino Due, how to direct stdio to USART0 or USART1  (Read 1772 times)

0 Members and 1 Guest are viewing this topic.

Offline riscy00Topic starter

  • Contributor
  • Posts: 22
Arduino Due, how to direct stdio to USART0 or USART1
« on: April 25, 2015, 07:09:27 am »
Hi

Arduino Due with SAM3X (Cortex M3), I could not find material that advise how to configure commands from STDIO.h to redirect serial message stream to specfic port, for example printf("Hello World!\n) to USART0 or USART1.....







 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: Arduino Due, how to direct stdio to USART0 or USART1
« Reply #1 on: April 25, 2015, 08:00:19 am »
I believe that Due uses newlib, so the following should work.  (I haven't actually tried it on a Due (I don't have one), but it works for Energia Stellaris platforms. See this thread: http://forum.43oh.com/topic/8308-yet-again-another-boring-question-about-energia-printf/ )

Code: [Select]
ssize_t myWrite(void *cookie, const char *buf, size_t n)
{
  return Serial.write((uint8_t*)buf, n);
}

cookie_io_functions_t myVectors = { 0, myWrite, 0, 0 };

void setup() {               
  Serial.begin(115200);
  stdout = fopencookie((void *)0, "w", myVectors);
  setlinebuf(stdout);
  printf( "This is an fprintf demo\n");
}

This sucks in the well-known large-ish printf(), and it also sucks in newlib's whole stdio/file/etc implementation, adding a total of about 20k to your sketch binary size.   And it may have weird buffering issues and need an occasional fflush(stdout) call.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf