serial.c 24 KB

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