serial_v2.c 50 KB

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