Uart 샘플코드

From Doowon Kim

Jump to: navigation, search
/*!
 * \example uart/uart.c
 *
 * This sample demonstrates the usage of the ATmega on-chip UART.
 * Note, that we don't do any error checking, because without this
 * UART we can't tell the user our problem.
 *
 * We use floating points. Make sure to link with nutlibcrtf.
 */

#include <cfg/crt.h>    /* Floating point configuration. */

#include <string.h>
#include <stdio.h>
#include <io.h>

#include <dev/board.h>
#include <sys/timer.h>

static char *banner = "\nNut/OS UART Sample\n";
static prog_char presskey_P[] = "Press any key...";
static prog_char pgm_ptr[] = "\nHello stranger!\n";

static char inbuf[128];
static char inbuf0[128];

/*
 * UART sample.
 *
 * Some functions do not work with ICCAVR.
 */
int main(void)
{
    int got;
	int got0;
	
    char *cp;
	char *cp0;
	
    u_long baud = 115200;
    FILE *uart;
	FILE *uart0;
	
#ifdef STDIO_FLOATING_POINT
    float dval = 0.0;
#endif

    /*
     * 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 first one on the ATmega chip. So it 
     * has no configurable base address or interrupt and we set both 
     * parameters to zero.
     */
    NutRegisterDevice(&DEV_UART, 0, 0);
	NutRegisterDevice(&DEV_UART0, 0, 0);

    /*
     * Now, as the device is registered, we can open it. The fopen()
     * function returns a pointer to a FILE structure, which we use 
     * for subsequent reading and writing.
     */
    uart = fopen("uart1", "r+");

	uart0 = fopen("uart0", "r+");
    /*
     * Before doing the first read or write, we set the baudrate.
     * This low level function doesn't know about FILE structures
     * and we use _fileno() to get the low level file descriptor
     * of the stream.
     *
     * The short sleep allows the UART to settle after the baudrate
     * change.
     */
    _ioctl(_fileno(uart), UART_SETSPEED, &baud);

	_ioctl(_fileno(uart0), UART_SETSPEED, &baud);
    /*
     * Stream devices can use low level read and write functions. 
     * Writing program space data is supported too.
     */
    _write(_fileno(uart), banner, strlen(banner));
    {
        _write_P(_fileno(uart), presskey_P, sizeof(presskey_P));
    }
	
	_write(_fileno(uart0), banner, strlen(banner));
    {
        _write_P(_fileno(uart0), presskey_P, sizeof(presskey_P));
    }
    /*
     * 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. With stream I/O we typically use
     * fflush(), but low level writing a null pointer will also flush 
     * the output buffer.
     */
    _write(_fileno(uart), 0, 0);
	
	_write(_fileno(uart0), 0, 0);
    /*
     * The low level function read() 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 = _read(_fileno(uart), inbuf, sizeof(inbuf));
	got0 = _read(_fileno(uart0), inbuf0, sizeof(inbuf0));
    
	_write(_fileno(uart), inbuf, got);
	_write(_fileno(uart0), inbuf0, got0);

    /*
     * Nut/OS never expects a thread to return. So we enter an 
     * endless loop here.
     */
    for (;;) {
        /*
         * 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.
         */
        fputs("\nEnter your name: ", uart);
        fflush(uart);
        fgets(inbuf, sizeof(inbuf), uart);

		fgets(inbuf0, sizeof(inbuf0), uart0);
        /*
         * Chop off trailing linefeed.
         */
        cp = strchr(inbuf, '\n');
		cp0 = strchr(inbuf, '\n');
        
		if (cp || cp0){
            *cp = 0;
			*cp0 = 0;
		}

        /*
         * Streams support formatted output as well as printing strings 
         * from program space.
         */
        if (inbuf[0])
            fprintf(uart, inbuf);
        else {
            fputs_P(pgm_ptr, uart);
        }
		if(inbuf0[0]){
			fprintf(uart, inbuf0);
		}

        /*
         * Just to demonstrate formatted floating point output.
         * In order to use this, we need to link the application
         * with nutcrtf instead of nutcrt for pure integer.
         */
#ifdef STDIO_FLOATING_POINT
        dval += 1.0125;
        fprintf(uart, "FP %f\n", dval);
#endif
    }
    return 0;
}