serial.c 7.2 KB

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