serial.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2006-03-13 bernard first version
  23. * 2012-05-15 lgnq modified according bernard's implementation.
  24. * 2012-05-28 bernard code cleanup
  25. * 2012-11-23 bernard fix compiler warning.
  26. * 2013-02-20 bernard use RT_SERIAL_RB_BUFSZ to define
  27. * the size of ring buffer.
  28. * 2014-02-26 bright use DeviceDriver ringbuffer.
  29. * add hardware flow support.
  30. * use new struct serial_ringbuffer.
  31. */
  32. #include <rthw.h>
  33. #include <rtthread.h>
  34. #include <rtdevice.h>
  35. rt_inline void serial_ringbuffer_init(struct serial_ringbuffer *rbuffer)
  36. {
  37. rt_ringbuffer_init(&rbuffer->rb, rbuffer->pool, rbuffer->size);
  38. }
  39. rt_inline void serial_ringbuffer_putc(struct serial_ringbuffer *rbuffer,
  40. char ch)
  41. {
  42. rt_base_t level;
  43. /* disable interrupt */
  44. level = rt_hw_interrupt_disable();
  45. rt_ringbuffer_putchar(&rbuffer->rb, ch);
  46. // enable interrupt
  47. rt_hw_interrupt_enable(level);
  48. }
  49. rt_inline int serial_ringbuffer_putchar(struct serial_ringbuffer *rbuffer,
  50. char ch)
  51. {
  52. rt_base_t level;
  53. //rt_uint16_t next_index;
  54. /* disable interrupt */
  55. level = rt_hw_interrupt_disable();
  56. rt_ringbuffer_putchar(&rbuffer->rb, ch);
  57. /* enable interrupt */
  58. rt_hw_interrupt_enable(level);
  59. return 1;
  60. }
  61. rt_inline int serial_ringbuffer_getc(struct serial_ringbuffer *rbuffer)
  62. {
  63. int ch = 0;
  64. rt_base_t level;
  65. /* disable interrupt */
  66. level = rt_hw_interrupt_disable();
  67. /* get char */
  68. if (!rt_ringbuffer_getchar(&rbuffer->rb, (rt_uint8_t *)&ch))
  69. ch = -1;
  70. /* enable interrupt */
  71. rt_hw_interrupt_enable(level);
  72. return ch;
  73. }
  74. rt_inline rt_uint32_t serial_ringbuffer_size(struct serial_ringbuffer *rbuffer)
  75. {
  76. rt_uint32_t size;
  77. rt_base_t level;
  78. level = rt_hw_interrupt_disable();
  79. size = rt_ringbuffer_space_len(&rbuffer->rb);
  80. /*
  81. size = (rbuffer->put_index - rbuffer->get_index) & (RT_SERIAL_RB_BUFSZ - 1);
  82. */
  83. rt_hw_interrupt_enable(level);
  84. return size;
  85. }
  86. /* RT-Thread Device Interface */
  87. /*
  88. * This function initializes serial
  89. */
  90. static rt_err_t rt_serial_init(struct rt_device *dev)
  91. {
  92. rt_err_t result = RT_EOK;
  93. struct rt_serial_device *serial;
  94. RT_ASSERT(dev != RT_NULL);
  95. serial = (struct rt_serial_device *)dev;
  96. if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
  97. {
  98. /* apply configuration */
  99. if (serial->ops->configure)
  100. result = serial->ops->configure(serial, &serial->config);
  101. if (result != RT_EOK)
  102. return result;
  103. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  104. serial_ringbuffer_init(serial->int_rx);
  105. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  106. {
  107. /* not supported yet */
  108. /*
  109. serial->ops->control(serial, RT_DEVICE_CTRL_SET_INT, (void *)RT_NULL);
  110. serial_ringbuffer_init(serial->int_tx);
  111. serial->int_sending_flag = RT_FALSE;
  112. */
  113. }
  114. if (dev->flag & RT_DEVICE_FLAG_DMA_TX)
  115. {
  116. serial->dma_flag = RT_FALSE;
  117. /* init data queue */
  118. rt_data_queue_init(&(serial->tx_dq), RT_SERIAL_TX_DATAQUEUE_SIZE,
  119. RT_SERIAL_TX_DATAQUEUE_LWM, RT_NULL);
  120. }
  121. /* set activated */
  122. dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
  123. }
  124. return result;
  125. }
  126. static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
  127. {
  128. struct rt_serial_device *serial;
  129. rt_uint32_t int_flags = 0;
  130. RT_ASSERT(dev != RT_NULL);
  131. serial = (struct rt_serial_device *)dev;
  132. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  133. int_flags = RT_SERIAL_RX_INT;
  134. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  135. int_flags |= RT_SERIAL_TX_INT;
  136. if (int_flags)
  137. {
  138. serial->ops->control(serial, RT_DEVICE_CTRL_SET_INT, (void *)int_flags);
  139. }
  140. return RT_EOK;
  141. }
  142. static rt_err_t rt_serial_close(struct rt_device *dev)
  143. {
  144. struct rt_serial_device *serial;
  145. rt_uint32_t int_flags = 0;
  146. RT_ASSERT(dev != RT_NULL);
  147. serial = (struct rt_serial_device *)dev;
  148. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  149. int_flags = RT_SERIAL_RX_INT;
  150. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  151. int_flags |= RT_SERIAL_TX_INT;
  152. if (int_flags)
  153. {
  154. serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void *)int_flags);
  155. }
  156. return RT_EOK;
  157. }
  158. static rt_size_t rt_serial_read(struct rt_device *dev,
  159. rt_off_t pos,
  160. void *buffer,
  161. rt_size_t size)
  162. {
  163. rt_uint8_t *ptr;
  164. rt_uint32_t read_nbytes;
  165. struct rt_serial_device *serial;
  166. RT_ASSERT(dev != RT_NULL);
  167. if (size == 0)
  168. return 0;
  169. serial = (struct rt_serial_device *)dev;
  170. ptr = (rt_uint8_t *)buffer;
  171. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  172. {
  173. /* interrupt mode Rx */
  174. while (size)
  175. {
  176. int ch;
  177. ch = serial_ringbuffer_getc(serial->int_rx);
  178. if (ch == -1)
  179. break;
  180. *ptr = ch & 0xff;
  181. ptr ++;
  182. size --;
  183. }
  184. }
  185. else
  186. {
  187. /* polling mode */
  188. while ((rt_uint32_t)ptr - (rt_uint32_t)buffer < size)
  189. {
  190. *ptr = serial->ops->getc(serial);
  191. ptr ++;
  192. }
  193. }
  194. read_nbytes = (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  195. /* set error code */
  196. if (read_nbytes == 0)
  197. {
  198. rt_set_errno(-RT_EEMPTY);
  199. }
  200. return read_nbytes;
  201. }
  202. static rt_size_t rt_serial_write(struct rt_device *dev,
  203. rt_off_t pos,
  204. const void *buffer,
  205. rt_size_t size)
  206. {
  207. rt_uint8_t *ptr;
  208. rt_size_t write_nbytes = 0;
  209. struct rt_serial_device *serial;
  210. RT_ASSERT(dev != RT_NULL);
  211. if (size == 0)
  212. return 0;
  213. serial = (struct rt_serial_device *)dev;
  214. ptr = (rt_uint8_t*)buffer;
  215. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  216. {
  217. /* warning: data will be discarded if buffer is full */
  218. while (size)
  219. {
  220. if (serial_ringbuffer_putchar(serial->int_tx, *ptr) != -1)
  221. {
  222. ptr ++;
  223. size --;
  224. }
  225. else
  226. break;
  227. }
  228. }
  229. else if (dev->flag & RT_DEVICE_FLAG_DMA_TX)
  230. {
  231. rt_base_t level;
  232. rt_err_t result;
  233. RT_ASSERT(0 == (dev->flag & RT_DEVICE_FLAG_STREAM));
  234. result = rt_data_queue_push(&(serial->tx_dq), buffer, size, 20);
  235. if (result == RT_EOK)
  236. {
  237. level = rt_hw_interrupt_disable();
  238. if (serial->dma_flag == RT_FALSE)
  239. {
  240. serial->dma_flag = RT_TRUE;
  241. rt_hw_interrupt_enable(level);
  242. serial->ops->dma_transmit(serial, buffer, size);
  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. default :
  301. /* control device */
  302. serial->ops->control(serial, cmd, args);
  303. break;
  304. }
  305. return RT_EOK;
  306. }
  307. /*
  308. * serial register
  309. */
  310. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
  311. const char *name,
  312. rt_uint32_t flag,
  313. void *data)
  314. {
  315. struct rt_device *device;
  316. RT_ASSERT(serial != RT_NULL);
  317. device = &(serial->parent);
  318. device->type = RT_Device_Class_Char;
  319. device->rx_indicate = RT_NULL;
  320. device->tx_complete = RT_NULL;
  321. device->init = rt_serial_init;
  322. device->open = rt_serial_open;
  323. device->close = rt_serial_close;
  324. device->read = rt_serial_read;
  325. device->write = rt_serial_write;
  326. device->control = rt_serial_control;
  327. device->user_data = data;
  328. /* register a character device */
  329. return rt_device_register(device, name, flag);
  330. }
  331. /* ISR for serial interrupt */
  332. void rt_hw_serial_isr(struct rt_serial_device *serial)
  333. {
  334. int ch = -1;
  335. /* interrupt mode receive */
  336. RT_ASSERT(serial->parent.flag & RT_DEVICE_FLAG_INT_RX);
  337. while (1)
  338. {
  339. ch = serial->ops->getc(serial);
  340. if (ch == -1)
  341. break;
  342. serial_ringbuffer_putc(serial->int_rx, ch);
  343. }
  344. /* invoke callback */
  345. if (serial->parent.rx_indicate != RT_NULL)
  346. {
  347. rt_size_t rx_length;
  348. /* get rx length */
  349. rx_length = serial_ringbuffer_size(serial->int_rx);
  350. serial->parent.rx_indicate(&serial->parent, rx_length);
  351. }
  352. }
  353. /*
  354. * ISR for DMA mode Tx
  355. */
  356. void rt_hw_serial_dma_tx_isr(struct rt_serial_device *serial)
  357. {
  358. const void *data_ptr;
  359. rt_size_t data_size;
  360. const void *last_data_ptr;
  361. rt_data_queue_pop(&(serial->tx_dq), &last_data_ptr, &data_size, 0);
  362. if (RT_EOK == rt_data_queue_peak(&(serial->tx_dq), &data_ptr, &data_size))
  363. {
  364. /* transmit next data node */
  365. serial->ops->dma_transmit(serial, data_ptr, data_size);
  366. }
  367. else
  368. {
  369. serial->dma_flag = RT_FALSE;
  370. }
  371. /* invoke callback */
  372. if (serial->parent.tx_complete != RT_NULL)
  373. {
  374. serial->parent.tx_complete(&serial->parent, (void*)last_data_ptr);
  375. }
  376. }