serial.c 36 KB

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