uart0driver.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "system.h"
  53. #include "uart0driver.h"
  54. //----------------------------------------------------------
  55. static FILE *stream=NULL;
  56. //----------------------------------------------------------
  57. /*!
  58. * \brief Handle input.
  59. */
  60. // THREAD(Uart0KeyEvents, arg)
  61. // {
  62. // NutThreadSetPriority(254); // low prio
  63. // for (;;)
  64. // {
  65. // if (stream==NULL)
  66. // {
  67. // NutSleep(2000); //Mhe
  68. // continue;
  69. // }
  70. // CommandHandler(stream);
  71. // }
  72. // }
  73. //----------------------------------------------------------
  74. /*!
  75. * \brief return stream that is connected to terminal program (serial or TCP/IP)
  76. */
  77. FILE *Uart0DriverGetStream(void)
  78. {
  79. return(stream);
  80. }
  81. /*!
  82. * \brief Uart0 process initialisation. Uses stdout in combination with Uart0.
  83. */
  84. void Uart0DriverInit(void)
  85. {
  86. stream = NULL;
  87. // register Uart0
  88. NutRegisterDevice(&devUart0, 0, 0);
  89. }
  90. /*!
  91. * \brief Creates a thread to handle incoming data from User.
  92. */
  93. void Uart0DriverStart(void)
  94. {
  95. u_long baud = 115200;
  96. char DeviceNameBuffer[6];
  97. char FileModeBuffer[3];
  98. strcpy_P(DeviceNameBuffer, PSTR("uart0"));
  99. strcpy_P(FileModeBuffer, PSTR("w"));
  100. /* Open the stream, connect to stdout */
  101. if ((stream=freopen(DeviceNameBuffer, FileModeBuffer, stdout)) != NULL)
  102. {
  103. _ioctl(_fileno(stream), UART_SETSPEED, &baud);
  104. }
  105. // if (stream != NULL)
  106. // {
  107. // if (GetThreadByName(DeviceNameBuffer) == NULL)
  108. // {
  109. // NutThreadCreate(DeviceNameBuffer, Uart0KeyEvents, 0, 512);
  110. // }
  111. // }
  112. }
  113. void Uart0DriverSetCookedMode(u_long CookedMode)
  114. {
  115. _ioctl(_fileno(stream), UART_SETCOOKEDMODE, &CookedMode);
  116. }
  117. /*!
  118. * \brief Close the stream.
  119. */
  120. void Uart0DriverStop(void)
  121. {
  122. if (stream)
  123. {
  124. // needed? (void)_close(_fileno(stream));
  125. (void)fclose(stream);
  126. stream = NULL;
  127. }
  128. }