serial.c 24 KB

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