serial_v2.c 50 KB

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