serialX.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637
  1. /*
  2. * Copyright (c) 2006-2021, 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. * 2020-12-14 Meco Man implement function of setting window's size(TIOCSWINSZ)
  28. * 2021-08-22 Meco Man implement function of getting window's size(TIOCGWINSZ)
  29. * 2022-04-10 THEWON serialX first version
  30. * 2022-06-08 THEWON add No TX Empty interrupt support
  31. * 2023-02-15 THEWON add init ops
  32. */
  33. #include <rthw.h>
  34. #include <rtthread.h>
  35. #include <rtdevice.h>
  36. #ifdef RT_USING_SERIAL
  37. #define DBG_TAG "UART"
  38. #define DBG_LVL DBG_INFO
  39. #include <rtdbg.h>
  40. #ifdef RT_USING_POSIX_STDIO
  41. #if RTTHREAD_VERSION <= RT_VERSION_CHECK(4, 0, 3)
  42. #include <dfs_posix.h>
  43. #include <dfs_poll.h>
  44. #else
  45. #include <dfs_file.h>
  46. #include <fcntl.h>
  47. #include <poll.h>
  48. #include <sys/errno.h>
  49. #include <sys/ioctl.h>
  50. #endif
  51. #ifdef RT_USING_POSIX_TERMIOS
  52. #if RTTHREAD_VERSION <= RT_VERSION_CHECK(4, 0, 3)
  53. #include <posix_termios.h>
  54. #else
  55. #include <termios.h>
  56. #endif
  57. #endif
  58. /* it's possible the 'getc/putc' is defined by stdio.h in gcc/newlib. */
  59. #ifdef getc
  60. #undef getc
  61. #endif
  62. #ifdef putc
  63. #undef putc
  64. #endif
  65. static rt_err_t serial_fops_rx_ind(rt_device_t dev, rt_size_t size)
  66. {
  67. rt_wqueue_wakeup(&(dev->wait_queue), (void*)POLLIN);
  68. return RT_EOK;
  69. }
  70. /* fops for serial */
  71. static int serial_fops_open(struct dfs_fd *fd)
  72. {
  73. rt_err_t ret = 0;
  74. rt_uint16_t flags = 0;
  75. rt_device_t device;
  76. device = (rt_device_t)fd->data;
  77. RT_ASSERT(device != RT_NULL);
  78. switch (fd->flags & O_ACCMODE)
  79. {
  80. case O_RDONLY:
  81. LOG_D("fops open: O_RDONLY!");
  82. flags = RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_RDONLY;
  83. break;
  84. case O_WRONLY:
  85. LOG_D("fops open: O_WRONLY!");
  86. flags = RT_DEVICE_FLAG_INT_TX | RT_DEVICE_FLAG_WRONLY;
  87. break;
  88. case O_RDWR:
  89. LOG_D("fops open: O_RDWR!");
  90. flags = RT_DEVICE_FLAG_RDWR
  91. | RT_DEVICE_FLAG_INT_RX
  92. | RT_DEVICE_FLAG_INT_TX;
  93. break;
  94. default:
  95. LOG_E("fops open: unknown mode - %d!", fd->flags & O_ACCMODE);
  96. break;
  97. }
  98. if ((fd->flags & O_NONBLOCK) != 0) {
  99. flags |= RT_DEVICE_OFLAG_NONBLOCKING;
  100. }
  101. ret = rt_device_open(device, flags);
  102. if (ret == RT_EOK) {
  103. if ((fd->flags & O_ACCMODE) != O_WRONLY) {
  104. rt_device_set_rx_indicate(device, serial_fops_rx_ind);
  105. }
  106. return 0;
  107. }
  108. return ret;
  109. }
  110. static int serial_fops_close(struct dfs_fd *fd)
  111. {
  112. rt_device_t device;
  113. device = (rt_device_t)fd->data;
  114. rt_device_close(device);
  115. return 0;
  116. }
  117. static int serial_fops_ioctl(struct dfs_fd *fd, int cmd, void *args)
  118. {
  119. rt_device_t device;
  120. device = (rt_device_t)fd->data;
  121. switch (cmd)
  122. {
  123. case FIONREAD:
  124. break;
  125. case FIONWRITE:
  126. break;
  127. }
  128. return rt_device_control(device, cmd, args);
  129. }
  130. static int serial_fops_read(struct dfs_fd *fd, void *buf, size_t count)
  131. {
  132. int size = 0;
  133. int flags = 0;
  134. rt_device_t device;
  135. flags = fd->flags & O_ACCMODE;
  136. if (flags == O_WRONLY) {
  137. return -EIO;
  138. }
  139. device = (rt_device_t)fd->data;
  140. do
  141. {
  142. size = rt_device_read(device, -1, buf, count);
  143. if (size <= 0)
  144. {
  145. if (fd->flags & O_NONBLOCK)
  146. {
  147. size = -EAGAIN;
  148. break;
  149. }
  150. rt_wqueue_wait(&(device->wait_queue), 0, RT_WAITING_FOREVER);
  151. }
  152. }while (size <= 0);
  153. return size;
  154. }
  155. static int serial_fops_write(struct dfs_fd *fd, const void *buf, size_t count)
  156. {
  157. int flags = 0;
  158. rt_device_t device;
  159. flags = fd->flags & O_ACCMODE;
  160. if (flags == O_RDONLY) {
  161. return -EIO;
  162. }
  163. device = (rt_device_t)fd->data;
  164. return rt_device_write(device, -1, buf, count);
  165. }
  166. static int serial_fops_flush(struct dfs_fd *fd)
  167. {
  168. rt_device_t device;
  169. device = (rt_device_t)fd->data;
  170. return rt_device_flush(device);
  171. }
  172. static int serial_fops_poll(struct dfs_fd *fd, struct rt_pollreq *req)
  173. {
  174. int mask = 0;
  175. int flags = 0;
  176. rt_device_t device;
  177. struct rt_serial_device *serial;
  178. device = (rt_device_t)fd->data;
  179. RT_ASSERT(device != RT_NULL);
  180. serial = (struct rt_serial_device *)device;
  181. /* only support POLLIN */
  182. flags = fd->flags & O_ACCMODE;
  183. if (flags == O_RDONLY || flags == O_RDWR)
  184. {
  185. rt_base_t level;
  186. struct rt_serial_fifo* rx_fifo;
  187. rt_poll_add(&(device->wait_queue), req);
  188. rx_fifo = (struct rt_serial_fifo*) serial->serial_rx;
  189. level = rt_hw_interrupt_disable();
  190. if ((rx_fifo->get_index != rx_fifo->put_index) || (rx_fifo->get_index == rx_fifo->put_index && rx_fifo->is_full == RT_TRUE))
  191. {
  192. mask |= POLLIN;
  193. }
  194. rt_hw_interrupt_enable(level);
  195. }
  196. return mask;
  197. }
  198. const static struct dfs_file_ops _serial_fops =
  199. {
  200. serial_fops_open,
  201. serial_fops_close,
  202. serial_fops_ioctl,
  203. serial_fops_read,
  204. serial_fops_write,
  205. serial_fops_flush, /* flush */
  206. RT_NULL, /* lseek */
  207. RT_NULL, /* getdents */
  208. serial_fops_poll,
  209. };
  210. #endif
  211. /**
  212. * Calculate fifo data length.
  213. *
  214. * @param fifo the data fifo of serial device
  215. *
  216. * @return length
  217. */
  218. rt_inline rt_ssize_t _serial_fifo_calc_data_len(struct rt_serial_fifo *fifo)
  219. {
  220. rt_ssize_t size;
  221. if (fifo->put_index == fifo->get_index) {
  222. size = (fifo->is_full == RT_FALSE) ? 0 : fifo->buf_sz;
  223. } else if (fifo->put_index > fifo->get_index) {
  224. size = fifo->put_index - fifo->get_index;
  225. } else {
  226. size = fifo->buf_sz - (fifo->get_index - fifo->put_index);
  227. }
  228. return size;
  229. }
  230. rt_inline void _serial_fifo_push_data(struct rt_serial_fifo *fifo, rt_uint8_t ch)
  231. {
  232. fifo->buffer[fifo->put_index] = ch;
  233. fifo->put_index += 1;
  234. if (fifo->put_index >= fifo->buf_sz) fifo->put_index = 0;
  235. }
  236. rt_inline rt_uint8_t _serial_fifo_pop_data(struct rt_serial_fifo *fifo)
  237. {
  238. rt_uint8_t ch;
  239. ch = fifo->buffer[fifo->get_index];
  240. fifo->get_index += 1;
  241. if (fifo->get_index >= fifo->buf_sz) fifo->get_index = 0;
  242. return ch;
  243. }
  244. /*
  245. * Serial poll routines
  246. */
  247. rt_inline int _serial_poll_rx(struct rt_serial_device *serial, rt_uint8_t *data, int length)
  248. {
  249. int ch;
  250. int size;
  251. RT_ASSERT(serial != RT_NULL);
  252. size = length;
  253. while (length)
  254. {
  255. ch = serial->ops->getc(serial);
  256. if (ch == -1) break;
  257. *data = ch;
  258. data++; length--;
  259. }
  260. return size - length;
  261. }
  262. rt_inline int _serial_poll_tx(struct rt_serial_device *serial, const rt_uint8_t *data, int length)
  263. {
  264. int size;
  265. rt_uint8_t last_char = 0;
  266. RT_ASSERT(serial != RT_NULL);
  267. size = length;
  268. while (length)
  269. {
  270. /*
  271. * to be polite with serial console add a line feed
  272. * to the carriage return character
  273. */
  274. if (*data == '\n' &&
  275. (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM) == RT_DEVICE_FLAG_STREAM &&
  276. last_char != '\r')
  277. {
  278. serial->ops->putc(serial, '\r');
  279. last_char = 0;
  280. } else if (*data == '\r') {
  281. last_char = '\r';
  282. } else {
  283. last_char = 0;
  284. }
  285. serial->ops->putc(serial, *data);
  286. data++; length--;
  287. }
  288. return size - length;
  289. }
  290. /*
  291. * Serial interrupt routines
  292. */
  293. rt_inline int _serial_fifo_rx(struct rt_serial_device *serial, rt_uint8_t *data, int length)
  294. {
  295. rt_size_t len, size;
  296. struct rt_serial_fifo* rx_fifo;
  297. rt_base_t level;
  298. RT_ASSERT(serial != RT_NULL);
  299. if (length == 0) return 0;
  300. rx_fifo = (struct rt_serial_fifo*) serial->serial_rx;
  301. /* disable interrupt */
  302. level = rt_hw_interrupt_disable();
  303. // serial->ops->disable_interrupt(serial);
  304. len = _serial_fifo_calc_data_len(rx_fifo);
  305. if ((len == 0) && // non-blocking io mode
  306. (serial->parent.open_flag & RT_DEVICE_OFLAG_NONBLOCKING) == RT_DEVICE_OFLAG_NONBLOCKING) {
  307. /* enable interrupt */
  308. rt_hw_interrupt_enable(level);
  309. // serial->ops->enable_interrupt(serial);
  310. return 0;
  311. }
  312. if ((len == 0) && // blocking io mode
  313. (serial->parent.open_flag & RT_DEVICE_OFLAG_NONBLOCKING) != RT_DEVICE_OFLAG_NONBLOCKING) {
  314. rt_err_t ret;
  315. do {
  316. /* enable interrupt */
  317. rt_hw_interrupt_enable(level);
  318. // serial->ops->enable_interrupt(serial);
  319. #ifndef RT_SERIAL_USE_EVENT
  320. ret = rt_completion_wait(&(serial->completion_rx), RT_WAITING_FOREVER);
  321. #else
  322. ret = rt_event_recv(serial->rx_done, RT_SERIAL_EVENT_RXDONE, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, RT_NULL);
  323. #endif
  324. if (ret == RT_EOK || ret == -RT_ETIMEOUT) {
  325. } else {
  326. return 0;
  327. }
  328. /* disable interrupt */
  329. level = rt_hw_interrupt_disable();
  330. // serial->ops->disable_interrupt(serial);
  331. len = _serial_fifo_calc_data_len(rx_fifo);
  332. } while(len == 0);
  333. }
  334. if (len > length) {
  335. len = length;
  336. }
  337. /* read from software FIFO */
  338. for (size = 0; size < len; size++) {
  339. /* otherwise there's the data: */
  340. *data = _serial_fifo_pop_data(rx_fifo);
  341. data++;
  342. }
  343. rx_fifo->is_full = RT_FALSE;
  344. /* enable interrupt */
  345. rt_hw_interrupt_enable(level);
  346. // serial->ops->enable_interrupt(serial);
  347. return size;
  348. }
  349. rt_inline int _serial_int_tx(struct rt_serial_device *serial, const rt_uint8_t *data, int length)
  350. {
  351. rt_size_t len, length_t, size;
  352. struct rt_serial_fifo *tx_fifo;
  353. rt_base_t level;
  354. rt_uint8_t ch = 0;
  355. static rt_uint8_t last_char = 0;
  356. RT_ASSERT(serial != RT_NULL);
  357. if (length == 0) return 0;
  358. tx_fifo = (struct rt_serial_fifo*) serial->serial_tx;
  359. size = 0;
  360. do {
  361. length_t = length - size;
  362. /* disable interrupt */
  363. level = rt_hw_interrupt_disable();
  364. // serial->ops->disable_interrupt(serial);
  365. len = tx_fifo->buf_sz - _serial_fifo_calc_data_len(tx_fifo);
  366. if ((len == 0) && // non-blocking io mode
  367. (serial->parent.open_flag & RT_DEVICE_OFLAG_NONBLOCKING) == RT_DEVICE_OFLAG_NONBLOCKING) {
  368. /* enable interrupt */
  369. rt_hw_interrupt_enable(level);
  370. // serial->ops->enable_interrupt(serial);
  371. break;
  372. }
  373. if ((len == 0) && // blocking io mode
  374. (serial->parent.open_flag & RT_DEVICE_OFLAG_NONBLOCKING) != RT_DEVICE_OFLAG_NONBLOCKING) {
  375. rt_err_t ret;
  376. /* enable interrupt */
  377. rt_hw_interrupt_enable(level);
  378. // serial->ops->enable_interrupt(serial);
  379. #ifndef RT_SERIAL_USE_EVENT
  380. ret = rt_completion_wait(&(serial->completion_tx), RT_WAITING_FOREVER);
  381. #else
  382. ret = rt_event_recv(serial->tx_done, RT_SERIAL_EVENT_TXDONE, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, RT_NULL);
  383. #endif
  384. if (ret == RT_EOK || ret == -RT_ETIMEOUT) {
  385. continue;
  386. } else {
  387. return size;
  388. }
  389. }
  390. if (len > length_t) {
  391. len = length_t;
  392. }
  393. /* copy to software FIFO */
  394. while (len > 0) {
  395. /*
  396. * to be polite with serial console add a line feed
  397. * to the carriage return character
  398. */
  399. if (*data == '\n' &&
  400. (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM) == RT_DEVICE_FLAG_STREAM &&
  401. last_char != '\r') {
  402. _serial_fifo_push_data(tx_fifo, '\r');
  403. last_char = '\r';
  404. if (len == 1) {
  405. break;
  406. } else {
  407. len--;
  408. }
  409. } else if (*data == '\r') {
  410. last_char = '\r';
  411. } else {
  412. last_char = 0;
  413. }
  414. _serial_fifo_push_data(tx_fifo, *data);
  415. data++; len--; size++;
  416. }
  417. /* if the next position is read index, discard this 'read char' */
  418. if (tx_fifo->put_index == tx_fifo->get_index) {
  419. tx_fifo->is_full = RT_TRUE;
  420. }
  421. // TODO: start tx
  422. #if defined (RT_SERIAL_NO_TXEIT)
  423. if (serial->ops->is_int_txing != RT_NULL && serial->ops->is_int_txing(serial) == RT_FALSE) {
  424. ch = _serial_fifo_pop_data(tx_fifo);
  425. serial->ops->start_tx(serial, ch);
  426. }
  427. #else
  428. serial->ops->start_tx(serial);
  429. #endif
  430. /* enable interrupt */
  431. rt_hw_interrupt_enable(level);
  432. // serial->ops->enable_interrupt(serial);
  433. } while(size < length);
  434. return size;
  435. }
  436. #ifdef RT_SERIAL_USING_DMA
  437. /*
  438. * Serial DMA routines
  439. */
  440. rt_inline int _serial_dma_tx(struct rt_serial_device *serial, const rt_uint8_t *data, int length)
  441. {
  442. rt_size_t len, length_t, size, i;
  443. struct rt_serial_fifo *tx_fifo;
  444. rt_base_t level;
  445. rt_uint8_t ch;
  446. static rt_uint8_t last_char = 0;
  447. RT_ASSERT(serial != RT_NULL);
  448. if (length == 0) return 0;
  449. tx_fifo = (struct rt_serial_fifo*) serial->serial_tx;
  450. size = 0;
  451. do {
  452. length_t = length - size;
  453. /* disable interrupt */
  454. level = rt_hw_interrupt_disable();
  455. // serial->ops->disable_interrupt(serial);
  456. len = tx_fifo->buf_sz - _serial_fifo_calc_data_len(tx_fifo);
  457. if ((len == 0) && // non-blocking io mode
  458. (serial->parent.open_flag & RT_DEVICE_OFLAG_NONBLOCKING) == RT_DEVICE_OFLAG_NONBLOCKING) {
  459. /* enable interrupt */
  460. rt_hw_interrupt_enable(level);
  461. // serial->ops->enable_interrupt(serial);
  462. break;
  463. }
  464. if ((len == 0) && // blocking io mode
  465. (serial->parent.open_flag & RT_DEVICE_OFLAG_NONBLOCKING) != RT_DEVICE_OFLAG_NONBLOCKING) {
  466. rt_err_t ret;
  467. /* enable interrupt */
  468. rt_hw_interrupt_enable(level);
  469. // serial->ops->enable_interrupt(serial);
  470. #ifndef RT_SERIAL_USE_EVENT
  471. ret = rt_completion_wait(&(serial->completion_tx), RT_WAITING_FOREVER);
  472. #else
  473. ret = rt_event_recv(serial->tx_done, RT_SERIAL_EVENT_TXDONE, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, RT_NULL);
  474. #endif
  475. if (ret == RT_EOK || ret == -RT_ETIMEOUT) {
  476. continue;
  477. } else {
  478. return size;
  479. }
  480. }
  481. if (len > length_t) {
  482. len = length_t;
  483. }
  484. /* copy to software FIFO */
  485. while (len > 0) {
  486. /*
  487. * to be polite with serial console add a line feed
  488. * to the carriage return character
  489. */
  490. if (*data == '\n' &&
  491. (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM) == RT_DEVICE_FLAG_STREAM &&
  492. last_char != '\r') {
  493. _serial_fifo_push_data(tx_fifo, '\r');
  494. last_char = '\r';
  495. if (len == 1) {
  496. break;
  497. } else {
  498. len--;
  499. }
  500. } else if (*data == '\r') {
  501. last_char = '\r';
  502. } else {
  503. last_char = 0;
  504. }
  505. _serial_fifo_push_data(tx_fifo, *data);
  506. data++; len--; size++;
  507. }
  508. /* if the next position is read index, discard this 'read char' */
  509. if (tx_fifo->put_index == tx_fifo->get_index) {
  510. tx_fifo->is_full = RT_TRUE;
  511. }
  512. /* enable interrupt */
  513. rt_hw_interrupt_enable(level);
  514. // serial->ops->enable_interrupt(serial);
  515. // TODO: start tx
  516. /* disable interrupt */
  517. level = rt_hw_interrupt_disable();
  518. // serial->ops->disable_interrupt(serial);
  519. if (serial->ops->is_dma_txing(serial) == RT_FALSE) {
  520. /* calucate fifo data size */
  521. len = _serial_fifo_calc_data_len(tx_fifo);
  522. if (len > RT_SERIAL_DMA_BUFSZ) {
  523. len = RT_SERIAL_DMA_BUFSZ;
  524. }
  525. /* read from software FIFO */
  526. for (i = 0; i < len; i++) {
  527. /* pop one byte data */
  528. ch = _serial_fifo_pop_data(tx_fifo);
  529. serial->serial_dma_tx[i] = ch;
  530. }
  531. tx_fifo->is_full = RT_FALSE;
  532. serial->ops->start_dma_tx(serial, serial->serial_dma_tx, len);
  533. }
  534. /* enable interrupt */
  535. rt_hw_interrupt_enable(level);
  536. // serial->ops->enable_interrupt(serial);
  537. } while(size < length);
  538. return size;
  539. }
  540. #endif /* RT_SERIAL_USING_DMA */
  541. /* RT-Thread Device Interface */
  542. /*
  543. * This function initializes serial device.
  544. */
  545. static rt_err_t rt_serial_init(struct rt_device *dev)
  546. {
  547. rt_err_t result = RT_EOK;
  548. struct rt_serial_device *serial;
  549. RT_ASSERT(dev != RT_NULL);
  550. serial = (struct rt_serial_device *)dev;
  551. /* initialize rx/tx */
  552. serial->serial_rx = RT_NULL;
  553. serial->serial_tx = RT_NULL;
  554. /* initialize hardware */
  555. if (serial->ops->init)
  556. result = serial->ops->init(serial);
  557. return result;
  558. }
  559. static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
  560. {
  561. rt_uint16_t stream_flag = 0;
  562. struct rt_serial_device *serial;
  563. RT_ASSERT(dev != RT_NULL);
  564. serial = (struct rt_serial_device *)dev;
  565. LOG_D("open serial device: 0x%08x with open flag: 0x%04x",
  566. dev, oflag);
  567. /* check device flag with the open flag */
  568. if ((oflag & RT_DEVICE_FLAG_DMA_RX) && !(dev->flag & RT_DEVICE_FLAG_DMA_RX))
  569. return -RT_EIO;
  570. if ((oflag & RT_DEVICE_FLAG_DMA_TX) && !(dev->flag & RT_DEVICE_FLAG_DMA_TX))
  571. return -RT_EIO;
  572. if ((oflag & RT_DEVICE_FLAG_INT_RX) && !(dev->flag & RT_DEVICE_FLAG_INT_RX))
  573. return -RT_EIO;
  574. if ((oflag & RT_DEVICE_FLAG_INT_TX) && !(dev->flag & RT_DEVICE_FLAG_INT_TX))
  575. return -RT_EIO;
  576. /* keep steam flag */
  577. if ((oflag & RT_DEVICE_FLAG_STREAM) || (dev->open_flag & RT_DEVICE_FLAG_STREAM))
  578. stream_flag = RT_DEVICE_FLAG_STREAM;
  579. /* get open flags */
  580. dev->open_flag = oflag & 0xff;
  581. if (oflag & RT_DEVICE_FLAG_INT_RX)
  582. {
  583. /* initialize the Rx/Tx structure according to open flag */
  584. if (serial->serial_rx == RT_NULL)
  585. {
  586. struct rt_serial_fifo* rx_fifo;
  587. rx_fifo = (struct rt_serial_fifo*)rt_malloc(sizeof(struct rt_serial_fifo) +
  588. serial->bufsz);
  589. RT_ASSERT(rx_fifo != RT_NULL);
  590. rx_fifo->buf_sz = serial->bufsz;
  591. rx_fifo->buffer = (rt_uint8_t*) (rx_fifo + 1);
  592. rt_memset(rx_fifo->buffer, 0, rx_fifo->buf_sz);
  593. rx_fifo->put_index = 0;
  594. rx_fifo->get_index = 0;
  595. rx_fifo->is_full = RT_FALSE;
  596. serial->serial_rx = rx_fifo;
  597. }
  598. dev->open_flag |= RT_DEVICE_FLAG_INT_RX;
  599. serial->_cb_rx = _serial_fifo_rx;
  600. #ifndef RT_SERIAL_USE_EVENT
  601. rt_completion_init(&(serial->completion_rx));
  602. #else
  603. serial->rx_done = rt_event_create("rx_done", RT_IPC_FLAG_PRIO);
  604. #endif
  605. /* configure low level device */
  606. serial->ops->control(serial, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_RX);
  607. }
  608. #ifdef RT_SERIAL_USING_DMA
  609. else if (oflag & RT_DEVICE_FLAG_DMA_RX)
  610. {
  611. /* initialize the Rx/Tx structure according to open flag */
  612. if (serial->serial_rx == RT_NULL)
  613. {
  614. struct rt_serial_fifo* rx_fifo;
  615. rx_fifo = (struct rt_serial_fifo*)rt_malloc(sizeof(struct rt_serial_fifo) +
  616. serial->bufsz);
  617. RT_ASSERT(rx_fifo != RT_NULL);
  618. rx_fifo->buf_sz = serial->bufsz;
  619. rx_fifo->buffer = (rt_uint8_t*) (rx_fifo + 1);
  620. rt_memset(rx_fifo->buffer, 0, rx_fifo->buf_sz);
  621. rx_fifo->put_index = 0;
  622. rx_fifo->get_index = 0;
  623. rx_fifo->is_full = RT_FALSE;
  624. serial->serial_rx = rx_fifo;
  625. }
  626. dev->open_flag |= RT_DEVICE_FLAG_DMA_RX;
  627. serial->dma_idx_rx = 0;
  628. serial->_cb_rx = _serial_fifo_rx;
  629. #ifndef RT_SERIAL_USE_EVENT
  630. rt_completion_init(&(serial->completion_rx));
  631. #else
  632. serial->rx_done = rt_event_create("rx_done", RT_IPC_FLAG_PRIO);
  633. #endif
  634. /* configure fifo address and length to low level device */
  635. serial->ops->control(serial, RT_DEVICE_CTRL_CONFIG, (void *) RT_DEVICE_FLAG_DMA_RX);
  636. }
  637. #endif /* RT_SERIAL_USING_DMA */
  638. else
  639. {
  640. serial->serial_rx = RT_NULL;
  641. serial->_cb_rx = _serial_poll_rx;
  642. }
  643. if (oflag & RT_DEVICE_FLAG_INT_TX)
  644. {
  645. if (serial->serial_tx == RT_NULL)
  646. {
  647. struct rt_serial_fifo *tx_fifo;
  648. tx_fifo = (struct rt_serial_fifo*)rt_malloc(sizeof(struct rt_serial_fifo) +
  649. serial->bufsz);
  650. RT_ASSERT(tx_fifo != RT_NULL);
  651. tx_fifo->buf_sz = serial->bufsz;
  652. tx_fifo->buffer = (rt_uint8_t*) (tx_fifo + 1);
  653. rt_memset(tx_fifo->buffer, 0, tx_fifo->buf_sz);
  654. tx_fifo->put_index = 0;
  655. tx_fifo->get_index = 0;
  656. tx_fifo->is_full = RT_FALSE;
  657. #ifndef RT_SERIAL_USE_EVENT
  658. rt_completion_init(&(serial->completion_tx));
  659. #else
  660. serial->tx_done = rt_event_create("tx_done", RT_IPC_FLAG_PRIO);
  661. #endif
  662. serial->serial_tx = tx_fifo;
  663. }
  664. dev->open_flag |= RT_DEVICE_FLAG_INT_TX;
  665. /* configure low level device */
  666. // serial->ops->control(serial, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_TX);
  667. serial->_cb_tx = _serial_int_tx;
  668. }
  669. #ifdef RT_SERIAL_USING_DMA
  670. else if (oflag & RT_DEVICE_FLAG_DMA_TX)
  671. {
  672. if (serial->serial_tx == RT_NULL)
  673. {
  674. struct rt_serial_fifo *tx_fifo;
  675. tx_fifo = (struct rt_serial_fifo*)rt_malloc(sizeof(struct rt_serial_fifo) +
  676. serial->bufsz);
  677. RT_ASSERT(tx_fifo != RT_NULL);
  678. tx_fifo->buf_sz = serial->bufsz;
  679. tx_fifo->buffer = (rt_uint8_t*) (tx_fifo + 1);
  680. rt_memset(tx_fifo->buffer, 0, tx_fifo->buf_sz);
  681. tx_fifo->put_index = 0;
  682. tx_fifo->get_index = 0;
  683. tx_fifo->is_full = RT_FALSE;
  684. #ifndef RT_SERIAL_USE_EVENT
  685. rt_completion_init(&(serial->completion_tx));
  686. #else
  687. serial->tx_done = rt_event_create("tx_done", RT_IPC_FLAG_PRIO);
  688. #endif
  689. serial->serial_tx = tx_fifo;
  690. }
  691. dev->open_flag |= RT_DEVICE_FLAG_DMA_TX;
  692. serial->_cb_tx = _serial_dma_tx;
  693. /* configure low level device */
  694. serial->ops->control(serial, RT_DEVICE_CTRL_CONFIG, (void *)RT_DEVICE_FLAG_DMA_TX);
  695. }
  696. #endif /* RT_SERIAL_USING_DMA */
  697. else
  698. {
  699. serial->serial_tx = RT_NULL;
  700. serial->_cb_tx = _serial_poll_tx;
  701. }
  702. serial->ops->control(serial, RT_DEVICE_CTRL_OPEN, (void *)0);
  703. /* set stream flag */
  704. dev->open_flag |= stream_flag;
  705. return RT_EOK;
  706. }
  707. static rt_err_t rt_serial_close(struct rt_device *dev)
  708. {
  709. struct rt_serial_device *serial;
  710. RT_ASSERT(dev != RT_NULL);
  711. serial = (struct rt_serial_device *)dev;
  712. serial->ops->control(serial, RT_DEVICE_CTRL_CLOSE, RT_NULL);
  713. if (dev->open_flag & RT_DEVICE_FLAG_INT_RX)
  714. {
  715. struct rt_serial_fifo *rx_fifo;
  716. /* configure low level device */
  717. serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void *)RT_DEVICE_FLAG_INT_RX);
  718. dev->open_flag &= ~RT_DEVICE_FLAG_INT_RX;
  719. rx_fifo = (struct rt_serial_fifo *)serial->serial_rx;
  720. rt_free(rx_fifo);
  721. serial->serial_rx = RT_NULL;
  722. #ifndef RT_SERIAL_USE_EVENT
  723. rt_completion_init(&(serial->completion_rx));
  724. #else
  725. rt_event_delete(serial->rx_done);
  726. #endif
  727. }
  728. #ifdef RT_SERIAL_USING_DMA
  729. else if (dev->open_flag & RT_DEVICE_FLAG_DMA_RX)
  730. {
  731. struct rt_serial_fifo *rx_fifo;
  732. /* configure low level device */
  733. serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void *) RT_DEVICE_FLAG_DMA_RX);
  734. dev->open_flag &= ~RT_DEVICE_FLAG_DMA_RX;
  735. rx_fifo = (struct rt_serial_fifo *)serial->serial_rx;
  736. rt_free(rx_fifo);
  737. serial->serial_rx = RT_NULL;
  738. #ifndef RT_SERIAL_USE_EVENT
  739. rt_completion_init(&(serial->completion_rx));
  740. #else
  741. rt_event_delete(serial->rx_done);
  742. #endif
  743. }
  744. #endif /* RT_SERIAL_USING_DMA */
  745. if (dev->open_flag & RT_DEVICE_FLAG_INT_TX)
  746. {
  747. struct rt_serial_fifo *tx_fifo;
  748. /* configure low level device */
  749. serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void *)RT_DEVICE_FLAG_INT_TX);
  750. dev->open_flag &= ~RT_DEVICE_FLAG_INT_TX;
  751. tx_fifo = (struct rt_serial_fifo *)serial->serial_tx;
  752. rt_free(tx_fifo);
  753. serial->serial_tx = RT_NULL;
  754. #ifndef RT_SERIAL_USE_EVENT
  755. rt_completion_init(&(serial->completion_tx));
  756. #else
  757. rt_event_delete(serial->tx_done);
  758. #endif
  759. }
  760. #ifdef RT_SERIAL_USING_DMA
  761. else if (dev->open_flag & RT_DEVICE_FLAG_DMA_TX)
  762. {
  763. struct rt_serial_fifo *tx_fifo;
  764. /* configure low level device */
  765. serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void *) RT_DEVICE_FLAG_DMA_TX);
  766. dev->open_flag &= ~RT_DEVICE_FLAG_DMA_TX;
  767. tx_fifo = (struct rt_serial_fifo *)serial->serial_tx;
  768. rt_free(tx_fifo);
  769. serial->serial_tx = RT_NULL;
  770. #ifndef RT_SERIAL_USE_EVENT
  771. rt_completion_init(&(serial->completion_tx));
  772. #else
  773. rt_event_delete(serial->tx_done);
  774. #endif
  775. }
  776. #endif /* RT_SERIAL_USING_DMA */
  777. dev->flag &= ~RT_DEVICE_FLAG_ACTIVATED;
  778. return RT_EOK;
  779. }
  780. static rt_ssize_t rt_serial_read(struct rt_device *dev,
  781. rt_off_t pos,
  782. void *buffer,
  783. rt_size_t size)
  784. {
  785. struct rt_serial_device *serial;
  786. RT_ASSERT(dev != RT_NULL);
  787. if (size == 0) return 0;
  788. serial = (struct rt_serial_device *)dev;
  789. return serial->_cb_rx(serial, (rt_uint8_t *)buffer, size);
  790. }
  791. static rt_ssize_t rt_serial_write(struct rt_device *dev,
  792. rt_off_t pos,
  793. const void *buffer,
  794. rt_size_t size)
  795. {
  796. struct rt_serial_device *serial;
  797. RT_ASSERT(dev != RT_NULL);
  798. if (size == 0) return 0;
  799. serial = (struct rt_serial_device *)dev;
  800. return serial->_cb_tx(serial, (const rt_uint8_t *)buffer, size);
  801. }
  802. static rt_err_t rt_serial_flush(struct rt_device *dev)
  803. {
  804. struct rt_serial_device *serial;
  805. rt_size_t len;
  806. struct rt_serial_fifo *tx_fifo, *rx_fifo;
  807. rt_base_t level;
  808. RT_ASSERT(dev != RT_NULL);
  809. serial = (struct rt_serial_device *)dev;
  810. if((dev->open_flag & RT_DEVICE_FLAG_INT_RX)
  811. #ifdef RT_SERIAL_USING_DMA
  812. || (dev->open_flag & RT_DEVICE_FLAG_DMA_RX)
  813. #endif /* RT_SERIAL_USING_DMA */
  814. ) {
  815. rx_fifo = (struct rt_serial_fifo*) serial->serial_rx;
  816. level = rt_hw_interrupt_disable();
  817. rx_fifo->get_index = rx_fifo->put_index = 0;
  818. rx_fifo->is_full = RT_FALSE;
  819. rt_hw_interrupt_enable(level);
  820. }
  821. if ((dev->open_flag & RT_DEVICE_FLAG_INT_TX)
  822. #ifdef RT_SERIAL_USING_DMA
  823. || (dev->open_flag & RT_DEVICE_FLAG_DMA_TX)
  824. #endif /* RT_SERIAL_USING_DMA */
  825. ) {
  826. tx_fifo = (struct rt_serial_fifo*) serial->serial_tx;
  827. while(1) {
  828. /* disable interrupt */
  829. level = rt_hw_interrupt_disable();
  830. // serial->ops->disable_interrupt(serial);
  831. len = _serial_fifo_calc_data_len(tx_fifo);
  832. if (len == 0) {
  833. /* enable interrupt */
  834. rt_hw_interrupt_enable(level);
  835. // serial->ops->enable_interrupt(serial);
  836. break;
  837. } else {
  838. /* enable interrupt */
  839. rt_hw_interrupt_enable(level);
  840. // serial->ops->enable_interrupt(serial);
  841. #ifndef RT_SERIAL_USE_EVENT
  842. rt_completion_wait(&(serial->completion_tx), RT_WAITING_FOREVER);
  843. #else
  844. rt_event_recv(serial->tx_done, RT_SERIAL_EVENT_TXDONE, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, RT_NULL);
  845. #endif
  846. }
  847. }
  848. if (dev->open_flag & RT_DEVICE_FLAG_INT_TX) {
  849. serial->ops->flush(serial);
  850. }
  851. #ifdef RT_SERIAL_USING_DMA
  852. else if (dev->open_flag & RT_DEVICE_FLAG_DMA_TX) {
  853. while (serial->ops->is_dma_txing(serial) == RT_TRUE);
  854. }
  855. #endif /* RT_SERIAL_USING_DMA */
  856. } else {
  857. serial->ops->flush(serial);
  858. }
  859. return RT_EOK;
  860. }
  861. #ifdef RT_USING_POSIX_TERMIOS
  862. struct speed_baudrate_item
  863. {
  864. speed_t speed;
  865. int baudrate;
  866. };
  867. const static struct speed_baudrate_item _tbl[] =
  868. {
  869. {B2400, BAUD_RATE_2400},
  870. {B4800, BAUD_RATE_4800},
  871. {B9600, BAUD_RATE_9600},
  872. {B19200, BAUD_RATE_19200},
  873. {B38400, BAUD_RATE_38400},
  874. {B57600, BAUD_RATE_57600},
  875. {B115200, BAUD_RATE_115200},
  876. {B230400, BAUD_RATE_230400},
  877. {B460800, BAUD_RATE_460800},
  878. {B921600, BAUD_RATE_921600},
  879. {B2000000, BAUD_RATE_2000000},
  880. {B3000000, BAUD_RATE_3000000},
  881. };
  882. static speed_t _get_speed(int baudrate)
  883. {
  884. int index;
  885. for (index = 0; index < sizeof(_tbl)/sizeof(_tbl[0]); index ++)
  886. {
  887. if (_tbl[index].baudrate == baudrate)
  888. return _tbl[index].speed;
  889. }
  890. return B0;
  891. }
  892. static int _get_baudrate(speed_t speed)
  893. {
  894. int index;
  895. for (index = 0; index < sizeof(_tbl)/sizeof(_tbl[0]); index ++)
  896. {
  897. if (_tbl[index].speed == speed)
  898. return _tbl[index].baudrate;
  899. }
  900. return 0;
  901. }
  902. static void _tc_flush(struct rt_serial_device *serial, int queue)
  903. {
  904. rt_base_t level;
  905. int ch = -1;
  906. struct rt_serial_fifo *rx_fifo = RT_NULL;
  907. struct rt_device *device = RT_NULL;
  908. RT_ASSERT(serial != RT_NULL);
  909. device = &(serial->parent);
  910. rx_fifo = (struct rt_serial_fifo *) serial->serial_rx;
  911. switch(queue)
  912. {
  913. case TCIFLUSH:
  914. case TCIOFLUSH:
  915. RT_ASSERT(rx_fifo != RT_NULL);
  916. if((device->open_flag & RT_DEVICE_FLAG_INT_RX) || (device->open_flag & RT_DEVICE_FLAG_DMA_RX))
  917. {
  918. RT_ASSERT(RT_NULL != rx_fifo);
  919. level = rt_hw_interrupt_disable();
  920. rx_fifo->get_index = rx_fifo->put_index;
  921. rx_fifo->is_full = RT_FALSE;
  922. rt_hw_interrupt_enable(level);
  923. }
  924. else
  925. {
  926. while (1)
  927. {
  928. ch = serial->ops->getc(serial);
  929. if (ch == -1) break;
  930. }
  931. }
  932. break;
  933. case TCOFLUSH:
  934. break;
  935. }
  936. }
  937. #endif
  938. static rt_err_t rt_serial_control(struct rt_device *dev,
  939. int cmd,
  940. void *args)
  941. {
  942. rt_err_t ret = RT_EOK;
  943. struct rt_serial_device *serial;
  944. RT_ASSERT(dev != RT_NULL);
  945. serial = (struct rt_serial_device *)dev;
  946. switch (cmd)
  947. {
  948. case RT_DEVICE_CTRL_SUSPEND:
  949. /* suspend device */
  950. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  951. break;
  952. case RT_DEVICE_CTRL_RESUME:
  953. /* resume device */
  954. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  955. break;
  956. case RT_DEVICE_CTRL_BLOCKING:
  957. if (((rt_uint32_t)args & RT_DEVICE_OFLAG_NONBLOCKING) == RT_DEVICE_OFLAG_NONBLOCKING) {
  958. dev->open_flag |= RT_DEVICE_OFLAG_NONBLOCKING;
  959. } else {
  960. dev->open_flag &= ~RT_DEVICE_OFLAG_NONBLOCKING;
  961. }
  962. break;
  963. case RT_DEVICE_CTRL_CONFIG:
  964. if (args)
  965. {
  966. struct serial_configure *pconfig = (struct serial_configure *) args;
  967. /* serial device has been opened, to configure it */
  968. ret = serial->ops->configure(serial, pconfig);
  969. if (ret == RT_EOK) {
  970. /* set serial configure */
  971. serial->config = *pconfig;
  972. }
  973. }
  974. break;
  975. #ifdef RT_USING_POSIX_STDIO
  976. #ifdef RT_USING_POSIX_TERMIOS
  977. case TCGETA:
  978. {
  979. struct termios *tio = (struct termios*)args;
  980. if (tio == RT_NULL) return -RT_EINVAL;
  981. tio->c_iflag = 0;
  982. tio->c_oflag = 0;
  983. tio->c_lflag = 0;
  984. /* update oflag for console device */
  985. if (rt_console_get_device() == dev)
  986. tio->c_oflag = OPOST | ONLCR;
  987. /* set cflag */
  988. tio->c_cflag = 0;
  989. if (serial->config.data_bits == DATA_BITS_5)
  990. tio->c_cflag = CS5;
  991. else if (serial->config.data_bits == DATA_BITS_6)
  992. tio->c_cflag = CS6;
  993. else if (serial->config.data_bits == DATA_BITS_7)
  994. tio->c_cflag = CS7;
  995. else if (serial->config.data_bits == DATA_BITS_8)
  996. tio->c_cflag = CS8;
  997. if (serial->config.stop_bits == STOP_BITS_2)
  998. tio->c_cflag |= CSTOPB;
  999. if (serial->config.parity == PARITY_EVEN)
  1000. tio->c_cflag |= PARENB;
  1001. else if (serial->config.parity == PARITY_ODD)
  1002. tio->c_cflag |= (PARODD | PARENB);
  1003. cfsetospeed(tio, _get_speed(serial->config.baud_rate));
  1004. }
  1005. break;
  1006. case TCSETAW:
  1007. case TCSETAF:
  1008. case TCSETA:
  1009. {
  1010. int baudrate;
  1011. struct serial_configure config;
  1012. struct termios *tio = (struct termios*)args;
  1013. if (tio == RT_NULL) return -RT_EINVAL;
  1014. config = serial->config;
  1015. baudrate = _get_baudrate(cfgetospeed(tio));
  1016. config.baud_rate = baudrate;
  1017. switch (tio->c_cflag & CSIZE)
  1018. {
  1019. case CS5:
  1020. config.data_bits = DATA_BITS_5;
  1021. break;
  1022. case CS6:
  1023. config.data_bits = DATA_BITS_6;
  1024. break;
  1025. case CS7:
  1026. config.data_bits = DATA_BITS_7;
  1027. break;
  1028. default:
  1029. config.data_bits = DATA_BITS_8;
  1030. break;
  1031. }
  1032. if (tio->c_cflag & CSTOPB) config.stop_bits = STOP_BITS_2;
  1033. else config.stop_bits = STOP_BITS_1;
  1034. if (tio->c_cflag & PARENB)
  1035. {
  1036. if (tio->c_cflag & PARODD) config.parity = PARITY_ODD;
  1037. else config.parity = PARITY_EVEN;
  1038. }
  1039. else config.parity = PARITY_NONE;
  1040. serial->ops->configure(serial, &config);
  1041. }
  1042. break;
  1043. case TCFLSH:
  1044. {
  1045. int queue = (int)args;
  1046. _tc_flush(serial, queue);
  1047. }
  1048. break;
  1049. case TCXONC:
  1050. break;
  1051. #endif /*RT_USING_POSIX_TERMIOS*/
  1052. case TIOCSWINSZ:
  1053. {
  1054. struct winsize* p_winsize;
  1055. p_winsize = (struct winsize*)args;
  1056. rt_kprintf("\x1b[8;%d;%dt", p_winsize->ws_col, p_winsize->ws_row);
  1057. }
  1058. break;
  1059. case TIOCGWINSZ:
  1060. {
  1061. struct winsize* p_winsize;
  1062. p_winsize = (struct winsize*)args;
  1063. if(rt_thread_self() != rt_thread_find("tshell"))
  1064. {
  1065. /* only can be used in tshell thread; otherwise, return default size */
  1066. p_winsize->ws_col = 80;
  1067. p_winsize->ws_row = 24;
  1068. }
  1069. else
  1070. {
  1071. #define _TIO_BUFLEN 20
  1072. char _tio_buf[_TIO_BUFLEN];
  1073. unsigned char cnt1, cnt2, cnt3, i;
  1074. char row_s[4], col_s[4];
  1075. char *p;
  1076. rt_memset(_tio_buf, 0, _TIO_BUFLEN);
  1077. /* send the command to terminal for getting the window size of the terminal */
  1078. rt_kprintf("\033[18t");
  1079. /* waiting for the response from the terminal */
  1080. i = 0;
  1081. while(i < _TIO_BUFLEN)
  1082. {
  1083. _tio_buf[i] = getchar();
  1084. if(_tio_buf[i] != 't')
  1085. {
  1086. i ++;
  1087. }
  1088. else
  1089. {
  1090. break;
  1091. }
  1092. }
  1093. if(i == _TIO_BUFLEN)
  1094. {
  1095. /* buffer overloaded, and return default size */
  1096. p_winsize->ws_col = 80;
  1097. p_winsize->ws_row = 24;
  1098. break;
  1099. }
  1100. /* interpreting data eg: "\033[8;1;15t" which means row is 1 and col is 15 (unit: size of ONE character) */
  1101. rt_memset(row_s,0,4);
  1102. rt_memset(col_s,0,4);
  1103. cnt1 = 0;
  1104. while(_tio_buf[cnt1] != ';' && cnt1 < _TIO_BUFLEN)
  1105. {
  1106. cnt1++;
  1107. }
  1108. cnt2 = ++cnt1;
  1109. while(_tio_buf[cnt2] != ';' && cnt2 < _TIO_BUFLEN)
  1110. {
  1111. cnt2++;
  1112. }
  1113. p = row_s;
  1114. while(cnt1 < cnt2)
  1115. {
  1116. *p++ = _tio_buf[cnt1++];
  1117. }
  1118. p = col_s;
  1119. cnt2++;
  1120. cnt3 = rt_strlen(_tio_buf) - 1;
  1121. while(cnt2 < cnt3)
  1122. {
  1123. *p++ = _tio_buf[cnt2++];
  1124. }
  1125. /* load the window size date */
  1126. p_winsize->ws_col = atoi(col_s);
  1127. p_winsize->ws_row = atoi(row_s);
  1128. #undef _TIO_BUFLEN
  1129. }
  1130. p_winsize->ws_xpixel = 0;/* unused */
  1131. p_winsize->ws_ypixel = 0;/* unused */
  1132. }
  1133. break;
  1134. case FIONREAD:
  1135. {
  1136. rt_size_t recved = 0;
  1137. rt_base_t level;
  1138. level = rt_hw_interrupt_disable();
  1139. recved = _serial_fifo_calc_data_len(serial->serial_rx);
  1140. rt_hw_interrupt_enable(level);
  1141. *(rt_size_t *)args = recved;
  1142. }
  1143. break;
  1144. #endif /*RT_USING_POSIX_STDIO*/
  1145. default :
  1146. /* control device */
  1147. ret = serial->ops->control(serial, cmd, args);
  1148. break;
  1149. }
  1150. return ret;
  1151. }
  1152. #ifdef RT_USING_DEVICE_OPS
  1153. const static struct rt_device_ops serial_ops =
  1154. {
  1155. .init = rt_serial_init,
  1156. .open = rt_serial_open,
  1157. .close = rt_serial_close,
  1158. .read = rt_serial_read,
  1159. .write = rt_serial_write,
  1160. .flush = rt_serial_flush,
  1161. .control = rt_serial_control
  1162. };
  1163. #endif
  1164. /*
  1165. * serial register
  1166. */
  1167. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
  1168. const char *name,
  1169. rt_uint32_t flag,
  1170. void *data)
  1171. {
  1172. rt_err_t ret;
  1173. struct rt_device *device;
  1174. RT_ASSERT(serial != RT_NULL);
  1175. serial->config = RT_SERIAL_CONFIG_DEFAULT;
  1176. serial->bufsz = RT_SERIAL_FIFO_BUFSZ;
  1177. device = &(serial->parent);
  1178. device->type = RT_Device_Class_Char;
  1179. device->rx_indicate = RT_NULL;
  1180. device->tx_complete = RT_NULL;
  1181. #ifdef RT_USING_DEVICE_OPS
  1182. device->ops = &serial_ops;
  1183. #else
  1184. device->init = rt_serial_init;
  1185. device->open = rt_serial_open;
  1186. device->close = rt_serial_close;
  1187. device->read = rt_serial_read;
  1188. device->write = rt_serial_write;
  1189. device->flush = rt_serial_flush;
  1190. device->control = rt_serial_control;
  1191. #endif
  1192. device->user_data = data;
  1193. /* register a character device */
  1194. ret = rt_device_register(device, name, flag);
  1195. #if defined(RT_USING_POSIX_STDIO)
  1196. /* set fops */
  1197. device->fops = &_serial_fops;
  1198. #endif
  1199. return ret;
  1200. }
  1201. /* ISR for serial interrupt */
  1202. void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
  1203. {
  1204. switch (event & 0xff)
  1205. {
  1206. case RT_SERIAL_EVENT_RX_IND:
  1207. {
  1208. int ch = -1;
  1209. struct rt_serial_fifo* rx_fifo;
  1210. /* interrupt mode receive */
  1211. rx_fifo = (struct rt_serial_fifo*)serial->serial_rx;
  1212. #if defined(RT_SERIAL_HARD_FIFO)
  1213. while (1)
  1214. #endif
  1215. {
  1216. ch = serial->ops->getc(serial);
  1217. #if defined(RT_SERIAL_HARD_FIFO)
  1218. if (ch == -1) break;
  1219. #endif
  1220. /* if fifo is full, discard one byte first */
  1221. if (rx_fifo->is_full == RT_TRUE) {
  1222. rx_fifo->get_index += 1;
  1223. if (rx_fifo->get_index >= rx_fifo->buf_sz) rx_fifo->get_index = 0;
  1224. }
  1225. /* push a new data */
  1226. _serial_fifo_push_data(rx_fifo, ch);
  1227. /* if put index equal to read index, fifo is full */
  1228. if (rx_fifo->put_index == rx_fifo->get_index)
  1229. {
  1230. rx_fifo->is_full = RT_TRUE;
  1231. }
  1232. }
  1233. #ifndef RT_SERIAL_USE_EVENT
  1234. rt_completion_done(&(serial->completion_rx));
  1235. #else
  1236. rt_event_send(serial->rx_done, RT_SERIAL_EVENT_RXDONE);
  1237. #endif
  1238. /* invoke callback */
  1239. if (serial->parent.rx_indicate != RT_NULL) {
  1240. rt_size_t rx_length;
  1241. /* get rx length */
  1242. rx_length = _serial_fifo_calc_data_len(rx_fifo);
  1243. if (rx_length) {
  1244. serial->parent.rx_indicate(&serial->parent, rx_length);
  1245. }
  1246. }
  1247. }
  1248. break;
  1249. case RT_SERIAL_EVENT_TX_DONE:
  1250. {
  1251. #if defined(RT_SERIAL_HARD_FIFO)
  1252. rt_size_t fifo_sz;
  1253. #endif
  1254. rt_size_t len;
  1255. rt_uint8_t ch = 0;
  1256. struct rt_serial_fifo *tx_fifo;
  1257. tx_fifo = (struct rt_serial_fifo*) serial->serial_tx;
  1258. /* calucate fifo data size */
  1259. len = _serial_fifo_calc_data_len(tx_fifo);
  1260. if (len == 0) {
  1261. // TODO: stop tx
  1262. serial->ops->stop_tx(serial);
  1263. #ifndef RT_SERIAL_USE_EVENT
  1264. rt_completion_done(&(serial->completion_tx));
  1265. #else
  1266. rt_event_send(serial->tx_done, RT_SERIAL_EVENT_TXDONE);
  1267. #endif
  1268. /* invoke callback */
  1269. if (serial->parent.tx_complete != RT_NULL) {
  1270. serial->parent.tx_complete(&serial->parent, (void*)len);
  1271. }
  1272. break;
  1273. }
  1274. #if defined(RT_SERIAL_HARD_FIFO)
  1275. fifo_sz = event >> 8;
  1276. if (len > fifo_sz) {
  1277. len = fifo_sz;
  1278. }
  1279. /* read from software FIFO */
  1280. while (len > 0) {
  1281. /* pop one byte data */
  1282. ch = _serial_fifo_pop_data(tx_fifo);
  1283. serial->ops->putc(serial, ch);
  1284. len--;
  1285. }
  1286. #else
  1287. /* pop one byte data */
  1288. ch = _serial_fifo_pop_data(tx_fifo);
  1289. serial->ops->putc(serial, ch);
  1290. #endif
  1291. tx_fifo->is_full = RT_FALSE;
  1292. }
  1293. break;
  1294. #ifdef RT_SERIAL_USING_DMA
  1295. case RT_SERIAL_EVENT_RX_DMADONE:
  1296. {
  1297. int dma_idx, ch = -1;
  1298. struct rt_serial_fifo* rx_fifo;
  1299. dma_idx = event >> 8;
  1300. if (serial->dma_idx_rx == dma_idx) break;
  1301. rx_fifo = (struct rt_serial_fifo*)serial->serial_rx;
  1302. while (serial->dma_idx_rx != dma_idx) {
  1303. ch = serial->serial_dma_rx[serial->dma_idx_rx];
  1304. /* if fifo is full, discard one byte first */
  1305. if (rx_fifo->is_full == RT_TRUE) {
  1306. rx_fifo->get_index += 1;
  1307. if (rx_fifo->get_index >= rx_fifo->buf_sz) rx_fifo->get_index = 0;
  1308. }
  1309. /* push a new data */
  1310. _serial_fifo_push_data(rx_fifo, ch);
  1311. /* if put index equal to read index, fifo is full */
  1312. if (rx_fifo->put_index == rx_fifo->get_index)
  1313. {
  1314. rx_fifo->is_full = RT_TRUE;
  1315. }
  1316. serial->dma_idx_rx++;
  1317. if (serial->dma_idx_rx == RT_SERIAL_DMA_BUFSZ) {
  1318. serial->dma_idx_rx = 0;
  1319. }
  1320. }
  1321. serial->dma_idx_rx = dma_idx;
  1322. #ifndef RT_SERIAL_USE_EVENT
  1323. rt_completion_done(&(serial->completion_rx));
  1324. #else
  1325. rt_event_send(serial->rx_done, RT_SERIAL_EVENT_RXDONE);
  1326. #endif
  1327. /* invoke callback */
  1328. if (serial->parent.rx_indicate != RT_NULL) {
  1329. rt_size_t rx_length;
  1330. /* get rx length */
  1331. rx_length = _serial_fifo_calc_data_len(rx_fifo);
  1332. if (rx_length) {
  1333. serial->parent.rx_indicate(&serial->parent, rx_length);
  1334. }
  1335. }
  1336. }
  1337. break;
  1338. case RT_SERIAL_EVENT_TX_DMADONE:
  1339. {
  1340. int i;
  1341. rt_size_t len;
  1342. rt_uint8_t ch = 0;
  1343. struct rt_serial_fifo *tx_fifo;
  1344. tx_fifo = (struct rt_serial_fifo*) serial->serial_tx;
  1345. /* calucate fifo data size */
  1346. len = _serial_fifo_calc_data_len(tx_fifo);
  1347. if (len == 0) {
  1348. // TODO: stop tx
  1349. serial->ops->stop_dma_tx(serial);
  1350. #ifndef RT_SERIAL_USE_EVENT
  1351. rt_completion_done(&(serial->completion_tx));
  1352. #else
  1353. rt_event_send(serial->tx_done, RT_SERIAL_EVENT_TXDONE);
  1354. #endif
  1355. /* invoke callback */
  1356. if (serial->parent.tx_complete != RT_NULL) {
  1357. serial->parent.tx_complete(&serial->parent, (void*)len);
  1358. }
  1359. break;
  1360. }
  1361. if (len > RT_SERIAL_DMA_BUFSZ) {
  1362. len = RT_SERIAL_DMA_BUFSZ;
  1363. }
  1364. /* read from software FIFO */
  1365. for (i = 0; i < len; i++) {
  1366. /* pop one byte data */
  1367. ch = _serial_fifo_pop_data(tx_fifo);
  1368. serial->serial_dma_tx[i] = ch;
  1369. }
  1370. tx_fifo->is_full = RT_FALSE;
  1371. serial->ops->start_dma_tx(serial, serial->serial_dma_tx, len);
  1372. }
  1373. break;
  1374. #endif /* RT_SERIAL_USING_DMA */
  1375. }
  1376. }
  1377. #endif /* RT_USING_SERIAL */