serial_v2.c 38 KB

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