serial.c 35 KB

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