serial_v2.c 48 KB

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