dev_serial_v2.c 50 KB

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