serialX.c 44 KB

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