Advertisement
kneefer

JA - NutOS

Oct 20th, 2014
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.94 KB | None | 0 0
  1. /*!
  2.  * Copyright (C) 2001-2003 by egnite Software GmbH
  3.  *
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * 1. Redistributions of source code must retain the above copyright
  11.  *    notice, this list of conditions and the following disclaimer.
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in the
  14.  *    documentation and/or other materials provided with the distribution.
  15.  * 3. Neither the name of the copyright holders nor the names of
  16.  *    contributors may be used to endorse or promote products derived
  17.  *    from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20.  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22.  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23.  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25.  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26.  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27.  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28.  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29.  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30.  * SUCH DAMAGE.
  31.  *
  32.  * For additional information see http://www.ethernut.de/
  33.  */
  34.  
  35. /*!
  36.  * $Id: uart.c 3538 2011-08-10 16:34:10Z haraldkipp $
  37.  */
  38.  
  39. /*!
  40.  * \example uart/uart.c
  41.  *
  42.  * This sample demonstrates the usage of the ATmega on-chip UART.
  43.  * Note, that we don't do any error checking, because without this
  44.  * UART we can't tell the user our problem.
  45.  *
  46.  * We use floating points. Make sure to link with nutlibcrtf.
  47.  */
  48.  
  49. #include <cfg/crt.h>    /* Floating point configuration. */
  50.  
  51. #include <string.h>
  52. #include <stdio.h>
  53. #include <io.h>
  54.  
  55. #include <dev/board.h>
  56. #include <sys/timer.h>
  57.  
  58. #include <dev/hd44780_bus.h>
  59. #include <dev/term.h>
  60.  
  61. static char *banner = "\nNut/OS UART Sample\n";
  62. static prog_char presskey_P[] = "Press any key...";
  63. static prog_char pgm_ptr[] = "\nHello stranger!\n";
  64.  
  65. static char inbuf[128];
  66.  
  67. /*
  68.  * UART sample.
  69.  *
  70.  * Some functions do not work with ICCAVR.
  71.  */
  72. int main(void)
  73. {
  74.     int got;
  75.     char *cp;
  76.     uint32_t baud = 115200;
  77.     FILE *uart;
  78.     NutRegisterDevice(&devLcdBus, 0xFF04, 0);
  79.     freopen("lcdbus", "w", stdout);
  80.    
  81. #ifdef STDIO_FLOATING_POINT
  82.     float dval = 0.0;
  83. #endif
  84.  
  85.     /*
  86.      * Each device must be registered. We do this by referencing the
  87.      * device structure of the driver. The advantage is, that only
  88.      * those device drivers are included in our flash code, which we
  89.      * really need.
  90.      *
  91.      * The uart0 device is the first one on the ATmega chip. So it
  92.      * has no configurable base address or interrupt and we set both
  93.      * parameters to zero.
  94.      */
  95.     NutRegisterDevice(&DEV_CONSOLE, 0, 0);
  96.  
  97.     /*
  98.      * Now, as the device is registered, we can open it. The fopen()
  99.      * function returns a pointer to a FILE structure, which we use
  100.      * for subsequent reading and writing.
  101.      */
  102.     uart = fopen(DEV_CONSOLE_NAME, "r+");
  103.  
  104.     /*
  105.      * Before doing the first read or write, we set the baudrate.
  106.      * This low level function doesn't know about FILE structures
  107.      * and we use _fileno() to get the low level file descriptor
  108.      * of the stream.
  109.      *
  110.      * The short sleep allows the UART to settle after the baudrate
  111.      * change.
  112.      */
  113.     _ioctl(_fileno(uart), UART_SETSPEED, &baud);
  114.  
  115.     /*
  116.      * Stream devices can use low level read and write functions.
  117.      * Writing program space data is supported too.
  118.      */
  119.     _write(_fileno(uart), banner, strlen(banner));
  120.     {
  121.         _write_P(_fileno(uart), presskey_P, sizeof(presskey_P));
  122.     }
  123.  
  124.     /*
  125.      * Stream devices do buffered I/O. That means, nothing will be
  126.      * passed to the hardware device until either the output buffer
  127.      * is full or we do a flush. With stream I/O we typically use
  128.      * fflush(), but low level writing a null pointer will also flush
  129.      * the output buffer.
  130.      */
  131.     _write(_fileno(uart), 0, 0);
  132.  
  133.     /*
  134.      * The low level function read() will grab all available bytes
  135.      * from the input buffer. If the buffer is empty, the call will
  136.      * block until something is available for reading.
  137.      */
  138.     got = _read(_fileno(uart), inbuf, sizeof(inbuf));
  139.     _write(_fileno(uart), inbuf, got);
  140.  
  141.     /*
  142.      * Nut/OS never expects a thread to return. So we enter an
  143.      * endless loop here.
  144.      */
  145.     for (;;) {
  146.         /*
  147.          * A bit more advanced input routine is able to read a string
  148.          * up to and including the first newline character or until a
  149.          * specified maximum number of characters, whichever comes first.
  150.          */
  151.         fputs("\nEnter your name: ", uart);
  152.         fflush(uart);
  153.         fgets(inbuf, sizeof(inbuf), uart);
  154.  
  155.         /*
  156.          * Chop off trailing linefeed.
  157.          */
  158.         cp = strchr(inbuf, '\n');
  159.         if (cp)
  160.             *cp = 0;
  161.  
  162.         /*
  163.          * Streams support formatted output as well as printing strings
  164.          * from program space.
  165.          */
  166.         if (inbuf[0])
  167.         {
  168.             fprintf(uart, "\nHello %s!\n", inbuf);
  169.             printf("\nHello %s!", inbuf);
  170.         }
  171.         else {
  172.             fputs_P(pgm_ptr, uart);
  173.         }
  174.  
  175.         /*
  176.          * Just to demonstrate formatted floating point output.
  177.          * In order to use this, we need to link the application
  178.          * with nutcrtf instead of nutcrt for pure integer.
  179.          */
  180. #ifdef STDIO_FLOATING_POINT
  181.         dval += 1.0125;
  182.         fprintf(uart, "FP %f\n", dval);
  183. #endif
  184.     }
  185.     return 0;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement