serial_v2.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  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_ASSERT(dev != RT_NULL);
  395. if (size == 0) return 0;
  396. serial = (struct rt_serial_device *)dev;
  397. RT_ASSERT((serial != RT_NULL) && (buffer != RT_NULL));
  398. tx_fifo = (struct rt_serial_tx_fifo *) serial->serial_tx;
  399. RT_ASSERT(tx_fifo != RT_NULL);
  400. /* When serial transmit in tx_blocking mode,
  401. * if the activated mode is RT_TRUE, it will return directly */
  402. if (tx_fifo->activated == RT_TRUE) return 0;
  403. tx_fifo->activated = RT_TRUE;
  404. rt_size_t length = size;
  405. rt_size_t offset = 0;
  406. while (size)
  407. {
  408. /* Copy one piece of data into the ringbuffer at a time
  409. * until the length of the data is equal to size */
  410. tx_fifo->put_size = rt_ringbuffer_put(&(tx_fifo->rb),
  411. (rt_uint8_t *)buffer + offset,
  412. size);
  413. offset += tx_fifo->put_size;
  414. size -= tx_fifo->put_size;
  415. /* Call the transmit interface for transmission */
  416. serial->ops->transmit(serial,
  417. (rt_uint8_t *)buffer + offset,
  418. tx_fifo->put_size,
  419. RT_SERIAL_TX_BLOCKING);
  420. /* Waiting for the transmission to complete */
  421. rt_completion_wait(&(tx_fifo->tx_cpt), RT_WAITING_FOREVER);
  422. }
  423. return length;
  424. }
  425. /**
  426. * @brief Serial transmit data routines, This function will transmit
  427. * data by using nonblocking.
  428. * @param dev The pointer of device driver structure
  429. * @param pos Empty parameter.
  430. * @param buffer Transmit data buffer.
  431. * @param size Transmit data buffer length.
  432. * @return Return the final length of data transmit.
  433. */
  434. static rt_size_t _serial_fifo_tx_nonblocking(struct rt_device *dev,
  435. rt_off_t pos,
  436. const void *buffer,
  437. rt_size_t size)
  438. {
  439. struct rt_serial_device *serial;
  440. struct rt_serial_tx_fifo *tx_fifo;
  441. rt_base_t level;
  442. rt_size_t length;
  443. RT_ASSERT(dev != RT_NULL);
  444. if (size == 0) return 0;
  445. serial = (struct rt_serial_device *)dev;
  446. RT_ASSERT((serial != RT_NULL) && (buffer != RT_NULL));
  447. tx_fifo = (struct rt_serial_tx_fifo *) serial->serial_tx;
  448. level = rt_hw_interrupt_disable();
  449. if (tx_fifo->activated == RT_FALSE)
  450. {
  451. /* When serial transmit in tx_non_blocking mode, if the activated mode is RT_FALSE,
  452. * start copying data into the ringbuffer */
  453. tx_fifo->activated = RT_TRUE;
  454. /* Copying data into the ringbuffer */
  455. length = rt_ringbuffer_put(&(tx_fifo->rb), buffer, size);
  456. rt_hw_interrupt_enable(level);
  457. rt_uint8_t *put_ptr = RT_NULL;
  458. /* Get the linear length buffer from rinbuffer */
  459. tx_fifo->put_size = rt_serial_get_linear_buffer(&(tx_fifo->rb), &put_ptr);
  460. /* Call the transmit interface for transmission */
  461. serial->ops->transmit(serial,
  462. put_ptr,
  463. tx_fifo->put_size,
  464. RT_SERIAL_TX_NON_BLOCKING);
  465. /* In tx_nonblocking mode, there is no need to call rt_completion_wait() APIs to wait
  466. * for the rt_current_thread to resume */
  467. return length;
  468. }
  469. /* If the activated mode is RT_FALSE, it means that serial device is transmitting,
  470. * where only the data in the ringbuffer and there is no need to call the transmit() API.
  471. * Note that this part of the code requires disable interrupts
  472. * to prevent multi thread reentrant */
  473. /* Copying data into the ringbuffer */
  474. length = rt_ringbuffer_put(&(tx_fifo->rb), buffer, size);
  475. rt_hw_interrupt_enable(level);
  476. return length;
  477. }
  478. /**
  479. * @brief Enable serial transmit mode.
  480. * @param dev The pointer of device driver structure
  481. * @param rx_oflag The flag of that the serial port opens.
  482. * @return Return the status of the operation.
  483. */
  484. static rt_err_t rt_serial_tx_enable(struct rt_device *dev,
  485. rt_uint16_t tx_oflag)
  486. {
  487. struct rt_serial_device *serial;
  488. struct rt_serial_tx_fifo *tx_fifo = RT_NULL;
  489. RT_ASSERT(dev != RT_NULL);
  490. serial = (struct rt_serial_device *)dev;
  491. if (serial->config.tx_bufsz == 0)
  492. {
  493. /* Cannot use RT_SERIAL_TX_NON_BLOCKING when tx_bufsz is 0 */
  494. if (tx_oflag == RT_SERIAL_TX_NON_BLOCKING)
  495. {
  496. LOG_E("(%s) serial device with misconfigure: tx_bufsz = 0",
  497. dev->parent.name);
  498. return -RT_EINVAL;
  499. }
  500. #ifndef RT_USING_DEVICE_OPS
  501. dev->write = _serial_poll_tx;
  502. #endif
  503. dev->open_flag |= RT_SERIAL_TX_BLOCKING;
  504. return RT_EOK;
  505. }
  506. /* Limits the minimum value of tx_bufsz */
  507. if (serial->config.tx_bufsz < RT_SERIAL_TX_MINBUFSZ)
  508. serial->config.tx_bufsz = RT_SERIAL_TX_MINBUFSZ;
  509. if (tx_oflag == RT_SERIAL_TX_BLOCKING)
  510. {
  511. /* When using RT_SERIAL_TX_BLOCKING, it is necessary to determine
  512. * whether serial device needs to use buffer */
  513. rt_err_t optmode; /* The operating mode used by serial device */
  514. /* Call the Control() API to get the operating mode */
  515. optmode = serial->ops->control(serial,
  516. RT_DEVICE_CHECK_OPTMODE,
  517. (void *)RT_DEVICE_FLAG_TX_BLOCKING);
  518. if (optmode == RT_SERIAL_TX_BLOCKING_BUFFER)
  519. {
  520. /* If use RT_SERIAL_TX_BLOCKING_BUFFER, the ringbuffer is initialized */
  521. tx_fifo = (struct rt_serial_tx_fifo *) rt_malloc
  522. (sizeof(struct rt_serial_tx_fifo) + serial->config.tx_bufsz);
  523. RT_ASSERT(tx_fifo != RT_NULL);
  524. rt_ringbuffer_init(&(tx_fifo->rb),
  525. tx_fifo->buffer,
  526. serial->config.tx_bufsz);
  527. serial->serial_tx = tx_fifo;
  528. #ifndef RT_USING_DEVICE_OPS
  529. dev->write = _serial_fifo_tx_blocking_buf;
  530. #endif
  531. }
  532. else
  533. {
  534. /* If not use RT_SERIAL_TX_BLOCKING_BUFFER,
  535. * the control() API is called to configure the serial device */
  536. tx_fifo = (struct rt_serial_tx_fifo*) rt_malloc
  537. (sizeof(struct rt_serial_tx_fifo));
  538. RT_ASSERT(tx_fifo != RT_NULL);
  539. serial->serial_tx = tx_fifo;
  540. #ifndef RT_USING_DEVICE_OPS
  541. dev->write = _serial_fifo_tx_blocking_nbuf;
  542. #endif
  543. /* Call the control() API to configure the serial device by RT_SERIAL_TX_BLOCKING*/
  544. serial->ops->control(serial,
  545. RT_DEVICE_CTRL_CONFIG,
  546. (void *)RT_SERIAL_TX_BLOCKING);
  547. }
  548. tx_fifo->activated = RT_FALSE;
  549. tx_fifo->put_size = 0;
  550. rt_completion_init(&(tx_fifo->tx_cpt));
  551. dev->open_flag |= RT_SERIAL_TX_BLOCKING;
  552. return RT_EOK;
  553. }
  554. /* When using RT_SERIAL_TX_NON_BLOCKING, ringbuffer needs to be initialized,
  555. * and initialize the tx_fifo->activated value is RT_FALSE.
  556. */
  557. tx_fifo = (struct rt_serial_tx_fifo *) rt_malloc
  558. (sizeof(struct rt_serial_tx_fifo) + serial->config.tx_bufsz);
  559. RT_ASSERT(tx_fifo != RT_NULL);
  560. tx_fifo->activated = RT_FALSE;
  561. tx_fifo->put_size = 0;
  562. rt_ringbuffer_init(&(tx_fifo->rb),
  563. tx_fifo->buffer,
  564. serial->config.tx_bufsz);
  565. serial->serial_tx = tx_fifo;
  566. #ifndef RT_USING_DEVICE_OPS
  567. dev->write = _serial_fifo_tx_nonblocking;
  568. #endif
  569. dev->open_flag |= RT_SERIAL_TX_NON_BLOCKING;
  570. /* Call the control() API to configure the serial device by RT_SERIAL_TX_NON_BLOCKING*/
  571. serial->ops->control(serial,
  572. RT_DEVICE_CTRL_CONFIG,
  573. (void *)RT_SERIAL_TX_NON_BLOCKING);
  574. return RT_EOK;
  575. }
  576. /**
  577. * @brief Enable serial receive mode.
  578. * @param dev The pointer of device driver structure
  579. * @param rx_oflag The flag of that the serial port opens.
  580. * @return Return the status of the operation.
  581. */
  582. static rt_err_t rt_serial_rx_enable(struct rt_device *dev,
  583. rt_uint16_t rx_oflag)
  584. {
  585. struct rt_serial_device *serial;
  586. struct rt_serial_rx_fifo *rx_fifo = RT_NULL;
  587. RT_ASSERT(dev != RT_NULL);
  588. serial = (struct rt_serial_device *)dev;
  589. if (serial->config.rx_bufsz == 0)
  590. {
  591. /* Cannot use RT_SERIAL_RX_NON_BLOCKING when rx_bufsz is 0 */
  592. if (rx_oflag == RT_SERIAL_RX_NON_BLOCKING)
  593. {
  594. LOG_E("(%s) serial device with misconfigure: rx_bufsz = 0",
  595. dev->parent.name);
  596. return -RT_EINVAL;
  597. }
  598. #ifndef RT_USING_DEVICE_OPS
  599. dev->read = _serial_poll_rx;
  600. #endif
  601. dev->open_flag |= RT_SERIAL_RX_BLOCKING;
  602. return RT_EOK;
  603. }
  604. /* Limits the minimum value of rx_bufsz */
  605. if (serial->config.rx_bufsz < RT_SERIAL_RX_MINBUFSZ)
  606. serial->config.rx_bufsz = RT_SERIAL_RX_MINBUFSZ;
  607. rx_fifo = (struct rt_serial_rx_fifo *) rt_malloc
  608. (sizeof(struct rt_serial_rx_fifo) + serial->config.rx_bufsz);
  609. RT_ASSERT(rx_fifo != RT_NULL);
  610. rt_ringbuffer_init(&(rx_fifo->rb), rx_fifo->buffer, serial->config.rx_bufsz);
  611. serial->serial_rx = rx_fifo;
  612. #ifndef RT_USING_DEVICE_OPS
  613. dev->read = _serial_fifo_rx;
  614. #endif
  615. if (rx_oflag == RT_SERIAL_RX_NON_BLOCKING)
  616. {
  617. dev->open_flag |= RT_SERIAL_RX_NON_BLOCKING;
  618. /* Call the control() API to configure the serial device by RT_SERIAL_RX_NON_BLOCKING*/
  619. serial->ops->control(serial,
  620. RT_DEVICE_CTRL_CONFIG,
  621. (void *) RT_SERIAL_RX_NON_BLOCKING);
  622. return RT_EOK;
  623. }
  624. /* When using RT_SERIAL_RX_BLOCKING, rt_completion_init() and rx_cpt_index are initialized */
  625. rx_fifo->rx_cpt_index = 0;
  626. rt_completion_init(&(rx_fifo->rx_cpt));
  627. dev->open_flag |= RT_SERIAL_RX_BLOCKING;
  628. /* Call the control() API to configure the serial device by RT_SERIAL_RX_BLOCKING*/
  629. serial->ops->control(serial,
  630. RT_DEVICE_CTRL_CONFIG,
  631. (void *) RT_SERIAL_RX_BLOCKING);
  632. return RT_EOK;
  633. }
  634. /**
  635. * @brief Disable serial receive mode.
  636. * @param dev The pointer of device driver structure
  637. * @param rx_oflag The flag of that the serial port opens.
  638. * @return Return the status of the operation.
  639. */
  640. static rt_err_t rt_serial_rx_disable(struct rt_device *dev,
  641. rt_uint16_t rx_oflag)
  642. {
  643. struct rt_serial_device *serial;
  644. struct rt_serial_rx_fifo *rx_fifo;
  645. RT_ASSERT(dev != RT_NULL);
  646. serial = (struct rt_serial_device *)dev;
  647. #ifndef RT_USING_DEVICE_OPS
  648. dev->read = RT_NULL;
  649. #endif
  650. if (serial->serial_rx == RT_NULL) return RT_EOK;
  651. do
  652. {
  653. if (rx_oflag == RT_SERIAL_RX_NON_BLOCKING)
  654. {
  655. dev->open_flag &= ~ RT_SERIAL_RX_NON_BLOCKING;
  656. serial->ops->control(serial,
  657. RT_DEVICE_CTRL_CLR_INT,
  658. (void *)RT_SERIAL_RX_NON_BLOCKING);
  659. break;
  660. }
  661. dev->open_flag &= ~ RT_SERIAL_RX_BLOCKING;
  662. serial->ops->control(serial,
  663. RT_DEVICE_CTRL_CLR_INT,
  664. (void *)RT_SERIAL_RX_BLOCKING);
  665. } while (0);
  666. rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
  667. RT_ASSERT(rx_fifo != RT_NULL);
  668. rt_free(rx_fifo);
  669. serial->serial_rx = RT_NULL;
  670. return RT_EOK;
  671. }
  672. /**
  673. * @brief Disable serial tranmit mode.
  674. * @param dev The pointer of device driver structure
  675. * @param rx_oflag The flag of that the serial port opens.
  676. * @return Return the status of the operation.
  677. */
  678. static rt_err_t rt_serial_tx_disable(struct rt_device *dev,
  679. rt_uint16_t tx_oflag)
  680. {
  681. struct rt_serial_device *serial;
  682. struct rt_serial_tx_fifo *tx_fifo;
  683. RT_ASSERT(dev != RT_NULL);
  684. serial = (struct rt_serial_device *)dev;
  685. #ifndef RT_USING_DEVICE_OPS
  686. dev->write = RT_NULL;
  687. #endif
  688. if (serial->serial_tx == RT_NULL) return RT_EOK;
  689. tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
  690. RT_ASSERT(tx_fifo != RT_NULL);
  691. do
  692. {
  693. if (tx_oflag == RT_SERIAL_TX_NON_BLOCKING)
  694. {
  695. dev->open_flag &= ~ RT_SERIAL_TX_NON_BLOCKING;
  696. serial->ops->control(serial,
  697. RT_DEVICE_CTRL_CLR_INT,
  698. (void *)RT_SERIAL_TX_NON_BLOCKING);
  699. break;
  700. }
  701. rt_completion_done(&(tx_fifo->tx_cpt));
  702. dev->open_flag &= ~ RT_SERIAL_TX_BLOCKING;
  703. serial->ops->control(serial,
  704. RT_DEVICE_CTRL_CLR_INT,
  705. (void *)RT_SERIAL_TX_BLOCKING);
  706. } while (0);
  707. rt_free(tx_fifo);
  708. serial->serial_tx = RT_NULL;
  709. return RT_EOK;
  710. }
  711. /**
  712. * @brief Initialize the serial device.
  713. * @param dev The pointer of device driver structure
  714. * @return Return the status of the operation.
  715. */
  716. static rt_err_t rt_serial_init(struct rt_device *dev)
  717. {
  718. rt_err_t result = RT_EOK;
  719. struct rt_serial_device *serial;
  720. RT_ASSERT(dev != RT_NULL);
  721. serial = (struct rt_serial_device *)dev;
  722. /* initialize rx/tx */
  723. serial->serial_rx = RT_NULL;
  724. serial->serial_tx = RT_NULL;
  725. /* apply configuration */
  726. if (serial->ops->configure)
  727. result = serial->ops->configure(serial, &serial->config);
  728. return result;
  729. }
  730. /**
  731. * @brief Open the serial device.
  732. * @param dev The pointer of device driver structure
  733. * @param oflag The flag of that the serial port opens.
  734. * @return Return the status of the operation.
  735. */
  736. static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
  737. {
  738. struct rt_serial_device *serial;
  739. RT_ASSERT(dev != RT_NULL);
  740. serial = (struct rt_serial_device *)dev;
  741. /* Check that the device has been turned on */
  742. if ((dev->open_flag) & (15 << 12))
  743. {
  744. LOG_D("(%s) serial device has already been opened, it will run in its original configuration", dev->parent.name);
  745. return RT_EOK;
  746. }
  747. LOG_D("open serial device: 0x%08x with open flag: 0x%04x",
  748. dev, oflag);
  749. /* By default, the receive mode of a serial devide is RT_SERIAL_RX_NON_BLOCKING */
  750. if ((oflag & RT_SERIAL_RX_BLOCKING) == RT_SERIAL_RX_BLOCKING)
  751. dev->open_flag |= RT_SERIAL_RX_BLOCKING;
  752. else
  753. dev->open_flag |= RT_SERIAL_RX_NON_BLOCKING;
  754. /* By default, the transmit mode of a serial devide is RT_SERIAL_TX_BLOCKING */
  755. if ((oflag & RT_SERIAL_TX_NON_BLOCKING) == RT_SERIAL_TX_NON_BLOCKING)
  756. dev->open_flag |= RT_SERIAL_TX_NON_BLOCKING;
  757. else
  758. dev->open_flag |= RT_SERIAL_TX_BLOCKING;
  759. /* set steam flag */
  760. if ((oflag & RT_DEVICE_FLAG_STREAM) ||
  761. (dev->open_flag & RT_DEVICE_FLAG_STREAM))
  762. dev->open_flag |= RT_DEVICE_FLAG_STREAM;
  763. /* initialize the Rx structure according to open flag */
  764. if (serial->serial_rx == RT_NULL)
  765. rt_serial_rx_enable(dev, dev->open_flag &
  766. (RT_SERIAL_RX_BLOCKING | RT_SERIAL_RX_NON_BLOCKING));
  767. /* initialize the Tx structure according to open flag */
  768. if (serial->serial_tx == RT_NULL)
  769. rt_serial_tx_enable(dev, dev->open_flag &
  770. (RT_SERIAL_TX_BLOCKING | RT_SERIAL_TX_NON_BLOCKING));
  771. return RT_EOK;
  772. }
  773. /**
  774. * @brief Close the serial device.
  775. * @param dev The pointer of device driver structure
  776. * @return Return the status of the operation.
  777. */
  778. static rt_err_t rt_serial_close(struct rt_device *dev)
  779. {
  780. struct rt_serial_device *serial;
  781. RT_ASSERT(dev != RT_NULL);
  782. serial = (struct rt_serial_device *)dev;
  783. /* this device has more reference count */
  784. if (dev->ref_count > 1) return -RT_ERROR;
  785. /* Disable serial receive mode. */
  786. rt_serial_rx_disable(dev, dev->open_flag &
  787. (RT_SERIAL_RX_BLOCKING | RT_SERIAL_RX_NON_BLOCKING));
  788. /* Disable serial tranmit mode. */
  789. rt_serial_tx_disable(dev, dev->open_flag &
  790. (RT_SERIAL_TX_BLOCKING | RT_SERIAL_TX_NON_BLOCKING));
  791. /* Clear the callback function */
  792. serial->parent.rx_indicate = RT_NULL;
  793. serial->parent.tx_complete = RT_NULL;
  794. /* Call the control() API to close the serial device */
  795. serial->ops->control(serial, RT_DEVICE_CTRL_CLOSE, RT_NULL);
  796. dev->flag &= ~RT_DEVICE_FLAG_ACTIVATED;
  797. return RT_EOK;
  798. }
  799. /**
  800. * @brief Control the serial device.
  801. * @param dev The pointer of device driver structure
  802. * @param cmd The command value that controls the serial device
  803. * @param args The parameter value that controls the serial device
  804. * @return Return the status of the operation.
  805. */
  806. static rt_err_t rt_serial_control(struct rt_device *dev,
  807. int cmd,
  808. void *args)
  809. {
  810. rt_err_t ret = RT_EOK;
  811. struct rt_serial_device *serial;
  812. RT_ASSERT(dev != RT_NULL);
  813. serial = (struct rt_serial_device *)dev;
  814. switch (cmd)
  815. {
  816. case RT_DEVICE_CTRL_SUSPEND:
  817. /* suspend device */
  818. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  819. break;
  820. case RT_DEVICE_CTRL_RESUME:
  821. /* resume device */
  822. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  823. break;
  824. case RT_DEVICE_CTRL_CONFIG:
  825. if (args != RT_NULL)
  826. {
  827. struct serial_configure *pconfig = (struct serial_configure *) args;
  828. if (serial->parent.ref_count)
  829. {
  830. /*can not change buffer size*/
  831. return -RT_EBUSY;
  832. }
  833. /* set serial configure */
  834. serial->config = *pconfig;
  835. serial->ops->configure(serial,
  836. (struct serial_configure *) args);
  837. }
  838. break;
  839. default :
  840. /* control device */
  841. ret = serial->ops->control(serial, cmd, args);
  842. break;
  843. }
  844. return ret;
  845. }
  846. #ifdef RT_USING_DEVICE_OPS
  847. static rt_size_t rt_serial_read(struct rt_device *dev,
  848. rt_off_t pos,
  849. void *buffer,
  850. rt_size_t size)
  851. {
  852. struct rt_serial_device *serial;
  853. RT_ASSERT(dev != RT_NULL);
  854. if (size == 0) return 0;
  855. serial = (struct rt_serial_device *)dev;
  856. if (serial->config.rx_bufsz)
  857. {
  858. return _serial_fifo_rx(dev, pos, buffer, size);
  859. }
  860. return _serial_poll_rx(dev, pos, buffer, size);
  861. }
  862. static rt_size_t rt_serial_write(struct rt_device *dev,
  863. rt_off_t pos,
  864. const void *buffer,
  865. rt_size_t size)
  866. {
  867. struct rt_serial_device *serial;
  868. struct rt_serial_tx_fifo *tx_fifo;
  869. RT_ASSERT(dev != RT_NULL);
  870. if (size == 0) return 0;
  871. serial = (struct rt_serial_device *)dev;
  872. RT_ASSERT((serial != RT_NULL) && (buffer != RT_NULL));
  873. tx_fifo = (struct rt_serial_tx_fifo *) serial->serial_tx;
  874. if (serial->config.tx_bufsz == 0)
  875. {
  876. return _serial_poll_tx(dev, pos, buffer, size);
  877. }
  878. if (dev->open_flag | RT_SERIAL_TX_BLOCKING)
  879. {
  880. if ((tx_fifo->rb.buffer_ptr) == RT_NULL)
  881. {
  882. return _serial_fifo_tx_blocking_nbuf(dev, pos, buffer, size);
  883. }
  884. return _serial_fifo_tx_blocking_buf(dev, pos, buffer, size);
  885. }
  886. return _serial_fifo_tx_nonblocking(dev, pos, buffer, size);
  887. }
  888. const static struct rt_device_ops serial_ops =
  889. {
  890. rt_serial_init,
  891. rt_serial_open,
  892. rt_serial_close,
  893. rt_serial_read,
  894. rt_serial_write,
  895. rt_serial_control
  896. };
  897. #endif
  898. /**
  899. * @brief Register the serial device.
  900. * @param serial RT-thread serial device.
  901. * @param name The device driver's name
  902. * @param flag The capabilities flag of device.
  903. * @param data The device driver's data.
  904. * @return Return the status of the operation.
  905. */
  906. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
  907. const char *name,
  908. rt_uint32_t flag,
  909. void *data)
  910. {
  911. rt_err_t ret;
  912. struct rt_device *device;
  913. RT_ASSERT(serial != RT_NULL);
  914. device = &(serial->parent);
  915. device->type = RT_Device_Class_Char;
  916. device->rx_indicate = RT_NULL;
  917. device->tx_complete = RT_NULL;
  918. #ifdef RT_USING_DEVICE_OPS
  919. device->ops = &serial_ops;
  920. #else
  921. device->init = rt_serial_init;
  922. device->open = rt_serial_open;
  923. device->close = rt_serial_close;
  924. device->read = RT_NULL;
  925. device->write = RT_NULL;
  926. device->control = rt_serial_control;
  927. #endif
  928. device->user_data = data;
  929. /* register a character device */
  930. ret = rt_device_register(device, name, flag);
  931. #ifdef RT_USING_POSIX_STDIO
  932. /* set fops */
  933. device->fops = &_serial_fops;
  934. #endif
  935. return ret;
  936. }
  937. /**
  938. * @brief ISR for serial interrupt
  939. * @param serial RT-thread serial device.
  940. * @param event ISR event type.
  941. */
  942. void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
  943. {
  944. RT_ASSERT(serial != RT_NULL);
  945. switch (event & 0xff)
  946. {
  947. /* Interrupt receive event */
  948. case RT_SERIAL_EVENT_RX_IND:
  949. case RT_SERIAL_EVENT_RX_DMADONE:
  950. {
  951. struct rt_serial_rx_fifo *rx_fifo;
  952. rt_size_t rx_length = 0;
  953. rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
  954. RT_ASSERT(rx_fifo != RT_NULL);
  955. /* If the event is RT_SERIAL_EVENT_RX_IND, rx_length is equal to 0 */
  956. rx_length = (event & (~0xff)) >> 8;
  957. if (rx_length)
  958. rt_serial_update_write_index(&(rx_fifo->rb), rx_length);
  959. /* Get the length of the data from the ringbuffer */
  960. rx_length = rt_ringbuffer_data_len(&rx_fifo->rb);
  961. if (rx_length == 0) break;
  962. if (serial->parent.open_flag & RT_SERIAL_RX_BLOCKING)
  963. {
  964. if (rx_fifo->rx_cpt_index && rx_length >= rx_fifo->rx_cpt_index )
  965. {
  966. rx_fifo->rx_cpt_index = 0;
  967. rt_completion_done(&(rx_fifo->rx_cpt));
  968. }
  969. }
  970. /* Trigger the receiving completion callback */
  971. if (serial->parent.rx_indicate != RT_NULL)
  972. serial->parent.rx_indicate(&(serial->parent), rx_length);
  973. break;
  974. }
  975. /* Interrupt transmit event */
  976. case RT_SERIAL_EVENT_TX_DONE:
  977. {
  978. struct rt_serial_tx_fifo *tx_fifo;
  979. rt_size_t tx_length = 0;
  980. tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
  981. RT_ASSERT(tx_fifo != RT_NULL);
  982. /* Get the length of the data from the ringbuffer */
  983. tx_length = rt_ringbuffer_data_len(&tx_fifo->rb);
  984. /* If there is no data in tx_ringbuffer,
  985. * then the transmit completion callback is triggered*/
  986. if (tx_length == 0)
  987. {
  988. tx_fifo->activated = RT_FALSE;
  989. /* Trigger the transmit completion callback */
  990. if (serial->parent.tx_complete != RT_NULL)
  991. serial->parent.tx_complete(&serial->parent, RT_NULL);
  992. if (serial->parent.open_flag & RT_SERIAL_TX_BLOCKING)
  993. rt_completion_done(&(tx_fifo->tx_cpt));
  994. break;
  995. }
  996. /* Call the transmit interface for transmission again */
  997. /* Note that in interrupt mode, tx_fifo->buffer and tx_length
  998. * are inactive parameters */
  999. serial->ops->transmit(serial,
  1000. tx_fifo->buffer,
  1001. tx_length,
  1002. serial->parent.open_flag & ( \
  1003. RT_SERIAL_TX_BLOCKING | \
  1004. RT_SERIAL_TX_NON_BLOCKING));
  1005. break;
  1006. }
  1007. case RT_SERIAL_EVENT_TX_DMADONE:
  1008. {
  1009. struct rt_serial_tx_fifo *tx_fifo;
  1010. tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
  1011. RT_ASSERT(tx_fifo != RT_NULL);
  1012. tx_fifo->activated = RT_FALSE;
  1013. /* Trigger the transmit completion callback */
  1014. if (serial->parent.tx_complete != RT_NULL)
  1015. serial->parent.tx_complete(&serial->parent, RT_NULL);
  1016. if (serial->parent.open_flag & RT_SERIAL_TX_BLOCKING)
  1017. {
  1018. rt_completion_done(&(tx_fifo->tx_cpt));
  1019. break;
  1020. }
  1021. rt_serial_update_read_index(&tx_fifo->rb, tx_fifo->put_size);
  1022. /* Get the length of the data from the ringbuffer.
  1023. * If there is some data in tx_ringbuffer,
  1024. * then call the transmit interface for transmission again */
  1025. if (rt_ringbuffer_data_len(&tx_fifo->rb))
  1026. {
  1027. tx_fifo->activated = RT_TRUE;
  1028. rt_uint8_t *put_ptr = RT_NULL;
  1029. /* Get the linear length buffer from rinbuffer */
  1030. tx_fifo->put_size = rt_serial_get_linear_buffer(&(tx_fifo->rb), &put_ptr);
  1031. /* Call the transmit interface for transmission again */
  1032. serial->ops->transmit(serial,
  1033. put_ptr,
  1034. tx_fifo->put_size,
  1035. RT_SERIAL_TX_NON_BLOCKING);
  1036. }
  1037. break;
  1038. }
  1039. default:
  1040. break;
  1041. }
  1042. }