serial.c 37 KB

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