serial.c 12 KB

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