uart0driver.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*!
  2. * Copyright (C) 2003 by Streamit All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. All advertising materials mentioning features or use of this
  14. * software must display the following acknowledgement:
  15. *
  16. * This product includes software developed by Streamit
  17. * and its contributors.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY STREAMIT 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 CALL DIRECT
  23. * CELLULAR SOLUTIONS 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.streamit.nl/
  33. */
  34. /*!
  35. *
  36. * COPYRIGHT STREAMIT BV 2010
  37. * Tested except for the DTR code. This will be done when the final hardware is available
  38. *
  39. *
  40. * [Note] this UART0 is used for serial communication with LTP (or a PC in general).
  41. */
  42. #define LOG_MODULE LOG_UART0DRIVER_MODULE
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include <io.h>
  47. #include <dev/uartavr.h>
  48. //#include <sys/heap.h>
  49. #include <sys/thread.h>
  50. #include <sys/timer.h>
  51. //#pragma text:appcode
  52. #include "log.h"
  53. #include "system.h"
  54. #include "uart0driver.h"
  55. #include "watchdog.h"
  56. //----------------------------------------------------------
  57. static FILE *stream=NULL;
  58. //----------------------------------------------------------
  59. /*!
  60. * \brief Handle input.
  61. */
  62. // THREAD(Uart0KeyEvents, arg)
  63. // {
  64. // NutThreadSetPriority(254); // low prio
  65. // for (;;)
  66. // {
  67. // if (stream==NULL)
  68. // {
  69. // NutSleep(2000); //Mhe
  70. // continue;
  71. // }
  72. // CommandHandler(stream);
  73. // }
  74. // }
  75. THREAD(Uart0KeyEvents, arg)
  76. {
  77. //LogMsg_P(LOG_INFO, PSTR("UART-Thread Start"));
  78. NutThreadSetPriority(200); // low prio
  79. for (;;)
  80. {
  81. char *result = "";
  82. char rst[] = "reset\n";
  83. if (stream==NULL)
  84. {
  85. //LogMsg_P(LOG_INFO, PSTR("Stream is NULL!"));
  86. NutSleep(2000); //Mhe
  87. continue;
  88. }
  89. result = fgets(result, 16, stream);
  90. short _reset_received = strcmp(rst, result);
  91. if (_reset_received == 0){
  92. LogMsg_P(LOG_INFO, PSTR(">>>>>> RESET COMMAND RECEIVED"));
  93. //RESET SIR!
  94. WatchDogStart(0);
  95. }
  96. }
  97. }
  98. //----------------------------------------------------------
  99. /*!
  100. * \brief return stream that is connected to terminal program (serial or TCP/IP)
  101. */
  102. FILE *Uart0DriverGetStream(void)
  103. {
  104. return(stream);
  105. }
  106. /*!
  107. * \brief Uart0 process initialisation. Uses stdout in combination with Uart0.
  108. */
  109. void Uart0DriverInit(void)
  110. {
  111. stream = NULL;
  112. // register Uart0
  113. NutRegisterDevice(&devUart0, 0, 0);
  114. }
  115. /*!
  116. * \brief Creates a thread to handle incoming data from User.
  117. */
  118. void Uart0DriverStart(void)
  119. {
  120. u_long baud = 115200;
  121. char DeviceNameBuffer[6];
  122. char FileModeBuffer[3];
  123. strcpy_P(DeviceNameBuffer, PSTR("uart0"));
  124. strcpy_P(FileModeBuffer, PSTR("w"));
  125. /* Open the stream, connect to stdout */
  126. if ((stream=freopen(DeviceNameBuffer, FileModeBuffer, stdout)) != NULL)
  127. {
  128. _ioctl(_fileno(stream), UART_SETSPEED, &baud);
  129. }
  130. // if (stream != NULL)
  131. // {
  132. // if (GetThreadByName(DeviceNameBuffer) == NULL)
  133. // {
  134. // NutThreadCreate(DeviceNameBuffer, Uart0KeyEvents, 0, 512);
  135. // }
  136. // }
  137. }
  138. void Uart0DriverSetCookedMode(u_long CookedMode)
  139. {
  140. _ioctl(_fileno(stream), UART_SETCOOKEDMODE, &CookedMode);
  141. }
  142. /*!
  143. * \brief Close the stream.
  144. */
  145. void Uart0DriverStop(void)
  146. {
  147. if (stream)
  148. {
  149. // needed? (void)_close(_fileno(stream));
  150. (void)fclose(stream);
  151. stream = NULL;
  152. }
  153. }