serial.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * File : serial.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://openlab.rt-thread.com/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2006-03-13 Bernard first version
  13. * 2009-04-20 yi.qiu modified according bernard's stm32 version
  14. * 2010-10-6 wangmeng added sep4020 surpport
  15. */
  16. #include <rtthread.h>
  17. #include <rthw.h>
  18. #include "serial.h"
  19. /**
  20. * @addtogroup SEP4020
  21. */
  22. /*@{*/
  23. /* RT-Thread Device Interface */
  24. /**
  25. * This function initializes serial
  26. */
  27. static rt_err_t rt_serial_init (rt_device_t dev)
  28. {
  29. struct serial_device* uart = (struct serial_device*) dev->user_data;
  30. if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
  31. {
  32. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  33. {
  34. rt_memset(uart->int_rx->rx_buffer, 0,
  35. sizeof(uart->int_rx->rx_buffer));
  36. uart->int_rx->read_index = uart->int_rx->save_index = 0;
  37. }
  38. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  39. {
  40. rt_memset(uart->int_tx->tx_buffer, 0,
  41. sizeof(uart->int_tx->tx_buffer));
  42. uart->int_tx->write_index = uart->int_tx->save_index = 0;
  43. }
  44. dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
  45. }
  46. return RT_EOK;
  47. }
  48. /* save a char to serial buffer */
  49. static void rt_serial_savechar(struct serial_device* uart, char ch)
  50. {
  51. rt_base_t level;
  52. /* disable interrupt */
  53. level = rt_hw_interrupt_disable();
  54. uart->int_rx->rx_buffer[uart->int_rx->save_index] = ch;
  55. uart->int_rx->save_index ++;
  56. if (uart->int_rx->save_index >= UART_RX_BUFFER_SIZE)
  57. uart->int_rx->save_index = 0;
  58. /* if the next position is read index, discard this 'read char' */
  59. if (uart->int_rx->save_index == uart->int_rx->read_index)
  60. {
  61. uart->int_rx->read_index ++;
  62. if (uart->int_rx->read_index >= UART_RX_BUFFER_SIZE)
  63. uart->int_rx->read_index = 0;
  64. }
  65. /* enable interrupt */
  66. rt_hw_interrupt_enable(level);
  67. }
  68. static rt_err_t rt_serial_open(rt_device_t dev, rt_uint16_t oflag)
  69. {
  70. RT_ASSERT(dev != RT_NULL);
  71. return RT_EOK;
  72. }
  73. static rt_err_t rt_serial_close(rt_device_t dev)
  74. {
  75. RT_ASSERT(dev != RT_NULL);
  76. return RT_EOK;
  77. }
  78. static rt_size_t rt_serial_read (rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  79. {
  80. rt_uint8_t* ptr;
  81. rt_err_t err_code;
  82. struct serial_device* uart;
  83. ptr = buffer;
  84. err_code = RT_EOK;
  85. uart = (struct serial_device*)dev->user_data;
  86. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  87. {
  88. rt_base_t level;
  89. /* interrupt mode Rx */
  90. while (size)
  91. {
  92. if (uart->int_rx->read_index != uart->int_rx->save_index)
  93. {
  94. *ptr++ = uart->int_rx->rx_buffer[uart->int_rx->read_index];
  95. size --;
  96. /* disable interrupt */
  97. level = rt_hw_interrupt_disable();
  98. uart->int_rx->read_index ++;
  99. if (uart->int_rx->read_index >= UART_RX_BUFFER_SIZE)
  100. uart->int_rx->read_index = 0;
  101. /* enable interrupt */
  102. rt_hw_interrupt_enable(level);
  103. }
  104. else
  105. {
  106. /* set error code */
  107. err_code = -RT_EEMPTY;
  108. break;
  109. }
  110. }
  111. }
  112. else
  113. {
  114. /* polling mode */
  115. while ((rt_uint32_t)ptr - (rt_uint32_t)buffer < size)
  116. {
  117. while (uart->uart_device->lsr & USTAT_RCV_READY)
  118. {
  119. *ptr = uart->uart_device->dlbl_fifo.txfifo & 0xff;
  120. ptr ++;
  121. }
  122. }
  123. }
  124. /* set error code */
  125. rt_set_errno(err_code);
  126. return (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  127. }
  128. static rt_size_t rt_serial_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  129. {
  130. rt_uint8_t* ptr;
  131. rt_err_t err_code;
  132. struct serial_device* uart;
  133. err_code = RT_EOK;
  134. ptr = (rt_uint8_t*)buffer;
  135. uart = (struct serial_device*)dev->user_data;
  136. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  137. {
  138. /* interrupt mode Tx */
  139. while (uart->int_tx->save_index != uart->int_tx->write_index)
  140. {
  141. /* save on tx buffer */
  142. uart->int_tx->tx_buffer[uart->int_tx->save_index] = *ptr++;
  143. -- size;
  144. /* move to next position */
  145. uart->int_tx->save_index ++;
  146. /* wrap save index */
  147. if (uart->int_tx->save_index >= UART_TX_BUFFER_SIZE)
  148. uart->int_tx->save_index = 0;
  149. }
  150. /* set error code */
  151. if (size > 0)
  152. err_code = -RT_EFULL;
  153. }
  154. else
  155. {
  156. /* polling mode */
  157. while (size)
  158. {
  159. /*
  160. * to be polite with serial console add a line feed
  161. * to the carriage return character
  162. */
  163. if (*ptr == '\n' && (dev->flag & RT_DEVICE_FLAG_STREAM))
  164. {
  165. while (!(uart->uart_device->lsr & USTAT_TXB_EMPTY));
  166. uart->uart_device->dlbl_fifo.txfifo = '\r';
  167. }
  168. while (!(uart->uart_device->lsr & USTAT_TXB_EMPTY));
  169. uart->uart_device->dlbl_fifo.txfifo = (*ptr & 0x1FF);
  170. ++ptr; --size;
  171. }
  172. }
  173. /* set error code */
  174. rt_set_errno(err_code);
  175. return (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  176. }
  177. static rt_err_t rt_serial_control (rt_device_t dev, int cmd, void *args)
  178. {
  179. RT_ASSERT(dev != RT_NULL);
  180. switch (cmd)
  181. {
  182. case RT_DEVICE_CTRL_SUSPEND:
  183. /* suspend device */
  184. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  185. break;
  186. case RT_DEVICE_CTRL_RESUME:
  187. /* resume device */
  188. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  189. break;
  190. }
  191. return RT_EOK;
  192. }
  193. /*
  194. * serial register
  195. */
  196. rt_err_t rt_hw_serial_register(rt_device_t device, const char* name, rt_uint32_t flag, struct serial_device *serial)
  197. {
  198. RT_ASSERT(device != RT_NULL);
  199. device->type = RT_Device_Class_Char;
  200. device->rx_indicate = RT_NULL;
  201. device->tx_complete = RT_NULL;
  202. device->init = rt_serial_init;
  203. device->open = rt_serial_open;
  204. device->close = rt_serial_close;
  205. device->read = rt_serial_read;
  206. device->write = rt_serial_write;
  207. device->control = rt_serial_control;
  208. device->user_data = serial;
  209. /* register a character device */
  210. return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | flag);
  211. }
  212. /* ISR for serial interrupt */
  213. void rt_hw_serial_isr(rt_device_t device)
  214. {
  215. struct serial_device* uart = (struct serial_device*) device->user_data;
  216. /* interrupt mode receive */
  217. RT_ASSERT(device->flag & RT_DEVICE_FLAG_INT_RX);
  218. /* save on rx buffer */
  219. while (uart->uart_device->lsr & USTAT_RCV_READY)
  220. {
  221. rt_serial_savechar(uart, uart->uart_device->dlbl_fifo.rxfifo & 0xff);
  222. }
  223. /* invoke callback */
  224. if (device->rx_indicate != RT_NULL)
  225. {
  226. rt_size_t rx_length;
  227. /* get rx length */
  228. rx_length = uart->int_rx->read_index > uart->int_rx->save_index ?
  229. UART_RX_BUFFER_SIZE - uart->int_rx->read_index + uart->int_rx->save_index :
  230. uart->int_rx->save_index - uart->int_rx->read_index;
  231. device->rx_indicate(device, rx_length);
  232. }
  233. }
  234. /*@}*/