serial_v2.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618
  1. /*
  2. * Copyright (c) 2006-2022, 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 "Serial"
  14. #define DBG_LVL DBG_INFO
  15. #include <rtdbg.h>
  16. #ifdef RT_USING_POSIX_STDIO
  17. #include <unistd.h>
  18. #include <fcntl.h>
  19. #include <poll.h>
  20. #include <sys/ioctl.h>
  21. #include <dfs_file.h>
  22. #ifdef RT_USING_POSIX_TERMIOS
  23. #include <termios.h>
  24. #endif
  25. #ifdef getc
  26. #undef getc
  27. #endif
  28. #ifdef putc
  29. #undef putc
  30. #endif
  31. static rt_err_t serial_fops_rx_ind(rt_device_t dev, rt_size_t size)
  32. {
  33. rt_wqueue_wakeup(&(dev->wait_queue), (void*)POLLIN);
  34. return RT_EOK;
  35. }
  36. /* fops for serial */
  37. static int serial_fops_open(struct dfs_fd *fd)
  38. {
  39. rt_err_t ret = 0;
  40. rt_uint16_t flags = 0;
  41. rt_device_t device;
  42. device = (rt_device_t)fd->vnode->data;
  43. RT_ASSERT(device != RT_NULL);
  44. switch (fd->flags & O_ACCMODE)
  45. {
  46. case O_RDONLY:
  47. LOG_D("fops open: O_RDONLY!");
  48. flags = RT_DEVICE_FLAG_RDONLY;
  49. break;
  50. case O_WRONLY:
  51. LOG_D("fops open: O_WRONLY!");
  52. flags = RT_DEVICE_FLAG_WRONLY;
  53. break;
  54. case O_RDWR:
  55. LOG_D("fops open: O_RDWR!");
  56. flags = RT_DEVICE_FLAG_RDWR;
  57. break;
  58. default:
  59. LOG_E("fops open: unknown mode - %d!", fd->flags & O_ACCMODE);
  60. break;
  61. }
  62. if ((fd->flags & O_ACCMODE) != O_WRONLY)
  63. rt_device_set_rx_indicate(device, serial_fops_rx_ind);
  64. ret = rt_device_open(device, flags);
  65. if (ret == RT_EOK) return 0;
  66. return ret;
  67. }
  68. static int serial_fops_close(struct dfs_fd *fd)
  69. {
  70. rt_device_t device;
  71. device = (rt_device_t)fd->vnode->data;
  72. rt_device_set_rx_indicate(device, RT_NULL);
  73. rt_device_close(device);
  74. return 0;
  75. }
  76. static int serial_fops_ioctl(struct dfs_fd *fd, int cmd, void *args)
  77. {
  78. rt_device_t device;
  79. int flags = (int)(rt_base_t)args;
  80. int mask = O_NONBLOCK | O_APPEND;
  81. device = (rt_device_t)fd->vnode->data;
  82. switch (cmd)
  83. {
  84. case FIONREAD:
  85. break;
  86. case FIONWRITE:
  87. break;
  88. case F_SETFL:
  89. flags &= mask;
  90. fd->flags &= ~mask;
  91. fd->flags |= flags;
  92. break;
  93. }
  94. return rt_device_control(device, cmd, args);
  95. }
  96. static int serial_fops_read(struct dfs_fd *fd, void *buf, size_t count)
  97. {
  98. int size = 0;
  99. rt_device_t device;
  100. device = (rt_device_t)fd->vnode->data;
  101. do
  102. {
  103. size = rt_device_read(device, -1, buf, count);
  104. if (size <= 0)
  105. {
  106. if (fd->flags & O_NONBLOCK)
  107. {
  108. size = -EAGAIN;
  109. break;
  110. }
  111. rt_wqueue_wait(&(device->wait_queue), 0, RT_WAITING_FOREVER);
  112. }
  113. }while (size <= 0);
  114. return size;
  115. }
  116. static int serial_fops_write(struct dfs_fd *fd, const void *buf, size_t count)
  117. {
  118. rt_device_t device;
  119. device = (rt_device_t)fd->vnode->data;
  120. return rt_device_write(device, -1, buf, count);
  121. }
  122. static int serial_fops_poll(struct dfs_fd *fd, struct rt_pollreq *req)
  123. {
  124. int mask = 0;
  125. int flags = 0;
  126. rt_device_t device;
  127. struct rt_serial_device *serial;
  128. device = (rt_device_t)fd->vnode->data;
  129. RT_ASSERT(device != RT_NULL);
  130. serial = (struct rt_serial_device *)device;
  131. /* only support POLLIN */
  132. flags = fd->flags & O_ACCMODE;
  133. if (flags == O_RDONLY || flags == O_RDWR)
  134. {
  135. rt_base_t level;
  136. struct rt_serial_rx_fifo* rx_fifo;
  137. rt_poll_add(&(device->wait_queue), req);
  138. rx_fifo = (struct rt_serial_rx_fifo*) serial->serial_rx;
  139. level = rt_hw_interrupt_disable();
  140. if (rt_ringbuffer_data_len(&rx_fifo->rb))
  141. mask |= POLLIN;
  142. rt_hw_interrupt_enable(level);
  143. }
  144. // mask|=POLLOUT;
  145. return mask;
  146. }
  147. const static struct dfs_file_ops _serial_fops =
  148. {
  149. serial_fops_open,
  150. serial_fops_close,
  151. serial_fops_ioctl,
  152. serial_fops_read,
  153. serial_fops_write,
  154. RT_NULL, /* flush */
  155. RT_NULL, /* lseek */
  156. RT_NULL, /* getdents */
  157. serial_fops_poll,
  158. };
  159. #endif /* RT_USING_POSIX_STDIO */
  160. static rt_size_t rt_serial_get_linear_buffer(struct rt_ringbuffer *rb,
  161. rt_uint8_t **ptr)
  162. {
  163. rt_size_t size;
  164. RT_ASSERT(rb != RT_NULL);
  165. *ptr = RT_NULL;
  166. /* whether has enough data */
  167. size = rt_ringbuffer_data_len(rb);
  168. /* no data */
  169. if (size == 0)
  170. return 0;
  171. *ptr = &rb->buffer_ptr[rb->read_index];
  172. if(rb->buffer_size - rb->read_index > size)
  173. {
  174. return size;
  175. }
  176. return rb->buffer_size - rb->read_index;
  177. }
  178. static rt_size_t rt_serial_update_read_index(struct rt_ringbuffer *rb,
  179. rt_uint16_t read_index)
  180. {
  181. rt_size_t size;
  182. RT_ASSERT(rb != RT_NULL);
  183. /* whether has enough data */
  184. size = rt_ringbuffer_data_len(rb);
  185. /* no data */
  186. if (size == 0)
  187. return 0;
  188. /* less data */
  189. if(size < read_index)
  190. read_index = size;
  191. if(rb->buffer_size - rb->read_index > read_index)
  192. {
  193. rb->read_index += read_index;
  194. return read_index;
  195. }
  196. read_index = rb->buffer_size - rb->read_index;
  197. /* we are going into the other side of the mirror */
  198. rb->read_mirror = ~rb->read_mirror;
  199. rb->read_index = 0;
  200. return read_index;
  201. }
  202. static rt_size_t rt_serial_update_write_index(struct rt_ringbuffer *rb,
  203. rt_uint16_t write_size)
  204. {
  205. rt_uint16_t size;
  206. RT_ASSERT(rb != RT_NULL);
  207. /* whether has enough space */
  208. size = rt_ringbuffer_space_len(rb);
  209. /* no space, drop some data */
  210. if (size < write_size)
  211. {
  212. write_size = size;
  213. #if !defined(RT_USING_ULOG) || defined(ULOG_USING_ISR_LOG)
  214. LOG_W("The serial buffer (len %d) is overflow.", rb->buffer_size);
  215. #endif
  216. }
  217. if (rb->buffer_size - rb->write_index > write_size)
  218. {
  219. /* this should not cause overflow because there is enough space for
  220. * length of data in current mirror */
  221. rb->write_index += write_size;
  222. return write_size;
  223. }
  224. /* we are going into the other side of the mirror */
  225. rb->write_mirror = ~rb->write_mirror;
  226. rb->write_index = write_size - (rb->buffer_size - rb->write_index);
  227. return write_size;
  228. }
  229. /**
  230. * @brief Serial polling receive data routine, This function will receive data
  231. * in a continuous loop by one by one byte.
  232. * @param dev The pointer of device driver structure
  233. * @param pos Empty parameter.
  234. * @param buffer Receive data buffer.
  235. * @param size Receive data buffer length.
  236. * @return Return the final length of data received.
  237. */
  238. rt_size_t _serial_poll_rx(struct rt_device *dev,
  239. rt_off_t pos,
  240. void *buffer,
  241. rt_size_t size)
  242. {
  243. struct rt_serial_device *serial;
  244. rt_size_t getc_size;
  245. int getc_element; /* Gets one byte of data received */
  246. rt_uint8_t *getc_buffer; /* Pointer to the receive data buffer */
  247. RT_ASSERT(dev != RT_NULL);
  248. serial = (struct rt_serial_device *)dev;
  249. RT_ASSERT(serial != RT_NULL);
  250. getc_buffer = (rt_uint8_t *)buffer;
  251. getc_size = size;
  252. while(size)
  253. {
  254. getc_element = serial->ops->getc(serial);
  255. if (getc_element == -1) break;
  256. *getc_buffer = getc_element;
  257. ++ getc_buffer;
  258. -- size;
  259. if (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM)
  260. {
  261. /* If open_flag satisfies RT_DEVICE_FLAG_STREAM
  262. * and the received character is '\n', exit the loop directly */
  263. if (getc_element == '\n') break;
  264. }
  265. }
  266. return getc_size - size;
  267. }
  268. /**
  269. * @brief Serial polling transmit data routines, This function will transmit
  270. * data in a continuous loop by one by one byte.
  271. * @param dev The pointer of device driver structure
  272. * @param pos Empty parameter.
  273. * @param buffer Transmit data buffer.
  274. * @param size Transmit data buffer length.
  275. * @return Return the final length of data received.
  276. */
  277. rt_size_t _serial_poll_tx(struct rt_device *dev,
  278. rt_off_t pos,
  279. const void *buffer,
  280. rt_size_t size)
  281. {
  282. struct rt_serial_device *serial;
  283. rt_size_t putc_size;
  284. rt_uint8_t *putc_buffer; /* Pointer to the transmit data buffer */
  285. RT_ASSERT(dev != RT_NULL);
  286. serial = (struct rt_serial_device *)dev;
  287. RT_ASSERT(serial != RT_NULL);
  288. putc_buffer = (rt_uint8_t *)buffer;
  289. putc_size = size;
  290. while (size)
  291. {
  292. if (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM)
  293. {
  294. /* If open_flag satisfies RT_DEVICE_FLAG_STREAM and the received character is '\n',
  295. * inserts '\r' character before '\n' character for the effect of carriage return newline */
  296. if (*putc_buffer == '\n')
  297. serial->ops->putc(serial, '\r');
  298. }
  299. serial->ops->putc(serial, *putc_buffer);
  300. ++ putc_buffer;
  301. -- size;
  302. }
  303. return putc_size - size;
  304. }
  305. /**
  306. * @brief Serial receive data routines, This function will receive
  307. * data by using fifo
  308. * @param dev The pointer of device driver structure
  309. * @param pos Empty parameter.
  310. * @param buffer Receive data buffer.
  311. * @param size Receive data buffer length.
  312. * @return Return the final length of data received.
  313. */
  314. static rt_size_t _serial_fifo_rx(struct rt_device *dev,
  315. rt_off_t pos,
  316. void *buffer,
  317. rt_size_t size)
  318. {
  319. struct rt_serial_device *serial;
  320. struct rt_serial_rx_fifo *rx_fifo;
  321. rt_base_t level;
  322. rt_size_t recv_len; /* The length of data from the ringbuffer */
  323. RT_ASSERT(dev != RT_NULL);
  324. if (size == 0) return 0;
  325. serial = (struct rt_serial_device *)dev;
  326. RT_ASSERT((serial != RT_NULL) && (buffer != RT_NULL));
  327. rx_fifo = (struct rt_serial_rx_fifo *) serial->serial_rx;
  328. if (dev->open_flag & RT_SERIAL_RX_BLOCKING)
  329. {
  330. if (size > serial->config.rx_bufsz)
  331. {
  332. LOG_W("(%s) serial device received data:[%d] larger than "
  333. "rx_bufsz:[%d], please increase the BSP_UARTx_RX_BUFSIZE option",
  334. dev->parent.name, size, serial->config.rx_bufsz);
  335. return 0;
  336. }
  337. /* Get the length of the data from the ringbuffer */
  338. recv_len = rt_ringbuffer_data_len(&(rx_fifo->rb));
  339. if (recv_len < size)
  340. {
  341. /* When recv_len is less than size, rx_cpt_index is updated to the size
  342. * and rt_current_thread is suspend until rx_cpt_index is equal to 0 */
  343. rx_fifo->rx_cpt_index = size;
  344. rt_completion_wait(&(rx_fifo->rx_cpt), RT_WAITING_FOREVER);
  345. }
  346. }
  347. /* This part of the code is open_flag as RT_SERIAL_RX_NON_BLOCKING */
  348. level = rt_hw_interrupt_disable();
  349. /* When open_flag is RT_SERIAL_RX_NON_BLOCKING,
  350. * the data is retrieved directly from the ringbuffer and returned */
  351. recv_len = rt_ringbuffer_get(&(rx_fifo->rb), buffer, size);
  352. rt_hw_interrupt_enable(level);
  353. return recv_len;
  354. }
  355. /**
  356. * @brief Serial transmit data routines, This function will transmit
  357. * data by using blocking_nbuf.
  358. * @param dev The pointer of device driver structure
  359. * @param pos Empty parameter.
  360. * @param buffer Transmit data buffer.
  361. * @param size Transmit data buffer length.
  362. * @return Return the final length of data transmit.
  363. */
  364. static rt_size_t _serial_fifo_tx_blocking_nbuf(struct rt_device *dev,
  365. rt_off_t pos,
  366. const void *buffer,
  367. rt_size_t size)
  368. {
  369. struct rt_serial_device *serial;
  370. struct rt_serial_tx_fifo *tx_fifo = RT_NULL;
  371. RT_ASSERT(dev != RT_NULL);
  372. if (size == 0) return 0;
  373. serial = (struct rt_serial_device *)dev;
  374. RT_ASSERT((serial != RT_NULL) && (buffer != RT_NULL));
  375. tx_fifo = (struct rt_serial_tx_fifo *) serial->serial_tx;
  376. RT_ASSERT(tx_fifo != RT_NULL);
  377. if (rt_thread_self() == RT_NULL || (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM))
  378. {
  379. /* using poll tx when the scheduler not startup or in stream mode */
  380. return _serial_poll_tx(dev, pos, buffer, size);
  381. }
  382. /* When serial transmit in tx_blocking mode,
  383. * if the activated mode is RT_TRUE, it will return directly */
  384. if (tx_fifo->activated == RT_TRUE) return 0;
  385. tx_fifo->activated = RT_TRUE;
  386. /* Call the transmit interface for transmission */
  387. serial->ops->transmit(serial,
  388. (rt_uint8_t *)buffer,
  389. size,
  390. RT_SERIAL_TX_BLOCKING);
  391. /* Waiting for the transmission to complete */
  392. rt_completion_wait(&(tx_fifo->tx_cpt), RT_WAITING_FOREVER);
  393. return size;
  394. }
  395. /**
  396. * @brief Serial transmit data routines, This function will transmit
  397. * data by using blocking_buf.
  398. * @param dev The pointer of device driver structure
  399. * @param pos Empty parameter.
  400. * @param buffer Transmit data buffer.
  401. * @param size Transmit data buffer length.
  402. * @return Return the final length of data transmit.
  403. */
  404. static rt_size_t _serial_fifo_tx_blocking_buf(struct rt_device *dev,
  405. rt_off_t pos,
  406. const void *buffer,
  407. rt_size_t size)
  408. {
  409. struct rt_serial_device *serial;
  410. struct rt_serial_tx_fifo *tx_fifo = RT_NULL;
  411. rt_size_t length = size;
  412. rt_size_t offset = 0;
  413. if (size == 0) return 0;
  414. RT_ASSERT(dev != RT_NULL);
  415. serial = (struct rt_serial_device *)dev;
  416. RT_ASSERT((serial != RT_NULL) && (buffer != RT_NULL));
  417. tx_fifo = (struct rt_serial_tx_fifo *) serial->serial_tx;
  418. RT_ASSERT(tx_fifo != RT_NULL);
  419. if (rt_thread_self() == RT_NULL || (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM))
  420. {
  421. /* using poll tx when the scheduler not startup or in stream mode */
  422. return _serial_poll_tx(dev, pos, buffer, size);
  423. }
  424. /* When serial transmit in tx_blocking mode,
  425. * if the activated mode is RT_TRUE, it will return directly */
  426. if (tx_fifo->activated == RT_TRUE) return 0;
  427. tx_fifo->activated = RT_TRUE;
  428. while (size)
  429. {
  430. /* Copy one piece of data into the ringbuffer at a time
  431. * until the length of the data is equal to size */
  432. tx_fifo->put_size = rt_ringbuffer_put(&(tx_fifo->rb),
  433. (rt_uint8_t *)buffer + offset,
  434. size);
  435. offset += tx_fifo->put_size;
  436. size -= tx_fifo->put_size;
  437. /* Call the transmit interface for transmission */
  438. serial->ops->transmit(serial,
  439. (rt_uint8_t *)buffer + offset,
  440. tx_fifo->put_size,
  441. RT_SERIAL_TX_BLOCKING);
  442. /* Waiting for the transmission to complete */
  443. rt_completion_wait(&(tx_fifo->tx_cpt), RT_WAITING_FOREVER);
  444. }
  445. return length;
  446. }
  447. /**
  448. * @brief Serial transmit data routines, This function will transmit
  449. * data by using nonblocking.
  450. * @param dev The pointer of device driver structure
  451. * @param pos Empty parameter.
  452. * @param buffer Transmit data buffer.
  453. * @param size Transmit data buffer length.
  454. * @return Return the final length of data transmit.
  455. */
  456. static rt_size_t _serial_fifo_tx_nonblocking(struct rt_device *dev,
  457. rt_off_t pos,
  458. const void *buffer,
  459. rt_size_t size)
  460. {
  461. struct rt_serial_device *serial;
  462. struct rt_serial_tx_fifo *tx_fifo;
  463. rt_base_t level;
  464. rt_size_t length;
  465. RT_ASSERT(dev != RT_NULL);
  466. if (size == 0) return 0;
  467. serial = (struct rt_serial_device *)dev;
  468. RT_ASSERT((serial != RT_NULL) && (buffer != RT_NULL));
  469. tx_fifo = (struct rt_serial_tx_fifo *) serial->serial_tx;
  470. level = rt_hw_interrupt_disable();
  471. if (tx_fifo->activated == RT_FALSE)
  472. {
  473. /* When serial transmit in tx_non_blocking mode, if the activated mode is RT_FALSE,
  474. * start copying data into the ringbuffer */
  475. tx_fifo->activated = RT_TRUE;
  476. /* Copying data into the ringbuffer */
  477. length = rt_ringbuffer_put(&(tx_fifo->rb), buffer, size);
  478. rt_hw_interrupt_enable(level);
  479. rt_uint8_t *put_ptr = RT_NULL;
  480. /* Get the linear length buffer from rinbuffer */
  481. tx_fifo->put_size = rt_serial_get_linear_buffer(&(tx_fifo->rb), &put_ptr);
  482. /* Call the transmit interface for transmission */
  483. serial->ops->transmit(serial,
  484. put_ptr,
  485. tx_fifo->put_size,
  486. RT_SERIAL_TX_NON_BLOCKING);
  487. /* In tx_nonblocking mode, there is no need to call rt_completion_wait() APIs to wait
  488. * for the rt_current_thread to resume */
  489. return length;
  490. }
  491. /* If the activated mode is RT_FALSE, it means that serial device is transmitting,
  492. * where only the data in the ringbuffer and there is no need to call the transmit() API.
  493. * Note that this part of the code requires disable interrupts
  494. * to prevent multi thread reentrant */
  495. /* Copying data into the ringbuffer */
  496. length = rt_ringbuffer_put(&(tx_fifo->rb), buffer, size);
  497. rt_hw_interrupt_enable(level);
  498. return length;
  499. }
  500. /**
  501. * @brief Enable serial transmit mode.
  502. * @param dev The pointer of device driver structure
  503. * @param rx_oflag The flag of that the serial port opens.
  504. * @return Return the status of the operation.
  505. */
  506. static rt_err_t rt_serial_tx_enable(struct rt_device *dev,
  507. rt_uint16_t tx_oflag)
  508. {
  509. struct rt_serial_device *serial;
  510. struct rt_serial_tx_fifo *tx_fifo = RT_NULL;
  511. RT_ASSERT(dev != RT_NULL);
  512. serial = (struct rt_serial_device *)dev;
  513. if (serial->config.tx_bufsz == 0)
  514. {
  515. /* Cannot use RT_SERIAL_TX_NON_BLOCKING when tx_bufsz is 0 */
  516. if (tx_oflag == RT_SERIAL_TX_NON_BLOCKING)
  517. {
  518. LOG_E("(%s) serial device with misconfigure: tx_bufsz = 0",
  519. dev->parent.name);
  520. return -RT_EINVAL;
  521. }
  522. #ifndef RT_USING_DEVICE_OPS
  523. dev->write = _serial_poll_tx;
  524. #endif
  525. dev->open_flag |= RT_SERIAL_TX_BLOCKING;
  526. return RT_EOK;
  527. }
  528. /* Limits the minimum value of tx_bufsz */
  529. if (serial->config.tx_bufsz < RT_SERIAL_TX_MINBUFSZ)
  530. serial->config.tx_bufsz = RT_SERIAL_TX_MINBUFSZ;
  531. if (tx_oflag == RT_SERIAL_TX_BLOCKING)
  532. {
  533. /* When using RT_SERIAL_TX_BLOCKING, it is necessary to determine
  534. * whether serial device needs to use buffer */
  535. rt_err_t optmode; /* The operating mode used by serial device */
  536. /* Call the Control() API to get the operating mode */
  537. optmode = serial->ops->control(serial,
  538. RT_DEVICE_CHECK_OPTMODE,
  539. (void *)RT_DEVICE_FLAG_TX_BLOCKING);
  540. if (optmode == RT_SERIAL_TX_BLOCKING_BUFFER)
  541. {
  542. /* If use RT_SERIAL_TX_BLOCKING_BUFFER, the ringbuffer is initialized */
  543. tx_fifo = (struct rt_serial_tx_fifo *) rt_malloc
  544. (sizeof(struct rt_serial_tx_fifo) + serial->config.tx_bufsz);
  545. RT_ASSERT(tx_fifo != RT_NULL);
  546. rt_ringbuffer_init(&(tx_fifo->rb),
  547. tx_fifo->buffer,
  548. serial->config.tx_bufsz);
  549. serial->serial_tx = tx_fifo;
  550. #ifndef RT_USING_DEVICE_OPS
  551. dev->write = _serial_fifo_tx_blocking_buf;
  552. #endif
  553. }
  554. else
  555. {
  556. /* If not use RT_SERIAL_TX_BLOCKING_BUFFER,
  557. * the control() API is called to configure the serial device */
  558. tx_fifo = (struct rt_serial_tx_fifo*) rt_malloc
  559. (sizeof(struct rt_serial_tx_fifo));
  560. RT_ASSERT(tx_fifo != RT_NULL);
  561. serial->serial_tx = tx_fifo;
  562. #ifndef RT_USING_DEVICE_OPS
  563. dev->write = _serial_fifo_tx_blocking_nbuf;
  564. #endif
  565. /* Call the control() API to configure the serial device by RT_SERIAL_TX_BLOCKING*/
  566. serial->ops->control(serial,
  567. RT_DEVICE_CTRL_CONFIG,
  568. (void *)RT_SERIAL_TX_BLOCKING);
  569. }
  570. tx_fifo->activated = RT_FALSE;
  571. tx_fifo->put_size = 0;
  572. rt_memset(&tx_fifo->rb, RT_NULL, sizeof(tx_fifo->rb));
  573. rt_completion_init(&(tx_fifo->tx_cpt));
  574. dev->open_flag |= RT_SERIAL_TX_BLOCKING;
  575. return RT_EOK;
  576. }
  577. /* When using RT_SERIAL_TX_NON_BLOCKING, ringbuffer needs to be initialized,
  578. * and initialize the tx_fifo->activated value is RT_FALSE.
  579. */
  580. tx_fifo = (struct rt_serial_tx_fifo *) rt_malloc
  581. (sizeof(struct rt_serial_tx_fifo) + serial->config.tx_bufsz);
  582. RT_ASSERT(tx_fifo != RT_NULL);
  583. tx_fifo->activated = RT_FALSE;
  584. tx_fifo->put_size = 0;
  585. rt_ringbuffer_init(&(tx_fifo->rb),
  586. tx_fifo->buffer,
  587. serial->config.tx_bufsz);
  588. serial->serial_tx = tx_fifo;
  589. #ifndef RT_USING_DEVICE_OPS
  590. dev->write = _serial_fifo_tx_nonblocking;
  591. #endif
  592. dev->open_flag |= RT_SERIAL_TX_NON_BLOCKING;
  593. /* Call the control() API to configure the serial device by RT_SERIAL_TX_NON_BLOCKING*/
  594. serial->ops->control(serial,
  595. RT_DEVICE_CTRL_CONFIG,
  596. (void *)RT_SERIAL_TX_NON_BLOCKING);
  597. return RT_EOK;
  598. }
  599. /**
  600. * @brief Enable serial receive mode.
  601. * @param dev The pointer of device driver structure
  602. * @param rx_oflag The flag of that the serial port opens.
  603. * @return Return the status of the operation.
  604. */
  605. static rt_err_t rt_serial_rx_enable(struct rt_device *dev,
  606. rt_uint16_t rx_oflag)
  607. {
  608. struct rt_serial_device *serial;
  609. struct rt_serial_rx_fifo *rx_fifo = RT_NULL;
  610. RT_ASSERT(dev != RT_NULL);
  611. serial = (struct rt_serial_device *)dev;
  612. if (serial->config.rx_bufsz == 0)
  613. {
  614. /* Cannot use RT_SERIAL_RX_NON_BLOCKING when rx_bufsz is 0 */
  615. if (rx_oflag == RT_SERIAL_RX_NON_BLOCKING)
  616. {
  617. LOG_E("(%s) serial device with misconfigure: rx_bufsz = 0",
  618. dev->parent.name);
  619. return -RT_EINVAL;
  620. }
  621. #ifndef RT_USING_DEVICE_OPS
  622. dev->read = _serial_poll_rx;
  623. #endif
  624. dev->open_flag |= RT_SERIAL_RX_BLOCKING;
  625. return RT_EOK;
  626. }
  627. /* Limits the minimum value of rx_bufsz */
  628. if (serial->config.rx_bufsz < RT_SERIAL_RX_MINBUFSZ)
  629. serial->config.rx_bufsz = RT_SERIAL_RX_MINBUFSZ;
  630. rx_fifo = (struct rt_serial_rx_fifo *) rt_malloc
  631. (sizeof(struct rt_serial_rx_fifo) + serial->config.rx_bufsz);
  632. RT_ASSERT(rx_fifo != RT_NULL);
  633. rt_ringbuffer_init(&(rx_fifo->rb), rx_fifo->buffer, serial->config.rx_bufsz);
  634. serial->serial_rx = rx_fifo;
  635. #ifndef RT_USING_DEVICE_OPS
  636. dev->read = _serial_fifo_rx;
  637. #endif
  638. if (rx_oflag == RT_SERIAL_RX_NON_BLOCKING)
  639. {
  640. dev->open_flag |= RT_SERIAL_RX_NON_BLOCKING;
  641. /* Call the control() API to configure the serial device by RT_SERIAL_RX_NON_BLOCKING*/
  642. serial->ops->control(serial,
  643. RT_DEVICE_CTRL_CONFIG,
  644. (void *) RT_SERIAL_RX_NON_BLOCKING);
  645. return RT_EOK;
  646. }
  647. /* When using RT_SERIAL_RX_BLOCKING, rt_completion_init() and rx_cpt_index are initialized */
  648. rx_fifo->rx_cpt_index = 0;
  649. rt_completion_init(&(rx_fifo->rx_cpt));
  650. dev->open_flag |= RT_SERIAL_RX_BLOCKING;
  651. /* Call the control() API to configure the serial device by RT_SERIAL_RX_BLOCKING*/
  652. serial->ops->control(serial,
  653. RT_DEVICE_CTRL_CONFIG,
  654. (void *) RT_SERIAL_RX_BLOCKING);
  655. return RT_EOK;
  656. }
  657. /**
  658. * @brief Disable serial receive mode.
  659. * @param dev The pointer of device driver structure
  660. * @param rx_oflag The flag of that the serial port opens.
  661. * @return Return the status of the operation.
  662. */
  663. static rt_err_t rt_serial_rx_disable(struct rt_device *dev,
  664. rt_uint16_t rx_oflag)
  665. {
  666. struct rt_serial_device *serial;
  667. struct rt_serial_rx_fifo *rx_fifo;
  668. RT_ASSERT(dev != RT_NULL);
  669. serial = (struct rt_serial_device *)dev;
  670. #ifndef RT_USING_DEVICE_OPS
  671. dev->read = RT_NULL;
  672. #endif
  673. if (serial->serial_rx == RT_NULL) return RT_EOK;
  674. do
  675. {
  676. if (rx_oflag == RT_SERIAL_RX_NON_BLOCKING)
  677. {
  678. dev->open_flag &= ~ RT_SERIAL_RX_NON_BLOCKING;
  679. serial->ops->control(serial,
  680. RT_DEVICE_CTRL_CLR_INT,
  681. (void *)RT_SERIAL_RX_NON_BLOCKING);
  682. break;
  683. }
  684. dev->open_flag &= ~ RT_SERIAL_RX_BLOCKING;
  685. serial->ops->control(serial,
  686. RT_DEVICE_CTRL_CLR_INT,
  687. (void *)RT_SERIAL_RX_BLOCKING);
  688. } while (0);
  689. rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
  690. RT_ASSERT(rx_fifo != RT_NULL);
  691. rt_free(rx_fifo);
  692. serial->serial_rx = RT_NULL;
  693. return RT_EOK;
  694. }
  695. /**
  696. * @brief Disable serial tranmit mode.
  697. * @param dev The pointer of device driver structure
  698. * @param rx_oflag The flag of that the serial port opens.
  699. * @return Return the status of the operation.
  700. */
  701. static rt_err_t rt_serial_tx_disable(struct rt_device *dev,
  702. rt_uint16_t tx_oflag)
  703. {
  704. struct rt_serial_device *serial;
  705. struct rt_serial_tx_fifo *tx_fifo;
  706. RT_ASSERT(dev != RT_NULL);
  707. serial = (struct rt_serial_device *)dev;
  708. #ifndef RT_USING_DEVICE_OPS
  709. dev->write = RT_NULL;
  710. #endif
  711. if (serial->serial_tx == RT_NULL) return RT_EOK;
  712. tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
  713. RT_ASSERT(tx_fifo != RT_NULL);
  714. do
  715. {
  716. if (tx_oflag == RT_SERIAL_TX_NON_BLOCKING)
  717. {
  718. dev->open_flag &= ~ RT_SERIAL_TX_NON_BLOCKING;
  719. serial->ops->control(serial,
  720. RT_DEVICE_CTRL_CLR_INT,
  721. (void *)RT_SERIAL_TX_NON_BLOCKING);
  722. break;
  723. }
  724. rt_completion_done(&(tx_fifo->tx_cpt));
  725. dev->open_flag &= ~ RT_SERIAL_TX_BLOCKING;
  726. serial->ops->control(serial,
  727. RT_DEVICE_CTRL_CLR_INT,
  728. (void *)RT_SERIAL_TX_BLOCKING);
  729. } while (0);
  730. rt_free(tx_fifo);
  731. serial->serial_tx = RT_NULL;
  732. rt_memset(&serial->rx_notify, 0, sizeof(struct rt_device_notify));
  733. return RT_EOK;
  734. }
  735. /**
  736. * @brief Initialize the serial device.
  737. * @param dev The pointer of device driver structure
  738. * @return Return the status of the operation.
  739. */
  740. static rt_err_t rt_serial_init(struct rt_device *dev)
  741. {
  742. rt_err_t result = RT_EOK;
  743. struct rt_serial_device *serial;
  744. RT_ASSERT(dev != RT_NULL);
  745. serial = (struct rt_serial_device *)dev;
  746. RT_ASSERT(serial->ops->transmit != RT_NULL);
  747. /* initialize rx/tx */
  748. serial->serial_rx = RT_NULL;
  749. serial->serial_tx = RT_NULL;
  750. /* apply configuration */
  751. if (serial->ops->configure)
  752. result = serial->ops->configure(serial, &serial->config);
  753. return result;
  754. }
  755. /**
  756. * @brief Open the serial device.
  757. * @param dev The pointer of device driver structure
  758. * @param oflag The flag of that the serial port opens.
  759. * @return Return the status of the operation.
  760. */
  761. static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
  762. {
  763. struct rt_serial_device *serial;
  764. RT_ASSERT(dev != RT_NULL);
  765. serial = (struct rt_serial_device *)dev;
  766. /* Check that the device has been turned on */
  767. if ((dev->open_flag) & (15 << 12))
  768. {
  769. LOG_D("(%s) serial device has already been opened, it will run in its original configuration", dev->parent.name);
  770. return RT_EOK;
  771. }
  772. LOG_D("open serial device: 0x%08x with open flag: 0x%04x",
  773. dev, oflag);
  774. /* By default, the receive mode of a serial devide is RT_SERIAL_RX_NON_BLOCKING */
  775. if ((oflag & RT_SERIAL_RX_BLOCKING) == RT_SERIAL_RX_BLOCKING)
  776. dev->open_flag |= RT_SERIAL_RX_BLOCKING;
  777. else
  778. dev->open_flag |= RT_SERIAL_RX_NON_BLOCKING;
  779. /* By default, the transmit mode of a serial devide is RT_SERIAL_TX_BLOCKING */
  780. if ((oflag & RT_SERIAL_TX_NON_BLOCKING) == RT_SERIAL_TX_NON_BLOCKING)
  781. dev->open_flag |= RT_SERIAL_TX_NON_BLOCKING;
  782. else
  783. dev->open_flag |= RT_SERIAL_TX_BLOCKING;
  784. /* set steam flag */
  785. if ((oflag & RT_DEVICE_FLAG_STREAM) ||
  786. (dev->open_flag & RT_DEVICE_FLAG_STREAM))
  787. dev->open_flag |= RT_DEVICE_FLAG_STREAM;
  788. /* initialize the Rx structure according to open flag */
  789. if (serial->serial_rx == RT_NULL)
  790. rt_serial_rx_enable(dev, dev->open_flag &
  791. (RT_SERIAL_RX_BLOCKING | RT_SERIAL_RX_NON_BLOCKING));
  792. /* initialize the Tx structure according to open flag */
  793. if (serial->serial_tx == RT_NULL)
  794. rt_serial_tx_enable(dev, dev->open_flag &
  795. (RT_SERIAL_TX_BLOCKING | RT_SERIAL_TX_NON_BLOCKING));
  796. return RT_EOK;
  797. }
  798. /**
  799. * @brief Close the serial device.
  800. * @param dev The pointer of device driver structure
  801. * @return Return the status of the operation.
  802. */
  803. static rt_err_t rt_serial_close(struct rt_device *dev)
  804. {
  805. struct rt_serial_device *serial;
  806. RT_ASSERT(dev != RT_NULL);
  807. serial = (struct rt_serial_device *)dev;
  808. /* this device has more reference count */
  809. if (dev->ref_count > 1) return -RT_ERROR;
  810. /* Disable serial receive mode. */
  811. rt_serial_rx_disable(dev, dev->open_flag &
  812. (RT_SERIAL_RX_BLOCKING | RT_SERIAL_RX_NON_BLOCKING));
  813. /* Disable serial tranmit mode. */
  814. rt_serial_tx_disable(dev, dev->open_flag &
  815. (RT_SERIAL_TX_BLOCKING | RT_SERIAL_TX_NON_BLOCKING));
  816. /* Clear the callback function */
  817. serial->parent.rx_indicate = RT_NULL;
  818. serial->parent.tx_complete = RT_NULL;
  819. /* Call the control() API to close the serial device */
  820. serial->ops->control(serial, RT_DEVICE_CTRL_CLOSE, RT_NULL);
  821. dev->flag &= ~RT_DEVICE_FLAG_ACTIVATED;
  822. return RT_EOK;
  823. }
  824. #ifdef RT_USING_POSIX_TERMIOS
  825. struct speed_baudrate_item
  826. {
  827. speed_t speed;
  828. int baudrate;
  829. };
  830. const static struct speed_baudrate_item _tbl[] =
  831. {
  832. {B2400, BAUD_RATE_2400},
  833. {B4800, BAUD_RATE_4800},
  834. {B9600, BAUD_RATE_9600},
  835. {B19200, BAUD_RATE_19200},
  836. {B38400, BAUD_RATE_38400},
  837. {B57600, BAUD_RATE_57600},
  838. {B115200, BAUD_RATE_115200},
  839. {B230400, BAUD_RATE_230400},
  840. {B460800, BAUD_RATE_460800},
  841. {B500000, BAUD_RATE_500000},
  842. {B921600, BAUD_RATE_921600},
  843. {B2000000, BAUD_RATE_2000000},
  844. {B3000000, BAUD_RATE_3000000},
  845. };
  846. static speed_t _get_speed(int baudrate)
  847. {
  848. int index;
  849. for (index = 0; index < sizeof(_tbl)/sizeof(_tbl[0]); index ++)
  850. {
  851. if (_tbl[index].baudrate == baudrate)
  852. return _tbl[index].speed;
  853. }
  854. return B0;
  855. }
  856. static int _get_baudrate(speed_t speed)
  857. {
  858. int index;
  859. for (index = 0; index < sizeof(_tbl)/sizeof(_tbl[0]); index ++)
  860. {
  861. if (_tbl[index].speed == speed)
  862. return _tbl[index].baudrate;
  863. }
  864. return 0;
  865. }
  866. static void _tc_flush(struct rt_serial_device *serial, int queue)
  867. {
  868. rt_base_t level;
  869. int ch = -1;
  870. struct rt_serial_rx_fifo *rx_fifo = RT_NULL;
  871. struct rt_device *device = RT_NULL;
  872. RT_ASSERT(serial != RT_NULL);
  873. device = &(serial->parent);
  874. rx_fifo = (struct rt_serial_rx_fifo *) serial->serial_rx;
  875. switch(queue)
  876. {
  877. case TCIFLUSH:
  878. case TCIOFLUSH:
  879. RT_ASSERT(rx_fifo != RT_NULL);
  880. if((device->open_flag & RT_DEVICE_FLAG_INT_RX) || (device->open_flag & RT_DEVICE_FLAG_DMA_RX))
  881. {
  882. RT_ASSERT(RT_NULL != rx_fifo);
  883. level = rt_hw_interrupt_disable();
  884. rx_fifo->rx_cpt_index = 0;
  885. rt_hw_interrupt_enable(level);
  886. }
  887. else
  888. {
  889. while (1)
  890. {
  891. ch = serial->ops->getc(serial);
  892. if (ch == -1) break;
  893. }
  894. }
  895. break;
  896. case TCOFLUSH:
  897. break;
  898. }
  899. }
  900. #endif /* RT_USING_POSIX_TERMIOS */
  901. /**
  902. * @brief Control the serial device.
  903. * @param dev The pointer of device driver structure
  904. * @param cmd The command value that controls the serial device
  905. * @param args The parameter value that controls the serial device
  906. * @return Return the status of the operation.
  907. */
  908. static rt_err_t rt_serial_control(struct rt_device *dev,
  909. int cmd,
  910. void *args)
  911. {
  912. rt_err_t ret = RT_EOK;
  913. struct rt_serial_device *serial;
  914. RT_ASSERT(dev != RT_NULL);
  915. serial = (struct rt_serial_device *)dev;
  916. switch (cmd)
  917. {
  918. case RT_DEVICE_CTRL_SUSPEND:
  919. /* suspend device */
  920. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  921. break;
  922. case RT_DEVICE_CTRL_RESUME:
  923. /* resume device */
  924. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  925. break;
  926. case RT_DEVICE_CTRL_CONFIG:
  927. if (args != RT_NULL)
  928. {
  929. struct serial_configure *pconfig = (struct serial_configure *) args;
  930. if (((pconfig->rx_bufsz != serial->config.rx_bufsz) || (pconfig->tx_bufsz != serial->config.tx_bufsz))
  931. && serial->parent.ref_count)
  932. {
  933. /*can not change buffer size*/
  934. return -RT_EBUSY;
  935. }
  936. /* set serial configure */
  937. serial->config = *pconfig;
  938. serial->ops->configure(serial, (struct serial_configure *) args);
  939. }
  940. break;
  941. case RT_DEVICE_CTRL_NOTIFY_SET:
  942. if (args)
  943. {
  944. rt_memcpy(&serial->rx_notify, args, sizeof(struct rt_device_notify));
  945. }
  946. break;
  947. case RT_DEVICE_CTRL_CONSOLE_OFLAG:
  948. if (args)
  949. {
  950. *(rt_uint16_t*)args = RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM;
  951. }
  952. break;
  953. #ifdef RT_USING_POSIX_STDIO
  954. #ifdef RT_USING_POSIX_TERMIOS
  955. case TCGETA:
  956. {
  957. struct termios *tio = (struct termios*)args;
  958. if (tio == RT_NULL) return -RT_EINVAL;
  959. tio->c_iflag = 0;
  960. tio->c_oflag = 0;
  961. tio->c_lflag = 0;
  962. /* update oflag for console device */
  963. if (rt_console_get_device() == dev)
  964. tio->c_oflag = OPOST | ONLCR;
  965. /* set cflag */
  966. tio->c_cflag = 0;
  967. if (serial->config.data_bits == DATA_BITS_5)
  968. tio->c_cflag = CS5;
  969. else if (serial->config.data_bits == DATA_BITS_6)
  970. tio->c_cflag = CS6;
  971. else if (serial->config.data_bits == DATA_BITS_7)
  972. tio->c_cflag = CS7;
  973. else if (serial->config.data_bits == DATA_BITS_8)
  974. tio->c_cflag = CS8;
  975. if (serial->config.stop_bits == STOP_BITS_2)
  976. tio->c_cflag |= CSTOPB;
  977. if (serial->config.parity == PARITY_EVEN)
  978. tio->c_cflag |= PARENB;
  979. else if (serial->config.parity == PARITY_ODD)
  980. tio->c_cflag |= (PARODD | PARENB);
  981. if (serial->config.flowcontrol == RT_SERIAL_FLOWCONTROL_CTSRTS)
  982. tio->c_cflag |= CRTSCTS;
  983. cfsetospeed(tio, _get_speed(serial->config.baud_rate));
  984. }
  985. break;
  986. case TCSETAW:
  987. case TCSETAF:
  988. case TCSETA:
  989. {
  990. int baudrate;
  991. struct serial_configure config;
  992. struct termios *tio = (struct termios*)args;
  993. if (tio == RT_NULL) return -RT_EINVAL;
  994. config = serial->config;
  995. baudrate = _get_baudrate(cfgetospeed(tio));
  996. config.baud_rate = baudrate;
  997. switch (tio->c_cflag & CSIZE)
  998. {
  999. case CS5:
  1000. config.data_bits = DATA_BITS_5;
  1001. break;
  1002. case CS6:
  1003. config.data_bits = DATA_BITS_6;
  1004. break;
  1005. case CS7:
  1006. config.data_bits = DATA_BITS_7;
  1007. break;
  1008. default:
  1009. config.data_bits = DATA_BITS_8;
  1010. break;
  1011. }
  1012. if (tio->c_cflag & CSTOPB) config.stop_bits = STOP_BITS_2;
  1013. else config.stop_bits = STOP_BITS_1;
  1014. if (tio->c_cflag & PARENB)
  1015. {
  1016. if (tio->c_cflag & PARODD) config.parity = PARITY_ODD;
  1017. else config.parity = PARITY_EVEN;
  1018. }
  1019. else config.parity = PARITY_NONE;
  1020. if (tio->c_cflag & CRTSCTS) config.flowcontrol = RT_SERIAL_FLOWCONTROL_CTSRTS;
  1021. else config.flowcontrol = RT_SERIAL_FLOWCONTROL_NONE;
  1022. /* set serial configure */
  1023. serial->config = config;
  1024. serial->ops->configure(serial, &config);
  1025. }
  1026. break;
  1027. case TCFLSH:
  1028. {
  1029. int queue = (int)args;
  1030. _tc_flush(serial, queue);
  1031. }
  1032. break;
  1033. case TCXONC:
  1034. break;
  1035. #endif /*RT_USING_POSIX_TERMIOS*/
  1036. case TIOCSWINSZ:
  1037. {
  1038. struct winsize* p_winsize;
  1039. p_winsize = (struct winsize*)args;
  1040. rt_kprintf("\x1b[8;%d;%dt", p_winsize->ws_col, p_winsize->ws_row);
  1041. }
  1042. break;
  1043. case TIOCGWINSZ:
  1044. {
  1045. struct winsize* p_winsize;
  1046. p_winsize = (struct winsize*)args;
  1047. if(rt_thread_self() != rt_thread_find(FINSH_THREAD_NAME))
  1048. {
  1049. /* only can be used in tshell thread; otherwise, return default size */
  1050. p_winsize->ws_col = 80;
  1051. p_winsize->ws_row = 24;
  1052. }
  1053. else
  1054. {
  1055. #include <shell.h>
  1056. #define _TIO_BUFLEN 20
  1057. char _tio_buf[_TIO_BUFLEN];
  1058. unsigned char cnt1, cnt2, cnt3, i;
  1059. char row_s[4], col_s[4];
  1060. char *p;
  1061. rt_memset(_tio_buf, 0, _TIO_BUFLEN);
  1062. /* send the command to terminal for getting the window size of the terminal */
  1063. rt_kprintf("\033[18t");
  1064. /* waiting for the response from the terminal */
  1065. i = 0;
  1066. while(i < _TIO_BUFLEN)
  1067. {
  1068. _tio_buf[i] = finsh_getchar();
  1069. if(_tio_buf[i] != 't')
  1070. {
  1071. i ++;
  1072. }
  1073. else
  1074. {
  1075. break;
  1076. }
  1077. }
  1078. if(i == _TIO_BUFLEN)
  1079. {
  1080. /* buffer overloaded, and return default size */
  1081. p_winsize->ws_col = 80;
  1082. p_winsize->ws_row = 24;
  1083. break;
  1084. }
  1085. /* interpreting data eg: "\033[8;1;15t" which means row is 1 and col is 15 (unit: size of ONE character) */
  1086. rt_memset(row_s,0,4);
  1087. rt_memset(col_s,0,4);
  1088. cnt1 = 0;
  1089. while(_tio_buf[cnt1] != ';' && cnt1 < _TIO_BUFLEN)
  1090. {
  1091. cnt1++;
  1092. }
  1093. cnt2 = ++cnt1;
  1094. while(_tio_buf[cnt2] != ';' && cnt2 < _TIO_BUFLEN)
  1095. {
  1096. cnt2++;
  1097. }
  1098. p = row_s;
  1099. while(cnt1 < cnt2)
  1100. {
  1101. *p++ = _tio_buf[cnt1++];
  1102. }
  1103. p = col_s;
  1104. cnt2++;
  1105. cnt3 = rt_strlen(_tio_buf) - 1;
  1106. while(cnt2 < cnt3)
  1107. {
  1108. *p++ = _tio_buf[cnt2++];
  1109. }
  1110. /* load the window size date */
  1111. p_winsize->ws_col = atoi(col_s);
  1112. p_winsize->ws_row = atoi(row_s);
  1113. #undef _TIO_BUFLEN
  1114. }
  1115. p_winsize->ws_xpixel = 0;/* unused */
  1116. p_winsize->ws_ypixel = 0;/* unused */
  1117. }
  1118. break;
  1119. case FIONREAD:
  1120. {
  1121. rt_size_t recved = 0;
  1122. rt_base_t level;
  1123. struct rt_serial_rx_fifo * rx_fifo = (struct rt_serial_rx_fifo *) serial->serial_rx;
  1124. level = rt_hw_interrupt_disable();
  1125. recved = rt_ringbuffer_data_len(&(rx_fifo->rb));
  1126. rt_hw_interrupt_enable(level);
  1127. *(rt_size_t *)args = recved;
  1128. }
  1129. break;
  1130. #endif /* RT_USING_POSIX_STDIO */
  1131. default :
  1132. /* control device */
  1133. ret = serial->ops->control(serial, cmd, args);
  1134. break;
  1135. }
  1136. return ret;
  1137. }
  1138. #ifdef RT_USING_DEVICE_OPS
  1139. static rt_size_t rt_serial_read(struct rt_device *dev,
  1140. rt_off_t pos,
  1141. void *buffer,
  1142. rt_size_t size)
  1143. {
  1144. struct rt_serial_device *serial;
  1145. RT_ASSERT(dev != RT_NULL);
  1146. if (size == 0) return 0;
  1147. serial = (struct rt_serial_device *)dev;
  1148. if (serial->config.rx_bufsz)
  1149. {
  1150. return _serial_fifo_rx(dev, pos, buffer, size);
  1151. }
  1152. return _serial_poll_rx(dev, pos, buffer, size);
  1153. }
  1154. static rt_size_t rt_serial_write(struct rt_device *dev,
  1155. rt_off_t pos,
  1156. const void *buffer,
  1157. rt_size_t size)
  1158. {
  1159. struct rt_serial_device *serial;
  1160. struct rt_serial_tx_fifo *tx_fifo;
  1161. RT_ASSERT(dev != RT_NULL);
  1162. if (size == 0) return 0;
  1163. serial = (struct rt_serial_device *)dev;
  1164. RT_ASSERT((serial != RT_NULL) && (buffer != RT_NULL));
  1165. tx_fifo = (struct rt_serial_tx_fifo *) serial->serial_tx;
  1166. if (serial->config.tx_bufsz == 0)
  1167. {
  1168. return _serial_poll_tx(dev, pos, buffer, size);
  1169. }
  1170. if (dev->open_flag & RT_SERIAL_TX_BLOCKING)
  1171. {
  1172. if ((tx_fifo->rb.buffer_ptr) == RT_NULL)
  1173. {
  1174. return _serial_fifo_tx_blocking_nbuf(dev, pos, buffer, size);
  1175. }
  1176. return _serial_fifo_tx_blocking_buf(dev, pos, buffer, size);
  1177. }
  1178. return _serial_fifo_tx_nonblocking(dev, pos, buffer, size);
  1179. }
  1180. const static struct rt_device_ops serial_ops =
  1181. {
  1182. rt_serial_init,
  1183. rt_serial_open,
  1184. rt_serial_close,
  1185. rt_serial_read,
  1186. rt_serial_write,
  1187. rt_serial_control
  1188. };
  1189. #endif
  1190. /**
  1191. * @brief Register the serial device.
  1192. * @param serial RT-thread serial device.
  1193. * @param name The device driver's name
  1194. * @param flag The capabilities flag of device.
  1195. * @param data The device driver's data.
  1196. * @return Return the status of the operation.
  1197. */
  1198. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
  1199. const char *name,
  1200. rt_uint32_t flag,
  1201. void *data)
  1202. {
  1203. rt_err_t ret;
  1204. struct rt_device *device;
  1205. RT_ASSERT(serial != RT_NULL);
  1206. device = &(serial->parent);
  1207. device->type = RT_Device_Class_Char;
  1208. device->rx_indicate = RT_NULL;
  1209. device->tx_complete = RT_NULL;
  1210. #ifdef RT_USING_DEVICE_OPS
  1211. device->ops = &serial_ops;
  1212. #else
  1213. device->init = rt_serial_init;
  1214. device->open = rt_serial_open;
  1215. device->close = rt_serial_close;
  1216. device->read = RT_NULL;
  1217. device->write = RT_NULL;
  1218. device->control = rt_serial_control;
  1219. #endif
  1220. device->user_data = data;
  1221. /* register a character device */
  1222. ret = rt_device_register(device, name, flag);
  1223. #ifdef RT_USING_POSIX_STDIO
  1224. /* set fops */
  1225. device->fops = &_serial_fops;
  1226. #endif
  1227. return ret;
  1228. }
  1229. /**
  1230. * @brief ISR for serial interrupt
  1231. * @param serial RT-thread serial device.
  1232. * @param event ISR event type.
  1233. */
  1234. void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
  1235. {
  1236. RT_ASSERT(serial != RT_NULL);
  1237. switch (event & 0xff)
  1238. {
  1239. /* Interrupt receive event */
  1240. case RT_SERIAL_EVENT_RX_IND:
  1241. case RT_SERIAL_EVENT_RX_DMADONE:
  1242. {
  1243. struct rt_serial_rx_fifo *rx_fifo;
  1244. rt_size_t rx_length = 0;
  1245. rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
  1246. rt_base_t level;
  1247. RT_ASSERT(rx_fifo != RT_NULL);
  1248. /* If the event is RT_SERIAL_EVENT_RX_IND, rx_length is equal to 0 */
  1249. rx_length = (event & (~0xff)) >> 8;
  1250. if (rx_length)
  1251. { /* RT_SERIAL_EVENT_RX_DMADONE MODE */
  1252. level = rt_hw_interrupt_disable();
  1253. rt_serial_update_write_index(&(rx_fifo->rb), rx_length);
  1254. rt_hw_interrupt_enable(level);
  1255. }
  1256. /* Get the length of the data from the ringbuffer */
  1257. rx_length = rt_ringbuffer_data_len(&rx_fifo->rb);
  1258. if (rx_length == 0) break;
  1259. if (serial->parent.open_flag & RT_SERIAL_RX_BLOCKING)
  1260. {
  1261. if (rx_fifo->rx_cpt_index && rx_length >= rx_fifo->rx_cpt_index )
  1262. {
  1263. rx_fifo->rx_cpt_index = 0;
  1264. rt_completion_done(&(rx_fifo->rx_cpt));
  1265. }
  1266. }
  1267. /* Trigger the receiving completion callback */
  1268. if (serial->parent.rx_indicate != RT_NULL)
  1269. serial->parent.rx_indicate(&(serial->parent), rx_length);
  1270. if (serial->rx_notify.notify)
  1271. {
  1272. serial->rx_notify.notify(serial->rx_notify.dev);
  1273. }
  1274. break;
  1275. }
  1276. /* Interrupt transmit event */
  1277. case RT_SERIAL_EVENT_TX_DONE:
  1278. {
  1279. struct rt_serial_tx_fifo *tx_fifo;
  1280. rt_size_t tx_length = 0;
  1281. tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
  1282. RT_ASSERT(tx_fifo != RT_NULL);
  1283. /* Get the length of the data from the ringbuffer */
  1284. tx_length = rt_ringbuffer_data_len(&tx_fifo->rb);
  1285. /* If there is no data in tx_ringbuffer,
  1286. * then the transmit completion callback is triggered*/
  1287. if (tx_length == 0)
  1288. {
  1289. tx_fifo->activated = RT_FALSE;
  1290. /* Trigger the transmit completion callback */
  1291. if (serial->parent.tx_complete != RT_NULL)
  1292. serial->parent.tx_complete(&serial->parent, RT_NULL);
  1293. if (serial->parent.open_flag & RT_SERIAL_TX_BLOCKING)
  1294. rt_completion_done(&(tx_fifo->tx_cpt));
  1295. break;
  1296. }
  1297. /* Call the transmit interface for transmission again */
  1298. /* Note that in interrupt mode, tx_fifo->buffer and tx_length
  1299. * are inactive parameters */
  1300. serial->ops->transmit(serial,
  1301. tx_fifo->buffer,
  1302. tx_length,
  1303. serial->parent.open_flag & ( \
  1304. RT_SERIAL_TX_BLOCKING | \
  1305. RT_SERIAL_TX_NON_BLOCKING));
  1306. break;
  1307. }
  1308. case RT_SERIAL_EVENT_TX_DMADONE:
  1309. {
  1310. struct rt_serial_tx_fifo *tx_fifo;
  1311. tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
  1312. RT_ASSERT(tx_fifo != RT_NULL);
  1313. tx_fifo->activated = RT_FALSE;
  1314. /* Trigger the transmit completion callback */
  1315. if (serial->parent.tx_complete != RT_NULL)
  1316. serial->parent.tx_complete(&serial->parent, RT_NULL);
  1317. if (serial->parent.open_flag & RT_SERIAL_TX_BLOCKING)
  1318. {
  1319. rt_completion_done(&(tx_fifo->tx_cpt));
  1320. break;
  1321. }
  1322. rt_serial_update_read_index(&tx_fifo->rb, tx_fifo->put_size);
  1323. /* Get the length of the data from the ringbuffer.
  1324. * If there is some data in tx_ringbuffer,
  1325. * then call the transmit interface for transmission again */
  1326. if (rt_ringbuffer_data_len(&tx_fifo->rb))
  1327. {
  1328. tx_fifo->activated = RT_TRUE;
  1329. rt_uint8_t *put_ptr = RT_NULL;
  1330. /* Get the linear length buffer from rinbuffer */
  1331. tx_fifo->put_size = rt_serial_get_linear_buffer(&(tx_fifo->rb), &put_ptr);
  1332. /* Call the transmit interface for transmission again */
  1333. serial->ops->transmit(serial,
  1334. put_ptr,
  1335. tx_fifo->put_size,
  1336. RT_SERIAL_TX_NON_BLOCKING);
  1337. }
  1338. break;
  1339. }
  1340. default:
  1341. break;
  1342. }
  1343. }