serial.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2006-03-13 bernard first version
  9. * 2012-05-15 lgnq modified according bernard's implementation.
  10. * 2012-05-28 bernard code cleanup
  11. * 2012-11-23 bernard fix compiler warning.
  12. * 2013-02-20 bernard use RT_SERIAL_RB_BUFSZ to define
  13. * the size of ring buffer.
  14. * 2014-07-10 bernard rewrite serial framework
  15. * 2014-12-31 bernard use open_flag for poll_tx stream mode.
  16. * 2015-05-19 Quintin fix DMA tx mod tx_dma->activated flag !=RT_FALSE BUG
  17. * in open function.
  18. * 2015-11-10 bernard fix the poll rx issue when there is no data.
  19. * 2016-05-10 armink add fifo mode to DMA rx when serial->config.bufsz != 0.
  20. * 2017-01-19 aubr.cool prevent change serial rx bufsz when serial is opened.
  21. * 2017-11-07 JasonJia fix data bits error issue when using tcsetattr.
  22. * 2017-11-15 JasonJia fix poll rx issue when data is full.
  23. * add TCFLSH and FIONREAD support.
  24. * 2018-12-08 Ernest Chen add DMA choice
  25. */
  26. #include <rthw.h>
  27. #include <rtthread.h>
  28. #include <rtdevice.h>
  29. #define DBG_TAG "UART"
  30. #define DBG_LVL DBG_INFO
  31. #include <rtdbg.h>
  32. #ifdef RT_USING_POSIX
  33. #include <dfs_posix.h>
  34. #include <dfs_poll.h>
  35. #ifdef RT_USING_POSIX_TERMIOS
  36. #include <posix_termios.h>
  37. #endif
  38. /* it's possible the 'getc/putc' is defined by stdio.h in gcc/newlib. */
  39. #ifdef getc
  40. #undef getc
  41. #endif
  42. #ifdef putc
  43. #undef putc
  44. #endif
  45. static rt_err_t serial_fops_rx_ind(rt_device_t dev, rt_size_t size)
  46. {
  47. rt_wqueue_wakeup(&(dev->wait_queue), (void*)POLLIN);
  48. return RT_EOK;
  49. }
  50. /* fops for serial */
  51. static int serial_fops_open(struct dfs_fd *fd)
  52. {
  53. rt_err_t ret = 0;
  54. rt_uint16_t flags = 0;
  55. rt_device_t device;
  56. device = (rt_device_t)fd->data;
  57. RT_ASSERT(device != RT_NULL);
  58. switch (fd->flags & O_ACCMODE)
  59. {
  60. case O_RDONLY:
  61. LOG_D("fops open: O_RDONLY!");
  62. flags = RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_RDONLY;
  63. break;
  64. case O_WRONLY:
  65. LOG_D("fops open: O_WRONLY!");
  66. flags = RT_DEVICE_FLAG_WRONLY;
  67. break;
  68. case O_RDWR:
  69. LOG_D("fops open: O_RDWR!");
  70. flags = RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_RDWR;
  71. break;
  72. default:
  73. LOG_E("fops open: unknown mode - %d!", fd->flags & O_ACCMODE);
  74. break;
  75. }
  76. if ((fd->flags & O_ACCMODE) != O_WRONLY)
  77. rt_device_set_rx_indicate(device, serial_fops_rx_ind);
  78. ret = rt_device_open(device, flags);
  79. if (ret == RT_EOK) return 0;
  80. return ret;
  81. }
  82. static int serial_fops_close(struct dfs_fd *fd)
  83. {
  84. rt_device_t device;
  85. device = (rt_device_t)fd->data;
  86. rt_device_set_rx_indicate(device, RT_NULL);
  87. rt_device_close(device);
  88. return 0;
  89. }
  90. static int serial_fops_ioctl(struct dfs_fd *fd, int cmd, void *args)
  91. {
  92. rt_device_t device;
  93. device = (rt_device_t)fd->data;
  94. switch (cmd)
  95. {
  96. case FIONREAD:
  97. break;
  98. case FIONWRITE:
  99. break;
  100. }
  101. return rt_device_control(device, cmd, args);
  102. }
  103. static int serial_fops_read(struct dfs_fd *fd, void *buf, size_t count)
  104. {
  105. int size = 0;
  106. rt_device_t device;
  107. device = (rt_device_t)fd->data;
  108. do
  109. {
  110. size = rt_device_read(device, -1, buf, count);
  111. if (size <= 0)
  112. {
  113. if (fd->flags & O_NONBLOCK)
  114. {
  115. size = -EAGAIN;
  116. break;
  117. }
  118. rt_wqueue_wait(&(device->wait_queue), 0, RT_WAITING_FOREVER);
  119. }
  120. }while (size <= 0);
  121. return size;
  122. }
  123. static int serial_fops_write(struct dfs_fd *fd, const void *buf, size_t count)
  124. {
  125. rt_device_t device;
  126. device = (rt_device_t)fd->data;
  127. return rt_device_write(device, -1, buf, count);
  128. }
  129. static int serial_fops_poll(struct dfs_fd *fd, struct rt_pollreq *req)
  130. {
  131. int mask = 0;
  132. int flags = 0;
  133. rt_device_t device;
  134. struct rt_serial_device *serial;
  135. device = (rt_device_t)fd->data;
  136. RT_ASSERT(device != RT_NULL);
  137. serial = (struct rt_serial_device *)device;
  138. /* only support POLLIN */
  139. flags = fd->flags & O_ACCMODE;
  140. if (flags == O_RDONLY || flags == O_RDWR)
  141. {
  142. rt_base_t level;
  143. struct rt_serial_rx_fifo* rx_fifo;
  144. rt_poll_add(&(device->wait_queue), req);
  145. rx_fifo = (struct rt_serial_rx_fifo*) serial->serial_rx;
  146. level = rt_hw_interrupt_disable();
  147. if ((rx_fifo->get_index != rx_fifo->put_index) || (rx_fifo->get_index == rx_fifo->put_index && rx_fifo->is_full == RT_TRUE))
  148. mask |= POLLIN;
  149. rt_hw_interrupt_enable(level);
  150. }
  151. return mask;
  152. }
  153. const static struct dfs_file_ops _serial_fops =
  154. {
  155. serial_fops_open,
  156. serial_fops_close,
  157. serial_fops_ioctl,
  158. serial_fops_read,
  159. serial_fops_write,
  160. RT_NULL, /* flush */
  161. RT_NULL, /* lseek */
  162. RT_NULL, /* getdents */
  163. serial_fops_poll,
  164. };
  165. #endif
  166. /*
  167. * Serial poll routines
  168. */
  169. rt_inline int _serial_poll_rx(struct rt_serial_device *serial, rt_uint8_t *data, int length)
  170. {
  171. int ch;
  172. int size;
  173. RT_ASSERT(serial != RT_NULL);
  174. size = length;
  175. while (length)
  176. {
  177. ch = serial->ops->getc(serial);
  178. if (ch == -1) break;
  179. *data = ch;
  180. data ++; length --;
  181. if(serial->parent.open_flag & RT_DEVICE_FLAG_STREAM)
  182. {
  183. if (ch == '\n') break;
  184. }
  185. }
  186. return size - length;
  187. }
  188. rt_inline int _serial_poll_tx(struct rt_serial_device *serial, const rt_uint8_t *data, int length)
  189. {
  190. int size;
  191. RT_ASSERT(serial != RT_NULL);
  192. size = length;
  193. while (length)
  194. {
  195. /*
  196. * to be polite with serial console add a line feed
  197. * to the carriage return character
  198. */
  199. if (*data == '\n' && (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM))
  200. {
  201. serial->ops->putc(serial, '\r');
  202. }
  203. serial->ops->putc(serial, *data);
  204. ++ data;
  205. -- length;
  206. }
  207. return size - length;
  208. }
  209. /*
  210. * Serial interrupt routines
  211. */
  212. rt_inline int _serial_int_rx(struct rt_serial_device *serial, rt_uint8_t *data, int length)
  213. {
  214. int size;
  215. struct rt_serial_rx_fifo* rx_fifo;
  216. RT_ASSERT(serial != RT_NULL);
  217. size = length;
  218. rx_fifo = (struct rt_serial_rx_fifo*) serial->serial_rx;
  219. RT_ASSERT(rx_fifo != RT_NULL);
  220. /* read from software FIFO */
  221. while (length)
  222. {
  223. int ch;
  224. rt_base_t level;
  225. /* disable interrupt */
  226. level = rt_hw_interrupt_disable();
  227. /* there's no data: */
  228. if ((rx_fifo->get_index == rx_fifo->put_index) && (rx_fifo->is_full == RT_FALSE))
  229. {
  230. /* no data, enable interrupt and break out */
  231. rt_hw_interrupt_enable(level);
  232. break;
  233. }
  234. /* otherwise there's the data: */
  235. ch = rx_fifo->buffer[rx_fifo->get_index];
  236. rx_fifo->get_index += 1;
  237. if (rx_fifo->get_index >= serial->config.bufsz) rx_fifo->get_index = 0;
  238. if (rx_fifo->is_full == RT_TRUE)
  239. {
  240. rx_fifo->is_full = RT_FALSE;
  241. }
  242. /* enable interrupt */
  243. rt_hw_interrupt_enable(level);
  244. *data = ch & 0xff;
  245. data ++; length --;
  246. }
  247. return size - length;
  248. }
  249. rt_inline int _serial_int_tx(struct rt_serial_device *serial, const rt_uint8_t *data, int length)
  250. {
  251. int size;
  252. struct rt_serial_tx_fifo *tx;
  253. RT_ASSERT(serial != RT_NULL);
  254. size = length;
  255. tx = (struct rt_serial_tx_fifo*) serial->serial_tx;
  256. RT_ASSERT(tx != RT_NULL);
  257. while (length)
  258. {
  259. /*
  260. * to be polite with serial console add a line feed
  261. * to the carriage return character
  262. */
  263. if (*data == '\n' && (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM))
  264. {
  265. if (serial->ops->putc(serial, '\r') == -1)
  266. {
  267. rt_completion_wait(&(tx->completion), RT_WAITING_FOREVER);
  268. continue;
  269. }
  270. }
  271. if (serial->ops->putc(serial, *(char*)data) == -1)
  272. {
  273. rt_completion_wait(&(tx->completion), RT_WAITING_FOREVER);
  274. continue;
  275. }
  276. data ++; length --;
  277. }
  278. return size - length;
  279. }
  280. static void _serial_check_buffer_size(void)
  281. {
  282. static rt_bool_t already_output = RT_FALSE;
  283. if (already_output == RT_FALSE)
  284. {
  285. #if !defined(RT_USING_ULOG) || defined(ULOG_USING_ISR_LOG)
  286. LOG_W("Warning: There is no enough buffer for saving data,"
  287. " please increase the RT_SERIAL_RB_BUFSZ option.");
  288. #endif
  289. already_output = RT_TRUE;
  290. }
  291. }
  292. #if defined(RT_USING_POSIX) || defined(RT_SERIAL_USING_DMA)
  293. static rt_size_t _serial_fifo_calc_recved_len(struct rt_serial_device *serial)
  294. {
  295. struct rt_serial_rx_fifo *rx_fifo = (struct rt_serial_rx_fifo *) serial->serial_rx;
  296. RT_ASSERT(rx_fifo != RT_NULL);
  297. if (rx_fifo->put_index == rx_fifo->get_index)
  298. {
  299. return (rx_fifo->is_full == RT_FALSE ? 0 : serial->config.bufsz);
  300. }
  301. else
  302. {
  303. if (rx_fifo->put_index > rx_fifo->get_index)
  304. {
  305. return rx_fifo->put_index - rx_fifo->get_index;
  306. }
  307. else
  308. {
  309. return serial->config.bufsz - (rx_fifo->get_index - rx_fifo->put_index);
  310. }
  311. }
  312. }
  313. #endif /* RT_USING_POSIX || RT_SERIAL_USING_DMA */
  314. #ifdef RT_SERIAL_USING_DMA
  315. /**
  316. * Calculate DMA received data length.
  317. *
  318. * @param serial serial device
  319. *
  320. * @return length
  321. */
  322. static rt_size_t rt_dma_calc_recved_len(struct rt_serial_device *serial)
  323. {
  324. return _serial_fifo_calc_recved_len(serial);
  325. }
  326. /**
  327. * Read data finish by DMA mode then update the get index for receive fifo.
  328. *
  329. * @param serial serial device
  330. * @param len get data length for this operate
  331. */
  332. static void rt_dma_recv_update_get_index(struct rt_serial_device *serial, rt_size_t len)
  333. {
  334. struct rt_serial_rx_fifo *rx_fifo = (struct rt_serial_rx_fifo *) serial->serial_rx;
  335. RT_ASSERT(rx_fifo != RT_NULL);
  336. RT_ASSERT(len <= rt_dma_calc_recved_len(serial));
  337. if (rx_fifo->is_full && len != 0) rx_fifo->is_full = RT_FALSE;
  338. rx_fifo->get_index += len;
  339. if (rx_fifo->get_index >= serial->config.bufsz)
  340. {
  341. rx_fifo->get_index %= serial->config.bufsz;
  342. }
  343. }
  344. /**
  345. * DMA received finish then update put index for receive fifo.
  346. *
  347. * @param serial serial device
  348. * @param len received length for this transmit
  349. */
  350. static void rt_dma_recv_update_put_index(struct rt_serial_device *serial, rt_size_t len)
  351. {
  352. struct rt_serial_rx_fifo *rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
  353. RT_ASSERT(rx_fifo != RT_NULL);
  354. if (rx_fifo->get_index <= rx_fifo->put_index)
  355. {
  356. rx_fifo->put_index += len;
  357. /* beyond the fifo end */
  358. if (rx_fifo->put_index >= serial->config.bufsz)
  359. {
  360. rx_fifo->put_index %= serial->config.bufsz;
  361. /* force overwrite get index */
  362. if (rx_fifo->put_index >= rx_fifo->get_index)
  363. {
  364. rx_fifo->is_full = RT_TRUE;
  365. }
  366. }
  367. }
  368. else
  369. {
  370. rx_fifo->put_index += len;
  371. if (rx_fifo->put_index >= rx_fifo->get_index)
  372. {
  373. /* beyond the fifo end */
  374. if (rx_fifo->put_index >= serial->config.bufsz)
  375. {
  376. rx_fifo->put_index %= serial->config.bufsz;
  377. }
  378. /* force overwrite get index */
  379. rx_fifo->is_full = RT_TRUE;
  380. }
  381. }
  382. if(rx_fifo->is_full == RT_TRUE)
  383. {
  384. _serial_check_buffer_size();
  385. rx_fifo->get_index = rx_fifo->put_index;
  386. }
  387. }
  388. /*
  389. * Serial DMA routines
  390. */
  391. rt_inline int _serial_dma_rx(struct rt_serial_device *serial, rt_uint8_t *data, int length)
  392. {
  393. rt_base_t level;
  394. RT_ASSERT((serial != RT_NULL) && (data != RT_NULL));
  395. level = rt_hw_interrupt_disable();
  396. if (serial->config.bufsz == 0)
  397. {
  398. int result = RT_EOK;
  399. struct rt_serial_rx_dma *rx_dma;
  400. rx_dma = (struct rt_serial_rx_dma*)serial->serial_rx;
  401. RT_ASSERT(rx_dma != RT_NULL);
  402. if (rx_dma->activated != RT_TRUE)
  403. {
  404. rx_dma->activated = RT_TRUE;
  405. RT_ASSERT(serial->ops->dma_transmit != RT_NULL);
  406. serial->ops->dma_transmit(serial, data, length, RT_SERIAL_DMA_RX);
  407. }
  408. else result = -RT_EBUSY;
  409. rt_hw_interrupt_enable(level);
  410. if (result == RT_EOK) return length;
  411. rt_set_errno(result);
  412. return 0;
  413. }
  414. else
  415. {
  416. struct rt_serial_rx_fifo *rx_fifo = (struct rt_serial_rx_fifo *) serial->serial_rx;
  417. rt_size_t recv_len = 0, fifo_recved_len = rt_dma_calc_recved_len(serial);
  418. RT_ASSERT(rx_fifo != RT_NULL);
  419. if (length < (int)fifo_recved_len)
  420. recv_len = length;
  421. else
  422. recv_len = fifo_recved_len;
  423. if (rx_fifo->get_index + recv_len < serial->config.bufsz)
  424. rt_memcpy(data, rx_fifo->buffer + rx_fifo->get_index, recv_len);
  425. else
  426. {
  427. rt_memcpy(data, rx_fifo->buffer + rx_fifo->get_index,
  428. serial->config.bufsz - rx_fifo->get_index);
  429. rt_memcpy(data + serial->config.bufsz - rx_fifo->get_index, rx_fifo->buffer,
  430. recv_len + rx_fifo->get_index - serial->config.bufsz);
  431. }
  432. rt_dma_recv_update_get_index(serial, recv_len);
  433. rt_hw_interrupt_enable(level);
  434. return recv_len;
  435. }
  436. }
  437. rt_inline int _serial_dma_tx(struct rt_serial_device *serial, const rt_uint8_t *data, int length)
  438. {
  439. rt_base_t level;
  440. rt_err_t result;
  441. struct rt_serial_tx_dma *tx_dma;
  442. tx_dma = (struct rt_serial_tx_dma*)(serial->serial_tx);
  443. result = rt_data_queue_push(&(tx_dma->data_queue), data, length, RT_WAITING_FOREVER);
  444. if (result == RT_EOK)
  445. {
  446. level = rt_hw_interrupt_disable();
  447. if (tx_dma->activated != RT_TRUE)
  448. {
  449. tx_dma->activated = RT_TRUE;
  450. rt_hw_interrupt_enable(level);
  451. /* make a DMA transfer */
  452. serial->ops->dma_transmit(serial, (rt_uint8_t *)data, length, RT_SERIAL_DMA_TX);
  453. }
  454. else
  455. {
  456. rt_hw_interrupt_enable(level);
  457. }
  458. return length;
  459. }
  460. else
  461. {
  462. rt_set_errno(result);
  463. return 0;
  464. }
  465. }
  466. #endif /* RT_SERIAL_USING_DMA */
  467. /* RT-Thread Device Interface */
  468. /*
  469. * This function initializes serial device.
  470. */
  471. static rt_err_t rt_serial_init(struct rt_device *dev)
  472. {
  473. rt_err_t result = RT_EOK;
  474. struct rt_serial_device *serial;
  475. RT_ASSERT(dev != RT_NULL);
  476. serial = (struct rt_serial_device *)dev;
  477. /* initialize rx/tx */
  478. serial->serial_rx = RT_NULL;
  479. serial->serial_tx = RT_NULL;
  480. /* apply configuration */
  481. if (serial->ops->configure)
  482. result = serial->ops->configure(serial, &serial->config);
  483. return result;
  484. }
  485. static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
  486. {
  487. rt_uint16_t stream_flag = 0;
  488. struct rt_serial_device *serial;
  489. RT_ASSERT(dev != RT_NULL);
  490. serial = (struct rt_serial_device *)dev;
  491. LOG_D("open serial device: 0x%08x with open flag: 0x%04x",
  492. dev, oflag);
  493. /* check device flag with the open flag */
  494. if ((oflag & RT_DEVICE_FLAG_DMA_RX) && !(dev->flag & RT_DEVICE_FLAG_DMA_RX))
  495. return -RT_EIO;
  496. if ((oflag & RT_DEVICE_FLAG_DMA_TX) && !(dev->flag & RT_DEVICE_FLAG_DMA_TX))
  497. return -RT_EIO;
  498. if ((oflag & RT_DEVICE_FLAG_INT_RX) && !(dev->flag & RT_DEVICE_FLAG_INT_RX))
  499. return -RT_EIO;
  500. if ((oflag & RT_DEVICE_FLAG_INT_TX) && !(dev->flag & RT_DEVICE_FLAG_INT_TX))
  501. return -RT_EIO;
  502. /* keep steam flag */
  503. if ((oflag & RT_DEVICE_FLAG_STREAM) || (dev->open_flag & RT_DEVICE_FLAG_STREAM))
  504. stream_flag = RT_DEVICE_FLAG_STREAM;
  505. /* get open flags */
  506. dev->open_flag = oflag & 0xff;
  507. /* initialize the Rx/Tx structure according to open flag */
  508. if (serial->serial_rx == RT_NULL)
  509. {
  510. if (oflag & RT_DEVICE_FLAG_INT_RX)
  511. {
  512. struct rt_serial_rx_fifo* rx_fifo;
  513. rx_fifo = (struct rt_serial_rx_fifo*) rt_malloc (sizeof(struct rt_serial_rx_fifo) +
  514. serial->config.bufsz);
  515. RT_ASSERT(rx_fifo != RT_NULL);
  516. rx_fifo->buffer = (rt_uint8_t*) (rx_fifo + 1);
  517. rt_memset(rx_fifo->buffer, 0, serial->config.bufsz);
  518. rx_fifo->put_index = 0;
  519. rx_fifo->get_index = 0;
  520. rx_fifo->is_full = RT_FALSE;
  521. serial->serial_rx = rx_fifo;
  522. dev->open_flag |= RT_DEVICE_FLAG_INT_RX;
  523. /* configure low level device */
  524. serial->ops->control(serial, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_RX);
  525. }
  526. #ifdef RT_SERIAL_USING_DMA
  527. else if (oflag & RT_DEVICE_FLAG_DMA_RX)
  528. {
  529. if (serial->config.bufsz == 0) {
  530. struct rt_serial_rx_dma* rx_dma;
  531. rx_dma = (struct rt_serial_rx_dma*) rt_malloc (sizeof(struct rt_serial_rx_dma));
  532. RT_ASSERT(rx_dma != RT_NULL);
  533. rx_dma->activated = RT_FALSE;
  534. serial->serial_rx = rx_dma;
  535. } else {
  536. struct rt_serial_rx_fifo* rx_fifo;
  537. rx_fifo = (struct rt_serial_rx_fifo*) rt_malloc (sizeof(struct rt_serial_rx_fifo) +
  538. serial->config.bufsz);
  539. RT_ASSERT(rx_fifo != RT_NULL);
  540. rx_fifo->buffer = (rt_uint8_t*) (rx_fifo + 1);
  541. rt_memset(rx_fifo->buffer, 0, serial->config.bufsz);
  542. rx_fifo->put_index = 0;
  543. rx_fifo->get_index = 0;
  544. rx_fifo->is_full = RT_FALSE;
  545. serial->serial_rx = rx_fifo;
  546. /* configure fifo address and length to low level device */
  547. serial->ops->control(serial, RT_DEVICE_CTRL_CONFIG, (void *) RT_DEVICE_FLAG_DMA_RX);
  548. }
  549. dev->open_flag |= RT_DEVICE_FLAG_DMA_RX;
  550. }
  551. #endif /* RT_SERIAL_USING_DMA */
  552. else
  553. {
  554. serial->serial_rx = RT_NULL;
  555. }
  556. }
  557. else
  558. {
  559. if (oflag & RT_DEVICE_FLAG_INT_RX)
  560. dev->open_flag |= RT_DEVICE_FLAG_INT_RX;
  561. #ifdef RT_SERIAL_USING_DMA
  562. else if (oflag & RT_DEVICE_FLAG_DMA_RX)
  563. dev->open_flag |= RT_DEVICE_FLAG_DMA_RX;
  564. #endif /* RT_SERIAL_USING_DMA */
  565. }
  566. if (serial->serial_tx == RT_NULL)
  567. {
  568. if (oflag & RT_DEVICE_FLAG_INT_TX)
  569. {
  570. struct rt_serial_tx_fifo *tx_fifo;
  571. tx_fifo = (struct rt_serial_tx_fifo*) rt_malloc(sizeof(struct rt_serial_tx_fifo));
  572. RT_ASSERT(tx_fifo != RT_NULL);
  573. rt_completion_init(&(tx_fifo->completion));
  574. serial->serial_tx = tx_fifo;
  575. dev->open_flag |= RT_DEVICE_FLAG_INT_TX;
  576. /* configure low level device */
  577. serial->ops->control(serial, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_TX);
  578. }
  579. #ifdef RT_SERIAL_USING_DMA
  580. else if (oflag & RT_DEVICE_FLAG_DMA_TX)
  581. {
  582. struct rt_serial_tx_dma* tx_dma;
  583. tx_dma = (struct rt_serial_tx_dma*) rt_malloc (sizeof(struct rt_serial_tx_dma));
  584. RT_ASSERT(tx_dma != RT_NULL);
  585. tx_dma->activated = RT_FALSE;
  586. rt_data_queue_init(&(tx_dma->data_queue), 8, 4, RT_NULL);
  587. serial->serial_tx = tx_dma;
  588. dev->open_flag |= RT_DEVICE_FLAG_DMA_TX;
  589. /* configure low level device */
  590. serial->ops->control(serial, RT_DEVICE_CTRL_CONFIG, (void *)RT_DEVICE_FLAG_DMA_TX);
  591. }
  592. #endif /* RT_SERIAL_USING_DMA */
  593. else
  594. {
  595. serial->serial_tx = RT_NULL;
  596. }
  597. }
  598. else
  599. {
  600. if (oflag & RT_DEVICE_FLAG_INT_TX)
  601. dev->open_flag |= RT_DEVICE_FLAG_INT_TX;
  602. #ifdef RT_SERIAL_USING_DMA
  603. else if (oflag & RT_DEVICE_FLAG_DMA_TX)
  604. dev->open_flag |= RT_DEVICE_FLAG_DMA_TX;
  605. #endif /* RT_SERIAL_USING_DMA */
  606. }
  607. /* set stream flag */
  608. dev->open_flag |= stream_flag;
  609. return RT_EOK;
  610. }
  611. static rt_err_t rt_serial_close(struct rt_device *dev)
  612. {
  613. struct rt_serial_device *serial;
  614. RT_ASSERT(dev != RT_NULL);
  615. serial = (struct rt_serial_device *)dev;
  616. /* this device has more reference count */
  617. if (dev->ref_count > 1) return RT_EOK;
  618. if (dev->open_flag & RT_DEVICE_FLAG_INT_RX)
  619. {
  620. struct rt_serial_rx_fifo* rx_fifo;
  621. rx_fifo = (struct rt_serial_rx_fifo*)serial->serial_rx;
  622. RT_ASSERT(rx_fifo != RT_NULL);
  623. rt_free(rx_fifo);
  624. serial->serial_rx = RT_NULL;
  625. dev->open_flag &= ~RT_DEVICE_FLAG_INT_RX;
  626. /* configure low level device */
  627. serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void*)RT_DEVICE_FLAG_INT_RX);
  628. }
  629. #ifdef RT_SERIAL_USING_DMA
  630. else if (dev->open_flag & RT_DEVICE_FLAG_DMA_RX)
  631. {
  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_free(rx_dma);
  637. } else {
  638. struct rt_serial_rx_fifo* rx_fifo;
  639. rx_fifo = (struct rt_serial_rx_fifo*)serial->serial_rx;
  640. RT_ASSERT(rx_fifo != RT_NULL);
  641. rt_free(rx_fifo);
  642. }
  643. serial->serial_rx = RT_NULL;
  644. dev->open_flag &= ~RT_DEVICE_FLAG_DMA_RX;
  645. /* configure low level device */
  646. serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void *) RT_DEVICE_FLAG_DMA_RX);
  647. }
  648. #endif /* RT_SERIAL_USING_DMA */
  649. if (dev->open_flag & RT_DEVICE_FLAG_INT_TX)
  650. {
  651. struct rt_serial_tx_fifo* tx_fifo;
  652. tx_fifo = (struct rt_serial_tx_fifo*)serial->serial_tx;
  653. RT_ASSERT(tx_fifo != RT_NULL);
  654. rt_free(tx_fifo);
  655. serial->serial_tx = RT_NULL;
  656. dev->open_flag &= ~RT_DEVICE_FLAG_INT_TX;
  657. /* configure low level device */
  658. serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void*)RT_DEVICE_FLAG_INT_TX);
  659. }
  660. #ifdef RT_SERIAL_USING_DMA
  661. else if (dev->open_flag & RT_DEVICE_FLAG_DMA_TX)
  662. {
  663. struct rt_serial_tx_dma* tx_dma;
  664. tx_dma = (struct rt_serial_tx_dma*)serial->serial_tx;
  665. RT_ASSERT(tx_dma != RT_NULL);
  666. rt_data_queue_deinit(&(tx_dma->data_queue));
  667. rt_free(tx_dma);
  668. serial->serial_tx = RT_NULL;
  669. dev->open_flag &= ~RT_DEVICE_FLAG_DMA_TX;
  670. /* configure low level device */
  671. serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void *) RT_DEVICE_FLAG_DMA_TX);
  672. }
  673. serial->ops->control(serial, RT_DEVICE_CTRL_CLOSE, RT_NULL);
  674. dev->flag &= ~RT_DEVICE_FLAG_ACTIVATED;
  675. #endif /* RT_SERIAL_USING_DMA */
  676. return RT_EOK;
  677. }
  678. static rt_size_t rt_serial_read(struct rt_device *dev,
  679. rt_off_t pos,
  680. void *buffer,
  681. rt_size_t size)
  682. {
  683. struct rt_serial_device *serial;
  684. RT_ASSERT(dev != RT_NULL);
  685. if (size == 0) return 0;
  686. serial = (struct rt_serial_device *)dev;
  687. if (dev->open_flag & RT_DEVICE_FLAG_INT_RX)
  688. {
  689. return _serial_int_rx(serial, (rt_uint8_t *)buffer, size);
  690. }
  691. #ifdef RT_SERIAL_USING_DMA
  692. else if (dev->open_flag & RT_DEVICE_FLAG_DMA_RX)
  693. {
  694. return _serial_dma_rx(serial, (rt_uint8_t *)buffer, size);
  695. }
  696. #endif /* RT_SERIAL_USING_DMA */
  697. return _serial_poll_rx(serial, (rt_uint8_t *)buffer, size);
  698. }
  699. static rt_size_t rt_serial_write(struct rt_device *dev,
  700. rt_off_t pos,
  701. const void *buffer,
  702. rt_size_t size)
  703. {
  704. struct rt_serial_device *serial;
  705. RT_ASSERT(dev != RT_NULL);
  706. if (size == 0) return 0;
  707. serial = (struct rt_serial_device *)dev;
  708. if (dev->open_flag & RT_DEVICE_FLAG_INT_TX)
  709. {
  710. return _serial_int_tx(serial, (const rt_uint8_t *)buffer, size);
  711. }
  712. #ifdef RT_SERIAL_USING_DMA
  713. else if (dev->open_flag & RT_DEVICE_FLAG_DMA_TX)
  714. {
  715. return _serial_dma_tx(serial, (const rt_uint8_t *)buffer, size);
  716. }
  717. #endif /* RT_SERIAL_USING_DMA */
  718. else
  719. {
  720. return _serial_poll_tx(serial, (const rt_uint8_t *)buffer, size);
  721. }
  722. }
  723. #ifdef RT_USING_POSIX_TERMIOS
  724. struct speed_baudrate_item
  725. {
  726. speed_t speed;
  727. int baudrate;
  728. };
  729. const static struct speed_baudrate_item _tbl[] =
  730. {
  731. {B2400, BAUD_RATE_2400},
  732. {B4800, BAUD_RATE_4800},
  733. {B9600, BAUD_RATE_9600},
  734. {B19200, BAUD_RATE_19200},
  735. {B38400, BAUD_RATE_38400},
  736. {B57600, BAUD_RATE_57600},
  737. {B115200, BAUD_RATE_115200},
  738. {B230400, BAUD_RATE_230400},
  739. {B460800, BAUD_RATE_460800},
  740. {B921600, BAUD_RATE_921600},
  741. {B2000000, BAUD_RATE_2000000},
  742. {B3000000, BAUD_RATE_3000000},
  743. };
  744. static speed_t _get_speed(int baudrate)
  745. {
  746. int index;
  747. for (index = 0; index < sizeof(_tbl)/sizeof(_tbl[0]); index ++)
  748. {
  749. if (_tbl[index].baudrate == baudrate)
  750. return _tbl[index].speed;
  751. }
  752. return B0;
  753. }
  754. static int _get_baudrate(speed_t speed)
  755. {
  756. int index;
  757. for (index = 0; index < sizeof(_tbl)/sizeof(_tbl[0]); index ++)
  758. {
  759. if (_tbl[index].speed == speed)
  760. return _tbl[index].baudrate;
  761. }
  762. return 0;
  763. }
  764. static void _tc_flush(struct rt_serial_device *serial, int queue)
  765. {
  766. rt_base_t level;
  767. int ch = -1;
  768. struct rt_serial_rx_fifo *rx_fifo = RT_NULL;
  769. struct rt_device *device = RT_NULL;
  770. RT_ASSERT(serial != RT_NULL);
  771. device = &(serial->parent);
  772. rx_fifo = (struct rt_serial_rx_fifo *) serial->serial_rx;
  773. switch(queue)
  774. {
  775. case TCIFLUSH:
  776. case TCIOFLUSH:
  777. RT_ASSERT(rx_fifo != RT_NULL);
  778. if((device->open_flag & RT_DEVICE_FLAG_INT_RX) || (device->open_flag & RT_DEVICE_FLAG_DMA_RX))
  779. {
  780. RT_ASSERT(RT_NULL != rx_fifo);
  781. level = rt_hw_interrupt_disable();
  782. rt_memset(rx_fifo->buffer, 0, serial->config.bufsz);
  783. rx_fifo->put_index = 0;
  784. rx_fifo->get_index = 0;
  785. rx_fifo->is_full = RT_FALSE;
  786. rt_hw_interrupt_enable(level);
  787. }
  788. else
  789. {
  790. while (1)
  791. {
  792. ch = serial->ops->getc(serial);
  793. if (ch == -1) break;
  794. }
  795. }
  796. break;
  797. case TCOFLUSH:
  798. break;
  799. }
  800. }
  801. #endif
  802. static rt_err_t rt_serial_control(struct rt_device *dev,
  803. int cmd,
  804. void *args)
  805. {
  806. rt_err_t ret = RT_EOK;
  807. struct rt_serial_device *serial;
  808. RT_ASSERT(dev != RT_NULL);
  809. serial = (struct rt_serial_device *)dev;
  810. switch (cmd)
  811. {
  812. case RT_DEVICE_CTRL_SUSPEND:
  813. /* suspend device */
  814. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  815. break;
  816. case RT_DEVICE_CTRL_RESUME:
  817. /* resume device */
  818. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  819. break;
  820. case RT_DEVICE_CTRL_CONFIG:
  821. if (args)
  822. {
  823. struct serial_configure *pconfig = (struct serial_configure *) args;
  824. if (pconfig->bufsz != serial->config.bufsz && serial->parent.ref_count)
  825. {
  826. /*can not change buffer size*/
  827. return RT_EBUSY;
  828. }
  829. /* set serial configure */
  830. serial->config = *pconfig;
  831. if (serial->parent.ref_count)
  832. {
  833. /* serial device has been opened, to configure it */
  834. serial->ops->configure(serial, (struct serial_configure *) args);
  835. }
  836. }
  837. break;
  838. #ifdef RT_USING_POSIX
  839. #ifdef RT_USING_POSIX_TERMIOS
  840. case TCGETA:
  841. {
  842. struct termios *tio = (struct termios*)args;
  843. if (tio == RT_NULL) return -RT_EINVAL;
  844. tio->c_iflag = 0;
  845. tio->c_oflag = 0;
  846. tio->c_lflag = 0;
  847. /* update oflag for console device */
  848. if (rt_console_get_device() == dev)
  849. tio->c_oflag = OPOST | ONLCR;
  850. /* set cflag */
  851. tio->c_cflag = 0;
  852. if (serial->config.data_bits == DATA_BITS_5)
  853. tio->c_cflag = CS5;
  854. else if (serial->config.data_bits == DATA_BITS_6)
  855. tio->c_cflag = CS6;
  856. else if (serial->config.data_bits == DATA_BITS_7)
  857. tio->c_cflag = CS7;
  858. else if (serial->config.data_bits == DATA_BITS_8)
  859. tio->c_cflag = CS8;
  860. if (serial->config.stop_bits == STOP_BITS_2)
  861. tio->c_cflag |= CSTOPB;
  862. if (serial->config.parity == PARITY_EVEN)
  863. tio->c_cflag |= PARENB;
  864. else if (serial->config.parity == PARITY_ODD)
  865. tio->c_cflag |= (PARODD | PARENB);
  866. cfsetospeed(tio, _get_speed(serial->config.baud_rate));
  867. }
  868. break;
  869. case TCSETAW:
  870. case TCSETAF:
  871. case TCSETA:
  872. {
  873. int baudrate;
  874. struct serial_configure config;
  875. struct termios *tio = (struct termios*)args;
  876. if (tio == RT_NULL) return -RT_EINVAL;
  877. config = serial->config;
  878. baudrate = _get_baudrate(cfgetospeed(tio));
  879. config.baud_rate = baudrate;
  880. switch (tio->c_cflag & CSIZE)
  881. {
  882. case CS5:
  883. config.data_bits = DATA_BITS_5;
  884. break;
  885. case CS6:
  886. config.data_bits = DATA_BITS_6;
  887. break;
  888. case CS7:
  889. config.data_bits = DATA_BITS_7;
  890. break;
  891. default:
  892. config.data_bits = DATA_BITS_8;
  893. break;
  894. }
  895. if (tio->c_cflag & CSTOPB) config.stop_bits = STOP_BITS_2;
  896. else config.stop_bits = STOP_BITS_1;
  897. if (tio->c_cflag & PARENB)
  898. {
  899. if (tio->c_cflag & PARODD) config.parity = PARITY_ODD;
  900. else config.parity = PARITY_EVEN;
  901. }
  902. else config.parity = PARITY_NONE;
  903. serial->ops->configure(serial, &config);
  904. }
  905. break;
  906. case TCFLSH:
  907. {
  908. int queue = (int)args;
  909. _tc_flush(serial, queue);
  910. }
  911. break;
  912. case TCXONC:
  913. break;
  914. #endif /*RT_USING_POSIX_TERMIOS*/
  915. case FIONREAD:
  916. {
  917. rt_size_t recved = 0;
  918. rt_base_t level;
  919. level = rt_hw_interrupt_disable();
  920. recved = _serial_fifo_calc_recved_len(serial);
  921. rt_hw_interrupt_enable(level);
  922. *(rt_size_t *)args = recved;
  923. }
  924. break;
  925. #endif
  926. default :
  927. /* control device */
  928. ret = serial->ops->control(serial, cmd, args);
  929. break;
  930. }
  931. return ret;
  932. }
  933. #ifdef RT_USING_DEVICE_OPS
  934. const static struct rt_device_ops serial_ops =
  935. {
  936. rt_serial_init,
  937. rt_serial_open,
  938. rt_serial_close,
  939. rt_serial_read,
  940. rt_serial_write,
  941. rt_serial_control
  942. };
  943. #endif
  944. /*
  945. * serial register
  946. */
  947. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
  948. const char *name,
  949. rt_uint32_t flag,
  950. void *data)
  951. {
  952. rt_err_t ret;
  953. struct rt_device *device;
  954. RT_ASSERT(serial != RT_NULL);
  955. device = &(serial->parent);
  956. device->type = RT_Device_Class_Char;
  957. device->rx_indicate = RT_NULL;
  958. device->tx_complete = RT_NULL;
  959. #ifdef RT_USING_DEVICE_OPS
  960. device->ops = &serial_ops;
  961. #else
  962. device->init = rt_serial_init;
  963. device->open = rt_serial_open;
  964. device->close = rt_serial_close;
  965. device->read = rt_serial_read;
  966. device->write = rt_serial_write;
  967. device->control = rt_serial_control;
  968. #endif
  969. device->user_data = data;
  970. /* register a character device */
  971. ret = rt_device_register(device, name, flag);
  972. #if defined(RT_USING_POSIX)
  973. /* set fops */
  974. device->fops = &_serial_fops;
  975. #endif
  976. return ret;
  977. }
  978. /* ISR for serial interrupt */
  979. void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
  980. {
  981. switch (event & 0xff)
  982. {
  983. case RT_SERIAL_EVENT_RX_IND:
  984. {
  985. int ch = -1;
  986. rt_base_t level;
  987. struct rt_serial_rx_fifo* rx_fifo;
  988. /* interrupt mode receive */
  989. rx_fifo = (struct rt_serial_rx_fifo*)serial->serial_rx;
  990. RT_ASSERT(rx_fifo != RT_NULL);
  991. while (1)
  992. {
  993. ch = serial->ops->getc(serial);
  994. if (ch == -1) break;
  995. /* disable interrupt */
  996. level = rt_hw_interrupt_disable();
  997. rx_fifo->buffer[rx_fifo->put_index] = ch;
  998. rx_fifo->put_index += 1;
  999. if (rx_fifo->put_index >= serial->config.bufsz) rx_fifo->put_index = 0;
  1000. /* if the next position is read index, discard this 'read char' */
  1001. if (rx_fifo->put_index == rx_fifo->get_index)
  1002. {
  1003. rx_fifo->get_index += 1;
  1004. rx_fifo->is_full = RT_TRUE;
  1005. if (rx_fifo->get_index >= serial->config.bufsz) rx_fifo->get_index = 0;
  1006. _serial_check_buffer_size();
  1007. }
  1008. /* enable interrupt */
  1009. rt_hw_interrupt_enable(level);
  1010. }
  1011. /* invoke callback */
  1012. if (serial->parent.rx_indicate != RT_NULL)
  1013. {
  1014. rt_size_t rx_length;
  1015. /* get rx length */
  1016. level = rt_hw_interrupt_disable();
  1017. rx_length = (rx_fifo->put_index >= rx_fifo->get_index)? (rx_fifo->put_index - rx_fifo->get_index):
  1018. (serial->config.bufsz - (rx_fifo->get_index - rx_fifo->put_index));
  1019. rt_hw_interrupt_enable(level);
  1020. if (rx_length)
  1021. {
  1022. serial->parent.rx_indicate(&serial->parent, rx_length);
  1023. }
  1024. }
  1025. break;
  1026. }
  1027. case RT_SERIAL_EVENT_TX_DONE:
  1028. {
  1029. struct rt_serial_tx_fifo* tx_fifo;
  1030. tx_fifo = (struct rt_serial_tx_fifo*)serial->serial_tx;
  1031. rt_completion_done(&(tx_fifo->completion));
  1032. break;
  1033. }
  1034. #ifdef RT_SERIAL_USING_DMA
  1035. case RT_SERIAL_EVENT_TX_DMADONE:
  1036. {
  1037. const void *data_ptr;
  1038. rt_size_t data_size;
  1039. const void *last_data_ptr;
  1040. struct rt_serial_tx_dma *tx_dma;
  1041. tx_dma = (struct rt_serial_tx_dma*) serial->serial_tx;
  1042. rt_data_queue_pop(&(tx_dma->data_queue), &last_data_ptr, &data_size, 0);
  1043. if (rt_data_queue_peek(&(tx_dma->data_queue), &data_ptr, &data_size) == RT_EOK)
  1044. {
  1045. /* transmit next data node */
  1046. tx_dma->activated = RT_TRUE;
  1047. serial->ops->dma_transmit(serial, (rt_uint8_t *)data_ptr, data_size, RT_SERIAL_DMA_TX);
  1048. }
  1049. else
  1050. {
  1051. tx_dma->activated = RT_FALSE;
  1052. }
  1053. /* invoke callback */
  1054. if (serial->parent.tx_complete != RT_NULL)
  1055. {
  1056. serial->parent.tx_complete(&serial->parent, (void*)last_data_ptr);
  1057. }
  1058. break;
  1059. }
  1060. case RT_SERIAL_EVENT_RX_DMADONE:
  1061. {
  1062. int length;
  1063. rt_base_t level;
  1064. /* get DMA rx length */
  1065. length = (event & (~0xff)) >> 8;
  1066. if (serial->config.bufsz == 0)
  1067. {
  1068. struct rt_serial_rx_dma* rx_dma;
  1069. rx_dma = (struct rt_serial_rx_dma*) serial->serial_rx;
  1070. RT_ASSERT(rx_dma != RT_NULL);
  1071. RT_ASSERT(serial->parent.rx_indicate != RT_NULL);
  1072. serial->parent.rx_indicate(&(serial->parent), length);
  1073. rx_dma->activated = RT_FALSE;
  1074. }
  1075. else
  1076. {
  1077. /* disable interrupt */
  1078. level = rt_hw_interrupt_disable();
  1079. /* update fifo put index */
  1080. rt_dma_recv_update_put_index(serial, length);
  1081. /* calculate received total length */
  1082. length = rt_dma_calc_recved_len(serial);
  1083. /* enable interrupt */
  1084. rt_hw_interrupt_enable(level);
  1085. /* invoke callback */
  1086. if (serial->parent.rx_indicate != RT_NULL)
  1087. {
  1088. serial->parent.rx_indicate(&(serial->parent), length);
  1089. }
  1090. }
  1091. break;
  1092. }
  1093. #endif /* RT_SERIAL_USING_DMA */
  1094. }
  1095. }