serial.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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->private;
  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->private;
  84. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  85. {
  86. rt_base_t level;
  87. /* interrupt mode Rx */
  88. while (size)
  89. {
  90. if (uart->int_rx->read_index != uart->int_rx->save_index)
  91. {
  92. *ptr++ = uart->int_rx->rx_buffer[uart->int_rx->read_index];
  93. size --;
  94. /* disable interrupt */
  95. level = rt_hw_interrupt_disable();
  96. uart->int_rx->read_index ++;
  97. if (uart->int_rx->read_index >= UART_RX_BUFFER_SIZE)
  98. uart->int_rx->read_index = 0;
  99. /* enable interrupt */
  100. rt_hw_interrupt_enable(level);
  101. }
  102. else
  103. {
  104. /* set error code */
  105. err_code = -RT_EEMPTY;
  106. break;
  107. }
  108. }
  109. }
  110. else
  111. {
  112. /* polling mode */
  113. while ((rt_uint32_t)ptr - (rt_uint32_t)buffer < size)
  114. {
  115. while (uart->uart_device->ustat & USTAT_RCV_READY)
  116. {
  117. *ptr = uart->uart_device->urxh & 0xff;
  118. ptr ++;
  119. }
  120. }
  121. }
  122. /* set error code */
  123. rt_set_errno(err_code);
  124. return (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  125. }
  126. static rt_size_t rt_serial_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  127. {
  128. rt_uint8_t* ptr;
  129. rt_err_t err_code;
  130. struct serial_device* uart;
  131. err_code = RT_EOK;
  132. ptr = (rt_uint8_t*)buffer;
  133. uart = (struct serial_device*)dev->private;
  134. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  135. {
  136. /* interrupt mode Tx */
  137. while (uart->int_tx->save_index != uart->int_tx->write_index)
  138. {
  139. /* save on tx buffer */
  140. uart->int_tx->tx_buffer[uart->int_tx->save_index] = *ptr++;
  141. -- size;
  142. /* move to next position */
  143. uart->int_tx->save_index ++;
  144. /* wrap save index */
  145. if (uart->int_tx->save_index >= UART_TX_BUFFER_SIZE)
  146. uart->int_tx->save_index = 0;
  147. }
  148. /* set error code */
  149. if (size > 0)
  150. err_code = -RT_EFULL;
  151. }
  152. else
  153. {
  154. /* polling mode */
  155. while (size)
  156. {
  157. /*
  158. * to be polite with serial console add a line feed
  159. * to the carriage return character
  160. */
  161. if (*ptr == '\n' && (dev->flag & RT_DEVICE_FLAG_STREAM))
  162. {
  163. while (!(uart->uart_device->ustat & USTAT_TXB_EMPTY));
  164. uart->uart_device->utxh = '\r';
  165. }
  166. while (!(uart->uart_device->ustat & USTAT_TXB_EMPTY));
  167. uart->uart_device->utxh = (*ptr & 0xFF);
  168. ++ptr; --size;
  169. }
  170. }
  171. /* set error code */
  172. rt_set_errno(err_code);
  173. return (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  174. }
  175. static rt_err_t rt_serial_control (rt_device_t dev, rt_uint8_t cmd, void *args)
  176. {
  177. RT_ASSERT(dev != RT_NULL);
  178. switch (cmd)
  179. {
  180. case RT_DEVICE_CTRL_SUSPEND:
  181. /* suspend device */
  182. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  183. break;
  184. case RT_DEVICE_CTRL_RESUME:
  185. /* resume device */
  186. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  187. break;
  188. }
  189. return RT_EOK;
  190. }
  191. /*
  192. * serial register
  193. */
  194. rt_err_t rt_hw_serial_register(rt_device_t device, const char* name, rt_uint32_t flag, struct serial_device *serial)
  195. {
  196. RT_ASSERT(device != RT_NULL);
  197. device->type = RT_Device_Class_Char;
  198. device->rx_indicate = RT_NULL;
  199. device->tx_complete = RT_NULL;
  200. device->init = rt_serial_init;
  201. device->open = rt_serial_open;
  202. device->close = rt_serial_close;
  203. device->read = rt_serial_read;
  204. device->write = rt_serial_write;
  205. device->control = rt_serial_control;
  206. device->private = serial;
  207. /* register a character device */
  208. return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | flag);
  209. }
  210. /* ISR for serial interrupt */
  211. void rt_hw_serial_isr(rt_device_t device)
  212. {
  213. struct serial_device* uart = (struct serial_device*) device->private;
  214. /* interrupt mode receive */
  215. RT_ASSERT(device->flag & RT_DEVICE_FLAG_INT_RX);
  216. /* save on rx buffer */
  217. while (uart->uart_device->ustat & USTAT_RCV_READY)
  218. {
  219. rt_serial_savechar(uart, uart->uart_device->urxh & 0xff);
  220. }
  221. /* invoke callback */
  222. if (device->rx_indicate != RT_NULL)
  223. {
  224. rt_size_t rx_length;
  225. /* get rx length */
  226. rx_length = uart->int_rx->read_index > uart->int_rx->save_index ?
  227. UART_RX_BUFFER_SIZE - uart->int_rx->read_index + uart->int_rx->save_index :
  228. uart->int_rx->save_index - uart->int_rx->read_index;
  229. device->rx_indicate(device, rx_length);
  230. }
  231. }
  232. /*@}*/