Basic functions for embedded software

This page is a collection of utility functions which I did not find easily on the net. Either because the source code was not available, or there were part of a big library not easy to extract from. The source code is released under LGPL (Lesser GNU Public License) or under the BSD license for the xmodem functions.

This page contains the source code for:

Small printf source code

This tiny printf function is a small implementation of the standard C library function, it does not include all the capabilities of the standard one. It has been designed to have a small footprint and to be easy to include in an embedded software. The only external dependency is the putchar() functions which output a single character to the output device. Once you provide this putchar function, you may link your code with this printf and use it. This code is released under the LGPL license.

This printf function will accept integer formats (%d, %x, %X, %u), string format (%s) and character format (%c); left and right alignement, padding with space or 'O'. See the comments and the test code at the end of the source code.

On my DragonEngine (68VZ328) board, I am using the following code to replace putchar :

void _outbyte(int c)
        while ((readb(UTX1) & 0x20) == 0)

        writeb ((char)c,UTX1+1);

void outbyte(int c)
        static char prev = 0;
        if (c < ' ' && c != '\r' && c != '\n' && c != '\t' && c != '\b')
        if (c == '\n' && prev != '\r') _outbyte('\r');
        prev = c;

_outbyte does the raw output of any char, while outbyte call it for printable chars only and add a CR character when needed. This is a very simple scheme, kind of raw versus cooked mode.

Xmodem source code

The xmodem functions has been tested with several terminal emulators on GNU/Linux and windows. I did use the following xmodem specification to write them. This code is released under the BSD license.

The xmodem functions depends on memcpy and memset which are standard C library functions, they are builtin function in gcc. The last two dependencies are _inbyte and _outbyte. The _outbyte function is the one shown before; on my 68VZ328 board, I am using the following code for _inbyte :

int _inbyte(unsigned short timeout) // msec timeout
        unsigned short c;
        int delay = timeout << 4;

        while (((c=readw(URX1)) & 0x2000) == 0) {
                usleep(60); /* 60 us * 16 = 960 us (~ 1 ms) */
                if (timeout) {
                        if (--delay == 0) return -2;

        return c & 0x0FF;

_inbyte has a timeout parameter with 1 msec resolution, the timeout is build using a usleep function which could be a busy loop of 1 µsec resolution.

As an alternative to the code using a table contained in crc16.c, you might use the following function. This is specially useful in case of memory constraints. (contributed by Steve Checkoway, thanks !)

unsigned short crc16_ccitt( const void *buf, int len )
	unsigned short crc = 0;
	while( len-- ) {
		int i;
		crc ^= *(char *)buf++ << 8;
		for( i = 0; i < 8; ++i ) {
			if( crc & 0x8000 )
				crc = (crc << 1) ^ 0x1021;
				crc = crc << 1;
	return crc;