serial_v2.c 48 KB

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