Main Page   Modules   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages   Examples  

uart/nutmain.c

This sample demonstrates the usage of the ATmega 103 on-chip UART. Note, that we don't do any error checking, because without this UART we can't tell the user our problem.

#include <string.h>

#include <dev/uartavr.h>    /* Reference to the uart0 device driver. */

#include <sys/thread.h>     /* Contains the THREAD macro. */

#include <sys/print.h>      /* Formatted output functions. */

static const char *banner = "\r\nNut/OS UART Sample\r\n";

static char inbuf[128];

#include <sys/timer.h>
THREAD(NutMain, arg)
{
    int got;
    int i;
    NUTDEVICE *uart;
    u_long baud = 115200;

    /*
     * Each device must be registered. We do this
     * by referencing the device structure of the
     * driver. The advantage is, that only those
     * device drivers are included in our flash
     * code, which we really need.
     *
     * The uart0 device is the one on the ATmega
     * chip. So it has no configurable base address
     * or interrupt and we set both items to zero.
     */
    NutRegisterDevice(&devUart0, 0, 0);

    /*
     * Now, as the device is registered, we can
     * open it. On stream devices, like uart0,
     * this call will initialize the I/O buffers.
     * The function returns a pointer to the
     * device structure, which we use for subsequent
     * reading and writing.
     */
    uart = NutDeviceOpen("uart0");
    NutDeviceIOCtl(uart, UART_SETSPEED, &baud);

    /*
     * Stream devices can use simple read and write
     * functions. Writing program space data is
     * supported too.
     */
    NutDeviceWrite(uart, banner, strlen(banner));
    NutDeviceWrite_P(uart, PSTR("Press key..."), 12);

    /*
     * Stream devices do buffered I/O. That means,
     * nothing will be passed to the hardware
     * device until either the output buffer is
     * full or we do a flush. Writing nothing
     * will flush the output buffer.
     */
    NutDeviceWrite(uart, 0, 0);

    /*
     * The NutDeviceRead() function will grab all
     * available bytes from the input buffer.
     * If the buffer is empty, the call will
     * block until something is available
     * for reading.
     */
    got = NutDeviceRead(uart, inbuf, sizeof(inbuf));
    NutDeviceWrite(uart, inbuf, got);

    /*
     * Nut/OS never expects a thread to return.
     * So we enter an endless loop here.
     */
    for(i = 0;; i++) {
        /*
         * A bit more advanced input routine is
         * able to read a string up to and including
         * the first newline character or until a
         * specified maximum number of characters,
         * whichever comes first.
         */
        NutDeviceWrite_P(uart, PSTR("\r\nEnter your name: "), 19);
        NutDeviceWrite(uart, 0, 0);
        got = NutDeviceGetLine(uart, inbuf, sizeof(inbuf) - 1);
        inbuf[got] = 0;

        /*
         * Stream devices can use the more advanced
         * NutPrint functions. They support formatted
         * output as well as printing strings from
         * program space.
         */
        if(inbuf[0])
            NutPrintFormat(uart, "Hello %s!\r\n", inbuf);
        else
            NutPrintString_P(uart, PSTR("Hello stranger!\r\n"));

        /*
         * We might have used NutDeviceWrite() to flush
         * the output buffer. However, NutPrintFlush
         * looks nicer.
         */
        NutPrintFlush(uart);

        /*
         * Simply to demonstrate some formatting
         * options.
         */
        NutPrintFormat(uart, "Loop +%udec %04Xhex\r\n", i + 1, i + 1);
    }
}

© 2000-2002 by egnite Software GmbH - visit http://www.ethernut.de/