serial.c 24 KB

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