uart.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. * $Id: uart.c 4942 2013-01-22 18:56:24Z haraldkipp $
  36. */
  37. /*!
  38. * \example uart/uart.c
  39. *
  40. * This sample demonstrates the usage of the ATmega on-chip UART.
  41. * Note, that we don't do any error checking, because without this
  42. * UART we can't tell the user our problem.
  43. *
  44. * We use floating points. Make sure to link with nutlibcrtf.
  45. */
  46. #include <cfg/crt.h> /* Floating point configuration. */
  47. #include <string.h>
  48. #include <stdio.h>
  49. #include <io.h>
  50. #include <dev/board.h>
  51. #include <sys/timer.h>
  52. static char *banner = "\nNut/OS UART Sample " __DATE__ " " __TIME__"\n";
  53. static const char presskey_P[] PROGMEM = "Press any key...";
  54. static const char pgm_ptr[] PROGMEM = "\nHello stranger!\n";
  55. static char inbuf[128];
  56. /*
  57. * UART sample.
  58. *
  59. * Some functions do not work with ICCAVR.
  60. */
  61. int main(void)
  62. {
  63. int got;
  64. char *cp;
  65. uint32_t baud = 115200;
  66. FILE *uart;
  67. #ifdef STDIO_FLOATING_POINT
  68. float dval = 0.0;
  69. #endif
  70. /*
  71. * Each device must be registered. We do this by referencing the
  72. * device structure of the driver. The advantage is, that only
  73. * those device drivers are included in our flash code, which we
  74. * really need.
  75. *
  76. * The uart0 device is the first one on the ATmega chip. So it
  77. * has no configurable base address or interrupt and we set both
  78. * parameters to zero.
  79. */
  80. NutRegisterDevice(&DEV_CONSOLE, 0, 0);
  81. /*
  82. * Now, as the device is registered, we can open it. The fopen()
  83. * function returns a pointer to a FILE structure, which we use
  84. * for subsequent reading and writing.
  85. */
  86. uart = fopen(DEV_CONSOLE.dev_name, "r+");
  87. /*
  88. * Before doing the first read or write, we set the baudrate.
  89. * This low level function doesn't know about FILE structures
  90. * and we use _fileno() to get the low level file descriptor
  91. * of the stream.
  92. *
  93. * The short sleep allows the UART to settle after the baudrate
  94. * change.
  95. */
  96. _ioctl(_fileno(uart), UART_SETSPEED, &baud);
  97. /*
  98. * Stream devices can use low level read and write functions.
  99. * Writing program space data is supported too.
  100. */
  101. _write(_fileno(uart), banner, strlen(banner));
  102. {
  103. _write_P(_fileno(uart), presskey_P, sizeof(presskey_P));
  104. }
  105. /*
  106. * Stream devices do buffered I/O. That means, nothing will be
  107. * passed to the hardware device until either the output buffer
  108. * is full or we do a flush. With stream I/O we typically use
  109. * fflush(), but low level writing a null pointer will also flush
  110. * the output buffer.
  111. */
  112. _write(_fileno(uart), 0, 0);
  113. /*
  114. * The low level function read() will grab all available bytes
  115. * from the input buffer. If the buffer is empty, the call will
  116. * block until something is available for reading.
  117. */
  118. got = _read(_fileno(uart), inbuf, sizeof(inbuf));
  119. _write(_fileno(uart), inbuf, got);
  120. /*
  121. * Nut/OS never expects a thread to return. So we enter an
  122. * endless loop here.
  123. */
  124. for (;;) {
  125. /*
  126. * A bit more advanced input routine is able to read a string
  127. * up to and including the first newline character or until a
  128. * specified maximum number of characters, whichever comes first.
  129. */
  130. fputs("\nEnter your name: ", uart);
  131. fflush(uart);
  132. fgets(inbuf, sizeof(inbuf), uart);
  133. /*
  134. * Chop off trailing linefeed.
  135. */
  136. cp = strchr(inbuf, '\n');
  137. if (cp)
  138. *cp = 0;
  139. /*
  140. * Streams support formatted output as well as printing strings
  141. * from program space.
  142. */
  143. if (inbuf[0])
  144. fprintf(uart, "\nHello %s!\n", inbuf);
  145. else {
  146. fputs_P(pgm_ptr, uart);
  147. }
  148. /*
  149. * Just to demonstrate formatted floating point output.
  150. * In order to use this, we need to link the application
  151. * with nutcrtf instead of nutcrt for pure integer.
  152. */
  153. #ifdef STDIO_FLOATING_POINT
  154. dval += 1.0125;
  155. fprintf(uart, "FP %f\n", dval);
  156. #endif
  157. }
  158. return 0;
  159. }