serial.c 36 KB

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