1
0

serial_v2.c 48 KB

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