serial_v2.c 49 KB

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