serial.c 23 KB

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