serial.c 7.5 KB

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