serial.c 37 KB

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