serial.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * File : serial.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, 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://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2006-03-13 bernard first version
  13. * 2012-05-15 lgnq modified according bernard's implementation.
  14. * 2012-05-28 bernard code cleanup
  15. */
  16. #include <rthw.h>
  17. #include <rtthread.h>
  18. #include <rtdevice.h>
  19. rt_inline void serial_ringbuffer_init(struct serial_ringbuffer *rbuffer)
  20. {
  21. rt_memset(rbuffer->buffer, 0, sizeof(rbuffer->buffer));
  22. rbuffer->put_index = 0;
  23. rbuffer->get_index = 0;
  24. }
  25. rt_inline void serial_ringbuffer_putc(struct serial_ringbuffer *rbuffer, char ch)
  26. {
  27. rt_base_t level;
  28. /* disable interrupt */
  29. level = rt_hw_interrupt_disable();
  30. rbuffer->buffer[rbuffer->put_index] = ch;
  31. rbuffer->put_index = (rbuffer->put_index + 1) & (SERIAL_RBUFFER_SIZE - 1);
  32. /* if the next position is read index, discard this 'read char' */
  33. if (rbuffer->put_index == rbuffer->get_index)
  34. {
  35. rbuffer->get_index = (rbuffer->get_index + 1) & (SERIAL_RBUFFER_SIZE - 1);
  36. }
  37. /* enable interrupt */
  38. rt_hw_interrupt_enable(level);
  39. }
  40. rt_inline int serial_ringbuffer_putchar(struct serial_ringbuffer *rbuffer, char ch)
  41. {
  42. rt_base_t level;
  43. rt_uint16_t next_index;
  44. /* disable interrupt */
  45. level = rt_hw_interrupt_disable();
  46. next_index = (rbuffer->put_index + 1) & (SERIAL_RBUFFER_SIZE - 1);
  47. if (next_index != rbuffer->get_index)
  48. {
  49. rbuffer->buffer[rbuffer->put_index] = ch;
  50. rbuffer->put_index = next_index;
  51. }
  52. else
  53. {
  54. /* enable interrupt */
  55. rt_hw_interrupt_enable(level);
  56. return -1;
  57. }
  58. /* enable interrupt */
  59. rt_hw_interrupt_enable(level);
  60. return 1;
  61. }
  62. rt_inline int serial_ringbuffer_getc(struct serial_ringbuffer *rbuffer)
  63. {
  64. int ch;
  65. rt_base_t level;
  66. ch = -1;
  67. /* disable interrupt */
  68. level = rt_hw_interrupt_disable();
  69. if (rbuffer->get_index != rbuffer->put_index)
  70. {
  71. ch = rbuffer->buffer[rbuffer->get_index];
  72. rbuffer->get_index = (rbuffer->get_index + 1) & (SERIAL_RBUFFER_SIZE - 1);
  73. }
  74. /* enable interrupt */
  75. rt_hw_interrupt_enable(level);
  76. return ch;
  77. }
  78. rt_inline rt_uint32_t serial_ringbuffer_size(struct serial_ringbuffer *rbuffer)
  79. {
  80. rt_uint32_t size;
  81. rt_base_t level;
  82. level = rt_hw_interrupt_disable();
  83. size = (rbuffer->put_index - rbuffer->get_index) & (SERIAL_RBUFFER_SIZE - 1);
  84. rt_hw_interrupt_enable(level);
  85. return size;
  86. }
  87. /* RT-Thread Device Interface */
  88. /*
  89. * This function initializes serial
  90. */
  91. static rt_err_t rt_serial_init(struct rt_device *dev)
  92. {
  93. rt_err_t result = RT_EOK;
  94. struct rt_serial_device *serial;
  95. RT_ASSERT(dev != RT_NULL);
  96. serial = (struct rt_serial_device *)dev;
  97. if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
  98. {
  99. /* apply configuration */
  100. if (serial->ops->configure)
  101. result = serial->ops->configure(serial, &serial->config);
  102. if (result != RT_EOK)
  103. return result;
  104. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  105. serial_ringbuffer_init(serial->int_rx);
  106. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  107. serial_ringbuffer_init(serial->int_tx);
  108. /* set activated */
  109. dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
  110. }
  111. return result;
  112. }
  113. static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
  114. {
  115. struct rt_serial_device *serial;
  116. rt_uint32_t int_flags = 0;
  117. RT_ASSERT(dev != RT_NULL);
  118. serial = (struct rt_serial_device *)dev;
  119. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  120. int_flags = RT_SERIAL_RX_INT;
  121. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  122. int_flags |= RT_SERIAL_TX_INT;
  123. if (int_flags)
  124. {
  125. serial->ops->control(serial, RT_DEVICE_CTRL_SET_INT, (void *)int_flags);
  126. }
  127. return RT_EOK;
  128. }
  129. static rt_err_t rt_serial_close(struct rt_device *dev)
  130. {
  131. struct rt_serial_device *serial;
  132. rt_uint32_t int_flags = 0;
  133. RT_ASSERT(dev != RT_NULL);
  134. serial = (struct rt_serial_device *)dev;
  135. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  136. int_flags = RT_SERIAL_RX_INT;
  137. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  138. int_flags |= RT_SERIAL_TX_INT;
  139. if (int_flags)
  140. {
  141. serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void *)int_flags);
  142. }
  143. return RT_EOK;
  144. }
  145. static rt_size_t rt_serial_read(struct rt_device *dev, rt_off_t pos, void *buffer, rt_size_t size)
  146. {
  147. rt_uint8_t *ptr;
  148. rt_uint32_t read_nbytes;
  149. struct rt_serial_device *serial;
  150. RT_ASSERT(dev != RT_NULL);
  151. serial = (struct rt_serial_device *)dev;
  152. ptr = (rt_uint8_t *)buffer;
  153. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  154. {
  155. /* interrupt mode Rx */
  156. while (size)
  157. {
  158. int ch;
  159. ch = serial_ringbuffer_getc(serial->int_rx);
  160. if (ch == -1)
  161. break;
  162. *ptr = ch & 0xff;
  163. ptr ++;
  164. size --;
  165. }
  166. }
  167. else
  168. {
  169. /* polling mode */
  170. while ((rt_uint32_t)ptr - (rt_uint32_t)buffer < size)
  171. {
  172. *ptr = serial->ops->getc(serial);
  173. ptr ++;
  174. }
  175. }
  176. read_nbytes = (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  177. /* set error code */
  178. if (read_nbytes == 0)
  179. {
  180. rt_set_errno(-RT_EEMPTY);
  181. }
  182. return read_nbytes;
  183. }
  184. static rt_size_t rt_serial_write(struct rt_device *dev, rt_off_t pos,
  185. const void *buffer, rt_size_t size)
  186. {
  187. rt_uint8_t *ptr;
  188. rt_size_t write_nbytes = 0;
  189. struct rt_serial_device *serial;
  190. RT_ASSERT(dev != RT_NULL);
  191. serial = (struct rt_serial_device *)dev;
  192. ptr = (rt_uint8_t*)buffer;
  193. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  194. {
  195. /* warning: data will be discarded if buffer is full */
  196. while (size)
  197. {
  198. if (serial_ringbuffer_putchar(serial->int_tx, *ptr) != -1)
  199. {
  200. ptr ++;
  201. size --;
  202. }
  203. else
  204. break;
  205. }
  206. }
  207. else
  208. {
  209. /* polling mode */
  210. while (size)
  211. {
  212. /*
  213. * to be polite with serial console add a line feed
  214. * to the carriage return character
  215. */
  216. if (*ptr == '\n' && (dev->flag & RT_DEVICE_FLAG_STREAM))
  217. {
  218. serial->ops->putc(serial, '\r');
  219. }
  220. serial->ops->putc(serial, *ptr);
  221. ++ptr;
  222. --size;
  223. }
  224. }
  225. write_nbytes = (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  226. if (write_nbytes == 0)
  227. {
  228. rt_set_errno(-RT_EFULL);
  229. }
  230. return write_nbytes;
  231. }
  232. static rt_err_t rt_serial_control(struct rt_device *dev, rt_uint8_t cmd, void *args)
  233. {
  234. struct rt_serial_device *serial;
  235. RT_ASSERT(dev != RT_NULL);
  236. serial = (struct rt_serial_device *)dev;
  237. switch (cmd)
  238. {
  239. case RT_DEVICE_CTRL_SUSPEND:
  240. /* suspend device */
  241. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  242. break;
  243. case RT_DEVICE_CTRL_RESUME:
  244. /* resume device */
  245. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  246. break;
  247. case RT_DEVICE_CTRL_CONFIG:
  248. /* configure device */
  249. serial->ops->configure(serial, (struct serial_configure *)args);
  250. break;
  251. }
  252. return RT_EOK;
  253. }
  254. /*
  255. * serial register
  256. */
  257. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial, const char *name, rt_uint32_t flag, void *data)
  258. {
  259. struct rt_device *device;
  260. RT_ASSERT(serial != RT_NULL);
  261. device = &(serial->parent);
  262. device->type = RT_Device_Class_Char;
  263. device->rx_indicate = RT_NULL;
  264. device->tx_complete = RT_NULL;
  265. device->init = rt_serial_init;
  266. device->open = rt_serial_open;
  267. device->close = rt_serial_close;
  268. device->read = rt_serial_read;
  269. device->write = rt_serial_write;
  270. device->control = rt_serial_control;
  271. device->user_data = data;
  272. /* register a character device */
  273. return rt_device_register(device, name, flag);
  274. }
  275. /* ISR for serial interrupt */
  276. void rt_hw_serial_isr(struct rt_serial_device *serial)
  277. {
  278. int ch = -1;
  279. /* interrupt mode receive */
  280. RT_ASSERT(serial->parent.flag & RT_DEVICE_FLAG_INT_RX);
  281. while (1)
  282. {
  283. ch = serial->ops->getc(serial);
  284. if (ch == -1)
  285. break;
  286. serial_ringbuffer_putc(serial->int_rx, ch);
  287. }
  288. /* invoke callback */
  289. if (serial->parent.rx_indicate != RT_NULL)
  290. {
  291. rt_size_t rx_length;
  292. /* get rx length */
  293. rx_length = serial_ringbuffer_size(serial->int_rx);
  294. serial->parent.rx_indicate(&serial->parent, rx_length);
  295. }
  296. }