serial.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  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. rt_memset(&serial->rx_notify, 0, sizeof(struct rt_device_notify));
  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. case RT_DEVICE_CTRL_NOTIFY_SET:
  839. if (args)
  840. {
  841. rt_memcpy(&serial->rx_notify, args, sizeof(struct rt_device_notify));
  842. }
  843. break;
  844. case RT_DEVICE_CTRL_CONSOLE_OFLAG:
  845. if (args)
  846. {
  847. *(rt_uint16_t*)args = RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM;
  848. }
  849. break;
  850. #ifdef RT_USING_POSIX_TERMIOS
  851. case TCGETA:
  852. {
  853. struct termios *tio = (struct termios*)args;
  854. if (tio == RT_NULL) return -RT_EINVAL;
  855. tio->c_iflag = 0;
  856. tio->c_oflag = 0;
  857. tio->c_lflag = 0;
  858. /* update oflag for console device */
  859. if (rt_console_get_device() == dev)
  860. tio->c_oflag = OPOST | ONLCR;
  861. /* set cflag */
  862. tio->c_cflag = 0;
  863. if (serial->config.data_bits == DATA_BITS_5)
  864. tio->c_cflag = CS5;
  865. else if (serial->config.data_bits == DATA_BITS_6)
  866. tio->c_cflag = CS6;
  867. else if (serial->config.data_bits == DATA_BITS_7)
  868. tio->c_cflag = CS7;
  869. else if (serial->config.data_bits == DATA_BITS_8)
  870. tio->c_cflag = CS8;
  871. if (serial->config.stop_bits == STOP_BITS_2)
  872. tio->c_cflag |= CSTOPB;
  873. if (serial->config.parity == PARITY_EVEN)
  874. tio->c_cflag |= PARENB;
  875. else if (serial->config.parity == PARITY_ODD)
  876. tio->c_cflag |= (PARODD | PARENB);
  877. cfsetospeed(tio, _get_speed(serial->config.baud_rate));
  878. }
  879. break;
  880. case TCSETAW:
  881. case TCSETAF:
  882. case TCSETA:
  883. {
  884. int baudrate;
  885. struct serial_configure config;
  886. struct termios *tio = (struct termios*)args;
  887. if (tio == RT_NULL) return -RT_EINVAL;
  888. config = serial->config;
  889. baudrate = _get_baudrate(cfgetospeed(tio));
  890. config.baud_rate = baudrate;
  891. switch (tio->c_cflag & CSIZE)
  892. {
  893. case CS5:
  894. config.data_bits = DATA_BITS_5;
  895. break;
  896. case CS6:
  897. config.data_bits = DATA_BITS_6;
  898. break;
  899. case CS7:
  900. config.data_bits = DATA_BITS_7;
  901. break;
  902. default:
  903. config.data_bits = DATA_BITS_8;
  904. break;
  905. }
  906. if (tio->c_cflag & CSTOPB) config.stop_bits = STOP_BITS_2;
  907. else config.stop_bits = STOP_BITS_1;
  908. if (tio->c_cflag & PARENB)
  909. {
  910. if (tio->c_cflag & PARODD) config.parity = PARITY_ODD;
  911. else config.parity = PARITY_EVEN;
  912. }
  913. else config.parity = PARITY_NONE;
  914. serial->ops->configure(serial, &config);
  915. }
  916. break;
  917. case TCFLSH:
  918. {
  919. int queue = (int)args;
  920. _tc_flush(serial, queue);
  921. }
  922. break;
  923. case TCXONC:
  924. break;
  925. #endif
  926. #ifdef RT_USING_POSIX
  927. case FIONREAD:
  928. {
  929. rt_size_t recved = 0;
  930. rt_base_t level;
  931. level = rt_hw_interrupt_disable();
  932. recved = _serial_fifo_calc_recved_len(serial);
  933. rt_hw_interrupt_enable(level);
  934. *(rt_size_t *)args = recved;
  935. }
  936. break;
  937. #endif
  938. default :
  939. /* control device */
  940. ret = serial->ops->control(serial, cmd, args);
  941. break;
  942. }
  943. return ret;
  944. }
  945. #ifdef RT_USING_DEVICE_OPS
  946. const static struct rt_device_ops serial_ops =
  947. {
  948. rt_serial_init,
  949. rt_serial_open,
  950. rt_serial_close,
  951. rt_serial_read,
  952. rt_serial_write,
  953. rt_serial_control
  954. };
  955. #endif
  956. /*
  957. * serial register
  958. */
  959. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
  960. const char *name,
  961. rt_uint32_t flag,
  962. void *data)
  963. {
  964. rt_err_t ret;
  965. struct rt_device *device;
  966. RT_ASSERT(serial != RT_NULL);
  967. device = &(serial->parent);
  968. device->type = RT_Device_Class_Char;
  969. device->rx_indicate = RT_NULL;
  970. device->tx_complete = RT_NULL;
  971. #ifdef RT_USING_DEVICE_OPS
  972. device->ops = &serial_ops;
  973. #else
  974. device->init = rt_serial_init;
  975. device->open = rt_serial_open;
  976. device->close = rt_serial_close;
  977. device->read = rt_serial_read;
  978. device->write = rt_serial_write;
  979. device->control = rt_serial_control;
  980. #endif
  981. device->user_data = data;
  982. /* register a character device */
  983. ret = rt_device_register(device, name, flag);
  984. #if defined(RT_USING_POSIX)
  985. /* set fops */
  986. device->fops = &_serial_fops;
  987. #endif
  988. return ret;
  989. }
  990. /* ISR for serial interrupt */
  991. void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
  992. {
  993. switch (event & 0xff)
  994. {
  995. case RT_SERIAL_EVENT_RX_IND:
  996. {
  997. int ch = -1;
  998. rt_base_t level;
  999. struct rt_serial_rx_fifo* rx_fifo;
  1000. /* interrupt mode receive */
  1001. rx_fifo = (struct rt_serial_rx_fifo*)serial->serial_rx;
  1002. RT_ASSERT(rx_fifo != RT_NULL);
  1003. while (1)
  1004. {
  1005. ch = serial->ops->getc(serial);
  1006. if (ch == -1) break;
  1007. /* disable interrupt */
  1008. level = rt_hw_interrupt_disable();
  1009. rx_fifo->buffer[rx_fifo->put_index] = ch;
  1010. rx_fifo->put_index += 1;
  1011. if (rx_fifo->put_index >= serial->config.bufsz) rx_fifo->put_index = 0;
  1012. /* if the next position is read index, discard this 'read char' */
  1013. if (rx_fifo->put_index == rx_fifo->get_index)
  1014. {
  1015. rx_fifo->get_index += 1;
  1016. rx_fifo->is_full = RT_TRUE;
  1017. if (rx_fifo->get_index >= serial->config.bufsz) rx_fifo->get_index = 0;
  1018. _serial_check_buffer_size();
  1019. }
  1020. /* enable interrupt */
  1021. rt_hw_interrupt_enable(level);
  1022. }
  1023. /* invoke callback */
  1024. if (serial->parent.rx_indicate != RT_NULL)
  1025. {
  1026. rt_size_t rx_length;
  1027. /* get rx length */
  1028. level = rt_hw_interrupt_disable();
  1029. rx_length = (rx_fifo->put_index >= rx_fifo->get_index)? (rx_fifo->put_index - rx_fifo->get_index):
  1030. (serial->config.bufsz - (rx_fifo->get_index - rx_fifo->put_index));
  1031. rt_hw_interrupt_enable(level);
  1032. if (rx_length)
  1033. {
  1034. serial->parent.rx_indicate(&serial->parent, rx_length);
  1035. }
  1036. }
  1037. if (serial->rx_notify.notify)
  1038. {
  1039. serial->rx_notify.notify(serial->rx_notify.dev);
  1040. }
  1041. break;
  1042. }
  1043. case RT_SERIAL_EVENT_TX_DONE:
  1044. {
  1045. struct rt_serial_tx_fifo* tx_fifo;
  1046. tx_fifo = (struct rt_serial_tx_fifo*)serial->serial_tx;
  1047. rt_completion_done(&(tx_fifo->completion));
  1048. break;
  1049. }
  1050. #ifdef RT_SERIAL_USING_DMA
  1051. case RT_SERIAL_EVENT_TX_DMADONE:
  1052. {
  1053. const void *data_ptr;
  1054. rt_size_t data_size;
  1055. const void *last_data_ptr;
  1056. struct rt_serial_tx_dma *tx_dma;
  1057. tx_dma = (struct rt_serial_tx_dma*) serial->serial_tx;
  1058. rt_data_queue_pop(&(tx_dma->data_queue), &last_data_ptr, &data_size, 0);
  1059. if (rt_data_queue_peak(&(tx_dma->data_queue), &data_ptr, &data_size) == RT_EOK)
  1060. {
  1061. /* transmit next data node */
  1062. tx_dma->activated = RT_TRUE;
  1063. serial->ops->dma_transmit(serial, (rt_uint8_t *)data_ptr, data_size, RT_SERIAL_DMA_TX);
  1064. }
  1065. else
  1066. {
  1067. tx_dma->activated = RT_FALSE;
  1068. }
  1069. /* invoke callback */
  1070. if (serial->parent.tx_complete != RT_NULL)
  1071. {
  1072. serial->parent.tx_complete(&serial->parent, (void*)last_data_ptr);
  1073. }
  1074. break;
  1075. }
  1076. case RT_SERIAL_EVENT_RX_DMADONE:
  1077. {
  1078. int length;
  1079. rt_base_t level;
  1080. /* get DMA rx length */
  1081. length = (event & (~0xff)) >> 8;
  1082. if (serial->config.bufsz == 0)
  1083. {
  1084. struct rt_serial_rx_dma* rx_dma;
  1085. rx_dma = (struct rt_serial_rx_dma*) serial->serial_rx;
  1086. RT_ASSERT(rx_dma != RT_NULL);
  1087. RT_ASSERT(serial->parent.rx_indicate != RT_NULL);
  1088. serial->parent.rx_indicate(&(serial->parent), length);
  1089. rx_dma->activated = RT_FALSE;
  1090. }
  1091. else
  1092. {
  1093. /* disable interrupt */
  1094. level = rt_hw_interrupt_disable();
  1095. /* update fifo put index */
  1096. rt_dma_recv_update_put_index(serial, length);
  1097. /* calculate received total length */
  1098. length = rt_dma_calc_recved_len(serial);
  1099. /* enable interrupt */
  1100. rt_hw_interrupt_enable(level);
  1101. /* invoke callback */
  1102. if (serial->parent.rx_indicate != RT_NULL)
  1103. {
  1104. serial->parent.rx_indicate(&(serial->parent), length);
  1105. }
  1106. }
  1107. break;
  1108. }
  1109. #endif /* RT_SERIAL_USING_DMA */
  1110. }
  1111. }