serial.c 36 KB

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