serial_v2.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  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. /* Call the control() API to close the serial device */
  773. serial->ops->control(serial, RT_DEVICE_CTRL_CLOSE, RT_NULL);
  774. dev->flag &= ~RT_DEVICE_FLAG_ACTIVATED;
  775. return RT_EOK;
  776. }
  777. /**
  778. * @brief Control the serial device.
  779. * @param dev The pointer of device driver structure
  780. * @param cmd The command value that controls the serial device
  781. * @param args The parameter value that controls the serial device
  782. * @return Return the status of the operation.
  783. */
  784. static rt_err_t rt_serial_control(struct rt_device *dev,
  785. int cmd,
  786. void *args)
  787. {
  788. rt_err_t ret = RT_EOK;
  789. struct rt_serial_device *serial;
  790. RT_ASSERT(dev != RT_NULL);
  791. serial = (struct rt_serial_device *)dev;
  792. switch (cmd)
  793. {
  794. case RT_DEVICE_CTRL_SUSPEND:
  795. /* suspend device */
  796. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  797. break;
  798. case RT_DEVICE_CTRL_RESUME:
  799. /* resume device */
  800. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  801. break;
  802. case RT_DEVICE_CTRL_CONFIG:
  803. if (args != RT_NULL)
  804. {
  805. struct serial_configure *pconfig = (struct serial_configure *) args;
  806. if (serial->parent.ref_count)
  807. {
  808. /*can not change buffer size*/
  809. return -RT_EBUSY;
  810. }
  811. /* set serial configure */
  812. serial->config = *pconfig;
  813. serial->ops->configure(serial,
  814. (struct serial_configure *) args);
  815. }
  816. break;
  817. default :
  818. /* control device */
  819. ret = serial->ops->control(serial, cmd, args);
  820. break;
  821. }
  822. return ret;
  823. }
  824. #ifdef RT_USING_DEVICE_OPS
  825. const static struct rt_device_ops serial_ops =
  826. {
  827. rt_serial_init,
  828. rt_serial_open,
  829. rt_serial_close,
  830. rt_serial_read,
  831. rt_serial_write,
  832. rt_serial_control
  833. };
  834. #endif
  835. /**
  836. * @brief Register the serial device.
  837. * @param serial RT-thread serial device.
  838. * @param name The device driver's name
  839. * @param flag The capabilities flag of device.
  840. * @param data The device driver's data.
  841. * @return Return the status of the operation.
  842. */
  843. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
  844. const char *name,
  845. rt_uint32_t flag,
  846. void *data)
  847. {
  848. rt_err_t ret;
  849. struct rt_device *device;
  850. RT_ASSERT(serial != RT_NULL);
  851. device = &(serial->parent);
  852. device->type = RT_Device_Class_Char;
  853. device->rx_indicate = RT_NULL;
  854. device->tx_complete = RT_NULL;
  855. #ifdef RT_USING_DEVICE_OPS
  856. device->ops = &serial_ops;
  857. #else
  858. device->init = rt_serial_init;
  859. device->open = rt_serial_open;
  860. device->close = rt_serial_close;
  861. device->read = RT_NULL;
  862. device->write = RT_NULL;
  863. device->control = rt_serial_control;
  864. #endif
  865. device->user_data = data;
  866. /* register a character device */
  867. ret = rt_device_register(device, name, flag);
  868. #if defined(RT_USING_POSIX)
  869. /* set fops */
  870. device->fops = &_serial_fops;
  871. #endif
  872. return ret;
  873. }
  874. /**
  875. * @brief ISR for serial interrupt
  876. * @param serial RT-thread serial device.
  877. * @param event ISR event type.
  878. */
  879. void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
  880. {
  881. RT_ASSERT(serial != RT_NULL);
  882. switch (event & 0xff)
  883. {
  884. /* Interrupt receive event */
  885. case RT_SERIAL_EVENT_RX_IND:
  886. case RT_SERIAL_EVENT_RX_DMADONE:
  887. {
  888. struct rt_serial_rx_fifo *rx_fifo;
  889. rt_size_t rx_length = 0;
  890. rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
  891. RT_ASSERT(rx_fifo != RT_NULL);
  892. /* If the event is RT_SERIAL_EVENT_RX_IND, rx_length is equal to 0 */
  893. rx_length = (event & (~0xff)) >> 8;
  894. if (rx_length)
  895. rt_serial_update_write_index(&(rx_fifo->rb), rx_length);
  896. /* Get the length of the data from the ringbuffer */
  897. rx_length = rt_ringbuffer_data_len(&rx_fifo->rb);
  898. if (rx_length == 0) break;
  899. if (serial->parent.open_flag & RT_SERIAL_RX_BLOCKING)
  900. {
  901. if (rx_fifo->rx_cpt_index && rx_length >= rx_fifo->rx_cpt_index )
  902. {
  903. rx_fifo->rx_cpt_index = 0;
  904. rt_completion_done(&(rx_fifo->rx_cpt));
  905. }
  906. }
  907. /* Trigger the receiving completion callback */
  908. if (serial->parent.rx_indicate != RT_NULL)
  909. serial->parent.rx_indicate(&(serial->parent), rx_length);
  910. break;
  911. }
  912. /* Interrupt transmit event */
  913. case RT_SERIAL_EVENT_TX_DONE:
  914. {
  915. struct rt_serial_tx_fifo *tx_fifo;
  916. rt_size_t tx_length = 0;
  917. tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
  918. RT_ASSERT(tx_fifo != RT_NULL);
  919. /* Get the length of the data from the ringbuffer */
  920. tx_length = rt_ringbuffer_data_len(&tx_fifo->rb);
  921. /* If there is no data in tx_ringbuffer,
  922. * then the transmit completion callback is triggered*/
  923. if (tx_length == 0)
  924. {
  925. tx_fifo->activated = RT_FALSE;
  926. /* Trigger the transmit completion callback */
  927. if (serial->parent.tx_complete != RT_NULL)
  928. serial->parent.tx_complete(&serial->parent, RT_NULL);
  929. if (serial->parent.open_flag & RT_SERIAL_TX_BLOCKING)
  930. rt_completion_done(&(tx_fifo->tx_cpt));
  931. break;
  932. }
  933. /* Call the transmit interface for transmission again */
  934. /* Note that in interrupt mode, tx_fifo->buffer and tx_length
  935. * are inactive parameters */
  936. serial->ops->transmit(serial,
  937. tx_fifo->buffer,
  938. tx_length,
  939. serial->parent.open_flag & ( \
  940. RT_SERIAL_TX_BLOCKING | \
  941. RT_SERIAL_TX_NON_BLOCKING));
  942. break;
  943. }
  944. case RT_SERIAL_EVENT_TX_DMADONE:
  945. {
  946. struct rt_serial_tx_fifo *tx_fifo;
  947. tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
  948. RT_ASSERT(tx_fifo != RT_NULL);
  949. tx_fifo->activated = RT_FALSE;
  950. /* Trigger the transmit completion callback */
  951. if (serial->parent.tx_complete != RT_NULL)
  952. serial->parent.tx_complete(&serial->parent, RT_NULL);
  953. if (serial->parent.open_flag & RT_SERIAL_TX_BLOCKING)
  954. {
  955. rt_completion_done(&(tx_fifo->tx_cpt));
  956. break;
  957. }
  958. rt_serial_update_read_index(&tx_fifo->rb, tx_fifo->put_size);
  959. /* Get the length of the data from the ringbuffer.
  960. * If there is some data in tx_ringbuffer,
  961. * then call the transmit interface for transmission again */
  962. if (rt_ringbuffer_data_len(&tx_fifo->rb))
  963. {
  964. tx_fifo->activated = RT_TRUE;
  965. rt_uint8_t *put_ptr = RT_NULL;
  966. /* Get the linear length buffer from rinbuffer */
  967. tx_fifo->put_size = rt_serial_get_linear_buffer(&(tx_fifo->rb), &put_ptr);
  968. /* Call the transmit interface for transmission again */
  969. serial->ops->transmit(serial,
  970. put_ptr,
  971. tx_fifo->put_size,
  972. RT_SERIAL_TX_NON_BLOCKING);
  973. }
  974. break;
  975. }
  976. default:
  977. break;
  978. }
  979. }