serial.c 37 KB

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