mprintf: a simple thread safe printf for DuinoOS Part A: Getting non threaded printf to work

I thought it best that I document how I got printf to work and how I made it thread safe for DuinoOS (or freeRTOS for that matter)

Getting non threaded printf to work.

printf makes use of a file handle called stdout. This file handle is null unless we do something to change its value. the stdio library provides us with a method to initialize its value. We, however, must initialize the serial port and provide a function for stdio to use when it needs to send a character to stdio.

The stdio routine we use to initialize stdout is: fdevopen(<function name>,NULL).

update 20100320:  Daniel Haney, std@salversan.org provided the following comment and improvement which I have implemented.  The latest cserial.cpp file contains the needed changes.

<<<<<<<<<<<<<<<<<snip>>>>>>>>>>>>>

Thanks for your wonderfully concise code for redefining stdout. Reading through stdio.h for avr-gcc, they say fdevopen() implicitly calls malloc, which is problematic given the Arduino’s small memory.
They recommend using fdev_setup_stream(). It works and I seem to gain ~200 bytes.

<<<<<<<<<<<<<<<<end snip>>>>>>>>>>>

We need to create two routines. One that we use as a call back function to be passed into the fdevopen routine and one that we will use in our sketch or c/c++ code to initialize our port and initialized fdevopen. We will use two files. ‘cserial.cpp’ file and a ‘cserial.h’ file. The ‘cserial.h’ file will be included in our sketch/c/c++ file so that we have access to call the initialization routine. The source of the two files are located at the bottom of this blog entry

These two files must be placed into ~/arduino-0017/hardware/libraries/cserial directory or equivalent. We may need to create this directory.

We can now use the initialization routine: serial_stdout_init(<speed>) in our sketches (or c or c++) setup function. Add a line that looks like this:

serial_stdout_init(9600);

Don’t forget to add the cserial.h include file at the top of your the sketch (or program)

We can now use the non-threaded version of printf which should be fine using the standard Arduino. Next, how to make a thread safe version..

———————– files——————-

cserial.h

<<<<<<<<<<<<<<<<snip>>>>>>>>>>>>>>>

#ifndef cserial_h
#define cserial_h

#include <stdio.h>

static int serial_write(char,FILE);
void serial_stdout_init(long);

#endif

<<<<<<<<<<<<<<<end snip>>>>>>>>>>>>>>>>>

cserial.cpp

<<<<<<<<<<<<<<snip>>>>>>>>>>>>>>>>>>>>>

/* cserial.cpp */
// these two c functions are required so that
// stdout can be initialized for the stdio library
// This binds the printf function to the hardware serial port

#include “cserial.h”
#include “wiring_private.h”

static int serial_write(char c, FILE *f) {

/* Wait for transmit buffer to be empty */
while ((UCSR0A & _BV(UDRE0)) == 0)
;

/* Put byte into transmit buffer */
UDR0 = c;

return 0;
}

static FILE sio_stdout = {0} ;                           /* added to support the DH mod eliminating stdio malloc requirements */

void serial_stdout_init (long speed) {
/* Set baud rate */
uint16_t factor = (F_CPU / 16 + speed / 2) / speed – 1;
UBRR0H = factor >> 8;
UBRR0L = factor;

/* Set format (8N1) */
UCSR0C = 3 << UCSZ00;

/* Enable transmitter */

UCSR0B = _BV(TXEN0);

/* Set up stdout to write to the serial port */
/* stdout = fdevopen (serial_write, NULL);  replaced with the following care of DH */

fdev_setup_stream(&sio_stdout, serial_write, NULL,_FDEV_SETUP_WRITE);
stdout = &sio_stdout ;

}

<<<<<<<<<<<<<<<<<<end snip>>>>>>>>>

Source : http://www.out--there.com/blog/mprintf-thread-safe-freertos-printf-part/
Date : Sunday, January 10, 2010