serial_v2.c 37 KB

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