serial.c 6.6 KB

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