serial.c 37 KB

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