serial.c 11 KB

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