serial_v2.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  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 getc
  23. #undef getc
  24. #endif
  25. #ifdef putc
  26. #undef putc
  27. #endif
  28. static rt_err_t serial_fops_rx_ind(rt_device_t dev, rt_size_t size)
  29. {
  30. rt_wqueue_wakeup(&(dev->wait_queue), (void*)POLLIN);
  31. return RT_EOK;
  32. }
  33. /* fops for serial */
  34. static int serial_fops_open(struct dfs_fd *fd)
  35. {
  36. rt_err_t ret = 0;
  37. rt_uint16_t flags = 0;
  38. rt_device_t device;
  39. device = (rt_device_t)fd->data;
  40. RT_ASSERT(device != RT_NULL);
  41. switch (fd->flags & O_ACCMODE)
  42. {
  43. case O_RDONLY:
  44. LOG_D("fops open: O_RDONLY!");
  45. flags = RT_DEVICE_FLAG_RDONLY;
  46. break;
  47. case O_WRONLY:
  48. LOG_D("fops open: O_WRONLY!");
  49. flags = RT_DEVICE_FLAG_WRONLY;
  50. break;
  51. case O_RDWR:
  52. LOG_D("fops open: O_RDWR!");
  53. flags = RT_DEVICE_FLAG_RDWR;
  54. break;
  55. default:
  56. LOG_E("fops open: unknown mode - %d!", fd->flags & O_ACCMODE);
  57. break;
  58. }
  59. if ((fd->flags & O_ACCMODE) != O_WRONLY)
  60. rt_device_set_rx_indicate(device, serial_fops_rx_ind);
  61. ret = rt_device_open(device, flags);
  62. if (ret == RT_EOK) return 0;
  63. return ret;
  64. }
  65. static int serial_fops_close(struct dfs_fd *fd)
  66. {
  67. rt_device_t device;
  68. device = (rt_device_t)fd->data;
  69. rt_device_set_rx_indicate(device, RT_NULL);
  70. rt_device_close(device);
  71. return 0;
  72. }
  73. static int serial_fops_ioctl(struct dfs_fd *fd, int cmd, void *args)
  74. {
  75. rt_device_t device;
  76. device = (rt_device_t)fd->data;
  77. switch (cmd)
  78. {
  79. case FIONREAD:
  80. break;
  81. case FIONWRITE:
  82. break;
  83. }
  84. return rt_device_control(device, cmd, args);
  85. }
  86. static int serial_fops_read(struct dfs_fd *fd, void *buf, size_t count)
  87. {
  88. int size = 0;
  89. rt_device_t device;
  90. device = (rt_device_t)fd->data;
  91. do
  92. {
  93. size = rt_device_read(device, -1, buf, count);
  94. if (size <= 0)
  95. {
  96. if (fd->flags & O_NONBLOCK)
  97. {
  98. size = -EAGAIN;
  99. break;
  100. }
  101. rt_wqueue_wait(&(device->wait_queue), 0, RT_WAITING_FOREVER);
  102. }
  103. }while (size <= 0);
  104. return size;
  105. }
  106. static int serial_fops_write(struct dfs_fd *fd, const void *buf, size_t count)
  107. {
  108. rt_device_t device;
  109. device = (rt_device_t)fd->data;
  110. return rt_device_write(device, -1, buf, count);
  111. }
  112. static int serial_fops_poll(struct dfs_fd *fd, struct rt_pollreq *req)
  113. {
  114. int mask = 0;
  115. int flags = 0;
  116. rt_device_t device;
  117. struct rt_serial_device *serial;
  118. device = (rt_device_t)fd->data;
  119. RT_ASSERT(device != RT_NULL);
  120. serial = (struct rt_serial_device *)device;
  121. /* only support POLLIN */
  122. flags = fd->flags & O_ACCMODE;
  123. if (flags == O_RDONLY || flags == O_RDWR)
  124. {
  125. rt_base_t level;
  126. struct rt_serial_rx_fifo* rx_fifo;
  127. rt_poll_add(&(device->wait_queue), req);
  128. rx_fifo = (struct rt_serial_rx_fifo*) serial->serial_rx;
  129. level = rt_hw_interrupt_disable();
  130. if (rt_ringbuffer_data_len(&rx_fifo->rb))
  131. mask |= POLLIN;
  132. rt_hw_interrupt_enable(level);
  133. }
  134. // mask|=POLLOUT;
  135. return mask;
  136. }
  137. const static struct dfs_file_ops _serial_fops =
  138. {
  139. serial_fops_open,
  140. serial_fops_close,
  141. serial_fops_ioctl,
  142. serial_fops_read,
  143. serial_fops_write,
  144. RT_NULL, /* flush */
  145. RT_NULL, /* lseek */
  146. RT_NULL, /* getdents */
  147. serial_fops_poll,
  148. };
  149. #endif /* RT_USING_POSIX_STDIO */
  150. static rt_size_t rt_serial_get_linear_buffer(struct rt_ringbuffer *rb,
  151. rt_uint8_t **ptr)
  152. {
  153. rt_size_t size;
  154. RT_ASSERT(rb != RT_NULL);
  155. *ptr = RT_NULL;
  156. /* whether has enough data */
  157. size = rt_ringbuffer_data_len(rb);
  158. /* no data */
  159. if (size == 0)
  160. return 0;
  161. *ptr = &rb->buffer_ptr[rb->read_index];
  162. if(rb->buffer_size - rb->read_index > size)
  163. {
  164. return size;
  165. }
  166. return rb->buffer_size - rb->read_index;
  167. }
  168. static rt_size_t rt_serial_update_read_index(struct rt_ringbuffer *rb,
  169. rt_uint16_t read_index)
  170. {
  171. rt_size_t size;
  172. RT_ASSERT(rb != RT_NULL);
  173. /* whether has enough data */
  174. size = rt_ringbuffer_data_len(rb);
  175. /* no data */
  176. if (size == 0)
  177. return 0;
  178. /* less data */
  179. if(size < read_index)
  180. read_index = size;
  181. if(rb->buffer_size - rb->read_index > read_index)
  182. {
  183. rb->read_index += read_index;
  184. return read_index;
  185. }
  186. read_index = rb->buffer_size - rb->read_index;
  187. /* we are going into the other side of the mirror */
  188. rb->read_mirror = ~rb->read_mirror;
  189. rb->read_index = 0;
  190. return read_index;
  191. }
  192. static rt_size_t rt_serial_update_write_index(struct rt_ringbuffer *rb,
  193. rt_uint16_t write_index)
  194. {
  195. rt_uint16_t size;
  196. RT_ASSERT(rb != RT_NULL);
  197. /* whether has enough space */
  198. size = rt_ringbuffer_space_len(rb);
  199. /* no space */
  200. if (size == 0)
  201. return 0;
  202. /* drop some data */
  203. if (size < write_index)
  204. write_index = size;
  205. if (rb->buffer_size - rb->write_index > write_index)
  206. {
  207. /* this should not cause overflow because there is enough space for
  208. * length of data in current mirror */
  209. rb->write_index += write_index;
  210. return write_index;
  211. }
  212. /* we are going into the other side of the mirror */
  213. rb->write_mirror = ~rb->write_mirror;
  214. rb->write_index = write_index - (rb->buffer_size - rb->write_index);
  215. return write_index;
  216. }
  217. /**
  218. * @brief Serial polling receive data routine, This function will receive data
  219. * in a continuous loop by one by one byte.
  220. * @param dev The pointer of device driver structure
  221. * @param pos Empty parameter.
  222. * @param buffer Receive data buffer.
  223. * @param size Receive data buffer length.
  224. * @return Return the final length of data received.
  225. */
  226. rt_size_t _serial_poll_rx(struct rt_device *dev,
  227. rt_off_t pos,
  228. void *buffer,
  229. rt_size_t size)
  230. {
  231. struct rt_serial_device *serial;
  232. rt_size_t getc_size;
  233. int getc_element; /* Gets one byte of data received */
  234. rt_uint8_t *getc_buffer; /* Pointer to the receive data buffer */
  235. RT_ASSERT(dev != RT_NULL);
  236. serial = (struct rt_serial_device *)dev;
  237. RT_ASSERT(serial != RT_NULL);
  238. getc_buffer = (rt_uint8_t *)buffer;
  239. getc_size = size;
  240. while(size)
  241. {
  242. getc_element = serial->ops->getc(serial);
  243. if (getc_element == -1) break;
  244. *getc_buffer = getc_element;
  245. ++ getc_buffer;
  246. -- size;
  247. if (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM)
  248. {
  249. /* If open_flag satisfies RT_DEVICE_FLAG_STREAM
  250. * and the received character is '\n', exit the loop directly */
  251. if (getc_element == '\n') break;
  252. }
  253. }
  254. return getc_size - size;
  255. }
  256. /**
  257. * @brief Serial polling transmit data routines, This function will transmit
  258. * data in a continuous loop by one by one byte.
  259. * @param dev The pointer of device driver structure
  260. * @param pos Empty parameter.
  261. * @param buffer Transmit data buffer.
  262. * @param size Transmit data buffer length.
  263. * @return Return the final length of data received.
  264. */
  265. rt_size_t _serial_poll_tx(struct rt_device *dev,
  266. rt_off_t pos,
  267. const void *buffer,
  268. rt_size_t size)
  269. {
  270. struct rt_serial_device *serial;
  271. rt_size_t putc_size;
  272. rt_uint8_t *putc_buffer; /* Pointer to the transmit data buffer */
  273. RT_ASSERT(dev != RT_NULL);
  274. serial = (struct rt_serial_device *)dev;
  275. RT_ASSERT(serial != RT_NULL);
  276. putc_buffer = (rt_uint8_t *)buffer;
  277. putc_size = size;
  278. while (size)
  279. {
  280. if (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM)
  281. {
  282. /* If open_flag satisfies RT_DEVICE_FLAG_STREAM and the received character is '\n',
  283. * inserts '\r' character before '\n' character for the effect of carriage return newline */
  284. if (*putc_buffer == '\n')
  285. serial->ops->putc(serial, '\r');
  286. }
  287. serial->ops->putc(serial, *putc_buffer);
  288. ++ putc_buffer;
  289. -- size;
  290. }
  291. return putc_size - size;
  292. }
  293. /**
  294. * @brief Serial receive data routines, This function will receive
  295. * data by using fifo
  296. * @param dev The pointer of device driver structure
  297. * @param pos Empty parameter.
  298. * @param buffer Receive data buffer.
  299. * @param size Receive data buffer length.
  300. * @return Return the final length of data received.
  301. */
  302. static rt_size_t _serial_fifo_rx(struct rt_device *dev,
  303. rt_off_t pos,
  304. void *buffer,
  305. rt_size_t size)
  306. {
  307. struct rt_serial_device *serial;
  308. struct rt_serial_rx_fifo *rx_fifo;
  309. rt_base_t level;
  310. rt_size_t recv_len; /* The length of data from the ringbuffer */
  311. RT_ASSERT(dev != RT_NULL);
  312. if (size == 0) return 0;
  313. serial = (struct rt_serial_device *)dev;
  314. RT_ASSERT((serial != RT_NULL) && (buffer != RT_NULL));
  315. rx_fifo = (struct rt_serial_rx_fifo *) serial->serial_rx;
  316. if (dev->open_flag & RT_SERIAL_RX_BLOCKING)
  317. {
  318. if (size > serial->config.rx_bufsz)
  319. {
  320. LOG_W("(%s) serial device received data:[%d] larger than "
  321. "rx_bufsz:[%d], please increase the BSP_UARTx_RX_BUFSIZE option",
  322. dev->parent.name, size, serial->config.rx_bufsz);
  323. return 0;
  324. }
  325. /* Get the length of the data from the ringbuffer */
  326. recv_len = rt_ringbuffer_data_len(&(rx_fifo->rb));
  327. if (recv_len < size)
  328. {
  329. /* When recv_len is less than size, rx_cpt_index is updated to the size
  330. * and rt_current_thread is suspend until rx_cpt_index is equal to 0 */
  331. rx_fifo->rx_cpt_index = size;
  332. rt_completion_wait(&(rx_fifo->rx_cpt), RT_WAITING_FOREVER);
  333. }
  334. }
  335. /* This part of the code is open_flag as RT_SERIAL_RX_NON_BLOCKING */
  336. level = rt_hw_interrupt_disable();
  337. /* When open_flag is RT_SERIAL_RX_NON_BLOCKING,
  338. * the data is retrieved directly from the ringbuffer and returned */
  339. recv_len = rt_ringbuffer_get(&(rx_fifo->rb), buffer, size);
  340. rt_hw_interrupt_enable(level);
  341. return recv_len;
  342. }
  343. /**
  344. * @brief Serial transmit data routines, This function will transmit
  345. * data by using blocking_nbuf.
  346. * @param dev The pointer of device driver structure
  347. * @param pos Empty parameter.
  348. * @param buffer Transmit data buffer.
  349. * @param size Transmit data buffer length.
  350. * @return Return the final length of data transmit.
  351. */
  352. static rt_size_t _serial_fifo_tx_blocking_nbuf(struct rt_device *dev,
  353. rt_off_t pos,
  354. const void *buffer,
  355. rt_size_t size)
  356. {
  357. struct rt_serial_device *serial;
  358. struct rt_serial_tx_fifo *tx_fifo = RT_NULL;
  359. RT_ASSERT(dev != RT_NULL);
  360. if (size == 0) return 0;
  361. serial = (struct rt_serial_device *)dev;
  362. RT_ASSERT((serial != RT_NULL) && (buffer != RT_NULL));
  363. tx_fifo = (struct rt_serial_tx_fifo *) serial->serial_tx;
  364. RT_ASSERT(tx_fifo != RT_NULL);
  365. /* When serial transmit in tx_blocking mode,
  366. * if the activated mode is RT_TRUE, it will return directly */
  367. if (tx_fifo->activated == RT_TRUE) return 0;
  368. tx_fifo->activated = RT_TRUE;
  369. /* Call the transmit interface for transmission */
  370. serial->ops->transmit(serial,
  371. (rt_uint8_t *)buffer,
  372. size,
  373. RT_SERIAL_TX_BLOCKING);
  374. /* Waiting for the transmission to complete */
  375. rt_completion_wait(&(tx_fifo->tx_cpt), RT_WAITING_FOREVER);
  376. return size;
  377. }
  378. /**
  379. * @brief Serial transmit data routines, This function will transmit
  380. * data by using blocking_buf.
  381. * @param dev The pointer of device driver structure
  382. * @param pos Empty parameter.
  383. * @param buffer Transmit data buffer.
  384. * @param size Transmit data buffer length.
  385. * @return Return the final length of data transmit.
  386. */
  387. static rt_size_t _serial_fifo_tx_blocking_buf(struct rt_device *dev,
  388. rt_off_t pos,
  389. const void *buffer,
  390. rt_size_t size)
  391. {
  392. struct rt_serial_device *serial;
  393. struct rt_serial_tx_fifo *tx_fifo = RT_NULL;
  394. rt_size_t length = size;
  395. rt_size_t offset = 0;
  396. if (size == 0) return 0;
  397. RT_ASSERT(dev != RT_NULL);
  398. serial = (struct rt_serial_device *)dev;
  399. RT_ASSERT((serial != RT_NULL) && (buffer != RT_NULL));
  400. tx_fifo = (struct rt_serial_tx_fifo *) serial->serial_tx;
  401. RT_ASSERT(tx_fifo != RT_NULL);
  402. if (rt_thread_self() == RT_NULL || (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM))
  403. {
  404. /* using poll tx when the scheduler not startup or in stream mode */
  405. return _serial_poll_tx(dev, pos, buffer, size);
  406. }
  407. /* When serial transmit in tx_blocking mode,
  408. * if the activated mode is RT_TRUE, it will return directly */
  409. if (tx_fifo->activated == RT_TRUE) return 0;
  410. tx_fifo->activated = RT_TRUE;
  411. while (size)
  412. {
  413. /* Copy one piece of data into the ringbuffer at a time
  414. * until the length of the data is equal to size */
  415. tx_fifo->put_size = rt_ringbuffer_put(&(tx_fifo->rb),
  416. (rt_uint8_t *)buffer + offset,
  417. size);
  418. offset += tx_fifo->put_size;
  419. size -= tx_fifo->put_size;
  420. /* Call the transmit interface for transmission */
  421. serial->ops->transmit(serial,
  422. (rt_uint8_t *)buffer + offset,
  423. tx_fifo->put_size,
  424. RT_SERIAL_TX_BLOCKING);
  425. /* Waiting for the transmission to complete */
  426. rt_completion_wait(&(tx_fifo->tx_cpt), RT_WAITING_FOREVER);
  427. }
  428. return length;
  429. }
  430. /**
  431. * @brief Serial transmit data routines, This function will transmit
  432. * data by using nonblocking.
  433. * @param dev The pointer of device driver structure
  434. * @param pos Empty parameter.
  435. * @param buffer Transmit data buffer.
  436. * @param size Transmit data buffer length.
  437. * @return Return the final length of data transmit.
  438. */
  439. static rt_size_t _serial_fifo_tx_nonblocking(struct rt_device *dev,
  440. rt_off_t pos,
  441. const void *buffer,
  442. rt_size_t size)
  443. {
  444. struct rt_serial_device *serial;
  445. struct rt_serial_tx_fifo *tx_fifo;
  446. rt_base_t level;
  447. rt_size_t length;
  448. RT_ASSERT(dev != RT_NULL);
  449. if (size == 0) return 0;
  450. serial = (struct rt_serial_device *)dev;
  451. RT_ASSERT((serial != RT_NULL) && (buffer != RT_NULL));
  452. tx_fifo = (struct rt_serial_tx_fifo *) serial->serial_tx;
  453. level = rt_hw_interrupt_disable();
  454. if (tx_fifo->activated == RT_FALSE)
  455. {
  456. /* When serial transmit in tx_non_blocking mode, if the activated mode is RT_FALSE,
  457. * start copying data into the ringbuffer */
  458. tx_fifo->activated = RT_TRUE;
  459. /* Copying data into the ringbuffer */
  460. length = rt_ringbuffer_put(&(tx_fifo->rb), buffer, size);
  461. rt_hw_interrupt_enable(level);
  462. rt_uint8_t *put_ptr = RT_NULL;
  463. /* Get the linear length buffer from rinbuffer */
  464. tx_fifo->put_size = rt_serial_get_linear_buffer(&(tx_fifo->rb), &put_ptr);
  465. /* Call the transmit interface for transmission */
  466. serial->ops->transmit(serial,
  467. put_ptr,
  468. tx_fifo->put_size,
  469. RT_SERIAL_TX_NON_BLOCKING);
  470. /* In tx_nonblocking mode, there is no need to call rt_completion_wait() APIs to wait
  471. * for the rt_current_thread to resume */
  472. return length;
  473. }
  474. /* If the activated mode is RT_FALSE, it means that serial device is transmitting,
  475. * where only the data in the ringbuffer and there is no need to call the transmit() API.
  476. * Note that this part of the code requires disable interrupts
  477. * to prevent multi thread reentrant */
  478. /* Copying data into the ringbuffer */
  479. length = rt_ringbuffer_put(&(tx_fifo->rb), buffer, size);
  480. rt_hw_interrupt_enable(level);
  481. return length;
  482. }
  483. /**
  484. * @brief Enable serial transmit mode.
  485. * @param dev The pointer of device driver structure
  486. * @param rx_oflag The flag of that the serial port opens.
  487. * @return Return the status of the operation.
  488. */
  489. static rt_err_t rt_serial_tx_enable(struct rt_device *dev,
  490. rt_uint16_t tx_oflag)
  491. {
  492. struct rt_serial_device *serial;
  493. struct rt_serial_tx_fifo *tx_fifo = RT_NULL;
  494. RT_ASSERT(dev != RT_NULL);
  495. serial = (struct rt_serial_device *)dev;
  496. if (serial->config.tx_bufsz == 0)
  497. {
  498. /* Cannot use RT_SERIAL_TX_NON_BLOCKING when tx_bufsz is 0 */
  499. if (tx_oflag == RT_SERIAL_TX_NON_BLOCKING)
  500. {
  501. LOG_E("(%s) serial device with misconfigure: tx_bufsz = 0",
  502. dev->parent.name);
  503. return -RT_EINVAL;
  504. }
  505. #ifndef RT_USING_DEVICE_OPS
  506. dev->write = _serial_poll_tx;
  507. #endif
  508. dev->open_flag |= RT_SERIAL_TX_BLOCKING;
  509. return RT_EOK;
  510. }
  511. /* Limits the minimum value of tx_bufsz */
  512. if (serial->config.tx_bufsz < RT_SERIAL_TX_MINBUFSZ)
  513. serial->config.tx_bufsz = RT_SERIAL_TX_MINBUFSZ;
  514. if (tx_oflag == RT_SERIAL_TX_BLOCKING)
  515. {
  516. /* When using RT_SERIAL_TX_BLOCKING, it is necessary to determine
  517. * whether serial device needs to use buffer */
  518. rt_err_t optmode; /* The operating mode used by serial device */
  519. /* Call the Control() API to get the operating mode */
  520. optmode = serial->ops->control(serial,
  521. RT_DEVICE_CHECK_OPTMODE,
  522. (void *)RT_DEVICE_FLAG_TX_BLOCKING);
  523. if (optmode == RT_SERIAL_TX_BLOCKING_BUFFER)
  524. {
  525. /* If use RT_SERIAL_TX_BLOCKING_BUFFER, the ringbuffer is initialized */
  526. tx_fifo = (struct rt_serial_tx_fifo *) rt_malloc
  527. (sizeof(struct rt_serial_tx_fifo) + serial->config.tx_bufsz);
  528. RT_ASSERT(tx_fifo != RT_NULL);
  529. rt_ringbuffer_init(&(tx_fifo->rb),
  530. tx_fifo->buffer,
  531. serial->config.tx_bufsz);
  532. serial->serial_tx = tx_fifo;
  533. #ifndef RT_USING_DEVICE_OPS
  534. dev->write = _serial_fifo_tx_blocking_buf;
  535. #endif
  536. }
  537. else
  538. {
  539. /* If not use RT_SERIAL_TX_BLOCKING_BUFFER,
  540. * the control() API is called to configure the serial device */
  541. tx_fifo = (struct rt_serial_tx_fifo*) rt_malloc
  542. (sizeof(struct rt_serial_tx_fifo));
  543. RT_ASSERT(tx_fifo != RT_NULL);
  544. serial->serial_tx = tx_fifo;
  545. #ifndef RT_USING_DEVICE_OPS
  546. dev->write = _serial_fifo_tx_blocking_nbuf;
  547. #endif
  548. /* Call the control() API to configure the serial device by RT_SERIAL_TX_BLOCKING*/
  549. serial->ops->control(serial,
  550. RT_DEVICE_CTRL_CONFIG,
  551. (void *)RT_SERIAL_TX_BLOCKING);
  552. }
  553. tx_fifo->activated = RT_FALSE;
  554. tx_fifo->put_size = 0;
  555. rt_completion_init(&(tx_fifo->tx_cpt));
  556. dev->open_flag |= RT_SERIAL_TX_BLOCKING;
  557. return RT_EOK;
  558. }
  559. /* When using RT_SERIAL_TX_NON_BLOCKING, ringbuffer needs to be initialized,
  560. * and initialize the tx_fifo->activated value is RT_FALSE.
  561. */
  562. tx_fifo = (struct rt_serial_tx_fifo *) rt_malloc
  563. (sizeof(struct rt_serial_tx_fifo) + serial->config.tx_bufsz);
  564. RT_ASSERT(tx_fifo != RT_NULL);
  565. tx_fifo->activated = RT_FALSE;
  566. tx_fifo->put_size = 0;
  567. rt_ringbuffer_init(&(tx_fifo->rb),
  568. tx_fifo->buffer,
  569. serial->config.tx_bufsz);
  570. serial->serial_tx = tx_fifo;
  571. #ifndef RT_USING_DEVICE_OPS
  572. dev->write = _serial_fifo_tx_nonblocking;
  573. #endif
  574. dev->open_flag |= RT_SERIAL_TX_NON_BLOCKING;
  575. /* Call the control() API to configure the serial device by RT_SERIAL_TX_NON_BLOCKING*/
  576. serial->ops->control(serial,
  577. RT_DEVICE_CTRL_CONFIG,
  578. (void *)RT_SERIAL_TX_NON_BLOCKING);
  579. return RT_EOK;
  580. }
  581. /**
  582. * @brief Enable serial receive mode.
  583. * @param dev The pointer of device driver structure
  584. * @param rx_oflag The flag of that the serial port opens.
  585. * @return Return the status of the operation.
  586. */
  587. static rt_err_t rt_serial_rx_enable(struct rt_device *dev,
  588. rt_uint16_t rx_oflag)
  589. {
  590. struct rt_serial_device *serial;
  591. struct rt_serial_rx_fifo *rx_fifo = RT_NULL;
  592. RT_ASSERT(dev != RT_NULL);
  593. serial = (struct rt_serial_device *)dev;
  594. if (serial->config.rx_bufsz == 0)
  595. {
  596. /* Cannot use RT_SERIAL_RX_NON_BLOCKING when rx_bufsz is 0 */
  597. if (rx_oflag == RT_SERIAL_RX_NON_BLOCKING)
  598. {
  599. LOG_E("(%s) serial device with misconfigure: rx_bufsz = 0",
  600. dev->parent.name);
  601. return -RT_EINVAL;
  602. }
  603. #ifndef RT_USING_DEVICE_OPS
  604. dev->read = _serial_poll_rx;
  605. #endif
  606. dev->open_flag |= RT_SERIAL_RX_BLOCKING;
  607. return RT_EOK;
  608. }
  609. /* Limits the minimum value of rx_bufsz */
  610. if (serial->config.rx_bufsz < RT_SERIAL_RX_MINBUFSZ)
  611. serial->config.rx_bufsz = RT_SERIAL_RX_MINBUFSZ;
  612. rx_fifo = (struct rt_serial_rx_fifo *) rt_malloc
  613. (sizeof(struct rt_serial_rx_fifo) + serial->config.rx_bufsz);
  614. RT_ASSERT(rx_fifo != RT_NULL);
  615. rt_ringbuffer_init(&(rx_fifo->rb), rx_fifo->buffer, serial->config.rx_bufsz);
  616. serial->serial_rx = rx_fifo;
  617. #ifndef RT_USING_DEVICE_OPS
  618. dev->read = _serial_fifo_rx;
  619. #endif
  620. if (rx_oflag == RT_SERIAL_RX_NON_BLOCKING)
  621. {
  622. dev->open_flag |= RT_SERIAL_RX_NON_BLOCKING;
  623. /* Call the control() API to configure the serial device by RT_SERIAL_RX_NON_BLOCKING*/
  624. serial->ops->control(serial,
  625. RT_DEVICE_CTRL_CONFIG,
  626. (void *) RT_SERIAL_RX_NON_BLOCKING);
  627. return RT_EOK;
  628. }
  629. /* When using RT_SERIAL_RX_BLOCKING, rt_completion_init() and rx_cpt_index are initialized */
  630. rx_fifo->rx_cpt_index = 0;
  631. rt_completion_init(&(rx_fifo->rx_cpt));
  632. dev->open_flag |= RT_SERIAL_RX_BLOCKING;
  633. /* Call the control() API to configure the serial device by RT_SERIAL_RX_BLOCKING*/
  634. serial->ops->control(serial,
  635. RT_DEVICE_CTRL_CONFIG,
  636. (void *) RT_SERIAL_RX_BLOCKING);
  637. return RT_EOK;
  638. }
  639. /**
  640. * @brief Disable serial receive mode.
  641. * @param dev The pointer of device driver structure
  642. * @param rx_oflag The flag of that the serial port opens.
  643. * @return Return the status of the operation.
  644. */
  645. static rt_err_t rt_serial_rx_disable(struct rt_device *dev,
  646. rt_uint16_t rx_oflag)
  647. {
  648. struct rt_serial_device *serial;
  649. struct rt_serial_rx_fifo *rx_fifo;
  650. RT_ASSERT(dev != RT_NULL);
  651. serial = (struct rt_serial_device *)dev;
  652. #ifndef RT_USING_DEVICE_OPS
  653. dev->read = RT_NULL;
  654. #endif
  655. if (serial->serial_rx == RT_NULL) return RT_EOK;
  656. do
  657. {
  658. if (rx_oflag == RT_SERIAL_RX_NON_BLOCKING)
  659. {
  660. dev->open_flag &= ~ RT_SERIAL_RX_NON_BLOCKING;
  661. serial->ops->control(serial,
  662. RT_DEVICE_CTRL_CLR_INT,
  663. (void *)RT_SERIAL_RX_NON_BLOCKING);
  664. break;
  665. }
  666. dev->open_flag &= ~ RT_SERIAL_RX_BLOCKING;
  667. serial->ops->control(serial,
  668. RT_DEVICE_CTRL_CLR_INT,
  669. (void *)RT_SERIAL_RX_BLOCKING);
  670. } while (0);
  671. rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
  672. RT_ASSERT(rx_fifo != RT_NULL);
  673. rt_free(rx_fifo);
  674. serial->serial_rx = RT_NULL;
  675. return RT_EOK;
  676. }
  677. /**
  678. * @brief Disable serial tranmit mode.
  679. * @param dev The pointer of device driver structure
  680. * @param rx_oflag The flag of that the serial port opens.
  681. * @return Return the status of the operation.
  682. */
  683. static rt_err_t rt_serial_tx_disable(struct rt_device *dev,
  684. rt_uint16_t tx_oflag)
  685. {
  686. struct rt_serial_device *serial;
  687. struct rt_serial_tx_fifo *tx_fifo;
  688. RT_ASSERT(dev != RT_NULL);
  689. serial = (struct rt_serial_device *)dev;
  690. #ifndef RT_USING_DEVICE_OPS
  691. dev->write = RT_NULL;
  692. #endif
  693. if (serial->serial_tx == RT_NULL) return RT_EOK;
  694. tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
  695. RT_ASSERT(tx_fifo != RT_NULL);
  696. do
  697. {
  698. if (tx_oflag == RT_SERIAL_TX_NON_BLOCKING)
  699. {
  700. dev->open_flag &= ~ RT_SERIAL_TX_NON_BLOCKING;
  701. serial->ops->control(serial,
  702. RT_DEVICE_CTRL_CLR_INT,
  703. (void *)RT_SERIAL_TX_NON_BLOCKING);
  704. break;
  705. }
  706. rt_completion_done(&(tx_fifo->tx_cpt));
  707. dev->open_flag &= ~ RT_SERIAL_TX_BLOCKING;
  708. serial->ops->control(serial,
  709. RT_DEVICE_CTRL_CLR_INT,
  710. (void *)RT_SERIAL_TX_BLOCKING);
  711. } while (0);
  712. rt_free(tx_fifo);
  713. serial->serial_tx = RT_NULL;
  714. return RT_EOK;
  715. }
  716. /**
  717. * @brief Initialize the serial device.
  718. * @param dev The pointer of device driver structure
  719. * @return Return the status of the operation.
  720. */
  721. static rt_err_t rt_serial_init(struct rt_device *dev)
  722. {
  723. rt_err_t result = RT_EOK;
  724. struct rt_serial_device *serial;
  725. RT_ASSERT(dev != RT_NULL);
  726. serial = (struct rt_serial_device *)dev;
  727. /* initialize rx/tx */
  728. serial->serial_rx = RT_NULL;
  729. serial->serial_tx = RT_NULL;
  730. /* apply configuration */
  731. if (serial->ops->configure)
  732. result = serial->ops->configure(serial, &serial->config);
  733. return result;
  734. }
  735. /**
  736. * @brief Open the serial device.
  737. * @param dev The pointer of device driver structure
  738. * @param oflag The flag of that the serial port opens.
  739. * @return Return the status of the operation.
  740. */
  741. static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
  742. {
  743. struct rt_serial_device *serial;
  744. RT_ASSERT(dev != RT_NULL);
  745. serial = (struct rt_serial_device *)dev;
  746. /* Check that the device has been turned on */
  747. if ((dev->open_flag) & (15 << 12))
  748. {
  749. LOG_D("(%s) serial device has already been opened, it will run in its original configuration", dev->parent.name);
  750. return RT_EOK;
  751. }
  752. LOG_D("open serial device: 0x%08x with open flag: 0x%04x",
  753. dev, oflag);
  754. /* By default, the receive mode of a serial devide is RT_SERIAL_RX_NON_BLOCKING */
  755. if ((oflag & RT_SERIAL_RX_BLOCKING) == RT_SERIAL_RX_BLOCKING)
  756. dev->open_flag |= RT_SERIAL_RX_BLOCKING;
  757. else
  758. dev->open_flag |= RT_SERIAL_RX_NON_BLOCKING;
  759. /* By default, the transmit mode of a serial devide is RT_SERIAL_TX_BLOCKING */
  760. if ((oflag & RT_SERIAL_TX_NON_BLOCKING) == RT_SERIAL_TX_NON_BLOCKING)
  761. dev->open_flag |= RT_SERIAL_TX_NON_BLOCKING;
  762. else
  763. dev->open_flag |= RT_SERIAL_TX_BLOCKING;
  764. /* set steam flag */
  765. if ((oflag & RT_DEVICE_FLAG_STREAM) ||
  766. (dev->open_flag & RT_DEVICE_FLAG_STREAM))
  767. dev->open_flag |= RT_DEVICE_FLAG_STREAM;
  768. /* initialize the Rx structure according to open flag */
  769. if (serial->serial_rx == RT_NULL)
  770. rt_serial_rx_enable(dev, dev->open_flag &
  771. (RT_SERIAL_RX_BLOCKING | RT_SERIAL_RX_NON_BLOCKING));
  772. /* initialize the Tx structure according to open flag */
  773. if (serial->serial_tx == RT_NULL)
  774. rt_serial_tx_enable(dev, dev->open_flag &
  775. (RT_SERIAL_TX_BLOCKING | RT_SERIAL_TX_NON_BLOCKING));
  776. return RT_EOK;
  777. }
  778. /**
  779. * @brief Close the serial device.
  780. * @param dev The pointer of device driver structure
  781. * @return Return the status of the operation.
  782. */
  783. static rt_err_t rt_serial_close(struct rt_device *dev)
  784. {
  785. struct rt_serial_device *serial;
  786. RT_ASSERT(dev != RT_NULL);
  787. serial = (struct rt_serial_device *)dev;
  788. /* this device has more reference count */
  789. if (dev->ref_count > 1) return -RT_ERROR;
  790. /* Disable serial receive mode. */
  791. rt_serial_rx_disable(dev, dev->open_flag &
  792. (RT_SERIAL_RX_BLOCKING | RT_SERIAL_RX_NON_BLOCKING));
  793. /* Disable serial tranmit mode. */
  794. rt_serial_tx_disable(dev, dev->open_flag &
  795. (RT_SERIAL_TX_BLOCKING | RT_SERIAL_TX_NON_BLOCKING));
  796. /* Clear the callback function */
  797. serial->parent.rx_indicate = RT_NULL;
  798. serial->parent.tx_complete = RT_NULL;
  799. /* Call the control() API to close the serial device */
  800. serial->ops->control(serial, RT_DEVICE_CTRL_CLOSE, RT_NULL);
  801. dev->flag &= ~RT_DEVICE_FLAG_ACTIVATED;
  802. return RT_EOK;
  803. }
  804. /**
  805. * @brief Control the serial device.
  806. * @param dev The pointer of device driver structure
  807. * @param cmd The command value that controls the serial device
  808. * @param args The parameter value that controls the serial device
  809. * @return Return the status of the operation.
  810. */
  811. static rt_err_t rt_serial_control(struct rt_device *dev,
  812. int cmd,
  813. void *args)
  814. {
  815. rt_err_t ret = RT_EOK;
  816. struct rt_serial_device *serial;
  817. RT_ASSERT(dev != RT_NULL);
  818. serial = (struct rt_serial_device *)dev;
  819. switch (cmd)
  820. {
  821. case RT_DEVICE_CTRL_SUSPEND:
  822. /* suspend device */
  823. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  824. break;
  825. case RT_DEVICE_CTRL_RESUME:
  826. /* resume device */
  827. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  828. break;
  829. case RT_DEVICE_CTRL_CONFIG:
  830. if (args != RT_NULL)
  831. {
  832. struct serial_configure *pconfig = (struct serial_configure *) args;
  833. if (serial->parent.ref_count)
  834. {
  835. /*can not change buffer size*/
  836. return -RT_EBUSY;
  837. }
  838. /* set serial configure */
  839. serial->config = *pconfig;
  840. serial->ops->configure(serial,
  841. (struct serial_configure *) args);
  842. }
  843. break;
  844. default :
  845. /* control device */
  846. ret = serial->ops->control(serial, cmd, args);
  847. break;
  848. }
  849. return ret;
  850. }
  851. #ifdef RT_USING_DEVICE_OPS
  852. static rt_size_t rt_serial_read(struct rt_device *dev,
  853. rt_off_t pos,
  854. void *buffer,
  855. rt_size_t size)
  856. {
  857. struct rt_serial_device *serial;
  858. RT_ASSERT(dev != RT_NULL);
  859. if (size == 0) return 0;
  860. serial = (struct rt_serial_device *)dev;
  861. if (serial->config.rx_bufsz)
  862. {
  863. return _serial_fifo_rx(dev, pos, buffer, size);
  864. }
  865. return _serial_poll_rx(dev, pos, buffer, size);
  866. }
  867. static rt_size_t rt_serial_write(struct rt_device *dev,
  868. rt_off_t pos,
  869. const void *buffer,
  870. rt_size_t size)
  871. {
  872. struct rt_serial_device *serial;
  873. struct rt_serial_tx_fifo *tx_fifo;
  874. RT_ASSERT(dev != RT_NULL);
  875. if (size == 0) return 0;
  876. serial = (struct rt_serial_device *)dev;
  877. RT_ASSERT((serial != RT_NULL) && (buffer != RT_NULL));
  878. tx_fifo = (struct rt_serial_tx_fifo *) serial->serial_tx;
  879. if (serial->config.tx_bufsz == 0)
  880. {
  881. return _serial_poll_tx(dev, pos, buffer, size);
  882. }
  883. if (dev->open_flag | RT_SERIAL_TX_BLOCKING)
  884. {
  885. if ((tx_fifo->rb.buffer_ptr) == RT_NULL)
  886. {
  887. return _serial_fifo_tx_blocking_nbuf(dev, pos, buffer, size);
  888. }
  889. return _serial_fifo_tx_blocking_buf(dev, pos, buffer, size);
  890. }
  891. return _serial_fifo_tx_nonblocking(dev, pos, buffer, size);
  892. }
  893. const static struct rt_device_ops serial_ops =
  894. {
  895. rt_serial_init,
  896. rt_serial_open,
  897. rt_serial_close,
  898. rt_serial_read,
  899. rt_serial_write,
  900. rt_serial_control
  901. };
  902. #endif
  903. /**
  904. * @brief Register the serial device.
  905. * @param serial RT-thread serial device.
  906. * @param name The device driver's name
  907. * @param flag The capabilities flag of device.
  908. * @param data The device driver's data.
  909. * @return Return the status of the operation.
  910. */
  911. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
  912. const char *name,
  913. rt_uint32_t flag,
  914. void *data)
  915. {
  916. rt_err_t ret;
  917. struct rt_device *device;
  918. RT_ASSERT(serial != RT_NULL);
  919. device = &(serial->parent);
  920. device->type = RT_Device_Class_Char;
  921. device->rx_indicate = RT_NULL;
  922. device->tx_complete = RT_NULL;
  923. #ifdef RT_USING_DEVICE_OPS
  924. device->ops = &serial_ops;
  925. #else
  926. device->init = rt_serial_init;
  927. device->open = rt_serial_open;
  928. device->close = rt_serial_close;
  929. device->read = RT_NULL;
  930. device->write = RT_NULL;
  931. device->control = rt_serial_control;
  932. #endif
  933. device->user_data = data;
  934. /* register a character device */
  935. ret = rt_device_register(device, name, flag);
  936. #ifdef RT_USING_POSIX_STDIO
  937. /* set fops */
  938. device->fops = &_serial_fops;
  939. #endif
  940. return ret;
  941. }
  942. /**
  943. * @brief ISR for serial interrupt
  944. * @param serial RT-thread serial device.
  945. * @param event ISR event type.
  946. */
  947. void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
  948. {
  949. RT_ASSERT(serial != RT_NULL);
  950. switch (event & 0xff)
  951. {
  952. /* Interrupt receive event */
  953. case RT_SERIAL_EVENT_RX_IND:
  954. case RT_SERIAL_EVENT_RX_DMADONE:
  955. {
  956. struct rt_serial_rx_fifo *rx_fifo;
  957. rt_size_t rx_length = 0;
  958. rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
  959. RT_ASSERT(rx_fifo != RT_NULL);
  960. /* If the event is RT_SERIAL_EVENT_RX_IND, rx_length is equal to 0 */
  961. rx_length = (event & (~0xff)) >> 8;
  962. if (rx_length)
  963. rt_serial_update_write_index(&(rx_fifo->rb), rx_length);
  964. /* Get the length of the data from the ringbuffer */
  965. rx_length = rt_ringbuffer_data_len(&rx_fifo->rb);
  966. if (rx_length == 0) break;
  967. if (serial->parent.open_flag & RT_SERIAL_RX_BLOCKING)
  968. {
  969. if (rx_fifo->rx_cpt_index && rx_length >= rx_fifo->rx_cpt_index )
  970. {
  971. rx_fifo->rx_cpt_index = 0;
  972. rt_completion_done(&(rx_fifo->rx_cpt));
  973. }
  974. }
  975. /* Trigger the receiving completion callback */
  976. if (serial->parent.rx_indicate != RT_NULL)
  977. serial->parent.rx_indicate(&(serial->parent), rx_length);
  978. break;
  979. }
  980. /* Interrupt transmit event */
  981. case RT_SERIAL_EVENT_TX_DONE:
  982. {
  983. struct rt_serial_tx_fifo *tx_fifo;
  984. rt_size_t tx_length = 0;
  985. tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
  986. RT_ASSERT(tx_fifo != RT_NULL);
  987. /* Get the length of the data from the ringbuffer */
  988. tx_length = rt_ringbuffer_data_len(&tx_fifo->rb);
  989. /* If there is no data in tx_ringbuffer,
  990. * then the transmit completion callback is triggered*/
  991. if (tx_length == 0)
  992. {
  993. tx_fifo->activated = RT_FALSE;
  994. /* Trigger the transmit completion callback */
  995. if (serial->parent.tx_complete != RT_NULL)
  996. serial->parent.tx_complete(&serial->parent, RT_NULL);
  997. if (serial->parent.open_flag & RT_SERIAL_TX_BLOCKING)
  998. rt_completion_done(&(tx_fifo->tx_cpt));
  999. break;
  1000. }
  1001. /* Call the transmit interface for transmission again */
  1002. /* Note that in interrupt mode, tx_fifo->buffer and tx_length
  1003. * are inactive parameters */
  1004. serial->ops->transmit(serial,
  1005. tx_fifo->buffer,
  1006. tx_length,
  1007. serial->parent.open_flag & ( \
  1008. RT_SERIAL_TX_BLOCKING | \
  1009. RT_SERIAL_TX_NON_BLOCKING));
  1010. break;
  1011. }
  1012. case RT_SERIAL_EVENT_TX_DMADONE:
  1013. {
  1014. struct rt_serial_tx_fifo *tx_fifo;
  1015. tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
  1016. RT_ASSERT(tx_fifo != RT_NULL);
  1017. tx_fifo->activated = RT_FALSE;
  1018. /* Trigger the transmit completion callback */
  1019. if (serial->parent.tx_complete != RT_NULL)
  1020. serial->parent.tx_complete(&serial->parent, RT_NULL);
  1021. if (serial->parent.open_flag & RT_SERIAL_TX_BLOCKING)
  1022. {
  1023. rt_completion_done(&(tx_fifo->tx_cpt));
  1024. break;
  1025. }
  1026. rt_serial_update_read_index(&tx_fifo->rb, tx_fifo->put_size);
  1027. /* Get the length of the data from the ringbuffer.
  1028. * If there is some data in tx_ringbuffer,
  1029. * then call the transmit interface for transmission again */
  1030. if (rt_ringbuffer_data_len(&tx_fifo->rb))
  1031. {
  1032. tx_fifo->activated = RT_TRUE;
  1033. rt_uint8_t *put_ptr = RT_NULL;
  1034. /* Get the linear length buffer from rinbuffer */
  1035. tx_fifo->put_size = rt_serial_get_linear_buffer(&(tx_fifo->rb), &put_ptr);
  1036. /* Call the transmit interface for transmission again */
  1037. serial->ops->transmit(serial,
  1038. put_ptr,
  1039. tx_fifo->put_size,
  1040. RT_SERIAL_TX_NON_BLOCKING);
  1041. }
  1042. break;
  1043. }
  1044. default:
  1045. break;
  1046. }
  1047. }