serial.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. {
  123. /* not supported yet */
  124. /*
  125. serial->ops->control(serial, RT_DEVICE_CTRL_SET_INT, (void *)RT_NULL);
  126. serial_ringbuffer_init(serial->int_tx);
  127. serial->int_sending_flag = RT_FALSE;
  128. */
  129. }
  130. if (dev->flag & RT_DEVICE_FLAG_DMA_TX)
  131. {
  132. serial->dma_flag = RT_FALSE;
  133. /* init data queue */
  134. rt_data_queue_init(&(serial->tx_dq), RT_SERIAL_TX_DATAQUEUE_SIZE,
  135. RT_SERIAL_TX_DATAQUEUE_LWM, RT_NULL);
  136. }
  137. /* set activated */
  138. dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
  139. }
  140. return result;
  141. }
  142. static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
  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_SET_INT, (void *)int_flags);
  155. }
  156. return RT_EOK;
  157. }
  158. static rt_err_t rt_serial_close(struct rt_device *dev)
  159. {
  160. struct rt_serial_device *serial;
  161. rt_uint32_t int_flags = 0;
  162. RT_ASSERT(dev != RT_NULL);
  163. serial = (struct rt_serial_device *)dev;
  164. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  165. int_flags = RT_SERIAL_RX_INT;
  166. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  167. int_flags |= RT_SERIAL_TX_INT;
  168. if (int_flags)
  169. {
  170. serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void *)int_flags);
  171. }
  172. return RT_EOK;
  173. }
  174. static rt_size_t rt_serial_read(struct rt_device *dev,
  175. rt_off_t pos,
  176. void *buffer,
  177. rt_size_t size)
  178. {
  179. rt_uint8_t *ptr;
  180. rt_uint32_t read_nbytes;
  181. struct rt_serial_device *serial;
  182. RT_ASSERT(dev != RT_NULL);
  183. if (size == 0)
  184. return 0;
  185. serial = (struct rt_serial_device *)dev;
  186. ptr = (rt_uint8_t *)buffer;
  187. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  188. {
  189. /* interrupt mode Rx */
  190. while (size)
  191. {
  192. int ch;
  193. ch = serial_ringbuffer_getc(serial->int_rx);
  194. if (ch == -1)
  195. break;
  196. *ptr = ch & 0xff;
  197. ptr ++;
  198. size --;
  199. }
  200. }
  201. else
  202. {
  203. /* polling mode */
  204. while ((rt_uint32_t)ptr - (rt_uint32_t)buffer < size)
  205. {
  206. *ptr = serial->ops->getc(serial);
  207. ptr ++;
  208. }
  209. }
  210. read_nbytes = (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  211. /* set error code if and only if in thread context */
  212. if (read_nbytes == 0 && !rt_interrupt_get_nest())
  213. {
  214. rt_set_errno(-RT_EEMPTY);
  215. }
  216. return read_nbytes;
  217. }
  218. static rt_size_t rt_serial_write(struct rt_device *dev,
  219. rt_off_t pos,
  220. const void *buffer,
  221. rt_size_t size)
  222. {
  223. rt_uint8_t *ptr;
  224. rt_size_t write_nbytes = 0;
  225. struct rt_serial_device *serial;
  226. RT_ASSERT(dev != RT_NULL);
  227. if (size == 0)
  228. return 0;
  229. serial = (struct rt_serial_device *)dev;
  230. ptr = (rt_uint8_t*)buffer;
  231. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  232. {
  233. /* warning: data will be discarded if buffer is full */
  234. while (size)
  235. {
  236. if (serial_ringbuffer_putchar(serial->int_tx, *ptr) != -1)
  237. {
  238. ptr ++;
  239. size --;
  240. }
  241. else
  242. break;
  243. }
  244. }
  245. else if (dev->flag & RT_DEVICE_FLAG_DMA_TX)
  246. {
  247. rt_base_t level;
  248. rt_err_t result;
  249. RT_ASSERT(0 == (dev->flag & RT_DEVICE_FLAG_STREAM));
  250. result = rt_data_queue_push(&(serial->tx_dq), buffer, size, 20);
  251. if (result == RT_EOK)
  252. {
  253. level = rt_hw_interrupt_disable();
  254. if (serial->dma_flag == RT_FALSE)
  255. {
  256. serial->dma_flag = RT_TRUE;
  257. rt_hw_interrupt_enable(level);
  258. serial->ops->dma_transmit(serial, buffer, size);
  259. }
  260. else
  261. rt_hw_interrupt_enable(level);
  262. return size;
  263. }
  264. else
  265. {
  266. rt_set_errno(result);
  267. return 0;
  268. }
  269. }
  270. else
  271. {
  272. /* polling mode */
  273. while (size)
  274. {
  275. /*
  276. * to be polite with serial console add a line feed
  277. * to the carriage return character
  278. */
  279. if (*ptr == '\n' && (dev->flag & RT_DEVICE_FLAG_STREAM))
  280. {
  281. serial->ops->putc(serial, '\r');
  282. }
  283. serial->ops->putc(serial, *ptr);
  284. ++ ptr;
  285. -- size;
  286. }
  287. }
  288. write_nbytes = (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  289. /* set error code if and only if in thread context */
  290. if (write_nbytes == 0 && !rt_interrupt_get_nest())
  291. {
  292. rt_set_errno(-RT_EFULL);
  293. }
  294. return write_nbytes;
  295. }
  296. static rt_err_t rt_serial_control(struct rt_device *dev,
  297. rt_uint8_t cmd,
  298. void *args)
  299. {
  300. struct rt_serial_device *serial;
  301. RT_ASSERT(dev != RT_NULL);
  302. serial = (struct rt_serial_device *)dev;
  303. switch (cmd)
  304. {
  305. case RT_DEVICE_CTRL_SUSPEND:
  306. /* suspend device */
  307. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  308. break;
  309. case RT_DEVICE_CTRL_RESUME:
  310. /* resume device */
  311. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  312. break;
  313. case RT_DEVICE_CTRL_CONFIG:
  314. /* configure device */
  315. serial->ops->configure(serial, (struct serial_configure *)args);
  316. break;
  317. }
  318. return RT_EOK;
  319. }
  320. /*
  321. * serial register
  322. */
  323. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
  324. const char *name,
  325. rt_uint32_t flag,
  326. void *data)
  327. {
  328. struct rt_device *device;
  329. RT_ASSERT(serial != RT_NULL);
  330. device = &(serial->parent);
  331. device->type = RT_Device_Class_Char;
  332. device->rx_indicate = RT_NULL;
  333. device->tx_complete = RT_NULL;
  334. device->init = rt_serial_init;
  335. device->open = rt_serial_open;
  336. device->close = rt_serial_close;
  337. device->read = rt_serial_read;
  338. device->write = rt_serial_write;
  339. device->control = rt_serial_control;
  340. device->user_data = data;
  341. /* register a character device */
  342. return rt_device_register(device, name, flag);
  343. }
  344. /* ISR for serial interrupt */
  345. void rt_hw_serial_isr(struct rt_serial_device *serial)
  346. {
  347. int ch = -1;
  348. /* interrupt mode receive */
  349. RT_ASSERT(serial->parent.flag & RT_DEVICE_FLAG_INT_RX);
  350. while (1)
  351. {
  352. ch = serial->ops->getc(serial);
  353. if (ch == -1)
  354. break;
  355. serial_ringbuffer_putc(serial->int_rx, ch);
  356. }
  357. /* invoke callback */
  358. if (serial->parent.rx_indicate != RT_NULL)
  359. {
  360. rt_size_t rx_length;
  361. /* get rx length */
  362. rx_length = serial_ringbuffer_size(serial->int_rx);
  363. serial->parent.rx_indicate(&serial->parent, rx_length);
  364. }
  365. }
  366. /*
  367. * ISR for DMA mode Tx
  368. */
  369. void rt_hw_serial_dma_tx_isr(struct rt_serial_device *serial)
  370. {
  371. const void *data_ptr;
  372. rt_size_t data_size;
  373. const void *last_data_ptr;
  374. rt_data_queue_pop(&(serial->tx_dq), &last_data_ptr, &data_size, 0);
  375. if (RT_EOK == rt_data_queue_peak(&(serial->tx_dq), &data_ptr, &data_size))
  376. {
  377. /* transmit next data node */
  378. serial->ops->dma_transmit(serial, data_ptr, data_size);
  379. }
  380. else
  381. {
  382. serial->dma_flag = RT_FALSE;
  383. }
  384. /* invoke callback */
  385. if (serial->parent.tx_complete != RT_NULL)
  386. {
  387. serial->parent.tx_complete(&serial->parent, (void*)last_data_ptr);
  388. }
  389. }