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 small printf function is a tiny 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.

source code for printf.c

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;