serial_v2.c 49 KB

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