serial_v2.c 50 KB

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