serial_v2.c 49 KB

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