Basic functions

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).

This page contains the source code for:

Printf source code

This 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 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.

source code for printf.c without stdarg.h, you might have to modify it to work with your compiler with regards to the variable parameter passing.

source code for printf.c with stdarg.h, this version contributed by Christian Ettinger, to use if you have a working stdarg.h for your compiler.

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')
                return;
        if (c == '\n' && prev != '\r') _outbyte('\r');
        _outbyte(c);
        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.

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.

sig
Valid HTML 4.01!