serial_v2.c 38 KB

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