serial.c 9.3 KB

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