serial.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  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. /* configure low level device */
  629. serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void *) RT_DEVICE_FLAG_DMA_RX);
  630. serial->serial_rx = RT_NULL;
  631. dev->open_flag &= ~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. }
  655. #endif /* RT_SERIAL_USING_DMA */
  656. return RT_EOK;
  657. }
  658. static rt_size_t rt_serial_read(struct rt_device *dev,
  659. rt_off_t pos,
  660. void *buffer,
  661. rt_size_t size)
  662. {
  663. struct rt_serial_device *serial;
  664. RT_ASSERT(dev != RT_NULL);
  665. if (size == 0) return 0;
  666. serial = (struct rt_serial_device *)dev;
  667. if (dev->open_flag & RT_DEVICE_FLAG_INT_RX)
  668. {
  669. return _serial_int_rx(serial, (rt_uint8_t *)buffer, size);
  670. }
  671. #ifdef RT_SERIAL_USING_DMA
  672. else if (dev->open_flag & RT_DEVICE_FLAG_DMA_RX)
  673. {
  674. return _serial_dma_rx(serial, (rt_uint8_t *)buffer, size);
  675. }
  676. #endif /* RT_SERIAL_USING_DMA */
  677. return _serial_poll_rx(serial, (rt_uint8_t *)buffer, size);
  678. }
  679. static rt_size_t rt_serial_write(struct rt_device *dev,
  680. rt_off_t pos,
  681. const void *buffer,
  682. rt_size_t size)
  683. {
  684. struct rt_serial_device *serial;
  685. RT_ASSERT(dev != RT_NULL);
  686. if (size == 0) return 0;
  687. serial = (struct rt_serial_device *)dev;
  688. if (dev->open_flag & RT_DEVICE_FLAG_INT_TX)
  689. {
  690. return _serial_int_tx(serial, (const rt_uint8_t *)buffer, size);
  691. }
  692. #ifdef RT_SERIAL_USING_DMA
  693. else if (dev->open_flag & RT_DEVICE_FLAG_DMA_TX)
  694. {
  695. return _serial_dma_tx(serial, (const rt_uint8_t *)buffer, size);
  696. }
  697. #endif /* RT_SERIAL_USING_DMA */
  698. else
  699. {
  700. return _serial_poll_tx(serial, (const rt_uint8_t *)buffer, size);
  701. }
  702. }
  703. #ifdef RT_USING_POSIX_TERMIOS
  704. struct speed_baudrate_item
  705. {
  706. speed_t speed;
  707. int baudrate;
  708. };
  709. const static struct speed_baudrate_item _tbl[] =
  710. {
  711. {B2400, BAUD_RATE_2400},
  712. {B4800, BAUD_RATE_4800},
  713. {B9600, BAUD_RATE_9600},
  714. {B19200, BAUD_RATE_19200},
  715. {B38400, BAUD_RATE_38400},
  716. {B57600, BAUD_RATE_57600},
  717. {B115200, BAUD_RATE_115200},
  718. {B230400, BAUD_RATE_230400},
  719. {B460800, BAUD_RATE_460800},
  720. {B921600, BAUD_RATE_921600},
  721. {B2000000, BAUD_RATE_2000000},
  722. {B3000000, BAUD_RATE_3000000},
  723. };
  724. static speed_t _get_speed(int baudrate)
  725. {
  726. int index;
  727. for (index = 0; index < sizeof(_tbl)/sizeof(_tbl[0]); index ++)
  728. {
  729. if (_tbl[index].baudrate == baudrate)
  730. return _tbl[index].speed;
  731. }
  732. return B0;
  733. }
  734. static int _get_baudrate(speed_t speed)
  735. {
  736. int index;
  737. for (index = 0; index < sizeof(_tbl)/sizeof(_tbl[0]); index ++)
  738. {
  739. if (_tbl[index].speed == speed)
  740. return _tbl[index].baudrate;
  741. }
  742. return 0;
  743. }
  744. static void _tc_flush(struct rt_serial_device *serial, int queue)
  745. {
  746. rt_base_t level;
  747. int ch = -1;
  748. struct rt_serial_rx_fifo *rx_fifo = RT_NULL;
  749. struct rt_device *device = RT_NULL;
  750. RT_ASSERT(serial != RT_NULL);
  751. device = &(serial->parent);
  752. rx_fifo = (struct rt_serial_rx_fifo *) serial->serial_rx;
  753. switch(queue)
  754. {
  755. case TCIFLUSH:
  756. case TCIOFLUSH:
  757. RT_ASSERT(rx_fifo != RT_NULL);
  758. if((device->open_flag & RT_DEVICE_FLAG_INT_RX) || (device->open_flag & RT_DEVICE_FLAG_DMA_RX))
  759. {
  760. RT_ASSERT(RT_NULL != rx_fifo);
  761. level = rt_hw_interrupt_disable();
  762. rt_memset(rx_fifo->buffer, 0, serial->config.bufsz);
  763. rx_fifo->put_index = 0;
  764. rx_fifo->get_index = 0;
  765. rx_fifo->is_full = RT_FALSE;
  766. rt_hw_interrupt_enable(level);
  767. }
  768. else
  769. {
  770. while (1)
  771. {
  772. ch = serial->ops->getc(serial);
  773. if (ch == -1) break;
  774. }
  775. }
  776. break;
  777. case TCOFLUSH:
  778. break;
  779. }
  780. }
  781. #endif
  782. static rt_err_t rt_serial_control(struct rt_device *dev,
  783. int cmd,
  784. void *args)
  785. {
  786. rt_err_t ret = RT_EOK;
  787. struct rt_serial_device *serial;
  788. RT_ASSERT(dev != RT_NULL);
  789. serial = (struct rt_serial_device *)dev;
  790. switch (cmd)
  791. {
  792. case RT_DEVICE_CTRL_SUSPEND:
  793. /* suspend device */
  794. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  795. break;
  796. case RT_DEVICE_CTRL_RESUME:
  797. /* resume device */
  798. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  799. break;
  800. case RT_DEVICE_CTRL_CONFIG:
  801. if (args)
  802. {
  803. struct serial_configure *pconfig = (struct serial_configure *) args;
  804. if (pconfig->bufsz != serial->config.bufsz && serial->parent.ref_count)
  805. {
  806. /*can not change buffer size*/
  807. return RT_EBUSY;
  808. }
  809. /* set serial configure */
  810. serial->config = *pconfig;
  811. if (serial->parent.ref_count)
  812. {
  813. /* serial device has been opened, to configure it */
  814. serial->ops->configure(serial, (struct serial_configure *) args);
  815. }
  816. }
  817. break;
  818. #ifdef RT_USING_POSIX_TERMIOS
  819. case TCGETA:
  820. {
  821. struct termios *tio = (struct termios*)args;
  822. if (tio == RT_NULL) return -RT_EINVAL;
  823. tio->c_iflag = 0;
  824. tio->c_oflag = 0;
  825. tio->c_lflag = 0;
  826. /* update oflag for console device */
  827. if (rt_console_get_device() == dev)
  828. tio->c_oflag = OPOST | ONLCR;
  829. /* set cflag */
  830. tio->c_cflag = 0;
  831. if (serial->config.data_bits == DATA_BITS_5)
  832. tio->c_cflag = CS5;
  833. else if (serial->config.data_bits == DATA_BITS_6)
  834. tio->c_cflag = CS6;
  835. else if (serial->config.data_bits == DATA_BITS_7)
  836. tio->c_cflag = CS7;
  837. else if (serial->config.data_bits == DATA_BITS_8)
  838. tio->c_cflag = CS8;
  839. if (serial->config.stop_bits == STOP_BITS_2)
  840. tio->c_cflag |= CSTOPB;
  841. if (serial->config.parity == PARITY_EVEN)
  842. tio->c_cflag |= PARENB;
  843. else if (serial->config.parity == PARITY_ODD)
  844. tio->c_cflag |= (PARODD | PARENB);
  845. cfsetospeed(tio, _get_speed(serial->config.baud_rate));
  846. }
  847. break;
  848. case TCSETAW:
  849. case TCSETAF:
  850. case TCSETA:
  851. {
  852. int baudrate;
  853. struct serial_configure config;
  854. struct termios *tio = (struct termios*)args;
  855. if (tio == RT_NULL) return -RT_EINVAL;
  856. config = serial->config;
  857. baudrate = _get_baudrate(cfgetospeed(tio));
  858. config.baud_rate = baudrate;
  859. switch (tio->c_cflag & CSIZE)
  860. {
  861. case CS5:
  862. config.data_bits = DATA_BITS_5;
  863. break;
  864. case CS6:
  865. config.data_bits = DATA_BITS_6;
  866. break;
  867. case CS7:
  868. config.data_bits = DATA_BITS_7;
  869. break;
  870. default:
  871. config.data_bits = DATA_BITS_8;
  872. break;
  873. }
  874. if (tio->c_cflag & CSTOPB) config.stop_bits = STOP_BITS_2;
  875. else config.stop_bits = STOP_BITS_1;
  876. if (tio->c_cflag & PARENB)
  877. {
  878. if (tio->c_cflag & PARODD) config.parity = PARITY_ODD;
  879. else config.parity = PARITY_EVEN;
  880. }
  881. else config.parity = PARITY_NONE;
  882. serial->ops->configure(serial, &config);
  883. }
  884. break;
  885. case TCFLSH:
  886. {
  887. int queue = (int)args;
  888. _tc_flush(serial, queue);
  889. }
  890. break;
  891. case TCXONC:
  892. break;
  893. #endif
  894. #ifdef RT_USING_POSIX
  895. case FIONREAD:
  896. {
  897. rt_size_t recved = 0;
  898. rt_base_t level;
  899. level = rt_hw_interrupt_disable();
  900. recved = _serial_fifo_calc_recved_len(serial);
  901. rt_hw_interrupt_enable(level);
  902. *(rt_size_t *)args = recved;
  903. }
  904. break;
  905. #endif
  906. default :
  907. /* control device */
  908. ret = serial->ops->control(serial, cmd, args);
  909. break;
  910. }
  911. return ret;
  912. }
  913. #ifdef RT_USING_DEVICE_OPS
  914. const static struct rt_device_ops serial_ops =
  915. {
  916. rt_serial_init,
  917. rt_serial_open,
  918. rt_serial_close,
  919. rt_serial_read,
  920. rt_serial_write,
  921. rt_serial_control
  922. };
  923. #endif
  924. /*
  925. * serial register
  926. */
  927. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
  928. const char *name,
  929. rt_uint32_t flag,
  930. void *data)
  931. {
  932. rt_err_t ret;
  933. struct rt_device *device;
  934. RT_ASSERT(serial != RT_NULL);
  935. device = &(serial->parent);
  936. device->type = RT_Device_Class_Char;
  937. device->rx_indicate = RT_NULL;
  938. device->tx_complete = RT_NULL;
  939. #ifdef RT_USING_DEVICE_OPS
  940. device->ops = &serial_ops;
  941. #else
  942. device->init = rt_serial_init;
  943. device->open = rt_serial_open;
  944. device->close = rt_serial_close;
  945. device->read = rt_serial_read;
  946. device->write = rt_serial_write;
  947. device->control = rt_serial_control;
  948. #endif
  949. device->user_data = data;
  950. /* register a character device */
  951. ret = rt_device_register(device, name, flag);
  952. #if defined(RT_USING_POSIX)
  953. /* set fops */
  954. device->fops = &_serial_fops;
  955. #endif
  956. return ret;
  957. }
  958. /* ISR for serial interrupt */
  959. void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
  960. {
  961. switch (event & 0xff)
  962. {
  963. case RT_SERIAL_EVENT_RX_IND:
  964. {
  965. int ch = -1;
  966. rt_base_t level;
  967. struct rt_serial_rx_fifo* rx_fifo;
  968. /* interrupt mode receive */
  969. rx_fifo = (struct rt_serial_rx_fifo*)serial->serial_rx;
  970. RT_ASSERT(rx_fifo != RT_NULL);
  971. while (1)
  972. {
  973. ch = serial->ops->getc(serial);
  974. if (ch == -1) break;
  975. /* disable interrupt */
  976. level = rt_hw_interrupt_disable();
  977. rx_fifo->buffer[rx_fifo->put_index] = ch;
  978. rx_fifo->put_index += 1;
  979. if (rx_fifo->put_index >= serial->config.bufsz) rx_fifo->put_index = 0;
  980. /* if the next position is read index, discard this 'read char' */
  981. if (rx_fifo->put_index == rx_fifo->get_index)
  982. {
  983. rx_fifo->get_index += 1;
  984. rx_fifo->is_full = RT_TRUE;
  985. if (rx_fifo->get_index >= serial->config.bufsz) rx_fifo->get_index = 0;
  986. _serial_check_buffer_size();
  987. }
  988. /* enable interrupt */
  989. rt_hw_interrupt_enable(level);
  990. }
  991. /* invoke callback */
  992. if (serial->parent.rx_indicate != RT_NULL)
  993. {
  994. rt_size_t rx_length;
  995. /* get rx length */
  996. level = rt_hw_interrupt_disable();
  997. rx_length = (rx_fifo->put_index >= rx_fifo->get_index)? (rx_fifo->put_index - rx_fifo->get_index):
  998. (serial->config.bufsz - (rx_fifo->get_index - rx_fifo->put_index));
  999. rt_hw_interrupt_enable(level);
  1000. if (rx_length)
  1001. {
  1002. serial->parent.rx_indicate(&serial->parent, rx_length);
  1003. }
  1004. }
  1005. break;
  1006. }
  1007. case RT_SERIAL_EVENT_TX_DONE:
  1008. {
  1009. struct rt_serial_tx_fifo* tx_fifo;
  1010. tx_fifo = (struct rt_serial_tx_fifo*)serial->serial_tx;
  1011. rt_completion_done(&(tx_fifo->completion));
  1012. break;
  1013. }
  1014. #ifdef RT_SERIAL_USING_DMA
  1015. case RT_SERIAL_EVENT_TX_DMADONE:
  1016. {
  1017. const void *data_ptr;
  1018. rt_size_t data_size;
  1019. const void *last_data_ptr;
  1020. struct rt_serial_tx_dma *tx_dma;
  1021. tx_dma = (struct rt_serial_tx_dma*) serial->serial_tx;
  1022. rt_data_queue_pop(&(tx_dma->data_queue), &last_data_ptr, &data_size, 0);
  1023. if (rt_data_queue_peak(&(tx_dma->data_queue), &data_ptr, &data_size) == RT_EOK)
  1024. {
  1025. /* transmit next data node */
  1026. tx_dma->activated = RT_TRUE;
  1027. serial->ops->dma_transmit(serial, (rt_uint8_t *)data_ptr, data_size, RT_SERIAL_DMA_TX);
  1028. }
  1029. else
  1030. {
  1031. tx_dma->activated = RT_FALSE;
  1032. }
  1033. /* invoke callback */
  1034. if (serial->parent.tx_complete != RT_NULL)
  1035. {
  1036. serial->parent.tx_complete(&serial->parent, (void*)last_data_ptr);
  1037. }
  1038. break;
  1039. }
  1040. case RT_SERIAL_EVENT_RX_DMADONE:
  1041. {
  1042. int length;
  1043. rt_base_t level;
  1044. /* get DMA rx length */
  1045. length = (event & (~0xff)) >> 8;
  1046. if (serial->config.bufsz == 0)
  1047. {
  1048. struct rt_serial_rx_dma* rx_dma;
  1049. rx_dma = (struct rt_serial_rx_dma*) serial->serial_rx;
  1050. RT_ASSERT(rx_dma != RT_NULL);
  1051. RT_ASSERT(serial->parent.rx_indicate != RT_NULL);
  1052. serial->parent.rx_indicate(&(serial->parent), length);
  1053. rx_dma->activated = RT_FALSE;
  1054. }
  1055. else
  1056. {
  1057. /* disable interrupt */
  1058. level = rt_hw_interrupt_disable();
  1059. /* update fifo put index */
  1060. rt_dma_recv_update_put_index(serial, length);
  1061. /* calculate received total length */
  1062. length = rt_dma_calc_recved_len(serial);
  1063. /* enable interrupt */
  1064. rt_hw_interrupt_enable(level);
  1065. /* invoke callback */
  1066. if (serial->parent.rx_indicate != RT_NULL)
  1067. {
  1068. serial->parent.rx_indicate(&(serial->parent), length);
  1069. }
  1070. }
  1071. break;
  1072. }
  1073. #endif /* RT_SERIAL_USING_DMA */
  1074. }
  1075. }