serial.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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-07-10 bernard rewrite serial framework
  29. * 2014-12-31 bernard use open_flag for poll_tx stream mode.
  30. * 2015-05-19 Quintin fix DMA tx mod tx_dma->activated flag !=RT_FALSE BUG
  31. * in open function.
  32. */
  33. #include <rthw.h>
  34. #include <rtthread.h>
  35. #include <rtdevice.h>
  36. /*
  37. * Serial poll routines
  38. */
  39. rt_inline int _serial_poll_rx(struct rt_serial_device *serial, rt_uint8_t *data, int length)
  40. {
  41. int ch;
  42. int size;
  43. RT_ASSERT(serial != RT_NULL);
  44. size = length;
  45. while (length)
  46. {
  47. ch = serial->ops->getc(serial);
  48. *data = ch;
  49. data ++; length --;
  50. if (ch == '\n') break;
  51. }
  52. return size - length;
  53. }
  54. rt_inline int _serial_poll_tx(struct rt_serial_device *serial, const rt_uint8_t *data, int length)
  55. {
  56. int size;
  57. RT_ASSERT(serial != RT_NULL);
  58. size = length;
  59. while (length)
  60. {
  61. /*
  62. * to be polite with serial console add a line feed
  63. * to the carriage return character
  64. */
  65. if (*data == '\n' && (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM))
  66. {
  67. serial->ops->putc(serial, '\r');
  68. }
  69. serial->ops->putc(serial, *data);
  70. ++ data;
  71. -- length;
  72. }
  73. return size - length;
  74. }
  75. /*
  76. * Serial interrupt routines
  77. */
  78. rt_inline int _serial_int_rx(struct rt_serial_device *serial, rt_uint8_t *data, int length)
  79. {
  80. int size;
  81. struct rt_serial_rx_fifo* rx_fifo;
  82. RT_ASSERT(serial != RT_NULL);
  83. size = length;
  84. rx_fifo = (struct rt_serial_rx_fifo*) serial->serial_rx;
  85. RT_ASSERT(rx_fifo != RT_NULL);
  86. /* read from software FIFO */
  87. while (length)
  88. {
  89. int ch;
  90. rt_base_t level;
  91. /* disable interrupt */
  92. level = rt_hw_interrupt_disable();
  93. if (rx_fifo->get_index != rx_fifo->put_index)
  94. {
  95. ch = rx_fifo->buffer[rx_fifo->get_index];
  96. rx_fifo->get_index += 1;
  97. if (rx_fifo->get_index >= serial->config.bufsz) rx_fifo->get_index = 0;
  98. }
  99. else
  100. {
  101. /* no data, enable interrupt and break out */
  102. rt_hw_interrupt_enable(level);
  103. break;
  104. }
  105. /* enable interrupt */
  106. rt_hw_interrupt_enable(level);
  107. *data = ch & 0xff;
  108. data ++; length --;
  109. }
  110. return size - length;
  111. }
  112. rt_inline int _serial_int_tx(struct rt_serial_device *serial, const rt_uint8_t *data, int length)
  113. {
  114. int size;
  115. struct rt_serial_tx_fifo *tx;
  116. RT_ASSERT(serial != RT_NULL);
  117. size = length;
  118. tx = (struct rt_serial_tx_fifo*) serial->serial_tx;
  119. RT_ASSERT(tx != RT_NULL);
  120. while (length)
  121. {
  122. if (serial->ops->putc(serial, *(char*)data) == -1)
  123. {
  124. rt_completion_wait(&(tx->completion), RT_WAITING_FOREVER);
  125. continue;
  126. }
  127. data ++; length --;
  128. }
  129. return size - length;
  130. }
  131. /*
  132. * Serial DMA routines
  133. */
  134. rt_inline int _serial_dma_rx(struct rt_serial_device *serial, rt_uint8_t *data, int length)
  135. {
  136. rt_base_t level;
  137. int result = RT_EOK;
  138. struct rt_serial_rx_dma *rx_dma;
  139. RT_ASSERT((serial != RT_NULL) && (data != RT_NULL));
  140. rx_dma = (struct rt_serial_rx_dma*)serial->serial_rx;
  141. RT_ASSERT(rx_dma != RT_NULL);
  142. level = rt_hw_interrupt_disable();
  143. if (rx_dma->activated != RT_TRUE)
  144. {
  145. rx_dma->activated = RT_TRUE;
  146. serial->ops->dma_transmit(serial, data, length, RT_SERIAL_DMA_RX);
  147. }
  148. else result = -RT_EBUSY;
  149. rt_hw_interrupt_enable(level);
  150. if (result == RT_EOK) return length;
  151. rt_set_errno(result);
  152. return 0;
  153. }
  154. rt_inline int _serial_dma_tx(struct rt_serial_device *serial, const rt_uint8_t *data, int length)
  155. {
  156. rt_base_t level;
  157. rt_err_t result;
  158. struct rt_serial_tx_dma *tx_dma;
  159. tx_dma = (struct rt_serial_tx_dma*)(serial->serial_tx);
  160. result = rt_data_queue_push(&(tx_dma->data_queue), data, length, RT_WAITING_FOREVER);
  161. if (result == RT_EOK)
  162. {
  163. level = rt_hw_interrupt_disable();
  164. if (tx_dma->activated != RT_TRUE)
  165. {
  166. tx_dma->activated = RT_TRUE;
  167. rt_hw_interrupt_enable(level);
  168. /* make a DMA transfer */
  169. serial->ops->dma_transmit(serial, data, length, RT_SERIAL_DMA_TX);
  170. }
  171. else
  172. {
  173. rt_hw_interrupt_enable(level);
  174. }
  175. return length;
  176. }
  177. else
  178. {
  179. rt_set_errno(result);
  180. return 0;
  181. }
  182. }
  183. /* RT-Thread Device Interface */
  184. /*
  185. * This function initializes serial device.
  186. */
  187. static rt_err_t rt_serial_init(struct rt_device *dev)
  188. {
  189. rt_err_t result = RT_EOK;
  190. struct rt_serial_device *serial;
  191. RT_ASSERT(dev != RT_NULL);
  192. serial = (struct rt_serial_device *)dev;
  193. /* initialize rx/tx */
  194. serial->serial_rx = RT_NULL;
  195. serial->serial_tx = RT_NULL;
  196. /* apply configuration */
  197. if (serial->ops->configure)
  198. result = serial->ops->configure(serial, &serial->config);
  199. return result;
  200. }
  201. static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
  202. {
  203. struct rt_serial_device *serial;
  204. RT_ASSERT(dev != RT_NULL);
  205. serial = (struct rt_serial_device *)dev;
  206. /* check device flag with the open flag */
  207. if ((oflag & RT_DEVICE_FLAG_DMA_RX) && !(dev->flag & RT_DEVICE_FLAG_DMA_RX))
  208. return -RT_EIO;
  209. if ((oflag & RT_DEVICE_FLAG_DMA_TX) && !(dev->flag & RT_DEVICE_FLAG_DMA_TX))
  210. return -RT_EIO;
  211. if ((oflag & RT_DEVICE_FLAG_INT_RX) && !(dev->flag & RT_DEVICE_FLAG_INT_RX))
  212. return -RT_EIO;
  213. if ((oflag & RT_DEVICE_FLAG_INT_TX) && !(dev->flag & RT_DEVICE_FLAG_INT_TX))
  214. return -RT_EIO;
  215. /* get open flags */
  216. dev->open_flag = oflag & 0xff;
  217. /* initialize the Rx/Tx structure according to open flag */
  218. if (serial->serial_rx == RT_NULL)
  219. {
  220. if (oflag & RT_DEVICE_FLAG_DMA_RX)
  221. {
  222. struct rt_serial_rx_dma* rx_dma;
  223. rx_dma = (struct rt_serial_rx_dma*) rt_malloc (sizeof(struct rt_serial_rx_dma));
  224. RT_ASSERT(rx_dma != RT_NULL);
  225. rx_dma->activated = RT_FALSE;
  226. serial->serial_rx = rx_dma;
  227. dev->open_flag |= RT_DEVICE_FLAG_DMA_RX;
  228. }
  229. else if (oflag & RT_DEVICE_FLAG_INT_RX)
  230. {
  231. struct rt_serial_rx_fifo* rx_fifo;
  232. rx_fifo = (struct rt_serial_rx_fifo*) rt_malloc (sizeof(struct rt_serial_rx_fifo) +
  233. serial->config.bufsz);
  234. RT_ASSERT(rx_fifo != RT_NULL);
  235. rx_fifo->buffer = (rt_uint8_t*) (rx_fifo + 1);
  236. rt_memset(rx_fifo->buffer, 0, serial->config.bufsz);
  237. rx_fifo->put_index = 0;
  238. rx_fifo->get_index = 0;
  239. serial->serial_rx = rx_fifo;
  240. dev->open_flag |= RT_DEVICE_FLAG_INT_RX;
  241. /* configure low level device */
  242. serial->ops->control(serial, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_RX);
  243. }
  244. else
  245. {
  246. serial->serial_rx = RT_NULL;
  247. }
  248. }
  249. if (serial->serial_tx == RT_NULL)
  250. {
  251. if (oflag & RT_DEVICE_FLAG_DMA_TX)
  252. {
  253. struct rt_serial_tx_dma* tx_dma;
  254. tx_dma = (struct rt_serial_tx_dma*) rt_malloc (sizeof(struct rt_serial_tx_dma));
  255. RT_ASSERT(tx_dma != RT_NULL);
  256. tx_dma->activated = RT_FALSE;
  257. rt_data_queue_init(&(tx_dma->data_queue), 8, 4, RT_NULL);
  258. serial->serial_tx = tx_dma;
  259. dev->open_flag |= RT_DEVICE_FLAG_DMA_TX;
  260. }
  261. else if (oflag & RT_DEVICE_FLAG_INT_TX)
  262. {
  263. struct rt_serial_tx_fifo *tx_fifo;
  264. tx_fifo = (struct rt_serial_tx_fifo*) rt_malloc(sizeof(struct rt_serial_tx_fifo));
  265. RT_ASSERT(tx_fifo != RT_NULL);
  266. rt_completion_init(&(tx_fifo->completion));
  267. serial->serial_tx = tx_fifo;
  268. dev->open_flag |= RT_DEVICE_FLAG_INT_TX;
  269. /* configure low level device */
  270. serial->ops->control(serial, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_TX);
  271. }
  272. else
  273. {
  274. serial->serial_tx = RT_NULL;
  275. }
  276. }
  277. return RT_EOK;
  278. }
  279. static rt_err_t rt_serial_close(struct rt_device *dev)
  280. {
  281. struct rt_serial_device *serial;
  282. RT_ASSERT(dev != RT_NULL);
  283. serial = (struct rt_serial_device *)dev;
  284. /* this device has more reference count */
  285. if (dev->ref_count > 1) return RT_EOK;
  286. if (dev->open_flag & RT_DEVICE_FLAG_INT_RX)
  287. {
  288. struct rt_serial_rx_fifo* rx_fifo;
  289. rx_fifo = (struct rt_serial_rx_fifo*)serial->serial_rx;
  290. RT_ASSERT(rx_fifo != RT_NULL);
  291. rt_free(rx_fifo);
  292. serial->serial_rx = RT_NULL;
  293. dev->open_flag &= ~RT_DEVICE_FLAG_INT_RX;
  294. /* configure low level device */
  295. serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void*)RT_DEVICE_FLAG_INT_TX);
  296. }
  297. else if (dev->open_flag & RT_DEVICE_FLAG_DMA_RX)
  298. {
  299. struct rt_serial_rx_dma* rx_dma;
  300. rx_dma = (struct rt_serial_rx_dma*)serial->serial_tx;
  301. RT_ASSERT(rx_dma != RT_NULL);
  302. rt_free(rx_dma);
  303. serial->serial_rx = RT_NULL;
  304. dev->open_flag &= ~RT_DEVICE_FLAG_DMA_RX;
  305. }
  306. if (dev->open_flag & RT_DEVICE_FLAG_INT_TX)
  307. {
  308. struct rt_serial_tx_fifo* tx_fifo;
  309. tx_fifo = (struct rt_serial_tx_fifo*)serial->serial_rx;
  310. RT_ASSERT(tx_fifo != RT_NULL);
  311. rt_free(tx_fifo);
  312. serial->serial_tx = RT_NULL;
  313. dev->open_flag &= ~RT_DEVICE_FLAG_INT_TX;
  314. /* configure low level device */
  315. serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void*)RT_DEVICE_FLAG_INT_TX);
  316. }
  317. else if (dev->open_flag & RT_DEVICE_FLAG_DMA_TX)
  318. {
  319. struct rt_serial_tx_dma* tx_dma;
  320. tx_dma = (struct rt_serial_tx_dma*)serial->serial_tx;
  321. RT_ASSERT(tx_dma != RT_NULL);
  322. rt_free(tx_dma);
  323. serial->serial_tx = RT_NULL;
  324. dev->open_flag &= ~RT_DEVICE_FLAG_DMA_TX;
  325. }
  326. return RT_EOK;
  327. }
  328. static rt_size_t rt_serial_read(struct rt_device *dev,
  329. rt_off_t pos,
  330. void *buffer,
  331. rt_size_t size)
  332. {
  333. struct rt_serial_device *serial;
  334. RT_ASSERT(dev != RT_NULL);
  335. if (size == 0) return 0;
  336. serial = (struct rt_serial_device *)dev;
  337. if (dev->open_flag & RT_DEVICE_FLAG_INT_RX)
  338. {
  339. return _serial_int_rx(serial, buffer, size);
  340. }
  341. else if (dev->open_flag & RT_DEVICE_FLAG_DMA_RX)
  342. {
  343. return _serial_dma_rx(serial, buffer, size);
  344. }
  345. return _serial_poll_rx(serial, buffer, size);
  346. }
  347. static rt_size_t rt_serial_write(struct rt_device *dev,
  348. rt_off_t pos,
  349. const void *buffer,
  350. rt_size_t size)
  351. {
  352. struct rt_serial_device *serial;
  353. RT_ASSERT(dev != RT_NULL);
  354. if (size == 0) return 0;
  355. serial = (struct rt_serial_device *)dev;
  356. if (dev->open_flag & RT_DEVICE_FLAG_INT_TX)
  357. {
  358. return _serial_int_tx(serial, buffer, size);
  359. }
  360. else if (dev->open_flag & RT_DEVICE_FLAG_DMA_TX)
  361. {
  362. return _serial_dma_tx(serial, buffer, size);
  363. }
  364. else
  365. {
  366. return _serial_poll_tx(serial, buffer, size);
  367. }
  368. }
  369. static rt_err_t rt_serial_control(struct rt_device *dev,
  370. rt_uint8_t cmd,
  371. void *args)
  372. {
  373. struct rt_serial_device *serial;
  374. RT_ASSERT(dev != RT_NULL);
  375. serial = (struct rt_serial_device *)dev;
  376. switch (cmd)
  377. {
  378. case RT_DEVICE_CTRL_SUSPEND:
  379. /* suspend device */
  380. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  381. break;
  382. case RT_DEVICE_CTRL_RESUME:
  383. /* resume device */
  384. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  385. break;
  386. case RT_DEVICE_CTRL_CONFIG:
  387. /* configure device */
  388. serial->ops->configure(serial, (struct serial_configure *)args);
  389. break;
  390. default :
  391. /* control device */
  392. serial->ops->control(serial, cmd, args);
  393. break;
  394. }
  395. return RT_EOK;
  396. }
  397. /*
  398. * serial register
  399. */
  400. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
  401. const char *name,
  402. rt_uint32_t flag,
  403. void *data)
  404. {
  405. struct rt_device *device;
  406. RT_ASSERT(serial != RT_NULL);
  407. device = &(serial->parent);
  408. device->type = RT_Device_Class_Char;
  409. device->rx_indicate = RT_NULL;
  410. device->tx_complete = RT_NULL;
  411. device->init = rt_serial_init;
  412. device->open = rt_serial_open;
  413. device->close = rt_serial_close;
  414. device->read = rt_serial_read;
  415. device->write = rt_serial_write;
  416. device->control = rt_serial_control;
  417. device->user_data = data;
  418. /* register a character device */
  419. return rt_device_register(device, name, flag);
  420. }
  421. /* ISR for serial interrupt */
  422. void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
  423. {
  424. switch (event & 0xff)
  425. {
  426. case RT_SERIAL_EVENT_RX_IND:
  427. {
  428. int ch = -1;
  429. rt_base_t level;
  430. struct rt_serial_rx_fifo* rx_fifo;
  431. /* interrupt mode receive */
  432. rx_fifo = (struct rt_serial_rx_fifo*)serial->serial_rx;
  433. RT_ASSERT(rx_fifo != RT_NULL);
  434. while (1)
  435. {
  436. ch = serial->ops->getc(serial);
  437. if (ch == -1) break;
  438. /* disable interrupt */
  439. level = rt_hw_interrupt_disable();
  440. rx_fifo->buffer[rx_fifo->put_index] = ch;
  441. rx_fifo->put_index += 1;
  442. if (rx_fifo->put_index >= serial->config.bufsz) rx_fifo->put_index = 0;
  443. /* if the next position is read index, discard this 'read char' */
  444. if (rx_fifo->put_index == rx_fifo->get_index)
  445. {
  446. rx_fifo->get_index += 1;
  447. if (rx_fifo->get_index >= serial->config.bufsz) rx_fifo->get_index = 0;
  448. }
  449. /* enable interrupt */
  450. rt_hw_interrupt_enable(level);
  451. }
  452. /* invoke callback */
  453. if (serial->parent.rx_indicate != RT_NULL)
  454. {
  455. rt_size_t rx_length;
  456. /* get rx length */
  457. level = rt_hw_interrupt_disable();
  458. rx_length = (rx_fifo->put_index >= rx_fifo->get_index)? (rx_fifo->put_index - rx_fifo->get_index):
  459. (serial->config.bufsz - (rx_fifo->get_index - rx_fifo->put_index));
  460. rt_hw_interrupt_enable(level);
  461. serial->parent.rx_indicate(&serial->parent, rx_length);
  462. }
  463. break;
  464. }
  465. case RT_SERIAL_EVENT_TX_DONE:
  466. {
  467. struct rt_serial_tx_fifo* tx_fifo;
  468. tx_fifo = (struct rt_serial_tx_fifo*)serial->serial_tx;
  469. rt_completion_done(&(tx_fifo->completion));
  470. break;
  471. }
  472. case RT_SERIAL_EVENT_TX_DMADONE:
  473. {
  474. const void *data_ptr;
  475. rt_size_t data_size;
  476. const void *last_data_ptr;
  477. struct rt_serial_tx_dma* tx_dma;
  478. tx_dma = (struct rt_serial_tx_dma*) serial->serial_tx;
  479. rt_data_queue_pop(&(tx_dma->data_queue), &last_data_ptr, &data_size, 0);
  480. if (rt_data_queue_peak(&(tx_dma->data_queue), &data_ptr, &data_size) == RT_EOK)
  481. {
  482. /* transmit next data node */
  483. tx_dma->activated = RT_TRUE;
  484. serial->ops->dma_transmit(serial, data_ptr, data_size, RT_SERIAL_DMA_TX);
  485. }
  486. else
  487. {
  488. tx_dma->activated = RT_FALSE;
  489. }
  490. /* invoke callback */
  491. if (serial->parent.tx_complete != RT_NULL)
  492. {
  493. serial->parent.tx_complete(&serial->parent, (void*)last_data_ptr);
  494. }
  495. break;
  496. }
  497. case RT_SERIAL_EVENT_RX_DMADONE:
  498. {
  499. int length;
  500. struct rt_serial_rx_dma* rx_dma;
  501. rx_dma = (struct rt_serial_rx_dma*)serial->serial_rx;
  502. /* get DMA rx length */
  503. length = (event & (~0xff)) >> 8;
  504. serial->parent.rx_indicate(&(serial->parent), length);
  505. rx_dma->activated = RT_FALSE;
  506. break;
  507. }
  508. }
  509. }