serial.c 40 KB

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