uart.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (C) 2001-2006 by egnite Software GmbH
  3. * Copyright (C) 2009 by egnite GmbH
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. */
  35. /*
  36. * $Id: uart.c 3597 2011-10-21 16:17:18Z haraldkipp $
  37. *
  38. * WARNING! Do not use any part of Basemon for your own applications. WARNING!
  39. *
  40. * This is not a typical application sample. It overrides parts of Nut/OS to
  41. * keep it running on broken hardware.
  42. */
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include <sys/timer.h>
  46. #include <arpa/inet.h>
  47. #include "utils.h"
  48. #include "uart.h"
  49. prog_char presskey_P[] = "Press any key to stop\n";
  50. char inbuff[32];
  51. /*
  52. * Wait for a character from the serial port.
  53. */
  54. uint8_t GetChar(void)
  55. {
  56. uint8_t ch;
  57. #ifdef HEARTBEAT_BIT
  58. HeartBeat();
  59. #endif
  60. #if defined (__AVR__)
  61. if ((inb(USR) & _BV(RXC)) == 0)
  62. return 0;
  63. ch = inb(UDR);
  64. #else
  65. ch = 0;
  66. #endif
  67. return ch;
  68. }
  69. /*
  70. * Get a line of input.
  71. */
  72. int GetLine(char * line, int size)
  73. {
  74. int cnt = 0;
  75. uint8_t ch;
  76. GetChar();
  77. for (;;) {
  78. if (cnt > size - 1)
  79. break;
  80. while ((ch = GetChar()) == 0);
  81. if (ch < 32)
  82. break;
  83. putchar(ch);
  84. *line++ = ch;
  85. cnt++;
  86. }
  87. *line = 0;
  88. putchar('\n');
  89. return cnt;
  90. }
  91. /*
  92. * Get a line of input.
  93. */
  94. char *GetIP(char * prompt, char * value)
  95. {
  96. static prog_char badip_P[] = "Bad IP address";
  97. for (;;) {
  98. printf("%s (%s): ", prompt, value);
  99. if (GetLine(inbuff, sizeof(inbuff)) == 0)
  100. break;
  101. if (inet_addr(inbuff) != (uint32_t) (-1L)) {
  102. strcpy(value, inbuff);
  103. break;
  104. }
  105. puts_P(badip_P);
  106. }
  107. return value;
  108. }
  109. /*
  110. * Automatic baudrate detection.
  111. *
  112. * Switch to different baud rates and wait
  113. * for a space character.
  114. */
  115. int DetectSpeed(void)
  116. {
  117. #if defined (__AVR__)
  118. uint8_t bstab[] = {
  119. 1, /* 115200 @ 3.6864 */
  120. 7, /* 115200 @ 14.7456 */
  121. 23, /* 9600 @ 3.6864, 19200 @ 7.3728, 38400 @ 14.7456 */
  122. 11, /* 19200 @ 3.6864, 38400 @ 7.3728, 76800 @ 14.7456 */
  123. 12, /* 19200 @ 4.0000, 38400 @ 8.0000, 57600 @ 12.0000, 76800 @ 16.0000 */
  124. 25, /* 9600 @ 4.0000, 19200 @ 8.0000, 38400 @ 16.0000 */
  125. 5, /* 38400 @ 3.6864, 76800 @ 7.3728 */
  126. 47, /* 9600 @ 7.3728, 19200 @ 14.7456 */
  127. 51, /* 9600 @ 8.0000, 19200 @ 16.0000 */
  128. 3, /* 115200 @ 7.3728 */
  129. 9, /* 115200 @ 18.4320 */
  130. 2, /* 76800 @ 3.6864 */
  131. 14, /* 76800 @ 18.4320 */
  132. 29, /* 38400 @ 18.4320 */
  133. 64, /* 19200 @ 20.0000 */
  134. 59, /* 19200 @ 18.4320 */
  135. 95, /* 9600 @ 14.7456 */
  136. 103, /* 9600 @ 16.0000 */
  137. 119, /* 9600 @ 18.4320 */
  138. 129, /* 9600 @ 20.0000 */
  139. 77 /* 9600 @ 12.0000 */
  140. };
  141. uint8_t bsx;
  142. int bs;
  143. uint16_t i;
  144. uint8_t t;
  145. uint8_t rec;
  146. uint8_t ict; /* Imagecraft dummy */
  147. /*
  148. * Enable UART transmitter and receiver.
  149. */
  150. outb(UCR, _BV(RXEN) | _BV(TXEN));
  151. /*
  152. * Sixteen retries.
  153. */
  154. for (t = 1; t < 16; t++) {
  155. for (bsx = 0; bsx < sizeof(bstab); bsx++) {
  156. bs = bstab[bsx];
  157. /*
  158. * Set baudrate selector.
  159. */
  160. Delay(1000);
  161. if ((inb(USR) & _BV(RXC)) != 0)
  162. ict = inb(UDR);
  163. outb(UBRR, (uint8_t) bs);
  164. Delay(1000);
  165. if ((inb(USR) & _BV(RXC)) != 0)
  166. ict = inb(UDR);
  167. /*
  168. * Now wait for some time for a character received.
  169. */
  170. for (i = 0; i < 40; i++) {
  171. if ((inb(USR) & _BV(RXC)) != 0)
  172. break;
  173. Delay(1000);
  174. }
  175. rec = 0;
  176. for (i = 1; i; i++) {
  177. if ((inb(USR) & _BV(RXC)) != 0) {
  178. if (inb(UDR) == 32) {
  179. if (rec++)
  180. return bs;
  181. i = 1;
  182. } else {
  183. rec = 0;
  184. break;
  185. }
  186. } else if (rec)
  187. Delay(100);
  188. }
  189. }
  190. }
  191. outb(UBRR, 23);
  192. #endif
  193. return -1;
  194. }