serial_v2.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-06-01 KyleChan first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #define DBG_TAG "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->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->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->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->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->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->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_completion_init(&(tx_fifo->tx_cpt));
  573. dev->open_flag |= RT_SERIAL_TX_BLOCKING;
  574. return RT_EOK;
  575. }
  576. /* When using RT_SERIAL_TX_NON_BLOCKING, ringbuffer needs to be initialized,
  577. * and initialize the tx_fifo->activated value is RT_FALSE.
  578. */
  579. tx_fifo = (struct rt_serial_tx_fifo *) rt_malloc
  580. (sizeof(struct rt_serial_tx_fifo) + serial->config.tx_bufsz);
  581. RT_ASSERT(tx_fifo != RT_NULL);
  582. tx_fifo->activated = RT_FALSE;
  583. tx_fifo->put_size = 0;
  584. rt_ringbuffer_init(&(tx_fifo->rb),
  585. tx_fifo->buffer,
  586. serial->config.tx_bufsz);
  587. serial->serial_tx = tx_fifo;
  588. #ifndef RT_USING_DEVICE_OPS
  589. dev->write = _serial_fifo_tx_nonblocking;
  590. #endif
  591. dev->open_flag |= RT_SERIAL_TX_NON_BLOCKING;
  592. /* Call the control() API to configure the serial device by RT_SERIAL_TX_NON_BLOCKING*/
  593. serial->ops->control(serial,
  594. RT_DEVICE_CTRL_CONFIG,
  595. (void *)RT_SERIAL_TX_NON_BLOCKING);
  596. return RT_EOK;
  597. }
  598. /**
  599. * @brief Enable serial receive mode.
  600. * @param dev The pointer of device driver structure
  601. * @param rx_oflag The flag of that the serial port opens.
  602. * @return Return the status of the operation.
  603. */
  604. static rt_err_t rt_serial_rx_enable(struct rt_device *dev,
  605. rt_uint16_t rx_oflag)
  606. {
  607. struct rt_serial_device *serial;
  608. struct rt_serial_rx_fifo *rx_fifo = RT_NULL;
  609. RT_ASSERT(dev != RT_NULL);
  610. serial = (struct rt_serial_device *)dev;
  611. if (serial->config.rx_bufsz == 0)
  612. {
  613. /* Cannot use RT_SERIAL_RX_NON_BLOCKING when rx_bufsz is 0 */
  614. if (rx_oflag == RT_SERIAL_RX_NON_BLOCKING)
  615. {
  616. LOG_E("(%s) serial device with misconfigure: rx_bufsz = 0",
  617. dev->parent.name);
  618. return -RT_EINVAL;
  619. }
  620. #ifndef RT_USING_DEVICE_OPS
  621. dev->read = _serial_poll_rx;
  622. #endif
  623. dev->open_flag |= RT_SERIAL_RX_BLOCKING;
  624. return RT_EOK;
  625. }
  626. /* Limits the minimum value of rx_bufsz */
  627. if (serial->config.rx_bufsz < RT_SERIAL_RX_MINBUFSZ)
  628. serial->config.rx_bufsz = RT_SERIAL_RX_MINBUFSZ;
  629. rx_fifo = (struct rt_serial_rx_fifo *) rt_malloc
  630. (sizeof(struct rt_serial_rx_fifo) + serial->config.rx_bufsz);
  631. RT_ASSERT(rx_fifo != RT_NULL);
  632. rt_ringbuffer_init(&(rx_fifo->rb), rx_fifo->buffer, serial->config.rx_bufsz);
  633. serial->serial_rx = rx_fifo;
  634. #ifndef RT_USING_DEVICE_OPS
  635. dev->read = _serial_fifo_rx;
  636. #endif
  637. if (rx_oflag == RT_SERIAL_RX_NON_BLOCKING)
  638. {
  639. dev->open_flag |= RT_SERIAL_RX_NON_BLOCKING;
  640. /* Call the control() API to configure the serial device by RT_SERIAL_RX_NON_BLOCKING*/
  641. serial->ops->control(serial,
  642. RT_DEVICE_CTRL_CONFIG,
  643. (void *) RT_SERIAL_RX_NON_BLOCKING);
  644. return RT_EOK;
  645. }
  646. /* When using RT_SERIAL_RX_BLOCKING, rt_completion_init() and rx_cpt_index are initialized */
  647. rx_fifo->rx_cpt_index = 0;
  648. rt_completion_init(&(rx_fifo->rx_cpt));
  649. dev->open_flag |= RT_SERIAL_RX_BLOCKING;
  650. /* Call the control() API to configure the serial device by RT_SERIAL_RX_BLOCKING*/
  651. serial->ops->control(serial,
  652. RT_DEVICE_CTRL_CONFIG,
  653. (void *) RT_SERIAL_RX_BLOCKING);
  654. return RT_EOK;
  655. }
  656. /**
  657. * @brief Disable serial receive mode.
  658. * @param dev The pointer of device driver structure
  659. * @param rx_oflag The flag of that the serial port opens.
  660. * @return Return the status of the operation.
  661. */
  662. static rt_err_t rt_serial_rx_disable(struct rt_device *dev,
  663. rt_uint16_t rx_oflag)
  664. {
  665. struct rt_serial_device *serial;
  666. struct rt_serial_rx_fifo *rx_fifo;
  667. RT_ASSERT(dev != RT_NULL);
  668. serial = (struct rt_serial_device *)dev;
  669. #ifndef RT_USING_DEVICE_OPS
  670. dev->read = RT_NULL;
  671. #endif
  672. if (serial->serial_rx == RT_NULL) return RT_EOK;
  673. do
  674. {
  675. if (rx_oflag == RT_SERIAL_RX_NON_BLOCKING)
  676. {
  677. dev->open_flag &= ~ RT_SERIAL_RX_NON_BLOCKING;
  678. serial->ops->control(serial,
  679. RT_DEVICE_CTRL_CLR_INT,
  680. (void *)RT_SERIAL_RX_NON_BLOCKING);
  681. break;
  682. }
  683. dev->open_flag &= ~ RT_SERIAL_RX_BLOCKING;
  684. serial->ops->control(serial,
  685. RT_DEVICE_CTRL_CLR_INT,
  686. (void *)RT_SERIAL_RX_BLOCKING);
  687. } while (0);
  688. rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
  689. RT_ASSERT(rx_fifo != RT_NULL);
  690. rt_free(rx_fifo);
  691. serial->serial_rx = RT_NULL;
  692. return RT_EOK;
  693. }
  694. /**
  695. * @brief Disable serial tranmit mode.
  696. * @param dev The pointer of device driver structure
  697. * @param rx_oflag The flag of that the serial port opens.
  698. * @return Return the status of the operation.
  699. */
  700. static rt_err_t rt_serial_tx_disable(struct rt_device *dev,
  701. rt_uint16_t tx_oflag)
  702. {
  703. struct rt_serial_device *serial;
  704. struct rt_serial_tx_fifo *tx_fifo;
  705. RT_ASSERT(dev != RT_NULL);
  706. serial = (struct rt_serial_device *)dev;
  707. #ifndef RT_USING_DEVICE_OPS
  708. dev->write = RT_NULL;
  709. #endif
  710. if (serial->serial_tx == RT_NULL) return RT_EOK;
  711. tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
  712. RT_ASSERT(tx_fifo != RT_NULL);
  713. do
  714. {
  715. if (tx_oflag == RT_SERIAL_TX_NON_BLOCKING)
  716. {
  717. dev->open_flag &= ~ RT_SERIAL_TX_NON_BLOCKING;
  718. serial->ops->control(serial,
  719. RT_DEVICE_CTRL_CLR_INT,
  720. (void *)RT_SERIAL_TX_NON_BLOCKING);
  721. break;
  722. }
  723. rt_completion_done(&(tx_fifo->tx_cpt));
  724. dev->open_flag &= ~ RT_SERIAL_TX_BLOCKING;
  725. serial->ops->control(serial,
  726. RT_DEVICE_CTRL_CLR_INT,
  727. (void *)RT_SERIAL_TX_BLOCKING);
  728. } while (0);
  729. rt_free(tx_fifo);
  730. serial->serial_tx = RT_NULL;
  731. return RT_EOK;
  732. }
  733. /**
  734. * @brief Initialize the serial device.
  735. * @param dev The pointer of device driver structure
  736. * @return Return the status of the operation.
  737. */
  738. static rt_err_t rt_serial_init(struct rt_device *dev)
  739. {
  740. rt_err_t result = RT_EOK;
  741. struct rt_serial_device *serial;
  742. RT_ASSERT(dev != RT_NULL);
  743. serial = (struct rt_serial_device *)dev;
  744. RT_ASSERT(serial->ops->transmit != RT_NULL);
  745. /* initialize rx/tx */
  746. serial->serial_rx = RT_NULL;
  747. serial->serial_tx = RT_NULL;
  748. /* apply configuration */
  749. if (serial->ops->configure)
  750. result = serial->ops->configure(serial, &serial->config);
  751. return result;
  752. }
  753. /**
  754. * @brief Open the serial device.
  755. * @param dev The pointer of device driver structure
  756. * @param oflag The flag of that the serial port opens.
  757. * @return Return the status of the operation.
  758. */
  759. static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
  760. {
  761. struct rt_serial_device *serial;
  762. RT_ASSERT(dev != RT_NULL);
  763. serial = (struct rt_serial_device *)dev;
  764. /* Check that the device has been turned on */
  765. if ((dev->open_flag) & (15 << 12))
  766. {
  767. LOG_D("(%s) serial device has already been opened, it will run in its original configuration", dev->parent.name);
  768. return RT_EOK;
  769. }
  770. LOG_D("open serial device: 0x%08x with open flag: 0x%04x",
  771. dev, oflag);
  772. /* By default, the receive mode of a serial devide is RT_SERIAL_RX_NON_BLOCKING */
  773. if ((oflag & RT_SERIAL_RX_BLOCKING) == RT_SERIAL_RX_BLOCKING)
  774. dev->open_flag |= RT_SERIAL_RX_BLOCKING;
  775. else
  776. dev->open_flag |= RT_SERIAL_RX_NON_BLOCKING;
  777. /* By default, the transmit mode of a serial devide is RT_SERIAL_TX_BLOCKING */
  778. if ((oflag & RT_SERIAL_TX_NON_BLOCKING) == RT_SERIAL_TX_NON_BLOCKING)
  779. dev->open_flag |= RT_SERIAL_TX_NON_BLOCKING;
  780. else
  781. dev->open_flag |= RT_SERIAL_TX_BLOCKING;
  782. /* set steam flag */
  783. if ((oflag & RT_DEVICE_FLAG_STREAM) ||
  784. (dev->open_flag & RT_DEVICE_FLAG_STREAM))
  785. dev->open_flag |= RT_DEVICE_FLAG_STREAM;
  786. /* initialize the Rx structure according to open flag */
  787. if (serial->serial_rx == RT_NULL)
  788. rt_serial_rx_enable(dev, dev->open_flag &
  789. (RT_SERIAL_RX_BLOCKING | RT_SERIAL_RX_NON_BLOCKING));
  790. /* initialize the Tx structure according to open flag */
  791. if (serial->serial_tx == RT_NULL)
  792. rt_serial_tx_enable(dev, dev->open_flag &
  793. (RT_SERIAL_TX_BLOCKING | RT_SERIAL_TX_NON_BLOCKING));
  794. return RT_EOK;
  795. }
  796. /**
  797. * @brief Close the serial device.
  798. * @param dev The pointer of device driver structure
  799. * @return Return the status of the operation.
  800. */
  801. static rt_err_t rt_serial_close(struct rt_device *dev)
  802. {
  803. struct rt_serial_device *serial;
  804. RT_ASSERT(dev != RT_NULL);
  805. serial = (struct rt_serial_device *)dev;
  806. /* this device has more reference count */
  807. if (dev->ref_count > 1) return -RT_ERROR;
  808. /* Disable serial receive mode. */
  809. rt_serial_rx_disable(dev, dev->open_flag &
  810. (RT_SERIAL_RX_BLOCKING | RT_SERIAL_RX_NON_BLOCKING));
  811. /* Disable serial tranmit mode. */
  812. rt_serial_tx_disable(dev, dev->open_flag &
  813. (RT_SERIAL_TX_BLOCKING | RT_SERIAL_TX_NON_BLOCKING));
  814. /* Clear the callback function */
  815. serial->parent.rx_indicate = RT_NULL;
  816. serial->parent.tx_complete = RT_NULL;
  817. /* Call the control() API to close the serial device */
  818. serial->ops->control(serial, RT_DEVICE_CTRL_CLOSE, RT_NULL);
  819. dev->flag &= ~RT_DEVICE_FLAG_ACTIVATED;
  820. return RT_EOK;
  821. }
  822. #ifdef RT_USING_POSIX_TERMIOS
  823. struct speed_baudrate_item
  824. {
  825. speed_t speed;
  826. int baudrate;
  827. };
  828. const static struct speed_baudrate_item _tbl[] =
  829. {
  830. {B2400, BAUD_RATE_2400},
  831. {B4800, BAUD_RATE_4800},
  832. {B9600, BAUD_RATE_9600},
  833. {B19200, BAUD_RATE_19200},
  834. {B38400, BAUD_RATE_38400},
  835. {B57600, BAUD_RATE_57600},
  836. {B115200, BAUD_RATE_115200},
  837. {B230400, BAUD_RATE_230400},
  838. {B460800, BAUD_RATE_460800},
  839. {B921600, BAUD_RATE_921600},
  840. {B2000000, BAUD_RATE_2000000},
  841. {B3000000, BAUD_RATE_3000000},
  842. };
  843. static speed_t _get_speed(int baudrate)
  844. {
  845. int index;
  846. for (index = 0; index < sizeof(_tbl)/sizeof(_tbl[0]); index ++)
  847. {
  848. if (_tbl[index].baudrate == baudrate)
  849. return _tbl[index].speed;
  850. }
  851. return B0;
  852. }
  853. static int _get_baudrate(speed_t speed)
  854. {
  855. int index;
  856. for (index = 0; index < sizeof(_tbl)/sizeof(_tbl[0]); index ++)
  857. {
  858. if (_tbl[index].speed == speed)
  859. return _tbl[index].baudrate;
  860. }
  861. return 0;
  862. }
  863. static void _tc_flush(struct rt_serial_device *serial, int queue)
  864. {
  865. rt_base_t level;
  866. int ch = -1;
  867. struct rt_serial_rx_fifo *rx_fifo = RT_NULL;
  868. struct rt_device *device = RT_NULL;
  869. RT_ASSERT(serial != RT_NULL);
  870. device = &(serial->parent);
  871. rx_fifo = (struct rt_serial_rx_fifo *) serial->serial_rx;
  872. switch(queue)
  873. {
  874. case TCIFLUSH:
  875. case TCIOFLUSH:
  876. RT_ASSERT(rx_fifo != RT_NULL);
  877. if((device->open_flag & RT_DEVICE_FLAG_INT_RX) || (device->open_flag & RT_DEVICE_FLAG_DMA_RX))
  878. {
  879. RT_ASSERT(RT_NULL != rx_fifo);
  880. level = rt_hw_interrupt_disable();
  881. rx_fifo->rx_cpt_index = 0;
  882. rt_hw_interrupt_enable(level);
  883. }
  884. else
  885. {
  886. while (1)
  887. {
  888. ch = serial->ops->getc(serial);
  889. if (ch == -1) break;
  890. }
  891. }
  892. break;
  893. case TCOFLUSH:
  894. break;
  895. }
  896. }
  897. #endif /* RT_USING_POSIX_TERMIOS */
  898. /**
  899. * @brief Control the serial device.
  900. * @param dev The pointer of device driver structure
  901. * @param cmd The command value that controls the serial device
  902. * @param args The parameter value that controls the serial device
  903. * @return Return the status of the operation.
  904. */
  905. static rt_err_t rt_serial_control(struct rt_device *dev,
  906. int cmd,
  907. void *args)
  908. {
  909. rt_err_t ret = RT_EOK;
  910. struct rt_serial_device *serial;
  911. RT_ASSERT(dev != RT_NULL);
  912. serial = (struct rt_serial_device *)dev;
  913. switch (cmd)
  914. {
  915. case RT_DEVICE_CTRL_SUSPEND:
  916. /* suspend device */
  917. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  918. break;
  919. case RT_DEVICE_CTRL_RESUME:
  920. /* resume device */
  921. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  922. break;
  923. case RT_DEVICE_CTRL_CONFIG:
  924. if (args != RT_NULL)
  925. {
  926. struct serial_configure *pconfig = (struct serial_configure *) args;
  927. if (((pconfig->rx_bufsz != serial->config.rx_bufsz) || (pconfig->tx_bufsz != serial->config.tx_bufsz))
  928. && serial->parent.ref_count)
  929. {
  930. /*can not change buffer size*/
  931. return -RT_EBUSY;
  932. }
  933. /* set serial configure */
  934. serial->config = *pconfig;
  935. serial->ops->configure(serial, (struct serial_configure *) args);
  936. }
  937. break;
  938. #ifdef RT_USING_POSIX_STDIO
  939. #ifdef RT_USING_POSIX_TERMIOS
  940. case TCGETA:
  941. {
  942. struct termios *tio = (struct termios*)args;
  943. if (tio == RT_NULL) return -RT_EINVAL;
  944. tio->c_iflag = 0;
  945. tio->c_oflag = 0;
  946. tio->c_lflag = 0;
  947. /* update oflag for console device */
  948. if (rt_console_get_device() == dev)
  949. tio->c_oflag = OPOST | ONLCR;
  950. /* set cflag */
  951. tio->c_cflag = 0;
  952. if (serial->config.data_bits == DATA_BITS_5)
  953. tio->c_cflag = CS5;
  954. else if (serial->config.data_bits == DATA_BITS_6)
  955. tio->c_cflag = CS6;
  956. else if (serial->config.data_bits == DATA_BITS_7)
  957. tio->c_cflag = CS7;
  958. else if (serial->config.data_bits == DATA_BITS_8)
  959. tio->c_cflag = CS8;
  960. if (serial->config.stop_bits == STOP_BITS_2)
  961. tio->c_cflag |= CSTOPB;
  962. if (serial->config.parity == PARITY_EVEN)
  963. tio->c_cflag |= PARENB;
  964. else if (serial->config.parity == PARITY_ODD)
  965. tio->c_cflag |= (PARODD | PARENB);
  966. if (serial->config.flowcontrol == RT_SERIAL_FLOWCONTROL_CTSRTS)
  967. tio->c_cflag |= CRTSCTS;
  968. cfsetospeed(tio, _get_speed(serial->config.baud_rate));
  969. }
  970. break;
  971. case TCSETAW:
  972. case TCSETAF:
  973. case TCSETA:
  974. {
  975. int baudrate;
  976. struct serial_configure config;
  977. struct termios *tio = (struct termios*)args;
  978. if (tio == RT_NULL) return -RT_EINVAL;
  979. config = serial->config;
  980. baudrate = _get_baudrate(cfgetospeed(tio));
  981. config.baud_rate = baudrate;
  982. switch (tio->c_cflag & CSIZE)
  983. {
  984. case CS5:
  985. config.data_bits = DATA_BITS_5;
  986. break;
  987. case CS6:
  988. config.data_bits = DATA_BITS_6;
  989. break;
  990. case CS7:
  991. config.data_bits = DATA_BITS_7;
  992. break;
  993. default:
  994. config.data_bits = DATA_BITS_8;
  995. break;
  996. }
  997. if (tio->c_cflag & CSTOPB) config.stop_bits = STOP_BITS_2;
  998. else config.stop_bits = STOP_BITS_1;
  999. if (tio->c_cflag & PARENB)
  1000. {
  1001. if (tio->c_cflag & PARODD) config.parity = PARITY_ODD;
  1002. else config.parity = PARITY_EVEN;
  1003. }
  1004. else config.parity = PARITY_NONE;
  1005. if (tio->c_cflag & CRTSCTS) config.flowcontrol = RT_SERIAL_FLOWCONTROL_CTSRTS;
  1006. else config.flowcontrol = RT_SERIAL_FLOWCONTROL_NONE;
  1007. /* set serial configure */
  1008. serial->config = config;
  1009. serial->ops->configure(serial, &config);
  1010. }
  1011. break;
  1012. case TCFLSH:
  1013. {
  1014. int queue = (int)args;
  1015. _tc_flush(serial, queue);
  1016. }
  1017. break;
  1018. case TCXONC:
  1019. break;
  1020. #endif /*RT_USING_POSIX_TERMIOS*/
  1021. case TIOCSWINSZ:
  1022. {
  1023. struct winsize* p_winsize;
  1024. p_winsize = (struct winsize*)args;
  1025. rt_kprintf("\x1b[8;%d;%dt", p_winsize->ws_col, p_winsize->ws_row);
  1026. }
  1027. break;
  1028. case TIOCGWINSZ:
  1029. {
  1030. struct winsize* p_winsize;
  1031. p_winsize = (struct winsize*)args;
  1032. if(rt_thread_self() != rt_thread_find(FINSH_THREAD_NAME))
  1033. {
  1034. /* only can be used in tshell thread; otherwise, return default size */
  1035. p_winsize->ws_col = 80;
  1036. p_winsize->ws_row = 24;
  1037. }
  1038. else
  1039. {
  1040. #include <shell.h>
  1041. #define _TIO_BUFLEN 20
  1042. char _tio_buf[_TIO_BUFLEN];
  1043. unsigned char cnt1, cnt2, cnt3, i;
  1044. char row_s[4], col_s[4];
  1045. char *p;
  1046. rt_memset(_tio_buf, 0, _TIO_BUFLEN);
  1047. /* send the command to terminal for getting the window size of the terminal */
  1048. rt_kprintf("\033[18t");
  1049. /* waiting for the response from the terminal */
  1050. i = 0;
  1051. while(i < _TIO_BUFLEN)
  1052. {
  1053. _tio_buf[i] = finsh_getchar();
  1054. if(_tio_buf[i] != 't')
  1055. {
  1056. i ++;
  1057. }
  1058. else
  1059. {
  1060. break;
  1061. }
  1062. }
  1063. if(i == _TIO_BUFLEN)
  1064. {
  1065. /* buffer overloaded, and return default size */
  1066. p_winsize->ws_col = 80;
  1067. p_winsize->ws_row = 24;
  1068. break;
  1069. }
  1070. /* interpreting data eg: "\033[8;1;15t" which means row is 1 and col is 15 (unit: size of ONE character) */
  1071. rt_memset(row_s,0,4);
  1072. rt_memset(col_s,0,4);
  1073. cnt1 = 0;
  1074. while(_tio_buf[cnt1] != ';' && cnt1 < _TIO_BUFLEN)
  1075. {
  1076. cnt1++;
  1077. }
  1078. cnt2 = ++cnt1;
  1079. while(_tio_buf[cnt2] != ';' && cnt2 < _TIO_BUFLEN)
  1080. {
  1081. cnt2++;
  1082. }
  1083. p = row_s;
  1084. while(cnt1 < cnt2)
  1085. {
  1086. *p++ = _tio_buf[cnt1++];
  1087. }
  1088. p = col_s;
  1089. cnt2++;
  1090. cnt3 = rt_strlen(_tio_buf) - 1;
  1091. while(cnt2 < cnt3)
  1092. {
  1093. *p++ = _tio_buf[cnt2++];
  1094. }
  1095. /* load the window size date */
  1096. p_winsize->ws_col = atoi(col_s);
  1097. p_winsize->ws_row = atoi(row_s);
  1098. #undef _TIO_BUFLEN
  1099. }
  1100. p_winsize->ws_xpixel = 0;/* unused */
  1101. p_winsize->ws_ypixel = 0;/* unused */
  1102. }
  1103. break;
  1104. case FIONREAD:
  1105. {
  1106. rt_size_t recved = 0;
  1107. rt_base_t level;
  1108. struct rt_serial_rx_fifo * rx_fifo = (struct rt_serial_rx_fifo *) serial->serial_rx;
  1109. level = rt_hw_interrupt_disable();
  1110. recved = rt_ringbuffer_data_len(&(rx_fifo->rb));
  1111. rt_hw_interrupt_enable(level);
  1112. *(rt_size_t *)args = recved;
  1113. }
  1114. break;
  1115. #endif /* RT_USING_POSIX_STDIO */
  1116. default :
  1117. /* control device */
  1118. ret = serial->ops->control(serial, cmd, args);
  1119. break;
  1120. }
  1121. return ret;
  1122. }
  1123. #ifdef RT_USING_DEVICE_OPS
  1124. static rt_size_t rt_serial_read(struct rt_device *dev,
  1125. rt_off_t pos,
  1126. void *buffer,
  1127. rt_size_t size)
  1128. {
  1129. struct rt_serial_device *serial;
  1130. RT_ASSERT(dev != RT_NULL);
  1131. if (size == 0) return 0;
  1132. serial = (struct rt_serial_device *)dev;
  1133. if (serial->config.rx_bufsz)
  1134. {
  1135. return _serial_fifo_rx(dev, pos, buffer, size);
  1136. }
  1137. return _serial_poll_rx(dev, pos, buffer, size);
  1138. }
  1139. static rt_size_t rt_serial_write(struct rt_device *dev,
  1140. rt_off_t pos,
  1141. const void *buffer,
  1142. rt_size_t size)
  1143. {
  1144. struct rt_serial_device *serial;
  1145. struct rt_serial_tx_fifo *tx_fifo;
  1146. RT_ASSERT(dev != RT_NULL);
  1147. if (size == 0) return 0;
  1148. serial = (struct rt_serial_device *)dev;
  1149. RT_ASSERT((serial != RT_NULL) && (buffer != RT_NULL));
  1150. tx_fifo = (struct rt_serial_tx_fifo *) serial->serial_tx;
  1151. if (serial->config.tx_bufsz == 0)
  1152. {
  1153. return _serial_poll_tx(dev, pos, buffer, size);
  1154. }
  1155. if (dev->open_flag & RT_SERIAL_TX_BLOCKING)
  1156. {
  1157. if ((tx_fifo->rb.buffer_ptr) == RT_NULL)
  1158. {
  1159. return _serial_fifo_tx_blocking_nbuf(dev, pos, buffer, size);
  1160. }
  1161. return _serial_fifo_tx_blocking_buf(dev, pos, buffer, size);
  1162. }
  1163. return _serial_fifo_tx_nonblocking(dev, pos, buffer, size);
  1164. }
  1165. const static struct rt_device_ops serial_ops =
  1166. {
  1167. rt_serial_init,
  1168. rt_serial_open,
  1169. rt_serial_close,
  1170. rt_serial_read,
  1171. rt_serial_write,
  1172. rt_serial_control
  1173. };
  1174. #endif
  1175. /**
  1176. * @brief Register the serial device.
  1177. * @param serial RT-thread serial device.
  1178. * @param name The device driver's name
  1179. * @param flag The capabilities flag of device.
  1180. * @param data The device driver's data.
  1181. * @return Return the status of the operation.
  1182. */
  1183. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
  1184. const char *name,
  1185. rt_uint32_t flag,
  1186. void *data)
  1187. {
  1188. rt_err_t ret;
  1189. struct rt_device *device;
  1190. RT_ASSERT(serial != RT_NULL);
  1191. device = &(serial->parent);
  1192. device->type = RT_Device_Class_Char;
  1193. device->rx_indicate = RT_NULL;
  1194. device->tx_complete = RT_NULL;
  1195. #ifdef RT_USING_DEVICE_OPS
  1196. device->ops = &serial_ops;
  1197. #else
  1198. device->init = rt_serial_init;
  1199. device->open = rt_serial_open;
  1200. device->close = rt_serial_close;
  1201. device->read = RT_NULL;
  1202. device->write = RT_NULL;
  1203. device->control = rt_serial_control;
  1204. #endif
  1205. device->user_data = data;
  1206. /* register a character device */
  1207. ret = rt_device_register(device, name, flag);
  1208. #ifdef RT_USING_POSIX_STDIO
  1209. /* set fops */
  1210. device->fops = &_serial_fops;
  1211. #endif
  1212. return ret;
  1213. }
  1214. /**
  1215. * @brief ISR for serial interrupt
  1216. * @param serial RT-thread serial device.
  1217. * @param event ISR event type.
  1218. */
  1219. void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
  1220. {
  1221. RT_ASSERT(serial != RT_NULL);
  1222. switch (event & 0xff)
  1223. {
  1224. /* Interrupt receive event */
  1225. case RT_SERIAL_EVENT_RX_IND:
  1226. case RT_SERIAL_EVENT_RX_DMADONE:
  1227. {
  1228. struct rt_serial_rx_fifo *rx_fifo;
  1229. rt_size_t rx_length = 0;
  1230. rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
  1231. rt_base_t level;
  1232. RT_ASSERT(rx_fifo != RT_NULL);
  1233. /* If the event is RT_SERIAL_EVENT_RX_IND, rx_length is equal to 0 */
  1234. rx_length = (event & (~0xff)) >> 8;
  1235. if (rx_length)
  1236. { /* RT_SERIAL_EVENT_RX_DMADONE MODE */
  1237. level = rt_hw_interrupt_disable();
  1238. rt_serial_update_write_index(&(rx_fifo->rb), rx_length);
  1239. rt_hw_interrupt_enable(level);
  1240. }
  1241. /* Get the length of the data from the ringbuffer */
  1242. rx_length = rt_ringbuffer_data_len(&rx_fifo->rb);
  1243. if (rx_length == 0) break;
  1244. if (serial->parent.open_flag & RT_SERIAL_RX_BLOCKING)
  1245. {
  1246. if (rx_fifo->rx_cpt_index && rx_length >= rx_fifo->rx_cpt_index )
  1247. {
  1248. rx_fifo->rx_cpt_index = 0;
  1249. rt_completion_done(&(rx_fifo->rx_cpt));
  1250. }
  1251. }
  1252. /* Trigger the receiving completion callback */
  1253. if (serial->parent.rx_indicate != RT_NULL)
  1254. serial->parent.rx_indicate(&(serial->parent), rx_length);
  1255. break;
  1256. }
  1257. /* Interrupt transmit event */
  1258. case RT_SERIAL_EVENT_TX_DONE:
  1259. {
  1260. struct rt_serial_tx_fifo *tx_fifo;
  1261. rt_size_t tx_length = 0;
  1262. tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
  1263. RT_ASSERT(tx_fifo != RT_NULL);
  1264. /* Get the length of the data from the ringbuffer */
  1265. tx_length = rt_ringbuffer_data_len(&tx_fifo->rb);
  1266. /* If there is no data in tx_ringbuffer,
  1267. * then the transmit completion callback is triggered*/
  1268. if (tx_length == 0)
  1269. {
  1270. tx_fifo->activated = RT_FALSE;
  1271. /* Trigger the transmit completion callback */
  1272. if (serial->parent.tx_complete != RT_NULL)
  1273. serial->parent.tx_complete(&serial->parent, RT_NULL);
  1274. if (serial->parent.open_flag & RT_SERIAL_TX_BLOCKING)
  1275. rt_completion_done(&(tx_fifo->tx_cpt));
  1276. break;
  1277. }
  1278. /* Call the transmit interface for transmission again */
  1279. /* Note that in interrupt mode, tx_fifo->buffer and tx_length
  1280. * are inactive parameters */
  1281. serial->ops->transmit(serial,
  1282. tx_fifo->buffer,
  1283. tx_length,
  1284. serial->parent.open_flag & ( \
  1285. RT_SERIAL_TX_BLOCKING | \
  1286. RT_SERIAL_TX_NON_BLOCKING));
  1287. break;
  1288. }
  1289. case RT_SERIAL_EVENT_TX_DMADONE:
  1290. {
  1291. struct rt_serial_tx_fifo *tx_fifo;
  1292. tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
  1293. RT_ASSERT(tx_fifo != RT_NULL);
  1294. tx_fifo->activated = RT_FALSE;
  1295. /* Trigger the transmit completion callback */
  1296. if (serial->parent.tx_complete != RT_NULL)
  1297. serial->parent.tx_complete(&serial->parent, RT_NULL);
  1298. if (serial->parent.open_flag & RT_SERIAL_TX_BLOCKING)
  1299. {
  1300. rt_completion_done(&(tx_fifo->tx_cpt));
  1301. break;
  1302. }
  1303. rt_serial_update_read_index(&tx_fifo->rb, tx_fifo->put_size);
  1304. /* Get the length of the data from the ringbuffer.
  1305. * If there is some data in tx_ringbuffer,
  1306. * then call the transmit interface for transmission again */
  1307. if (rt_ringbuffer_data_len(&tx_fifo->rb))
  1308. {
  1309. tx_fifo->activated = RT_TRUE;
  1310. rt_uint8_t *put_ptr = RT_NULL;
  1311. /* Get the linear length buffer from rinbuffer */
  1312. tx_fifo->put_size = rt_serial_get_linear_buffer(&(tx_fifo->rb), &put_ptr);
  1313. /* Call the transmit interface for transmission again */
  1314. serial->ops->transmit(serial,
  1315. put_ptr,
  1316. tx_fifo->put_size,
  1317. RT_SERIAL_TX_NON_BLOCKING);
  1318. }
  1319. break;
  1320. }
  1321. default:
  1322. break;
  1323. }
  1324. }