serial.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2006-03-13 Bernard first version
  9. * 2009-04-20 yi.qiu modified according bernard's stm32 version
  10. */
  11. #include <rtthread.h>
  12. #include "serial.h"
  13. /**
  14. * @addtogroup S3C24X0
  15. */
  16. /*@{*/
  17. /* RT-Thread Device Interface */
  18. /**
  19. * This function initializes serial
  20. */
  21. static rt_err_t rt_serial_init(rt_device_t dev)
  22. {
  23. struct serial_device* uart = (struct serial_device*) dev->user_data;
  24. if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
  25. {
  26. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  27. {
  28. rt_memset(uart->int_rx->rx_buffer, 0,
  29. sizeof(uart->int_rx->rx_buffer));
  30. uart->int_rx->read_index = uart->int_rx->save_index = 0;
  31. }
  32. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  33. {
  34. rt_memset(uart->int_tx->tx_buffer, 0,
  35. sizeof(uart->int_tx->tx_buffer));
  36. uart->int_tx->write_index = uart->int_tx->save_index = 0;
  37. }
  38. dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
  39. }
  40. return RT_EOK;
  41. }
  42. /* save a char to serial buffer */
  43. static void rt_serial_savechar(struct serial_device* uart, char ch)
  44. {
  45. rt_base_t level;
  46. /* disable interrupt */
  47. level = rt_hw_interrupt_disable();
  48. uart->int_rx->rx_buffer[uart->int_rx->save_index] = ch;
  49. uart->int_rx->save_index ++;
  50. if (uart->int_rx->save_index >= UART_RX_BUFFER_SIZE)
  51. uart->int_rx->save_index = 0;
  52. /* if the next position is read index, discard this 'read char' */
  53. if (uart->int_rx->save_index == uart->int_rx->read_index)
  54. {
  55. uart->int_rx->read_index ++;
  56. if (uart->int_rx->read_index >= UART_RX_BUFFER_SIZE)
  57. uart->int_rx->read_index = 0;
  58. }
  59. /* enable interrupt */
  60. rt_hw_interrupt_enable(level);
  61. }
  62. static rt_err_t rt_serial_open(rt_device_t dev, rt_uint16_t oflag)
  63. {
  64. RT_ASSERT(dev != RT_NULL);
  65. return RT_EOK;
  66. }
  67. static rt_err_t rt_serial_close(rt_device_t dev)
  68. {
  69. RT_ASSERT(dev != RT_NULL);
  70. return RT_EOK;
  71. }
  72. static rt_size_t rt_serial_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  73. {
  74. rt_uint8_t* ptr;
  75. rt_err_t err_code;
  76. struct serial_device* uart;
  77. ptr = buffer;
  78. err_code = RT_EOK;
  79. uart = (struct serial_device*)dev->user_data;
  80. if (ptr == RT_NULL)
  81. {
  82. err_code = -RT_ENOMEM;
  83. rt_set_errno(err_code);
  84. return -RT_ENOMEM;
  85. }
  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->ustat & USTAT_RCV_READY)
  118. {
  119. *ptr = uart->uart_device->urxh & 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 (ptr == RT_NULL)
  137. {
  138. err_code = -RT_ENOMEM;
  139. rt_set_errno(err_code);
  140. return -RT_ENOMEM;
  141. }
  142. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  143. {
  144. /* interrupt mode Tx */
  145. while (uart->int_tx->save_index != uart->int_tx->write_index)
  146. {
  147. /* save on tx buffer */
  148. uart->int_tx->tx_buffer[uart->int_tx->save_index] = *ptr++;
  149. -- size;
  150. /* move to next position */
  151. uart->int_tx->save_index ++;
  152. /* wrap save index */
  153. if (uart->int_tx->save_index >= UART_TX_BUFFER_SIZE)
  154. uart->int_tx->save_index = 0;
  155. }
  156. /* set error code */
  157. if (size > 0)
  158. err_code = -RT_EFULL;
  159. }
  160. else
  161. {
  162. /* polling mode */
  163. while (size)
  164. {
  165. /*
  166. * to be polite with serial console add a line feed
  167. * to the carriage return character
  168. */
  169. if (*ptr == '\n' && (dev->flag & RT_DEVICE_FLAG_STREAM))
  170. {
  171. while (!(uart->uart_device->ustat & USTAT_TXB_EMPTY));
  172. uart->uart_device->utxh = '\r';
  173. }
  174. while (!(uart->uart_device->ustat & USTAT_TXB_EMPTY));
  175. uart->uart_device->utxh = (*ptr & 0xFF);
  176. ++ptr;
  177. --size;
  178. }
  179. }
  180. /* set error code */
  181. rt_set_errno(err_code);
  182. return (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  183. }
  184. static rt_err_t rt_serial_control(rt_device_t dev, int cmd, void *args)
  185. {
  186. RT_ASSERT(dev != RT_NULL);
  187. switch (cmd)
  188. {
  189. case RT_DEVICE_CTRL_SUSPEND:
  190. /* suspend device */
  191. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  192. break;
  193. case RT_DEVICE_CTRL_RESUME:
  194. /* resume device */
  195. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  196. break;
  197. }
  198. return RT_EOK;
  199. }
  200. /*
  201. * serial register
  202. */
  203. rt_err_t rt_hw_serial_register(rt_device_t device, const char* name, rt_uint32_t flag, struct serial_device *serial)
  204. {
  205. RT_ASSERT(device != RT_NULL);
  206. device->type = RT_Device_Class_Char;
  207. device->rx_indicate = RT_NULL;
  208. device->tx_complete = RT_NULL;
  209. device->init = rt_serial_init;
  210. device->open = rt_serial_open;
  211. device->close = rt_serial_close;
  212. device->read = rt_serial_read;
  213. device->write = rt_serial_write;
  214. device->control = rt_serial_control;
  215. device->user_data = serial;
  216. /* register a character device */
  217. return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | flag);
  218. }
  219. /* ISR for serial interrupt */
  220. void rt_hw_serial_isr(rt_device_t device)
  221. {
  222. struct serial_device* uart = (struct serial_device*) device->user_data;
  223. /* interrupt mode receive */
  224. RT_ASSERT(device->flag & RT_DEVICE_FLAG_INT_RX);
  225. /* save on rx buffer */
  226. while (uart->uart_device->ustat & USTAT_RCV_READY)
  227. {
  228. rt_serial_savechar(uart, uart->uart_device->urxh & 0xff);
  229. }
  230. /* invoke callback */
  231. if (device->rx_indicate != RT_NULL)
  232. {
  233. rt_size_t rx_length;
  234. /* get rx length */
  235. rx_length = uart->int_rx->read_index > uart->int_rx->save_index ?
  236. UART_RX_BUFFER_SIZE - uart->int_rx->read_index + uart->int_rx->save_index :
  237. uart->int_rx->save_index - uart->int_rx->read_index;
  238. device->rx_indicate(device, rx_length);
  239. }
  240. }
  241. /*@}*/