serial.c 6.7 KB

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