serial_v2.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633
  1. /*
  2. * Copyright (c) 2006-2023, 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_file *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_file *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_file *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_file *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_file *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_file *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_ssize_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_ssize_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_ssize_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_ssize_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_ssize_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_ssize_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_ssize_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_ssize_t rst;
  372. RT_ASSERT(dev != RT_NULL);
  373. if (size == 0) return 0;
  374. serial = (struct rt_serial_device *)dev;
  375. RT_ASSERT((serial != RT_NULL) && (buffer != RT_NULL));
  376. tx_fifo = (struct rt_serial_tx_fifo *) serial->serial_tx;
  377. RT_ASSERT(tx_fifo != RT_NULL);
  378. if (rt_thread_self() == RT_NULL || (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM))
  379. {
  380. /* using poll tx when the scheduler not startup or in stream mode */
  381. return _serial_poll_tx(dev, pos, buffer, size);
  382. }
  383. /* When serial transmit in tx_blocking mode,
  384. * if the activated mode is RT_TRUE, it will return directly */
  385. if (tx_fifo->activated == RT_TRUE) return 0;
  386. tx_fifo->activated = RT_TRUE;
  387. /* Call the transmit interface for transmission */
  388. rst = serial->ops->transmit(serial,
  389. (rt_uint8_t *)buffer,
  390. size,
  391. RT_SERIAL_TX_BLOCKING);
  392. /* Waiting for the transmission to complete */
  393. rt_completion_wait(&(tx_fifo->tx_cpt), RT_WAITING_FOREVER);
  394. /* Inactive tx mode flag */
  395. tx_fifo->activated = RT_FALSE;
  396. return rst;
  397. }
  398. /**
  399. * @brief Serial transmit data routines, This function will transmit
  400. * data by using blocking_buf.
  401. * @param dev The pointer of device driver structure
  402. * @param pos Empty parameter.
  403. * @param buffer Transmit data buffer.
  404. * @param size Transmit data buffer length.
  405. * @return Return the final length of data transmit.
  406. */
  407. static rt_ssize_t _serial_fifo_tx_blocking_buf(struct rt_device *dev,
  408. rt_off_t pos,
  409. const void *buffer,
  410. rt_size_t size)
  411. {
  412. struct rt_serial_device *serial;
  413. struct rt_serial_tx_fifo *tx_fifo = RT_NULL;
  414. rt_size_t length = size;
  415. rt_size_t offset = 0;
  416. if (size == 0) return 0;
  417. RT_ASSERT(dev != RT_NULL);
  418. serial = (struct rt_serial_device *)dev;
  419. RT_ASSERT((serial != RT_NULL) && (buffer != RT_NULL));
  420. tx_fifo = (struct rt_serial_tx_fifo *) serial->serial_tx;
  421. RT_ASSERT(tx_fifo != RT_NULL);
  422. if (rt_thread_self() == RT_NULL || (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM))
  423. {
  424. /* using poll tx when the scheduler not startup or in stream mode */
  425. return _serial_poll_tx(dev, pos, buffer, size);
  426. }
  427. /* When serial transmit in tx_blocking mode,
  428. * if the activated mode is RT_TRUE, it will return directly */
  429. if (tx_fifo->activated == RT_TRUE) return 0;
  430. tx_fifo->activated = RT_TRUE;
  431. while (size)
  432. {
  433. /* Copy one piece of data into the ringbuffer at a time
  434. * until the length of the data is equal to size */
  435. tx_fifo->put_size = rt_ringbuffer_put(&(tx_fifo->rb),
  436. (rt_uint8_t *)buffer + offset,
  437. size);
  438. /* Call the transmit interface for transmission */
  439. serial->ops->transmit(serial,
  440. (rt_uint8_t *)buffer + offset,
  441. tx_fifo->put_size,
  442. RT_SERIAL_TX_BLOCKING);
  443. offset += tx_fifo->put_size;
  444. size -= tx_fifo->put_size;
  445. /* Waiting for the transmission to complete */
  446. rt_completion_wait(&(tx_fifo->tx_cpt), RT_WAITING_FOREVER);
  447. }
  448. /* Finally Inactivate the tx->fifo */
  449. tx_fifo->activated = RT_FALSE;
  450. return length;
  451. }
  452. /**
  453. * @brief Serial transmit data routines, This function will transmit
  454. * data by using nonblocking.
  455. * @param dev The pointer of device driver structure
  456. * @param pos Empty parameter.
  457. * @param buffer Transmit data buffer.
  458. * @param size Transmit data buffer length.
  459. * @return Return the final length of data transmit.
  460. */
  461. static rt_ssize_t _serial_fifo_tx_nonblocking(struct rt_device *dev,
  462. rt_off_t pos,
  463. const void *buffer,
  464. rt_size_t size)
  465. {
  466. struct rt_serial_device *serial;
  467. struct rt_serial_tx_fifo *tx_fifo;
  468. rt_base_t level;
  469. rt_size_t length;
  470. RT_ASSERT(dev != RT_NULL);
  471. if (size == 0) return 0;
  472. serial = (struct rt_serial_device *)dev;
  473. RT_ASSERT((serial != RT_NULL) && (buffer != RT_NULL));
  474. tx_fifo = (struct rt_serial_tx_fifo *) serial->serial_tx;
  475. level = rt_hw_interrupt_disable();
  476. if (tx_fifo->activated == RT_FALSE)
  477. {
  478. /* When serial transmit in tx_non_blocking mode, if the activated mode is RT_FALSE,
  479. * start copying data into the ringbuffer */
  480. tx_fifo->activated = RT_TRUE;
  481. /* Copying data into the ringbuffer */
  482. length = rt_ringbuffer_put(&(tx_fifo->rb), buffer, size);
  483. rt_hw_interrupt_enable(level);
  484. rt_uint8_t *put_ptr = RT_NULL;
  485. /* Get the linear length buffer from rinbuffer */
  486. tx_fifo->put_size = rt_serial_get_linear_buffer(&(tx_fifo->rb), &put_ptr);
  487. /* Call the transmit interface for transmission */
  488. serial->ops->transmit(serial,
  489. put_ptr,
  490. tx_fifo->put_size,
  491. RT_SERIAL_TX_NON_BLOCKING);
  492. /* In tx_nonblocking mode, there is no need to call rt_completion_wait() APIs to wait
  493. * for the rt_current_thread to resume */
  494. return length;
  495. }
  496. /* If the activated mode is RT_TRUE, it means that serial device is transmitting,
  497. * where only the data in the ringbuffer and there is no need to call the transmit() API.
  498. * Note that this part of the code requires disable interrupts
  499. * to prevent multi thread reentrant */
  500. /* Copying data into the ringbuffer */
  501. length = rt_ringbuffer_put(&(tx_fifo->rb), buffer, size);
  502. rt_hw_interrupt_enable(level);
  503. return length;
  504. }
  505. /**
  506. * @brief Enable serial transmit mode.
  507. * @param dev The pointer of device driver structure
  508. * @param rx_oflag The flag of that the serial port opens.
  509. * @return Return the status of the operation.
  510. */
  511. static rt_err_t rt_serial_tx_enable(struct rt_device *dev,
  512. rt_uint16_t tx_oflag)
  513. {
  514. struct rt_serial_device *serial;
  515. struct rt_serial_tx_fifo *tx_fifo = RT_NULL;
  516. RT_ASSERT(dev != RT_NULL);
  517. serial = (struct rt_serial_device *)dev;
  518. if (serial->config.tx_bufsz == 0)
  519. {
  520. /* Cannot use RT_SERIAL_TX_NON_BLOCKING when tx_bufsz is 0 */
  521. if (tx_oflag == RT_SERIAL_TX_NON_BLOCKING)
  522. {
  523. LOG_E("(%s) serial device with misconfigure: tx_bufsz = 0",
  524. dev->parent.name);
  525. return -RT_EINVAL;
  526. }
  527. #ifndef RT_USING_DEVICE_OPS
  528. dev->write = _serial_poll_tx;
  529. #endif
  530. dev->open_flag |= RT_SERIAL_TX_BLOCKING;
  531. return RT_EOK;
  532. }
  533. /* Limits the minimum value of tx_bufsz */
  534. if (serial->config.tx_bufsz < RT_SERIAL_TX_MINBUFSZ)
  535. serial->config.tx_bufsz = RT_SERIAL_TX_MINBUFSZ;
  536. if (tx_oflag == RT_SERIAL_TX_BLOCKING)
  537. {
  538. /* When using RT_SERIAL_TX_BLOCKING, it is necessary to determine
  539. * whether serial device needs to use buffer */
  540. rt_err_t optmode; /* The operating mode used by serial device */
  541. /* Call the Control() API to get the operating mode */
  542. optmode = serial->ops->control(serial,
  543. RT_DEVICE_CHECK_OPTMODE,
  544. (void *)RT_DEVICE_FLAG_TX_BLOCKING);
  545. if (optmode == RT_SERIAL_TX_BLOCKING_BUFFER)
  546. {
  547. /* If use RT_SERIAL_TX_BLOCKING_BUFFER, the ringbuffer is initialized */
  548. tx_fifo = (struct rt_serial_tx_fifo *) rt_malloc
  549. (sizeof(struct rt_serial_tx_fifo) + serial->config.tx_bufsz);
  550. RT_ASSERT(tx_fifo != RT_NULL);
  551. rt_ringbuffer_init(&(tx_fifo->rb),
  552. tx_fifo->buffer,
  553. serial->config.tx_bufsz);
  554. serial->serial_tx = tx_fifo;
  555. #ifndef RT_USING_DEVICE_OPS
  556. dev->write = _serial_fifo_tx_blocking_buf;
  557. #endif
  558. }
  559. else
  560. {
  561. /* If not use RT_SERIAL_TX_BLOCKING_BUFFER,
  562. * the control() API is called to configure the serial device */
  563. tx_fifo = (struct rt_serial_tx_fifo*) rt_malloc
  564. (sizeof(struct rt_serial_tx_fifo));
  565. RT_ASSERT(tx_fifo != RT_NULL);
  566. /* Init rb.buffer_ptr to RT_NULL, in rt_serial_write() need check it
  567. * otherwise buffer_ptr maybe a random value, as rt_malloc not init memory */
  568. tx_fifo->rb.buffer_ptr = RT_NULL;
  569. serial->serial_tx = tx_fifo;
  570. #ifndef RT_USING_DEVICE_OPS
  571. dev->write = _serial_fifo_tx_blocking_nbuf;
  572. #endif
  573. /* Call the control() API to configure the serial device by RT_SERIAL_TX_BLOCKING*/
  574. serial->ops->control(serial,
  575. RT_DEVICE_CTRL_CONFIG,
  576. (void *)RT_SERIAL_TX_BLOCKING);
  577. rt_memset(&tx_fifo->rb, RT_NULL, sizeof(tx_fifo->rb));
  578. }
  579. tx_fifo->activated = RT_FALSE;
  580. tx_fifo->put_size = 0;
  581. rt_completion_init(&(tx_fifo->tx_cpt));
  582. dev->open_flag |= RT_SERIAL_TX_BLOCKING;
  583. return RT_EOK;
  584. }
  585. /* When using RT_SERIAL_TX_NON_BLOCKING, ringbuffer needs to be initialized,
  586. * and initialize the tx_fifo->activated value is RT_FALSE.
  587. */
  588. tx_fifo = (struct rt_serial_tx_fifo *) rt_malloc
  589. (sizeof(struct rt_serial_tx_fifo) + serial->config.tx_bufsz);
  590. RT_ASSERT(tx_fifo != RT_NULL);
  591. tx_fifo->activated = RT_FALSE;
  592. tx_fifo->put_size = 0;
  593. rt_ringbuffer_init(&(tx_fifo->rb),
  594. tx_fifo->buffer,
  595. serial->config.tx_bufsz);
  596. serial->serial_tx = tx_fifo;
  597. #ifndef RT_USING_DEVICE_OPS
  598. dev->write = _serial_fifo_tx_nonblocking;
  599. #endif
  600. dev->open_flag |= RT_SERIAL_TX_NON_BLOCKING;
  601. /* Call the control() API to configure the serial device by RT_SERIAL_TX_NON_BLOCKING*/
  602. serial->ops->control(serial,
  603. RT_DEVICE_CTRL_CONFIG,
  604. (void *)RT_SERIAL_TX_NON_BLOCKING);
  605. return RT_EOK;
  606. }
  607. /**
  608. * @brief Enable serial receive mode.
  609. * @param dev The pointer of device driver structure
  610. * @param rx_oflag The flag of that the serial port opens.
  611. * @return Return the status of the operation.
  612. */
  613. static rt_err_t rt_serial_rx_enable(struct rt_device *dev,
  614. rt_uint16_t rx_oflag)
  615. {
  616. struct rt_serial_device *serial;
  617. struct rt_serial_rx_fifo *rx_fifo = RT_NULL;
  618. RT_ASSERT(dev != RT_NULL);
  619. serial = (struct rt_serial_device *)dev;
  620. if (serial->config.rx_bufsz == 0)
  621. {
  622. /* Cannot use RT_SERIAL_RX_NON_BLOCKING when rx_bufsz is 0 */
  623. if (rx_oflag == RT_SERIAL_RX_NON_BLOCKING)
  624. {
  625. LOG_E("(%s) serial device with misconfigure: rx_bufsz = 0",
  626. dev->parent.name);
  627. return -RT_EINVAL;
  628. }
  629. #ifndef RT_USING_DEVICE_OPS
  630. dev->read = _serial_poll_rx;
  631. #endif
  632. dev->open_flag |= RT_SERIAL_RX_BLOCKING;
  633. return RT_EOK;
  634. }
  635. /* Limits the minimum value of rx_bufsz */
  636. if (serial->config.rx_bufsz < RT_SERIAL_RX_MINBUFSZ)
  637. serial->config.rx_bufsz = RT_SERIAL_RX_MINBUFSZ;
  638. rx_fifo = (struct rt_serial_rx_fifo *) rt_malloc
  639. (sizeof(struct rt_serial_rx_fifo) + serial->config.rx_bufsz);
  640. RT_ASSERT(rx_fifo != RT_NULL);
  641. rt_ringbuffer_init(&(rx_fifo->rb), rx_fifo->buffer, serial->config.rx_bufsz);
  642. serial->serial_rx = rx_fifo;
  643. #ifndef RT_USING_DEVICE_OPS
  644. dev->read = _serial_fifo_rx;
  645. #endif
  646. if (rx_oflag == RT_SERIAL_RX_NON_BLOCKING)
  647. {
  648. dev->open_flag |= RT_SERIAL_RX_NON_BLOCKING;
  649. /* Call the control() API to configure the serial device by RT_SERIAL_RX_NON_BLOCKING*/
  650. serial->ops->control(serial,
  651. RT_DEVICE_CTRL_CONFIG,
  652. (void *) RT_SERIAL_RX_NON_BLOCKING);
  653. return RT_EOK;
  654. }
  655. /* When using RT_SERIAL_RX_BLOCKING, rt_completion_init() and rx_cpt_index are initialized */
  656. rx_fifo->rx_cpt_index = 0;
  657. rt_completion_init(&(rx_fifo->rx_cpt));
  658. dev->open_flag |= RT_SERIAL_RX_BLOCKING;
  659. /* Call the control() API to configure the serial device by RT_SERIAL_RX_BLOCKING*/
  660. serial->ops->control(serial,
  661. RT_DEVICE_CTRL_CONFIG,
  662. (void *) RT_SERIAL_RX_BLOCKING);
  663. return RT_EOK;
  664. }
  665. /**
  666. * @brief Disable serial receive mode.
  667. * @param dev The pointer of device driver structure
  668. * @param rx_oflag The flag of that the serial port opens.
  669. * @return Return the status of the operation.
  670. */
  671. static rt_err_t rt_serial_rx_disable(struct rt_device *dev,
  672. rt_uint16_t rx_oflag)
  673. {
  674. struct rt_serial_device *serial;
  675. struct rt_serial_rx_fifo *rx_fifo;
  676. RT_ASSERT(dev != RT_NULL);
  677. serial = (struct rt_serial_device *)dev;
  678. #ifndef RT_USING_DEVICE_OPS
  679. dev->read = RT_NULL;
  680. #endif
  681. if (serial->serial_rx == RT_NULL) return RT_EOK;
  682. do
  683. {
  684. if (rx_oflag == RT_SERIAL_RX_NON_BLOCKING)
  685. {
  686. dev->open_flag &= ~ RT_SERIAL_RX_NON_BLOCKING;
  687. serial->ops->control(serial,
  688. RT_DEVICE_CTRL_CLR_INT,
  689. (void *)RT_SERIAL_RX_NON_BLOCKING);
  690. break;
  691. }
  692. dev->open_flag &= ~ RT_SERIAL_RX_BLOCKING;
  693. serial->ops->control(serial,
  694. RT_DEVICE_CTRL_CLR_INT,
  695. (void *)RT_SERIAL_RX_BLOCKING);
  696. } while (0);
  697. rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
  698. RT_ASSERT(rx_fifo != RT_NULL);
  699. rt_free(rx_fifo);
  700. serial->serial_rx = RT_NULL;
  701. return RT_EOK;
  702. }
  703. /**
  704. * @brief Disable serial tranmit mode.
  705. * @param dev The pointer of device driver structure
  706. * @param rx_oflag The flag of that the serial port opens.
  707. * @return Return the status of the operation.
  708. */
  709. static rt_err_t rt_serial_tx_disable(struct rt_device *dev,
  710. rt_uint16_t tx_oflag)
  711. {
  712. struct rt_serial_device *serial;
  713. struct rt_serial_tx_fifo *tx_fifo;
  714. RT_ASSERT(dev != RT_NULL);
  715. serial = (struct rt_serial_device *)dev;
  716. #ifndef RT_USING_DEVICE_OPS
  717. dev->write = RT_NULL;
  718. #endif
  719. if (serial->serial_tx == RT_NULL) return RT_EOK;
  720. tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
  721. RT_ASSERT(tx_fifo != RT_NULL);
  722. do
  723. {
  724. if (tx_oflag == RT_SERIAL_TX_NON_BLOCKING)
  725. {
  726. dev->open_flag &= ~ RT_SERIAL_TX_NON_BLOCKING;
  727. serial->ops->control(serial,
  728. RT_DEVICE_CTRL_CLR_INT,
  729. (void *)RT_SERIAL_TX_NON_BLOCKING);
  730. break;
  731. }
  732. rt_completion_done(&(tx_fifo->tx_cpt));
  733. dev->open_flag &= ~ RT_SERIAL_TX_BLOCKING;
  734. serial->ops->control(serial,
  735. RT_DEVICE_CTRL_CLR_INT,
  736. (void *)RT_SERIAL_TX_BLOCKING);
  737. } while (0);
  738. rt_free(tx_fifo);
  739. serial->serial_tx = RT_NULL;
  740. rt_memset(&serial->rx_notify, 0, sizeof(struct rt_device_notify));
  741. return RT_EOK;
  742. }
  743. /**
  744. * @brief Initialize the serial device.
  745. * @param dev The pointer of device driver structure
  746. * @return Return the status of the operation.
  747. */
  748. static rt_err_t rt_serial_init(struct rt_device *dev)
  749. {
  750. rt_err_t result = RT_EOK;
  751. struct rt_serial_device *serial;
  752. RT_ASSERT(dev != RT_NULL);
  753. serial = (struct rt_serial_device *)dev;
  754. RT_ASSERT(serial->ops->transmit != RT_NULL);
  755. /* initialize rx/tx */
  756. serial->serial_rx = RT_NULL;
  757. serial->serial_tx = RT_NULL;
  758. /* apply configuration */
  759. if (serial->ops->configure)
  760. result = serial->ops->configure(serial, &serial->config);
  761. return result;
  762. }
  763. /**
  764. * @brief Open the serial device.
  765. * @param dev The pointer of device driver structure
  766. * @param oflag The flag of that the serial port opens.
  767. * @return Return the status of the operation.
  768. */
  769. static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
  770. {
  771. struct rt_serial_device *serial;
  772. RT_ASSERT(dev != RT_NULL);
  773. serial = (struct rt_serial_device *)dev;
  774. /* Check that the device has been turned on */
  775. if ((dev->open_flag) & (15 << 12))
  776. {
  777. LOG_D("(%s) serial device has already been opened, it will run in its original configuration", dev->parent.name);
  778. return RT_EOK;
  779. }
  780. LOG_D("open serial device: 0x%08x with open flag: 0x%04x",
  781. dev, oflag);
  782. /* By default, the receive mode of a serial devide is RT_SERIAL_RX_NON_BLOCKING */
  783. if ((oflag & RT_SERIAL_RX_BLOCKING) == RT_SERIAL_RX_BLOCKING)
  784. dev->open_flag |= RT_SERIAL_RX_BLOCKING;
  785. else
  786. dev->open_flag |= RT_SERIAL_RX_NON_BLOCKING;
  787. /* By default, the transmit mode of a serial devide is RT_SERIAL_TX_BLOCKING */
  788. if ((oflag & RT_SERIAL_TX_NON_BLOCKING) == RT_SERIAL_TX_NON_BLOCKING)
  789. dev->open_flag |= RT_SERIAL_TX_NON_BLOCKING;
  790. else
  791. dev->open_flag |= RT_SERIAL_TX_BLOCKING;
  792. /* set steam flag */
  793. if ((oflag & RT_DEVICE_FLAG_STREAM) ||
  794. (dev->open_flag & RT_DEVICE_FLAG_STREAM))
  795. dev->open_flag |= RT_DEVICE_FLAG_STREAM;
  796. /* initialize the Rx structure according to open flag */
  797. if (serial->serial_rx == RT_NULL)
  798. rt_serial_rx_enable(dev, dev->open_flag &
  799. (RT_SERIAL_RX_BLOCKING | RT_SERIAL_RX_NON_BLOCKING));
  800. /* initialize the Tx structure according to open flag */
  801. if (serial->serial_tx == RT_NULL)
  802. rt_serial_tx_enable(dev, dev->open_flag &
  803. (RT_SERIAL_TX_BLOCKING | RT_SERIAL_TX_NON_BLOCKING));
  804. return RT_EOK;
  805. }
  806. /**
  807. * @brief Close the serial device.
  808. * @param dev The pointer of device driver structure
  809. * @return Return the status of the operation.
  810. */
  811. static rt_err_t rt_serial_close(struct rt_device *dev)
  812. {
  813. struct rt_serial_device *serial;
  814. RT_ASSERT(dev != RT_NULL);
  815. serial = (struct rt_serial_device *)dev;
  816. /* this device has more reference count */
  817. if (dev->ref_count > 1) return -RT_ERROR;
  818. /* Disable serial receive mode. */
  819. rt_serial_rx_disable(dev, dev->open_flag &
  820. (RT_SERIAL_RX_BLOCKING | RT_SERIAL_RX_NON_BLOCKING));
  821. /* Disable serial tranmit mode. */
  822. rt_serial_tx_disable(dev, dev->open_flag &
  823. (RT_SERIAL_TX_BLOCKING | RT_SERIAL_TX_NON_BLOCKING));
  824. /* Clear the callback function */
  825. serial->parent.rx_indicate = RT_NULL;
  826. serial->parent.tx_complete = RT_NULL;
  827. /* Call the control() API to close the serial device */
  828. serial->ops->control(serial, RT_DEVICE_CTRL_CLOSE, RT_NULL);
  829. dev->flag &= ~RT_DEVICE_FLAG_ACTIVATED;
  830. return RT_EOK;
  831. }
  832. #ifdef RT_USING_POSIX_TERMIOS
  833. struct speed_baudrate_item
  834. {
  835. speed_t speed;
  836. int baudrate;
  837. };
  838. const static struct speed_baudrate_item _tbl[] =
  839. {
  840. {B2400, BAUD_RATE_2400},
  841. {B4800, BAUD_RATE_4800},
  842. {B9600, BAUD_RATE_9600},
  843. {B19200, BAUD_RATE_19200},
  844. {B38400, BAUD_RATE_38400},
  845. {B57600, BAUD_RATE_57600},
  846. {B115200, BAUD_RATE_115200},
  847. {B230400, BAUD_RATE_230400},
  848. {B460800, BAUD_RATE_460800},
  849. {B500000, BAUD_RATE_500000},
  850. {B921600, BAUD_RATE_921600},
  851. {B2000000, BAUD_RATE_2000000},
  852. {B3000000, BAUD_RATE_3000000},
  853. };
  854. static speed_t _get_speed(int baudrate)
  855. {
  856. int index;
  857. for (index = 0; index < sizeof(_tbl)/sizeof(_tbl[0]); index ++)
  858. {
  859. if (_tbl[index].baudrate == baudrate)
  860. return _tbl[index].speed;
  861. }
  862. return B0;
  863. }
  864. static int _get_baudrate(speed_t speed)
  865. {
  866. int index;
  867. for (index = 0; index < sizeof(_tbl)/sizeof(_tbl[0]); index ++)
  868. {
  869. if (_tbl[index].speed == speed)
  870. return _tbl[index].baudrate;
  871. }
  872. return 0;
  873. }
  874. static void _tc_flush(struct rt_serial_device *serial, int queue)
  875. {
  876. rt_base_t level;
  877. int ch = -1;
  878. struct rt_serial_rx_fifo *rx_fifo = RT_NULL;
  879. struct rt_device *device = RT_NULL;
  880. RT_ASSERT(serial != RT_NULL);
  881. device = &(serial->parent);
  882. rx_fifo = (struct rt_serial_rx_fifo *) serial->serial_rx;
  883. switch(queue)
  884. {
  885. case TCIFLUSH:
  886. case TCIOFLUSH:
  887. RT_ASSERT(rx_fifo != RT_NULL);
  888. if((device->open_flag & RT_DEVICE_FLAG_INT_RX) || (device->open_flag & RT_DEVICE_FLAG_DMA_RX))
  889. {
  890. RT_ASSERT(RT_NULL != rx_fifo);
  891. level = rt_hw_interrupt_disable();
  892. rx_fifo->rx_cpt_index = 0;
  893. rt_hw_interrupt_enable(level);
  894. }
  895. else
  896. {
  897. while (1)
  898. {
  899. ch = serial->ops->getc(serial);
  900. if (ch == -1) break;
  901. }
  902. }
  903. break;
  904. case TCOFLUSH:
  905. break;
  906. }
  907. }
  908. #endif /* RT_USING_POSIX_TERMIOS */
  909. /**
  910. * @brief Control the serial device.
  911. * @param dev The pointer of device driver structure
  912. * @param cmd The command value that controls the serial device
  913. * @param args The parameter value that controls the serial device
  914. * @return Return the status of the operation.
  915. */
  916. static rt_err_t rt_serial_control(struct rt_device *dev,
  917. int cmd,
  918. void *args)
  919. {
  920. rt_err_t ret = RT_EOK;
  921. struct rt_serial_device *serial;
  922. RT_ASSERT(dev != RT_NULL);
  923. serial = (struct rt_serial_device *)dev;
  924. switch (cmd)
  925. {
  926. case RT_DEVICE_CTRL_SUSPEND:
  927. /* suspend device */
  928. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  929. break;
  930. case RT_DEVICE_CTRL_RESUME:
  931. /* resume device */
  932. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  933. break;
  934. case RT_DEVICE_CTRL_CONFIG:
  935. if (args != RT_NULL)
  936. {
  937. struct serial_configure *pconfig = (struct serial_configure *) args;
  938. if (((pconfig->rx_bufsz != serial->config.rx_bufsz) || (pconfig->tx_bufsz != serial->config.tx_bufsz))
  939. && serial->parent.ref_count)
  940. {
  941. /*can not change buffer size*/
  942. return -RT_EBUSY;
  943. }
  944. /* set serial configure */
  945. serial->config = *pconfig;
  946. serial->ops->configure(serial, (struct serial_configure *) args);
  947. }
  948. break;
  949. case RT_DEVICE_CTRL_NOTIFY_SET:
  950. if (args)
  951. {
  952. rt_memcpy(&serial->rx_notify, args, sizeof(struct rt_device_notify));
  953. }
  954. break;
  955. case RT_DEVICE_CTRL_CONSOLE_OFLAG:
  956. if (args)
  957. {
  958. *(rt_uint16_t*)args = RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM;
  959. }
  960. break;
  961. #ifdef RT_USING_POSIX_STDIO
  962. #ifdef RT_USING_POSIX_TERMIOS
  963. case TCGETA:
  964. {
  965. struct termios *tio = (struct termios*)args;
  966. if (tio == RT_NULL) return -RT_EINVAL;
  967. tio->c_iflag = 0;
  968. tio->c_oflag = 0;
  969. tio->c_lflag = 0;
  970. /* update oflag for console device */
  971. if (rt_console_get_device() == dev)
  972. tio->c_oflag = OPOST | ONLCR;
  973. /* set cflag */
  974. tio->c_cflag = 0;
  975. if (serial->config.data_bits == DATA_BITS_5)
  976. tio->c_cflag = CS5;
  977. else if (serial->config.data_bits == DATA_BITS_6)
  978. tio->c_cflag = CS6;
  979. else if (serial->config.data_bits == DATA_BITS_7)
  980. tio->c_cflag = CS7;
  981. else if (serial->config.data_bits == DATA_BITS_8)
  982. tio->c_cflag = CS8;
  983. if (serial->config.stop_bits == STOP_BITS_2)
  984. tio->c_cflag |= CSTOPB;
  985. if (serial->config.parity == PARITY_EVEN)
  986. tio->c_cflag |= PARENB;
  987. else if (serial->config.parity == PARITY_ODD)
  988. tio->c_cflag |= (PARODD | PARENB);
  989. if (serial->config.flowcontrol == RT_SERIAL_FLOWCONTROL_CTSRTS)
  990. tio->c_cflag |= CRTSCTS;
  991. cfsetospeed(tio, _get_speed(serial->config.baud_rate));
  992. }
  993. break;
  994. case TCSETAW:
  995. case TCSETAF:
  996. case TCSETA:
  997. {
  998. int baudrate;
  999. struct serial_configure config;
  1000. struct termios *tio = (struct termios*)args;
  1001. if (tio == RT_NULL) return -RT_EINVAL;
  1002. config = serial->config;
  1003. baudrate = _get_baudrate(cfgetospeed(tio));
  1004. config.baud_rate = baudrate;
  1005. switch (tio->c_cflag & CSIZE)
  1006. {
  1007. case CS5:
  1008. config.data_bits = DATA_BITS_5;
  1009. break;
  1010. case CS6:
  1011. config.data_bits = DATA_BITS_6;
  1012. break;
  1013. case CS7:
  1014. config.data_bits = DATA_BITS_7;
  1015. break;
  1016. default:
  1017. config.data_bits = DATA_BITS_8;
  1018. break;
  1019. }
  1020. if (tio->c_cflag & CSTOPB) config.stop_bits = STOP_BITS_2;
  1021. else config.stop_bits = STOP_BITS_1;
  1022. if (tio->c_cflag & PARENB)
  1023. {
  1024. if (tio->c_cflag & PARODD) config.parity = PARITY_ODD;
  1025. else config.parity = PARITY_EVEN;
  1026. }
  1027. else config.parity = PARITY_NONE;
  1028. if (tio->c_cflag & CRTSCTS) config.flowcontrol = RT_SERIAL_FLOWCONTROL_CTSRTS;
  1029. else config.flowcontrol = RT_SERIAL_FLOWCONTROL_NONE;
  1030. /* set serial configure */
  1031. serial->config = config;
  1032. serial->ops->configure(serial, &config);
  1033. }
  1034. break;
  1035. case TCFLSH:
  1036. {
  1037. int queue = (int)args;
  1038. _tc_flush(serial, queue);
  1039. }
  1040. break;
  1041. case TCXONC:
  1042. break;
  1043. #endif /*RT_USING_POSIX_TERMIOS*/
  1044. case TIOCSWINSZ:
  1045. {
  1046. struct winsize* p_winsize;
  1047. p_winsize = (struct winsize*)args;
  1048. rt_kprintf("\x1b[8;%d;%dt", p_winsize->ws_col, p_winsize->ws_row);
  1049. }
  1050. break;
  1051. case TIOCGWINSZ:
  1052. {
  1053. struct winsize* p_winsize;
  1054. p_winsize = (struct winsize*)args;
  1055. if(rt_thread_self() != rt_thread_find(FINSH_THREAD_NAME))
  1056. {
  1057. /* only can be used in tshell thread; otherwise, return default size */
  1058. p_winsize->ws_col = 80;
  1059. p_winsize->ws_row = 24;
  1060. }
  1061. else
  1062. {
  1063. #include <shell.h>
  1064. #define _TIO_BUFLEN 20
  1065. char _tio_buf[_TIO_BUFLEN];
  1066. unsigned char cnt1, cnt2, cnt3, i;
  1067. char row_s[4], col_s[4];
  1068. char *p;
  1069. rt_memset(_tio_buf, 0, _TIO_BUFLEN);
  1070. /* send the command to terminal for getting the window size of the terminal */
  1071. rt_kprintf("\033[18t");
  1072. /* waiting for the response from the terminal */
  1073. i = 0;
  1074. while(i < _TIO_BUFLEN)
  1075. {
  1076. _tio_buf[i] = finsh_getchar();
  1077. if(_tio_buf[i] != 't')
  1078. {
  1079. i ++;
  1080. }
  1081. else
  1082. {
  1083. break;
  1084. }
  1085. }
  1086. if(i == _TIO_BUFLEN)
  1087. {
  1088. /* buffer overloaded, and return default size */
  1089. p_winsize->ws_col = 80;
  1090. p_winsize->ws_row = 24;
  1091. break;
  1092. }
  1093. /* interpreting data eg: "\033[8;1;15t" which means row is 1 and col is 15 (unit: size of ONE character) */
  1094. rt_memset(row_s,0,4);
  1095. rt_memset(col_s,0,4);
  1096. cnt1 = 0;
  1097. while(cnt1 < _TIO_BUFLEN && _tio_buf[cnt1] != ';')
  1098. {
  1099. cnt1++;
  1100. }
  1101. cnt2 = ++cnt1;
  1102. while(cnt2 < _TIO_BUFLEN && _tio_buf[cnt2] != ';')
  1103. {
  1104. cnt2++;
  1105. }
  1106. p = row_s;
  1107. while(cnt1 < cnt2)
  1108. {
  1109. *p++ = _tio_buf[cnt1++];
  1110. }
  1111. p = col_s;
  1112. cnt2++;
  1113. cnt3 = rt_strlen(_tio_buf) - 1;
  1114. while(cnt2 < cnt3)
  1115. {
  1116. *p++ = _tio_buf[cnt2++];
  1117. }
  1118. /* load the window size date */
  1119. p_winsize->ws_col = atoi(col_s);
  1120. p_winsize->ws_row = atoi(row_s);
  1121. #undef _TIO_BUFLEN
  1122. }
  1123. p_winsize->ws_xpixel = 0;/* unused */
  1124. p_winsize->ws_ypixel = 0;/* unused */
  1125. }
  1126. break;
  1127. case FIONREAD:
  1128. {
  1129. rt_size_t recved = 0;
  1130. rt_base_t level;
  1131. struct rt_serial_rx_fifo * rx_fifo = (struct rt_serial_rx_fifo *) serial->serial_rx;
  1132. level = rt_hw_interrupt_disable();
  1133. recved = rt_ringbuffer_data_len(&(rx_fifo->rb));
  1134. rt_hw_interrupt_enable(level);
  1135. *(rt_size_t *)args = recved;
  1136. }
  1137. break;
  1138. #endif /* RT_USING_POSIX_STDIO */
  1139. default :
  1140. /* control device */
  1141. ret = serial->ops->control(serial, cmd, args);
  1142. break;
  1143. }
  1144. return ret;
  1145. }
  1146. #ifdef RT_USING_DEVICE_OPS
  1147. static rt_ssize_t rt_serial_read(struct rt_device *dev,
  1148. rt_off_t pos,
  1149. void *buffer,
  1150. rt_size_t size)
  1151. {
  1152. struct rt_serial_device *serial;
  1153. RT_ASSERT(dev != RT_NULL);
  1154. if (size == 0) return 0;
  1155. serial = (struct rt_serial_device *)dev;
  1156. if (serial->config.rx_bufsz)
  1157. {
  1158. return _serial_fifo_rx(dev, pos, buffer, size);
  1159. }
  1160. return _serial_poll_rx(dev, pos, buffer, size);
  1161. }
  1162. static rt_ssize_t rt_serial_write(struct rt_device *dev,
  1163. rt_off_t pos,
  1164. const void *buffer,
  1165. rt_size_t size)
  1166. {
  1167. struct rt_serial_device *serial;
  1168. struct rt_serial_tx_fifo *tx_fifo;
  1169. RT_ASSERT(dev != RT_NULL);
  1170. if (size == 0) return 0;
  1171. serial = (struct rt_serial_device *)dev;
  1172. RT_ASSERT((serial != RT_NULL) && (buffer != RT_NULL));
  1173. tx_fifo = (struct rt_serial_tx_fifo *) serial->serial_tx;
  1174. if (serial->config.tx_bufsz == 0)
  1175. {
  1176. return _serial_poll_tx(dev, pos, buffer, size);
  1177. }
  1178. if (dev->open_flag & RT_SERIAL_TX_BLOCKING)
  1179. {
  1180. if ((tx_fifo->rb.buffer_ptr) == RT_NULL)
  1181. {
  1182. return _serial_fifo_tx_blocking_nbuf(dev, pos, buffer, size);
  1183. }
  1184. return _serial_fifo_tx_blocking_buf(dev, pos, buffer, size);
  1185. }
  1186. return _serial_fifo_tx_nonblocking(dev, pos, buffer, size);
  1187. }
  1188. const static struct rt_device_ops serial_ops =
  1189. {
  1190. rt_serial_init,
  1191. rt_serial_open,
  1192. rt_serial_close,
  1193. rt_serial_read,
  1194. rt_serial_write,
  1195. rt_serial_control
  1196. };
  1197. #endif
  1198. /**
  1199. * @brief Register the serial device.
  1200. * @param serial RT-thread serial device.
  1201. * @param name The device driver's name
  1202. * @param flag The capabilities flag of device.
  1203. * @param data The device driver's data.
  1204. * @return Return the status of the operation.
  1205. */
  1206. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
  1207. const char *name,
  1208. rt_uint32_t flag,
  1209. void *data)
  1210. {
  1211. rt_err_t ret;
  1212. struct rt_device *device;
  1213. RT_ASSERT(serial != RT_NULL);
  1214. device = &(serial->parent);
  1215. device->type = RT_Device_Class_Char;
  1216. device->rx_indicate = RT_NULL;
  1217. device->tx_complete = RT_NULL;
  1218. #ifdef RT_USING_DEVICE_OPS
  1219. device->ops = &serial_ops;
  1220. #else
  1221. device->init = rt_serial_init;
  1222. device->open = rt_serial_open;
  1223. device->close = rt_serial_close;
  1224. device->read = RT_NULL;
  1225. device->write = RT_NULL;
  1226. device->control = rt_serial_control;
  1227. #endif
  1228. device->user_data = data;
  1229. /* register a character device */
  1230. ret = rt_device_register(device, name, flag);
  1231. #ifdef RT_USING_POSIX_STDIO
  1232. /* set fops */
  1233. device->fops = &_serial_fops;
  1234. #endif
  1235. return ret;
  1236. }
  1237. /**
  1238. * @brief ISR for serial interrupt
  1239. * @param serial RT-thread serial device.
  1240. * @param event ISR event type.
  1241. */
  1242. void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
  1243. {
  1244. RT_ASSERT(serial != RT_NULL);
  1245. switch (event & 0xff)
  1246. {
  1247. /* Interrupt receive event */
  1248. case RT_SERIAL_EVENT_RX_IND:
  1249. case RT_SERIAL_EVENT_RX_DMADONE:
  1250. {
  1251. struct rt_serial_rx_fifo *rx_fifo;
  1252. rt_size_t rx_length = 0;
  1253. rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
  1254. rt_base_t level;
  1255. RT_ASSERT(rx_fifo != RT_NULL);
  1256. /* If the event is RT_SERIAL_EVENT_RX_IND, rx_length is equal to 0 */
  1257. rx_length = (event & (~0xff)) >> 8;
  1258. if (rx_length)
  1259. { /* RT_SERIAL_EVENT_RX_DMADONE MODE */
  1260. level = rt_hw_interrupt_disable();
  1261. rt_serial_update_write_index(&(rx_fifo->rb), rx_length);
  1262. rt_hw_interrupt_enable(level);
  1263. }
  1264. /* Get the length of the data from the ringbuffer */
  1265. rx_length = rt_ringbuffer_data_len(&rx_fifo->rb);
  1266. if (rx_length == 0) break;
  1267. if (serial->parent.open_flag & RT_SERIAL_RX_BLOCKING)
  1268. {
  1269. if (rx_fifo->rx_cpt_index && rx_length >= rx_fifo->rx_cpt_index )
  1270. {
  1271. rx_fifo->rx_cpt_index = 0;
  1272. rt_completion_done(&(rx_fifo->rx_cpt));
  1273. }
  1274. }
  1275. /* Trigger the receiving completion callback */
  1276. if (serial->parent.rx_indicate != RT_NULL)
  1277. serial->parent.rx_indicate(&(serial->parent), rx_length);
  1278. if (serial->rx_notify.notify)
  1279. {
  1280. serial->rx_notify.notify(serial->rx_notify.dev);
  1281. }
  1282. break;
  1283. }
  1284. /* Interrupt transmit event */
  1285. case RT_SERIAL_EVENT_TX_DONE:
  1286. {
  1287. struct rt_serial_tx_fifo *tx_fifo;
  1288. rt_size_t tx_length = 0;
  1289. tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
  1290. RT_ASSERT(tx_fifo != RT_NULL);
  1291. /* Get the length of the data from the ringbuffer */
  1292. tx_length = rt_ringbuffer_data_len(&tx_fifo->rb);
  1293. /* If there is no data in tx_ringbuffer,
  1294. * then the transmit completion callback is triggered*/
  1295. if (tx_length == 0)
  1296. {
  1297. /* Trigger the transmit completion callback */
  1298. if (serial->parent.tx_complete != RT_NULL)
  1299. serial->parent.tx_complete(&serial->parent, RT_NULL);
  1300. /* Maybe some datas left in the buffer still need to be sent in block mode,
  1301. * so tx_fifo->activated should be RT_TRUE */
  1302. if (serial->parent.open_flag & RT_SERIAL_TX_BLOCKING)
  1303. {
  1304. rt_completion_done(&(tx_fifo->tx_cpt));
  1305. }
  1306. else
  1307. {
  1308. tx_fifo->activated = RT_FALSE;
  1309. }
  1310. break;
  1311. }
  1312. /* Call the transmit interface for transmission again */
  1313. /* Note that in interrupt mode, tx_fifo->buffer and tx_length
  1314. * are inactive parameters */
  1315. serial->ops->transmit(serial,
  1316. tx_fifo->buffer,
  1317. tx_length,
  1318. serial->parent.open_flag & ( \
  1319. RT_SERIAL_TX_BLOCKING | \
  1320. RT_SERIAL_TX_NON_BLOCKING));
  1321. break;
  1322. }
  1323. case RT_SERIAL_EVENT_TX_DMADONE:
  1324. {
  1325. struct rt_serial_tx_fifo *tx_fifo;
  1326. tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
  1327. RT_ASSERT(tx_fifo != RT_NULL);
  1328. tx_fifo->activated = RT_FALSE;
  1329. /* Trigger the transmit completion callback */
  1330. if (serial->parent.tx_complete != RT_NULL)
  1331. serial->parent.tx_complete(&serial->parent, RT_NULL);
  1332. if (serial->parent.open_flag & RT_SERIAL_TX_BLOCKING)
  1333. {
  1334. rt_completion_done(&(tx_fifo->tx_cpt));
  1335. break;
  1336. }
  1337. rt_serial_update_read_index(&tx_fifo->rb, tx_fifo->put_size);
  1338. /* Get the length of the data from the ringbuffer.
  1339. * If there is some data in tx_ringbuffer,
  1340. * then call the transmit interface for transmission again */
  1341. if (rt_ringbuffer_data_len(&tx_fifo->rb))
  1342. {
  1343. tx_fifo->activated = RT_TRUE;
  1344. rt_uint8_t *put_ptr = RT_NULL;
  1345. /* Get the linear length buffer from rinbuffer */
  1346. tx_fifo->put_size = rt_serial_get_linear_buffer(&(tx_fifo->rb), &put_ptr);
  1347. /* Call the transmit interface for transmission again */
  1348. serial->ops->transmit(serial,
  1349. put_ptr,
  1350. tx_fifo->put_size,
  1351. RT_SERIAL_TX_NON_BLOCKING);
  1352. }
  1353. break;
  1354. }
  1355. default:
  1356. break;
  1357. }
  1358. }