serial.c 12 KB

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