dev_serial_v2.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150
  1. /*
  2. * Copyright (c) 2006-2024 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_SERIAL_BUF_STRATEGY_DROP
  17. #define RT_SERIAL_FIFO_LOCK(spinlock) ((rt_base_t)0)
  18. #define RT_SERIAL_FIFO_UNLOCK(spinlock, level) \
  19. do { \
  20. RT_UNUSED(spinlock); \
  21. RT_UNUSED(level); \
  22. } while (0)
  23. #else
  24. #define RT_SERIAL_FIFO_LOCK(spinlock) rt_spin_lock_irqsave(spinlock)
  25. #define RT_SERIAL_FIFO_UNLOCK(spinlock, level) rt_spin_unlock_irqrestore(spinlock, level)
  26. #endif /* RT_SERIAL_BUF_STRATEGY_DROP */
  27. #ifdef RT_USING_POSIX_STDIO
  28. #include <unistd.h>
  29. #include <fcntl.h>
  30. #include <poll.h>
  31. #include <sys/ioctl.h>
  32. #include <dfs_file.h>
  33. #ifdef RT_USING_POSIX_TERMIOS
  34. #include <termios.h>
  35. #endif
  36. #ifdef getc
  37. #undef getc
  38. #endif
  39. #ifdef putc
  40. #undef putc
  41. #endif
  42. RT_OBJECT_HOOKLIST_DEFINE(rt_hw_serial_rxind);
  43. static rt_err_t serial_fops_rx_ind(rt_device_t dev, rt_size_t size)
  44. {
  45. rt_wqueue_wakeup(&dev->wait_queue, (void *)POLLIN);
  46. RT_OBJECT_HOOKLIST_CALL(rt_hw_serial_rxind, (dev, size));
  47. return RT_EOK;
  48. }
  49. /* fops for serial */
  50. static int serial_fops_open(struct dfs_file *fd)
  51. {
  52. rt_err_t ret = 0;
  53. rt_uint16_t flags = 0;
  54. rt_device_t device;
  55. device = (rt_device_t)fd->vnode->data;
  56. RT_ASSERT(device != RT_NULL);
  57. switch (fd->flags & O_ACCMODE)
  58. {
  59. case O_RDONLY:
  60. LOG_D("fops open: O_RDONLY!");
  61. flags = RT_DEVICE_FLAG_RDONLY;
  62. break;
  63. case O_WRONLY:
  64. LOG_D("fops open: O_WRONLY!");
  65. flags = RT_DEVICE_FLAG_WRONLY;
  66. break;
  67. case O_RDWR:
  68. LOG_D("fops open: O_RDWR!");
  69. flags = RT_DEVICE_FLAG_RDWR;
  70. break;
  71. default:
  72. LOG_E("fops open: unknown mode - %d!", fd->flags & O_ACCMODE);
  73. break;
  74. }
  75. if ((fd->flags & O_ACCMODE) != O_WRONLY)
  76. rt_device_set_rx_indicate(device, serial_fops_rx_ind);
  77. ret = rt_device_open(device, flags | RT_SERIAL_RX_BLOCKING | RT_SERIAL_TX_BLOCKING);
  78. if (ret == RT_EOK) return 0;
  79. return ret;
  80. }
  81. static int serial_fops_close(struct dfs_file *fd)
  82. {
  83. rt_device_t device;
  84. device = (rt_device_t)fd->vnode->data;
  85. rt_device_set_rx_indicate(device, RT_NULL);
  86. rt_device_close(device);
  87. return 0;
  88. }
  89. static int serial_fops_ioctl(struct dfs_file *fd, int cmd, void *args)
  90. {
  91. rt_device_t device;
  92. int flags = (int)(rt_base_t)args;
  93. int mask = O_NONBLOCK | O_APPEND;
  94. device = (rt_device_t)fd->vnode->data;
  95. switch (cmd)
  96. {
  97. case FIONREAD:
  98. break;
  99. case FIONWRITE:
  100. break;
  101. case F_SETFL:
  102. flags &= mask;
  103. fd->flags &= ~mask;
  104. fd->flags |= flags;
  105. break;
  106. }
  107. return rt_device_control(device, cmd, args);
  108. }
  109. #ifdef RT_USING_DFS_V2
  110. static ssize_t serial_fops_read(struct dfs_file *fd, void *buf, size_t count, off_t *pos)
  111. #else
  112. static ssize_t serial_fops_read(struct dfs_file *fd, void *buf, size_t count)
  113. #endif
  114. {
  115. ssize_t size = 0;
  116. rt_device_t device;
  117. rt_int32_t rx_timout;
  118. if (count == 0) return 0;
  119. RT_ASSERT(fd != RT_NULL && buf != RT_NULL);
  120. device = (rt_device_t)fd->vnode->data;
  121. RT_ASSERT(device != RT_NULL);
  122. /* nonblock mode */
  123. if (fd->flags & O_NONBLOCK)
  124. {
  125. rx_timout = RT_WAITING_NO;
  126. rt_device_control(device, RT_SERIAL_CTRL_SET_RX_TIMEOUT, (void *)&rx_timout);
  127. size = rt_device_read(device, -1, buf, count);
  128. if (size <= 0)
  129. {
  130. size = -1;
  131. rt_set_errno(EAGAIN);
  132. }
  133. }
  134. else
  135. {
  136. rx_timout = RT_WAITING_FOREVER;
  137. rt_device_control(device, RT_SERIAL_CTRL_SET_RX_TIMEOUT, (void *)&rx_timout);
  138. size = rt_device_read(device, -1, buf, count);
  139. }
  140. return size;
  141. }
  142. #ifdef RT_USING_DFS_V2
  143. static ssize_t serial_fops_write(struct dfs_file *fd, const void *buf, size_t count, off_t *pos)
  144. #else
  145. static ssize_t serial_fops_write(struct dfs_file *fd, const void *buf, size_t count)
  146. #endif
  147. {
  148. ssize_t size = 0;
  149. rt_device_t device;
  150. rt_int32_t tx_timeout;
  151. device = (rt_device_t)fd->vnode->data;
  152. if (fd->flags & O_NONBLOCK)
  153. {
  154. tx_timeout = RT_WAITING_NO;
  155. rt_device_control(device, RT_SERIAL_CTRL_SET_TX_TIMEOUT, (void *)&tx_timeout);
  156. size = rt_device_write(device, -1, buf, count);
  157. if (size <= 0)
  158. {
  159. size = -1;
  160. rt_set_errno(EAGAIN);
  161. }
  162. }
  163. else
  164. {
  165. tx_timeout = RT_WAITING_FOREVER;
  166. rt_device_control(device, RT_SERIAL_CTRL_SET_TX_TIMEOUT, (void *)&tx_timeout);
  167. size = rt_device_write(device, -1, buf, count);
  168. }
  169. return size;
  170. }
  171. static int serial_fops_flush(struct dfs_file *fd)
  172. {
  173. rt_device_t device;
  174. struct rt_serial_device *serial;
  175. device = (rt_device_t)fd->vnode->data;
  176. RT_ASSERT(device != RT_NULL);
  177. serial = (struct rt_serial_device *)device;
  178. rt_device_control(device, RT_SERIAL_CTRL_TX_FLUSH, (void *)RT_NULL);
  179. rt_device_control(device, RT_SERIAL_CTRL_RX_FLUSH, (void *)RT_NULL);
  180. return 0;
  181. }
  182. static int serial_fops_poll(struct dfs_file *fd, struct rt_pollreq *req)
  183. {
  184. int mask = 0;
  185. int flags = 0;
  186. rt_device_t device;
  187. struct rt_serial_device *serial;
  188. device = (rt_device_t)fd->vnode->data;
  189. RT_ASSERT(device != RT_NULL);
  190. serial = (struct rt_serial_device *)device;
  191. /* only support POLLIN */
  192. flags = fd->flags & O_ACCMODE;
  193. if (flags == O_RDONLY || flags == O_RDWR)
  194. {
  195. rt_base_t level;
  196. struct rt_serial_rx_fifo *rx_fifo;
  197. rt_poll_add(&device->wait_queue, req);
  198. rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
  199. level = RT_SERIAL_FIFO_LOCK(&serial->spinlock);
  200. if (rt_ringbuffer_data_len(&rx_fifo->rb))
  201. mask |= POLLIN;
  202. RT_SERIAL_FIFO_UNLOCK(&serial->spinlock, level);
  203. }
  204. /* mask|=POLLOUT; */
  205. return mask;
  206. }
  207. const static struct dfs_file_ops _serial_fops =
  208. {
  209. .open = serial_fops_open,
  210. .close = serial_fops_close,
  211. .ioctl = serial_fops_ioctl,
  212. .read = serial_fops_read,
  213. .write = serial_fops_write,
  214. .flush = serial_fops_flush,
  215. .poll = serial_fops_poll,
  216. };
  217. #endif /* RT_USING_POSIX_STDIO */
  218. static rt_ssize_t rt_serial_get_linear_buffer(struct rt_ringbuffer *rb,
  219. rt_uint8_t **ptr)
  220. {
  221. rt_size_t size;
  222. RT_ASSERT(rb != RT_NULL);
  223. /* whether has enough data */
  224. size = rt_ringbuffer_data_len(rb);
  225. /* no data */
  226. if (size == 0)
  227. return 0;
  228. *ptr = &rb->buffer_ptr[rb->read_index];
  229. if (rb->buffer_size - rb->read_index > size)
  230. {
  231. return size;
  232. }
  233. return rb->buffer_size - rb->read_index;
  234. }
  235. #ifdef RT_SERIAL_USING_DMA
  236. static void rt_serial_update_read_index(struct rt_ringbuffer *rb,
  237. rt_uint16_t length)
  238. {
  239. rt_size_t size;
  240. RT_ASSERT(rb != RT_NULL);
  241. /* whether has enough data */
  242. size = rt_ringbuffer_data_len(rb);
  243. /* no data */
  244. if (size == 0)
  245. return;
  246. /* less data */
  247. if (size < length)
  248. length = size;
  249. if (rb->buffer_size - rb->read_index > length)
  250. {
  251. rb->read_index += length;
  252. return;
  253. }
  254. /* we are going into the other side of the mirror */
  255. rb->read_mirror = ~rb->read_mirror;
  256. rb->read_index = length - (rb->buffer_size - rb->read_index);
  257. return;
  258. }
  259. static void rt_serial_update_write_index(struct rt_ringbuffer *rb,
  260. rt_uint16_t length)
  261. {
  262. #ifdef RT_SERIAL_BUF_STRATEGY_DROP
  263. rt_uint16_t space_length;
  264. RT_ASSERT(rb != RT_NULL);
  265. /* whether has enough space */
  266. space_length = rt_ringbuffer_space_len(rb);
  267. /* no space */
  268. if (space_length == 0)
  269. return;
  270. /* drop some data */
  271. if (space_length < length)
  272. length = space_length;
  273. if (rb->buffer_size - rb->write_index > length)
  274. {
  275. /* this should not cause overflow because there is enough space for
  276. * length of data in current mirror */
  277. rb->write_index += length;
  278. return;
  279. }
  280. /* we are going into the other side of the mirror */
  281. rb->write_mirror = ~rb->write_mirror;
  282. rb->write_index = length - (rb->buffer_size - rb->write_index);
  283. return;
  284. #else
  285. rt_uint16_t space_length;
  286. RT_ASSERT(rb != RT_NULL);
  287. /* whether has enough space */
  288. space_length = rt_ringbuffer_space_len(rb);
  289. if (length > rb->buffer_size)
  290. {
  291. length = rb->buffer_size;
  292. #if !defined(RT_USING_ULOG) || defined(ULOG_USING_ISR_LOG)
  293. LOG_W("The serial buffer (len %d) is overflow.", rb->buffer_size);
  294. #endif
  295. }
  296. if (rb->buffer_size - rb->write_index > length)
  297. {
  298. /* this should not cause overflow because there is enough space for
  299. * length of data in current mirror */
  300. rb->write_index += length;
  301. if (length > space_length)
  302. rb->read_index = rb->write_index;
  303. return;
  304. }
  305. /* we are going into the other side of the mirror */
  306. rb->write_mirror = ~rb->write_mirror;
  307. rb->write_index = length - (rb->buffer_size - rb->write_index);
  308. if (length > space_length)
  309. {
  310. if (rb->write_index <= rb->read_index)
  311. rb->read_mirror = ~rb->read_mirror;
  312. rb->read_index = rb->write_index;
  313. }
  314. return;
  315. #endif /* RT_SERIAL_BUF_STRATEGY_DROP */
  316. }
  317. #endif /* RT_SERIAL_USING_DMA */
  318. /**
  319. * @brief Serial polling receive data routine, This function will receive data
  320. * in a continuous loop by one by one byte.
  321. * @param dev The pointer of device driver structure
  322. * @param pos Empty parameter.
  323. * @param buffer Receive data buffer.
  324. * @param size Receive data buffer length.
  325. * @return Return the final length of data received.
  326. */
  327. rt_ssize_t _serial_poll_rx(struct rt_device *dev,
  328. rt_off_t pos,
  329. void *buffer,
  330. rt_size_t size)
  331. {
  332. struct rt_serial_device *serial;
  333. rt_size_t getc_size;
  334. int getc_element; /* Gets one byte of data received */
  335. rt_uint8_t *getc_buffer; /* Pointer to the receive data buffer */
  336. if (size == 0) return 0;
  337. RT_ASSERT(dev != RT_NULL && buffer != RT_NULL);
  338. serial = (struct rt_serial_device *)dev;
  339. getc_buffer = (rt_uint8_t *)buffer;
  340. getc_size = size;
  341. while (size)
  342. {
  343. getc_element = serial->ops->getc(serial);
  344. if (getc_element == -1) break;
  345. *getc_buffer = getc_element;
  346. ++getc_buffer;
  347. --size;
  348. if (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM)
  349. {
  350. /* If open_flag satisfies RT_DEVICE_FLAG_STREAM
  351. * and the received character is '\n', exit the loop directly */
  352. if (getc_element == '\n') break;
  353. }
  354. }
  355. return getc_size - size;
  356. }
  357. /**
  358. * @brief Serial polling transmit data routines, This function will transmit
  359. * data in a continuous loop by one by one byte.
  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. rt_ssize_t _serial_poll_tx(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. rt_size_t putc_size;
  373. rt_uint8_t *putc_buffer; /* Pointer to the transmit data buffer */
  374. if (size == 0) return 0;
  375. RT_ASSERT(dev != RT_NULL && buffer != RT_NULL);
  376. serial = (struct rt_serial_device *)dev;
  377. putc_buffer = (rt_uint8_t *)buffer;
  378. putc_size = size;
  379. while (size)
  380. {
  381. if (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM)
  382. {
  383. /* If open_flag satisfies RT_DEVICE_FLAG_STREAM and the received character is '\n',
  384. * inserts '\r' character before '\n' character for the effect of carriage return newline */
  385. if (*putc_buffer == '\n')
  386. serial->ops->putc(serial, '\r');
  387. }
  388. serial->ops->putc(serial, *putc_buffer);
  389. ++putc_buffer;
  390. --size;
  391. }
  392. return putc_size - size;
  393. }
  394. /**
  395. * @brief Serial receive data routines, This function will receive
  396. * data by using fifo
  397. *
  398. * @note In blocking mode, the function will wait until the specified amount of data is received or until a timeout occurs.
  399. * In non-blocking mode, the function will immediately attempt to retrieve as much data as possible from the ring buffer and return.
  400. *
  401. * @param dev The pointer of device driver structure
  402. * @param pos Empty parameter.
  403. * @param buffer Receive data buffer.
  404. * @param size Receive data buffer length.
  405. * @return Returns the actual length of data received. If a timeout occurs in blocking mode, it returns -RT_ETIMEOUT.
  406. */
  407. static rt_ssize_t _serial_fifo_rx(struct rt_device *dev,
  408. rt_off_t pos,
  409. void *buffer,
  410. rt_size_t size)
  411. {
  412. struct rt_serial_device *serial;
  413. struct rt_serial_rx_fifo *rx_fifo;
  414. rt_base_t level;
  415. rt_size_t recv_size = 0;
  416. if (size == 0) return 0;
  417. RT_ASSERT(dev != RT_NULL && buffer != RT_NULL);
  418. serial = (struct rt_serial_device *)dev;
  419. rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
  420. if (dev->open_flag & RT_SERIAL_RX_BLOCKING)
  421. {
  422. rt_size_t data_len = 0;
  423. rt_size_t rx_bufsz_third = serial->config.rx_bufsz / 2;
  424. rt_int32_t base_rx_timeout = rt_atomic_load(&rx_fifo->rx_timeout);
  425. rt_int32_t rx_timeout = base_rx_timeout;
  426. rt_tick_t now_tick = 0;
  427. rt_tick_t begin_tick = rt_tick_get();
  428. while (1)
  429. {
  430. if (rx_timeout != RT_WAITING_NO)
  431. {
  432. level = rt_spin_lock_irqsave(&serial->spinlock);
  433. data_len = rt_ringbuffer_data_len(&rx_fifo->rb);
  434. if (data_len < size - recv_size)
  435. {
  436. if (size - (recv_size + data_len) >= rx_bufsz_third)
  437. {
  438. rx_fifo->rx_cpt_index = rx_bufsz_third;
  439. }
  440. else
  441. {
  442. rx_fifo->rx_cpt_index = size - (recv_size + data_len);
  443. }
  444. rt_completion_wait(&rx_fifo->rx_cpt, 0);
  445. }
  446. rt_spin_unlock_irqrestore(&serial->spinlock, level);
  447. }
  448. level = RT_SERIAL_FIFO_LOCK(&serial->spinlock);
  449. recv_size += rt_ringbuffer_get(&rx_fifo->rb, (rt_uint8_t *)buffer + recv_size, size - recv_size);
  450. RT_SERIAL_FIFO_UNLOCK(&serial->spinlock, level);
  451. if (recv_size == size || rx_timeout == RT_WAITING_NO)
  452. {
  453. break;
  454. }
  455. rt_completion_wait(&rx_fifo->rx_cpt, rx_timeout);
  456. if (rx_timeout != RT_WAITING_FOREVER)
  457. {
  458. now_tick = rt_tick_get();
  459. if (now_tick - begin_tick >= base_rx_timeout)
  460. {
  461. return -RT_ETIMEOUT;
  462. }
  463. else
  464. {
  465. if (now_tick > begin_tick)
  466. rx_timeout = base_rx_timeout - (now_tick - begin_tick);
  467. else
  468. rx_timeout = begin_tick + base_rx_timeout - now_tick + 1;
  469. }
  470. }
  471. }
  472. }
  473. else if (dev->open_flag & RT_SERIAL_RX_NON_BLOCKING)
  474. {
  475. /* When open_flag is RT_SERIAL_RX_NON_BLOCKING,
  476. * the data is retrieved directly from the ringbuffer and returned */
  477. level = RT_SERIAL_FIFO_LOCK(&serial->spinlock);
  478. recv_size = rt_ringbuffer_get(&rx_fifo->rb, buffer, size);
  479. RT_SERIAL_FIFO_UNLOCK(&serial->spinlock, level);
  480. }
  481. return recv_size;
  482. }
  483. /**
  484. * @brief Serial transmit data routines, This function will transmit
  485. * data by using blocking_nbuf.
  486. * @param dev The pointer of device driver structure
  487. * @param pos Empty parameter.
  488. * @param buffer Transmit data buffer.
  489. * @param size Transmit data buffer length.
  490. * @return Returns the actual length of data transmitted. If a timeout occurs, it returns -RT_ETIMEOUT.
  491. */
  492. static rt_ssize_t _serial_fifo_tx_blocking_nbuf(struct rt_device *dev,
  493. rt_off_t pos,
  494. const void *buffer,
  495. rt_size_t size)
  496. {
  497. struct rt_serial_device *serial;
  498. struct rt_serial_tx_fifo *tx_fifo;
  499. rt_ssize_t send_size;
  500. rt_err_t ret;
  501. if (size == 0) return 0;
  502. RT_ASSERT(dev != RT_NULL && buffer != RT_NULL);
  503. serial = (struct rt_serial_device *)dev;
  504. tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
  505. RT_ASSERT(tx_fifo != RT_NULL);
  506. if (rt_thread_self() == RT_NULL || (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM))
  507. {
  508. /* using poll tx when the scheduler not startup or in stream mode */
  509. return _serial_poll_tx(dev, pos, buffer, size);
  510. }
  511. /* When serial transmit in tx_blocking mode,
  512. * if the activated mode is RT_TRUE, it will return directly */
  513. if (rt_atomic_flag_test_and_set(&tx_fifo->activated))
  514. {
  515. return 0;
  516. }
  517. /* clear tx_cpt flag */
  518. rt_completion_wait(&tx_fifo->tx_cpt, 0);
  519. /* Call the transmit interface for transmission */
  520. send_size = serial->ops->transmit(serial,
  521. (rt_uint8_t *)buffer,
  522. size,
  523. RT_SERIAL_TX_BLOCKING);
  524. if (rt_atomic_load(&tx_fifo->tx_timeout) == RT_WAITING_NO)
  525. {
  526. return send_size;
  527. }
  528. /* Waiting for the transmission to complete */
  529. ret = rt_completion_wait(&tx_fifo->tx_cpt, rt_atomic_load(&tx_fifo->tx_timeout));
  530. if (ret != RT_EOK)
  531. {
  532. if (ret == -RT_ETIMEOUT)
  533. {
  534. return ret;
  535. }
  536. else
  537. {
  538. return 0;
  539. }
  540. }
  541. /* Inactive tx mode flag */
  542. rt_atomic_flag_clear(&tx_fifo->activated);
  543. return send_size;
  544. }
  545. /**
  546. * @brief Serial transmit data routines, This function will transmit
  547. * data by using blocking_buf.
  548. * @param dev The pointer of device driver structure
  549. * @param pos Empty parameter.
  550. * @param buffer Transmit data buffer.
  551. * @param size Transmit data buffer length.
  552. * @return Returns the final length of data transmitted. If not all data can be sent within the timeout period, returns -RT_ETIMEOUT.
  553. */
  554. static rt_ssize_t _serial_fifo_tx_blocking_buf(struct rt_device *dev,
  555. rt_off_t pos,
  556. const void *buffer,
  557. rt_size_t size)
  558. {
  559. struct rt_serial_device *serial;
  560. struct rt_serial_tx_fifo *tx_fifo;
  561. rt_base_t level;
  562. rt_size_t send_size = 0;
  563. if (size == 0) return 0;
  564. RT_ASSERT(dev != RT_NULL && buffer != RT_NULL);
  565. serial = (struct rt_serial_device *)dev;
  566. tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
  567. RT_ASSERT(tx_fifo != RT_NULL);
  568. if (rt_thread_self() == RT_NULL || (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM))
  569. {
  570. /* using poll tx when the scheduler not startup or in stream mode */
  571. return _serial_poll_tx(dev, pos, buffer, size);
  572. }
  573. /* When serial transmit in tx_blocking mode,
  574. * if the activated mode is RT_TRUE, it will return directly */
  575. if (rt_atomic_flag_test_and_set(&tx_fifo->activated))
  576. {
  577. return 0;
  578. }
  579. rt_int32_t base_tx_timeout = rt_atomic_load(&tx_fifo->tx_timeout);
  580. rt_int32_t tx_timeout = base_tx_timeout;
  581. rt_tick_t now_tick = 0;
  582. rt_tick_t begin_tick = rt_tick_get();
  583. while (send_size != size)
  584. {
  585. /* Copy one piece of data into the ringbuffer at a time
  586. * until the length of the data is equal to size */
  587. level = RT_SERIAL_FIFO_LOCK(&serial->spinlock);
  588. tx_fifo->put_size = rt_ringbuffer_put(&tx_fifo->rb,
  589. (rt_uint8_t *)buffer + send_size,
  590. size - send_size);
  591. RT_SERIAL_FIFO_UNLOCK(&serial->spinlock, level);
  592. /* clear tx_cpt flag */
  593. rt_completion_wait(&tx_fifo->tx_cpt, 0);
  594. /* Call the transmit interface for transmission again */
  595. /* Note that in interrupt mode, buffer and tx_fifo->put_size
  596. * are inactive parameters */
  597. serial->ops->transmit(serial,
  598. (rt_uint8_t *)buffer + send_size,
  599. tx_fifo->put_size,
  600. RT_SERIAL_TX_BLOCKING);
  601. send_size += tx_fifo->put_size;
  602. if (tx_timeout == RT_WAITING_NO)
  603. {
  604. break;
  605. }
  606. /* Waiting for the transmission to complete */
  607. rt_completion_wait(&tx_fifo->tx_cpt, tx_timeout);
  608. if (tx_timeout != RT_WAITING_FOREVER)
  609. {
  610. now_tick = rt_tick_get();
  611. if (now_tick - begin_tick >= base_tx_timeout)
  612. {
  613. return -RT_ETIMEOUT;
  614. }
  615. else
  616. {
  617. if (now_tick > begin_tick)
  618. tx_timeout = base_tx_timeout - (now_tick - begin_tick);
  619. else
  620. tx_timeout = begin_tick + base_tx_timeout - now_tick + 1;
  621. }
  622. }
  623. }
  624. /* Finally Inactivate the tx->fifo */
  625. rt_atomic_flag_clear(&tx_fifo->activated);
  626. return send_size;
  627. }
  628. /**
  629. * @brief Serial transmit data routines, This function will transmit
  630. * data by using nonblocking.
  631. * @param dev The pointer of device driver structure
  632. * @param pos Empty parameter.
  633. * @param buffer Transmit data buffer.
  634. * @param size Transmit data buffer length.
  635. * @return Return the final length of data transmit.
  636. */
  637. static rt_ssize_t _serial_fifo_tx_nonblocking(struct rt_device *dev,
  638. rt_off_t pos,
  639. const void *buffer,
  640. rt_size_t size)
  641. {
  642. struct rt_serial_device *serial;
  643. struct rt_serial_tx_fifo *tx_fifo;
  644. rt_uint8_t *put_ptr;
  645. rt_base_t level;
  646. rt_size_t send_size = 0;
  647. if (size == 0) return 0;
  648. RT_ASSERT(dev != RT_NULL && buffer != RT_NULL);
  649. serial = (struct rt_serial_device *)dev;
  650. tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
  651. RT_ASSERT(tx_fifo != RT_NULL);
  652. /* When serial transmit in tx_non_blocking mode, if the activated mode is RT_FALSE,
  653. * start copying data into the ringbuffer */
  654. if (!rt_atomic_flag_test_and_set(&tx_fifo->activated))
  655. {
  656. level = RT_SERIAL_FIFO_LOCK(&serial->spinlock);
  657. /* Copying data into the ringbuffer */
  658. send_size = rt_ringbuffer_put(&tx_fifo->rb, buffer, size);
  659. /* Get the linear length buffer from ringbuffer */
  660. tx_fifo->put_size = rt_serial_get_linear_buffer(&tx_fifo->rb, &put_ptr);
  661. RT_SERIAL_FIFO_UNLOCK(&serial->spinlock, level);
  662. /* clear tx_cpt flag */
  663. rt_completion_wait(&tx_fifo->tx_cpt, 0);
  664. /* Call the transmit interface for transmission again */
  665. /* Note that in interrupt mode, put_ptr and tx_fifo->put_size
  666. * are inactive parameters */
  667. serial->ops->transmit(serial,
  668. put_ptr,
  669. tx_fifo->put_size,
  670. RT_SERIAL_TX_NON_BLOCKING);
  671. /* In tx_nonblocking mode, there is no need to call rt_completion_wait() APIs to wait
  672. * for the rt_current_thread to resume */
  673. return send_size;
  674. }
  675. /* If the activated mode is RT_TRUE, it means that serial device is transmitting,
  676. * where only the data in the ringbuffer and there is no need to call the transmit() API.
  677. * Note that this part of the code requires disable interrupts
  678. * to prevent multi thread reentrant */
  679. level = RT_SERIAL_FIFO_LOCK(&serial->spinlock);
  680. /* Copying data into the ringbuffer */
  681. send_size = rt_ringbuffer_put(&tx_fifo->rb, buffer, size);
  682. RT_SERIAL_FIFO_UNLOCK(&serial->spinlock, level);
  683. return send_size;
  684. }
  685. /**
  686. * @brief Enable serial transmit mode.
  687. * @param dev The pointer of device driver structure
  688. * @param rx_oflag The flag of that the serial port opens.
  689. * @return Return the status of the operation.
  690. */
  691. static rt_err_t rt_serial_tx_enable(struct rt_device *dev,
  692. rt_uint16_t tx_oflag)
  693. {
  694. struct rt_serial_device *serial;
  695. struct rt_serial_tx_fifo *tx_fifo = RT_NULL;
  696. RT_ASSERT(dev != RT_NULL);
  697. serial = (struct rt_serial_device *)dev;
  698. if (serial->config.tx_bufsz == 0)
  699. {
  700. /* Cannot use RT_SERIAL_TX_NON_BLOCKING when tx_bufsz is 0 */
  701. if (tx_oflag == RT_SERIAL_TX_NON_BLOCKING)
  702. {
  703. LOG_E("(%s) serial device with misconfigure: tx_bufsz = 0",
  704. dev->parent.name);
  705. return -RT_EINVAL;
  706. }
  707. #ifndef RT_USING_DEVICE_OPS
  708. dev->write = _serial_poll_tx;
  709. #endif
  710. dev->open_flag |= RT_SERIAL_TX_BLOCKING;
  711. return RT_EOK;
  712. }
  713. /* Limits the minimum value of tx_bufsz */
  714. if (serial->config.tx_bufsz < RT_SERIAL_TX_MINBUFSZ)
  715. serial->config.tx_bufsz = RT_SERIAL_TX_MINBUFSZ;
  716. if (tx_oflag == RT_SERIAL_TX_BLOCKING)
  717. {
  718. /* When using RT_SERIAL_TX_BLOCKING, it is necessary to determine
  719. * whether serial device needs to use buffer */
  720. rt_err_t optmode; /* The operating mode used by serial device */
  721. /* Call the Control() API to get the operating mode */
  722. optmode = serial->ops->control(serial,
  723. RT_DEVICE_CHECK_OPTMODE,
  724. (void *)RT_DEVICE_FLAG_TX_BLOCKING);
  725. if (optmode == RT_SERIAL_TX_BLOCKING_BUFFER)
  726. {
  727. /* If use RT_SERIAL_TX_BLOCKING_BUFFER, the ringbuffer is initialized */
  728. tx_fifo = (struct rt_serial_tx_fifo *)rt_malloc(sizeof(struct rt_serial_tx_fifo) + serial->config.tx_bufsz);
  729. RT_ASSERT(tx_fifo != RT_NULL);
  730. rt_memset(tx_fifo, RT_NULL, sizeof(struct rt_serial_tx_fifo) + serial->config.tx_bufsz);
  731. rt_ringbuffer_init(&tx_fifo->rb,
  732. (rt_uint8_t *)tx_fifo + sizeof(struct rt_serial_tx_fifo),
  733. serial->config.tx_bufsz);
  734. serial->serial_tx = tx_fifo;
  735. #ifndef RT_USING_DEVICE_OPS
  736. dev->write = _serial_fifo_tx_blocking_buf;
  737. #endif
  738. }
  739. else
  740. {
  741. /* If not use RT_SERIAL_TX_BLOCKING_BUFFER,
  742. * the control() API is called to configure the serial device */
  743. tx_fifo = (struct rt_serial_tx_fifo *)rt_malloc(sizeof(struct rt_serial_tx_fifo));
  744. RT_ASSERT(tx_fifo != RT_NULL);
  745. rt_memset(tx_fifo, RT_NULL, sizeof(struct rt_serial_tx_fifo));
  746. /* Init rb.buffer_ptr to RT_NULL, in rt_serial_write() need check it
  747. * otherwise buffer_ptr maybe a random value, as rt_malloc not init memory */
  748. serial->serial_tx = tx_fifo;
  749. #ifndef RT_USING_DEVICE_OPS
  750. dev->write = _serial_fifo_tx_blocking_nbuf;
  751. #endif
  752. /* Call the control() API to configure the serial device by RT_SERIAL_TX_BLOCKING*/
  753. serial->ops->control(serial,
  754. RT_DEVICE_CTRL_CONFIG,
  755. (void *)RT_SERIAL_TX_BLOCKING);
  756. }
  757. rt_atomic_flag_clear(&tx_fifo->activated);
  758. tx_fifo->put_size = 0;
  759. rt_atomic_store(&tx_fifo->tx_timeout, RT_WAITING_FOREVER);
  760. rt_completion_init(&tx_fifo->tx_cpt);
  761. dev->open_flag |= RT_SERIAL_TX_BLOCKING;
  762. return RT_EOK;
  763. }
  764. /* When using RT_SERIAL_TX_NON_BLOCKING, ringbuffer needs to be initialized,
  765. * and initialize the tx_fifo->activated value is RT_FALSE.
  766. */
  767. tx_fifo = (struct rt_serial_tx_fifo *)rt_malloc(sizeof(struct rt_serial_tx_fifo) + serial->config.tx_bufsz);
  768. RT_ASSERT(tx_fifo != RT_NULL);
  769. rt_memset(tx_fifo, RT_NULL, sizeof(struct rt_serial_tx_fifo) + serial->config.tx_bufsz);
  770. rt_ringbuffer_init(&tx_fifo->rb,
  771. (rt_uint8_t *)tx_fifo + sizeof(struct rt_serial_tx_fifo),
  772. serial->config.tx_bufsz);
  773. serial->serial_tx = tx_fifo;
  774. rt_atomic_flag_clear(&tx_fifo->activated);
  775. tx_fifo->put_size = 0;
  776. #ifndef RT_USING_DEVICE_OPS
  777. dev->write = _serial_fifo_tx_nonblocking;
  778. #endif
  779. rt_completion_init(&tx_fifo->tx_cpt);
  780. dev->open_flag |= RT_SERIAL_TX_NON_BLOCKING;
  781. /* Call the control() API to configure the serial device by RT_SERIAL_TX_NON_BLOCKING*/
  782. serial->ops->control(serial,
  783. RT_DEVICE_CTRL_CONFIG,
  784. (void *)RT_SERIAL_TX_NON_BLOCKING);
  785. return RT_EOK;
  786. }
  787. /**
  788. * @brief Enable serial receive mode.
  789. * @param dev The pointer of device driver structure
  790. * @param rx_oflag The flag of that the serial port opens.
  791. * @return Return the status of the operation.
  792. */
  793. static rt_err_t rt_serial_rx_enable(struct rt_device *dev,
  794. rt_uint16_t rx_oflag)
  795. {
  796. struct rt_serial_device *serial;
  797. struct rt_serial_rx_fifo *rx_fifo = RT_NULL;
  798. rt_size_t rx_fifo_size = 0;
  799. RT_ASSERT(dev != RT_NULL);
  800. serial = (struct rt_serial_device *)dev;
  801. if (serial->config.rx_bufsz == 0)
  802. {
  803. /* Cannot use RT_SERIAL_RX_NON_BLOCKING when rx_bufsz is 0 */
  804. if (rx_oflag == RT_SERIAL_RX_NON_BLOCKING)
  805. {
  806. LOG_E("(%s) serial device with misconfigure: rx_bufsz = 0",
  807. dev->parent.name);
  808. return -RT_EINVAL;
  809. }
  810. #ifndef RT_USING_DEVICE_OPS
  811. dev->read = _serial_poll_rx;
  812. #endif
  813. dev->open_flag |= RT_SERIAL_RX_BLOCKING;
  814. return RT_EOK;
  815. }
  816. /* Limits the minimum value of rx_bufsz */
  817. if (serial->config.rx_bufsz < RT_SERIAL_RX_MINBUFSZ)
  818. serial->config.rx_bufsz = RT_SERIAL_RX_MINBUFSZ;
  819. #ifdef RT_SERIAL_USING_DMA
  820. if (serial->config.dma_ping_bufsz > 0 && serial->config.dma_ping_bufsz < RT_SERIAL_RX_MINBUFSZ / 2)
  821. serial->config.dma_ping_bufsz = RT_SERIAL_RX_MINBUFSZ / 2;
  822. rx_fifo_size = sizeof(struct rt_serial_rx_fifo) + serial->config.rx_bufsz + serial->config.dma_ping_bufsz;
  823. #else
  824. rx_fifo_size = sizeof(struct rt_serial_rx_fifo) + serial->config.rx_bufsz;
  825. #endif
  826. rx_fifo = (struct rt_serial_rx_fifo *)rt_malloc(rx_fifo_size);
  827. RT_ASSERT(rx_fifo != RT_NULL);
  828. rt_memset(rx_fifo, RT_NULL, rx_fifo_size);
  829. rt_ringbuffer_init(&rx_fifo->rb,
  830. (rt_uint8_t *)rx_fifo + sizeof(struct rt_serial_rx_fifo),
  831. serial->config.rx_bufsz);
  832. #ifdef RT_SERIAL_USING_DMA
  833. if (serial->config.dma_ping_bufsz > 0)
  834. {
  835. rt_ringbuffer_init(&rx_fifo->dma_ping_rb,
  836. (rt_uint8_t *)rx_fifo + sizeof(struct rt_serial_rx_fifo) + serial->config.rx_bufsz,
  837. serial->config.dma_ping_bufsz);
  838. }
  839. #endif
  840. serial->serial_rx = rx_fifo;
  841. #ifndef RT_USING_DEVICE_OPS
  842. dev->read = _serial_fifo_rx;
  843. #endif
  844. if (rx_oflag == RT_SERIAL_RX_NON_BLOCKING)
  845. {
  846. dev->open_flag |= RT_SERIAL_RX_NON_BLOCKING;
  847. /* Call the control() API to configure the serial device by RT_SERIAL_RX_NON_BLOCKING*/
  848. serial->ops->control(serial,
  849. RT_DEVICE_CTRL_CONFIG,
  850. (void *)RT_SERIAL_RX_NON_BLOCKING);
  851. return RT_EOK;
  852. }
  853. /* When using RT_SERIAL_RX_BLOCKING, rt_completion_init() and rx_cpt_index are initialized */
  854. rx_fifo->rx_cpt_index = 0;
  855. rt_atomic_store(&rx_fifo->rx_timeout, RT_WAITING_FOREVER);
  856. rt_completion_init(&rx_fifo->rx_cpt);
  857. dev->open_flag |= RT_SERIAL_RX_BLOCKING;
  858. /* Call the control() API to configure the serial device by RT_SERIAL_RX_BLOCKING*/
  859. serial->ops->control(serial,
  860. RT_DEVICE_CTRL_CONFIG,
  861. (void *)RT_SERIAL_RX_BLOCKING);
  862. return RT_EOK;
  863. }
  864. /**
  865. * @brief Disable serial receive mode.
  866. * @param dev The pointer of device driver structure
  867. * @param rx_oflag The flag of that the serial port opens.
  868. * @return Return the status of the operation.
  869. */
  870. static rt_err_t rt_serial_rx_disable(struct rt_device *dev,
  871. rt_uint16_t rx_oflag)
  872. {
  873. struct rt_serial_device *serial;
  874. struct rt_serial_rx_fifo *rx_fifo;
  875. RT_ASSERT(dev != RT_NULL);
  876. serial = (struct rt_serial_device *)dev;
  877. #ifndef RT_USING_DEVICE_OPS
  878. dev->read = RT_NULL;
  879. #endif
  880. if (serial->serial_rx == RT_NULL) return RT_EOK;
  881. if (rx_oflag == RT_SERIAL_RX_NON_BLOCKING)
  882. {
  883. dev->open_flag &= ~RT_SERIAL_RX_NON_BLOCKING;
  884. serial->ops->control(serial,
  885. RT_DEVICE_CTRL_CLR_INT,
  886. (void *)RT_SERIAL_RX_NON_BLOCKING);
  887. }
  888. else
  889. {
  890. dev->open_flag &= ~RT_SERIAL_RX_BLOCKING;
  891. serial->ops->control(serial,
  892. RT_DEVICE_CTRL_CLR_INT,
  893. (void *)RT_SERIAL_RX_BLOCKING);
  894. }
  895. rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
  896. RT_ASSERT(rx_fifo != RT_NULL);
  897. rt_free(rx_fifo);
  898. serial->serial_rx = RT_NULL;
  899. return RT_EOK;
  900. }
  901. /**
  902. * @brief Disable serial tranmit mode.
  903. * @param dev The pointer of device driver structure
  904. * @param rx_oflag The flag of that the serial port opens.
  905. * @return Return the status of the operation.
  906. */
  907. static rt_err_t rt_serial_tx_disable(struct rt_device *dev,
  908. rt_uint16_t tx_oflag)
  909. {
  910. struct rt_serial_device *serial;
  911. struct rt_serial_tx_fifo *tx_fifo;
  912. RT_ASSERT(dev != RT_NULL);
  913. serial = (struct rt_serial_device *)dev;
  914. #ifndef RT_USING_DEVICE_OPS
  915. dev->write = RT_NULL;
  916. #endif
  917. if (serial->serial_tx == RT_NULL) return RT_EOK;
  918. tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
  919. RT_ASSERT(tx_fifo != RT_NULL);
  920. if (tx_oflag == RT_SERIAL_TX_NON_BLOCKING)
  921. {
  922. dev->open_flag &= ~RT_SERIAL_TX_NON_BLOCKING;
  923. serial->ops->control(serial,
  924. RT_DEVICE_CTRL_CLR_INT,
  925. (void *)RT_SERIAL_TX_NON_BLOCKING);
  926. }
  927. else
  928. {
  929. rt_completion_done(&tx_fifo->tx_cpt);
  930. dev->open_flag &= ~RT_SERIAL_TX_BLOCKING;
  931. serial->ops->control(serial,
  932. RT_DEVICE_CTRL_CLR_INT,
  933. (void *)RT_SERIAL_TX_BLOCKING);
  934. }
  935. rt_free(tx_fifo);
  936. serial->serial_tx = RT_NULL;
  937. return RT_EOK;
  938. }
  939. /**
  940. * @brief Initialize the serial device.
  941. * @param dev The pointer of device driver structure
  942. * @return Return the status of the operation.
  943. */
  944. static rt_err_t rt_serial_init(struct rt_device *dev)
  945. {
  946. rt_err_t result = RT_EOK;
  947. struct rt_serial_device *serial;
  948. RT_ASSERT(dev != RT_NULL);
  949. serial = (struct rt_serial_device *)dev;
  950. RT_ASSERT(serial->ops->transmit != RT_NULL);
  951. /* initialize rx/tx */
  952. serial->serial_rx = RT_NULL;
  953. serial->serial_tx = RT_NULL;
  954. /* apply configuration */
  955. if (serial->ops->configure)
  956. result = serial->ops->configure(serial, &serial->config);
  957. return result;
  958. }
  959. /**
  960. * @brief Open the serial device.
  961. * @param dev The pointer of device driver structure
  962. * @param oflag The flag of that the serial port opens.
  963. * @return Return the status of the operation.
  964. */
  965. static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
  966. {
  967. struct rt_serial_device *serial;
  968. RT_ASSERT(dev != RT_NULL);
  969. serial = (struct rt_serial_device *)dev;
  970. /* Check that the device has been turned on */
  971. if ((dev->open_flag) & (15 << 12))
  972. {
  973. LOG_D("(%s) serial device has already been opened, it will run in its original configuration", dev->parent.name);
  974. return RT_EOK;
  975. }
  976. LOG_D("open serial device: 0x%08x with open flag: 0x%04x",
  977. dev, oflag);
  978. /* By default, the receive mode of a serial devide is RT_SERIAL_RX_NON_BLOCKING */
  979. if ((oflag & RT_SERIAL_RX_BLOCKING) == RT_SERIAL_RX_BLOCKING)
  980. dev->open_flag |= RT_SERIAL_RX_BLOCKING;
  981. else
  982. dev->open_flag |= RT_SERIAL_RX_NON_BLOCKING;
  983. /* By default, the transmit mode of a serial devide is RT_SERIAL_TX_BLOCKING */
  984. if ((oflag & RT_SERIAL_TX_NON_BLOCKING) == RT_SERIAL_TX_NON_BLOCKING)
  985. dev->open_flag |= RT_SERIAL_TX_NON_BLOCKING;
  986. else
  987. dev->open_flag |= RT_SERIAL_TX_BLOCKING;
  988. /* set steam flag */
  989. if ((oflag & RT_DEVICE_FLAG_STREAM) || (dev->open_flag & RT_DEVICE_FLAG_STREAM))
  990. dev->open_flag |= RT_DEVICE_FLAG_STREAM;
  991. /* initialize the Rx structure according to open flag */
  992. if (serial->serial_rx == RT_NULL)
  993. rt_serial_rx_enable(dev, dev->open_flag & (RT_SERIAL_RX_BLOCKING | RT_SERIAL_RX_NON_BLOCKING));
  994. /* initialize the Tx structure according to open flag */
  995. if (serial->serial_tx == RT_NULL)
  996. rt_serial_tx_enable(dev, dev->open_flag & (RT_SERIAL_TX_BLOCKING | RT_SERIAL_TX_NON_BLOCKING));
  997. return RT_EOK;
  998. }
  999. /**
  1000. * @brief Close the serial device.
  1001. * @param dev The pointer of device driver structure
  1002. * @return Return the status of the operation.
  1003. */
  1004. static rt_err_t rt_serial_close(struct rt_device *dev)
  1005. {
  1006. struct rt_serial_device *serial;
  1007. RT_ASSERT(dev != RT_NULL);
  1008. serial = (struct rt_serial_device *)dev;
  1009. /* this device has more reference count */
  1010. if (dev->ref_count > 1) return -RT_ERROR;
  1011. /* Disable serial receive mode. */
  1012. rt_serial_rx_disable(dev, dev->open_flag & (RT_SERIAL_RX_BLOCKING | RT_SERIAL_RX_NON_BLOCKING));
  1013. /* Disable serial tranmit mode. */
  1014. rt_serial_tx_disable(dev, dev->open_flag & (RT_SERIAL_TX_BLOCKING | RT_SERIAL_TX_NON_BLOCKING));
  1015. /* Clear the callback function */
  1016. serial->parent.rx_indicate = RT_NULL;
  1017. serial->parent.tx_complete = RT_NULL;
  1018. rt_memset(&serial->rx_notify, RT_NULL, sizeof(struct rt_device_notify));
  1019. /* Call the control() API to close the serial device */
  1020. serial->ops->control(serial, RT_DEVICE_CTRL_CLOSE, RT_NULL);
  1021. dev->flag &= ~RT_DEVICE_FLAG_ACTIVATED;
  1022. return RT_EOK;
  1023. }
  1024. static void _serial_rx_flush(struct rt_serial_device *serial)
  1025. {
  1026. rt_base_t level;
  1027. struct rt_serial_rx_fifo *rx_fifo;
  1028. RT_ASSERT(serial != RT_NULL);
  1029. if (serial->config.rx_bufsz == 0)
  1030. {
  1031. while (serial->ops->getc(serial) != -1)
  1032. {
  1033. }
  1034. }
  1035. else
  1036. {
  1037. rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
  1038. RT_ASSERT(rx_fifo != RT_NULL);
  1039. level = rt_spin_lock_irqsave(&serial->spinlock);
  1040. rt_completion_wait(&rx_fifo->rx_cpt, 0);
  1041. rt_ringbuffer_reset(&rx_fifo->rb);
  1042. rx_fifo->rx_cpt_index = 0;
  1043. #ifdef RT_SERIAL_USING_DMA
  1044. if (serial->config.dma_ping_bufsz > 0)
  1045. {
  1046. rt_ringbuffer_reset(&rx_fifo->dma_ping_rb);
  1047. }
  1048. #endif
  1049. rt_spin_unlock_irqrestore(&serial->spinlock, level);
  1050. }
  1051. }
  1052. static void _serial_tx_flush(struct rt_serial_device *serial)
  1053. {
  1054. struct rt_serial_tx_fifo *tx_fifo;
  1055. RT_ASSERT(serial != RT_NULL);
  1056. if (serial->config.tx_bufsz != 0)
  1057. {
  1058. tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
  1059. RT_ASSERT(tx_fifo != RT_NULL);
  1060. if (rt_atomic_load(&tx_fifo->activated))
  1061. {
  1062. rt_completion_wait(&tx_fifo->tx_cpt, RT_WAITING_FOREVER);
  1063. return;
  1064. }
  1065. }
  1066. }
  1067. static rt_err_t _serial_get_unread_bytes_count(struct rt_serial_device *serial, rt_ssize_t *unread_bytes)
  1068. {
  1069. rt_base_t level;
  1070. struct rt_serial_rx_fifo *rx_fifo;
  1071. RT_ASSERT(serial != RT_NULL);
  1072. if (serial->config.rx_bufsz == 0)
  1073. {
  1074. LOG_W("get unread bytes count not support in poll mode.");
  1075. *unread_bytes = -1;
  1076. return -RT_EPERM;
  1077. }
  1078. else
  1079. {
  1080. rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
  1081. RT_ASSERT(rx_fifo != RT_NULL);
  1082. level = RT_SERIAL_FIFO_LOCK(&serial->spinlock);
  1083. *unread_bytes = rt_ringbuffer_data_len(&rx_fifo->rb);
  1084. RT_SERIAL_FIFO_UNLOCK(&serial->spinlock, level);
  1085. }
  1086. return RT_EOK;
  1087. }
  1088. #ifdef RT_USING_POSIX_TERMIOS
  1089. struct speed_baudrate_item
  1090. {
  1091. speed_t speed;
  1092. int baudrate;
  1093. };
  1094. const static struct speed_baudrate_item _tbl[] =
  1095. {
  1096. { B2400, BAUD_RATE_2400},
  1097. { B4800, BAUD_RATE_4800},
  1098. { B9600, BAUD_RATE_9600},
  1099. { B19200, BAUD_RATE_19200},
  1100. { B38400, BAUD_RATE_38400},
  1101. { B57600, BAUD_RATE_57600},
  1102. { B115200, BAUD_RATE_115200},
  1103. { B230400, BAUD_RATE_230400},
  1104. { B460800, BAUD_RATE_460800},
  1105. { B500000, BAUD_RATE_500000},
  1106. { B921600, BAUD_RATE_921600},
  1107. {B2000000, BAUD_RATE_2000000},
  1108. {B3000000, BAUD_RATE_3000000},
  1109. };
  1110. static speed_t _get_speed(int baudrate)
  1111. {
  1112. int index;
  1113. for (index = 0; index < sizeof(_tbl) / sizeof(_tbl[0]); index++)
  1114. {
  1115. if (_tbl[index].baudrate == baudrate)
  1116. return _tbl[index].speed;
  1117. }
  1118. return B0;
  1119. }
  1120. static int _get_baudrate(speed_t speed)
  1121. {
  1122. int index;
  1123. for (index = 0; index < sizeof(_tbl) / sizeof(_tbl[0]); index++)
  1124. {
  1125. if (_tbl[index].speed == speed)
  1126. return _tbl[index].baudrate;
  1127. }
  1128. return 0;
  1129. }
  1130. static void _tc_flush(struct rt_serial_device *serial, int queue)
  1131. {
  1132. RT_ASSERT(serial != RT_NULL);
  1133. if (queue == TCIFLUSH || queue == TCIOFLUSH)
  1134. {
  1135. _serial_rx_flush(serial);
  1136. }
  1137. if (queue == TCOFLUSH || queue == TCIOFLUSH)
  1138. {
  1139. _serial_tx_flush(serial);
  1140. }
  1141. }
  1142. #endif /* RT_USING_POSIX_TERMIOS */
  1143. /**
  1144. * @brief Control the serial device.
  1145. * @param dev The pointer of device driver structure
  1146. * @param cmd The command value that controls the serial device
  1147. * @param args The parameter value that controls the serial device
  1148. * @return Return the status of the operation.
  1149. */
  1150. static rt_err_t rt_serial_control(struct rt_device *dev,
  1151. int cmd,
  1152. void *args)
  1153. {
  1154. rt_err_t ret = RT_EOK;
  1155. struct rt_serial_device *serial;
  1156. RT_ASSERT(dev != RT_NULL);
  1157. serial = (struct rt_serial_device *)dev;
  1158. switch (cmd)
  1159. {
  1160. case RT_DEVICE_CTRL_SUSPEND:
  1161. /* suspend device */
  1162. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  1163. break;
  1164. case RT_DEVICE_CTRL_RESUME:
  1165. /* resume device */
  1166. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  1167. break;
  1168. case RT_DEVICE_CTRL_CONFIG:
  1169. if (args == RT_NULL)
  1170. {
  1171. ret = -RT_EINVAL;
  1172. }
  1173. else
  1174. {
  1175. struct serial_configure *pconfig = (struct serial_configure *)args;
  1176. if (((pconfig->rx_bufsz != serial->config.rx_bufsz)
  1177. || (pconfig->tx_bufsz != serial->config.tx_bufsz)
  1178. #ifdef RT_SERIAL_USING_DMA
  1179. || (pconfig->dma_ping_bufsz != serial->config.dma_ping_bufsz)
  1180. #endif
  1181. )
  1182. && serial->parent.ref_count != 0)
  1183. {
  1184. /*can not change buffer size*/
  1185. ret = -RT_EBUSY;
  1186. break;
  1187. }
  1188. /* set serial configure */
  1189. serial->config = *pconfig;
  1190. serial->ops->configure(serial, (struct serial_configure *)args);
  1191. }
  1192. break;
  1193. case RT_DEVICE_CTRL_NOTIFY_SET:
  1194. if (args == RT_NULL)
  1195. {
  1196. ret = -RT_EINVAL;
  1197. }
  1198. else
  1199. {
  1200. rt_memcpy(&serial->rx_notify, args, sizeof(struct rt_device_notify));
  1201. }
  1202. break;
  1203. case RT_DEVICE_CTRL_CONSOLE_OFLAG:
  1204. if (args == RT_NULL)
  1205. {
  1206. ret = -RT_EINVAL;
  1207. }
  1208. else
  1209. {
  1210. *(rt_uint16_t *)args = RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM;
  1211. }
  1212. break;
  1213. /* Call before rt_device_read */
  1214. case RT_SERIAL_CTRL_SET_RX_TIMEOUT:
  1215. if (args == RT_NULL)
  1216. {
  1217. ret = -RT_EINVAL;
  1218. }
  1219. else
  1220. {
  1221. if (serial->config.rx_bufsz == 0)
  1222. {
  1223. ret = -RT_EPERM;
  1224. }
  1225. struct rt_serial_rx_fifo *rx_fifo = RT_NULL;
  1226. rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
  1227. RT_ASSERT(rx_fifo != RT_NULL);
  1228. rt_atomic_store(&rx_fifo->rx_timeout, *(rt_int32_t *)args);
  1229. }
  1230. break;
  1231. /* Call before rt_device_write */
  1232. case RT_SERIAL_CTRL_SET_TX_TIMEOUT:
  1233. if (args == RT_NULL)
  1234. {
  1235. ret = -RT_EINVAL;
  1236. }
  1237. else
  1238. {
  1239. if (serial->config.tx_bufsz == 0)
  1240. {
  1241. ret = -RT_EPERM;
  1242. }
  1243. struct rt_serial_tx_fifo *tx_fifo = RT_NULL;
  1244. tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
  1245. RT_ASSERT(tx_fifo != RT_NULL);
  1246. rt_atomic_store(&tx_fifo->tx_timeout, *(rt_int32_t *)args);
  1247. }
  1248. break;
  1249. case RT_SERIAL_CTRL_GET_RX_TIMEOUT:
  1250. if (args == RT_NULL)
  1251. {
  1252. ret = -RT_EINVAL;
  1253. }
  1254. else
  1255. {
  1256. if (serial->config.rx_bufsz == 0)
  1257. {
  1258. ret = -RT_EPERM;
  1259. }
  1260. struct rt_serial_rx_fifo *rx_fifo = RT_NULL;
  1261. rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
  1262. RT_ASSERT(rx_fifo != RT_NULL);
  1263. *(rt_int32_t *)args = rt_atomic_load(&rx_fifo->rx_timeout);
  1264. }
  1265. break;
  1266. case RT_SERIAL_CTRL_GET_TX_TIMEOUT:
  1267. if (args == RT_NULL)
  1268. {
  1269. ret = -RT_EINVAL;
  1270. }
  1271. else
  1272. {
  1273. if (serial->config.tx_bufsz == 0)
  1274. {
  1275. ret = -RT_EPERM;
  1276. }
  1277. struct rt_serial_tx_fifo *tx_fifo = RT_NULL;
  1278. tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
  1279. RT_ASSERT(tx_fifo != RT_NULL);
  1280. *(rt_int32_t *)args = rt_atomic_load(&tx_fifo->tx_timeout);
  1281. }
  1282. break;
  1283. /* Discard all data */
  1284. case RT_SERIAL_CTRL_RX_FLUSH:
  1285. _serial_rx_flush(serial);
  1286. break;
  1287. /* Blocking and wait for the send buffer data to be sent. */
  1288. case RT_SERIAL_CTRL_TX_FLUSH:
  1289. _serial_tx_flush(serial);
  1290. break;
  1291. /* get unread bytes count. */
  1292. case RT_SERIAL_CTRL_GET_UNREAD_BYTES_COUNT:
  1293. if (args == RT_NULL)
  1294. {
  1295. ret = -RT_EINVAL;
  1296. }
  1297. else
  1298. {
  1299. ret = _serial_get_unread_bytes_count(serial, (rt_ssize_t *)args);
  1300. }
  1301. break;
  1302. #ifdef RT_USING_POSIX_STDIO
  1303. #ifdef RT_USING_POSIX_TERMIOS
  1304. case TCGETA: {
  1305. struct termios *tio = (struct termios *)args;
  1306. if (tio == RT_NULL) return -RT_EINVAL;
  1307. tio->c_iflag = 0;
  1308. tio->c_oflag = 0;
  1309. tio->c_lflag = 0;
  1310. /* update oflag for console device */
  1311. if (rt_console_get_device() == dev)
  1312. tio->c_oflag = OPOST | ONLCR;
  1313. /* set cflag */
  1314. tio->c_cflag = 0;
  1315. if (serial->config.data_bits == DATA_BITS_5)
  1316. tio->c_cflag = CS5;
  1317. else if (serial->config.data_bits == DATA_BITS_6)
  1318. tio->c_cflag = CS6;
  1319. else if (serial->config.data_bits == DATA_BITS_7)
  1320. tio->c_cflag = CS7;
  1321. else if (serial->config.data_bits == DATA_BITS_8)
  1322. tio->c_cflag = CS8;
  1323. if (serial->config.stop_bits == STOP_BITS_2)
  1324. tio->c_cflag |= CSTOPB;
  1325. if (serial->config.parity == PARITY_EVEN)
  1326. tio->c_cflag |= PARENB;
  1327. else if (serial->config.parity == PARITY_ODD)
  1328. tio->c_cflag |= (PARODD | PARENB);
  1329. if (serial->config.flowcontrol == RT_SERIAL_FLOWCONTROL_CTSRTS)
  1330. tio->c_cflag |= CRTSCTS;
  1331. cfsetospeed(tio, _get_speed(serial->config.baud_rate));
  1332. }
  1333. break;
  1334. case TCSETAW:
  1335. case TCSETAF:
  1336. case TCSETA: {
  1337. int baudrate;
  1338. struct serial_configure config;
  1339. struct termios *tio = (struct termios *)args;
  1340. if (tio == RT_NULL) return -RT_EINVAL;
  1341. config = serial->config;
  1342. baudrate = _get_baudrate(cfgetospeed(tio));
  1343. config.baud_rate = baudrate;
  1344. switch (tio->c_cflag & CSIZE)
  1345. {
  1346. case CS5:
  1347. config.data_bits = DATA_BITS_5;
  1348. break;
  1349. case CS6:
  1350. config.data_bits = DATA_BITS_6;
  1351. break;
  1352. case CS7:
  1353. config.data_bits = DATA_BITS_7;
  1354. break;
  1355. default:
  1356. config.data_bits = DATA_BITS_8;
  1357. break;
  1358. }
  1359. if (tio->c_cflag & CSTOPB)
  1360. config.stop_bits = STOP_BITS_2;
  1361. else
  1362. config.stop_bits = STOP_BITS_1;
  1363. if (tio->c_cflag & PARENB)
  1364. {
  1365. if (tio->c_cflag & PARODD)
  1366. config.parity = PARITY_ODD;
  1367. else
  1368. config.parity = PARITY_EVEN;
  1369. }
  1370. else
  1371. config.parity = PARITY_NONE;
  1372. if (tio->c_cflag & CRTSCTS)
  1373. config.flowcontrol = RT_SERIAL_FLOWCONTROL_CTSRTS;
  1374. else
  1375. config.flowcontrol = RT_SERIAL_FLOWCONTROL_NONE;
  1376. /* set serial configure */
  1377. serial->config = config;
  1378. serial->ops->configure(serial, &config);
  1379. }
  1380. break;
  1381. case TCFLSH: {
  1382. int queue = (int)args;
  1383. _tc_flush(serial, queue);
  1384. }
  1385. break;
  1386. case TCXONC:
  1387. break;
  1388. #endif /*RT_USING_POSIX_TERMIOS*/
  1389. case TIOCSWINSZ: {
  1390. struct winsize *p_winsize;
  1391. p_winsize = (struct winsize *)args;
  1392. rt_kprintf("\x1b[8;%d;%dt", p_winsize->ws_col, p_winsize->ws_row);
  1393. }
  1394. break;
  1395. case TIOCGWINSZ: {
  1396. struct winsize *p_winsize;
  1397. p_winsize = (struct winsize *)args;
  1398. if (rt_thread_self() != rt_thread_find(FINSH_THREAD_NAME))
  1399. {
  1400. /* only can be used in tshell thread; otherwise, return default size */
  1401. p_winsize->ws_col = 80;
  1402. p_winsize->ws_row = 24;
  1403. }
  1404. else
  1405. {
  1406. #include <shell.h>
  1407. #define _TIO_BUFLEN 20
  1408. char _tio_buf[_TIO_BUFLEN];
  1409. unsigned char cnt1, cnt2, cnt3, i;
  1410. char row_s[4], col_s[4];
  1411. char *p;
  1412. rt_memset(_tio_buf, 0, _TIO_BUFLEN);
  1413. /* send the command to terminal for getting the window size of the terminal */
  1414. rt_kprintf("\033[18t");
  1415. /* waiting for the response from the terminal */
  1416. i = 0;
  1417. while (i < _TIO_BUFLEN)
  1418. {
  1419. _tio_buf[i] = finsh_getchar();
  1420. if (_tio_buf[i] != 't')
  1421. {
  1422. i++;
  1423. }
  1424. else
  1425. {
  1426. break;
  1427. }
  1428. }
  1429. if (i == _TIO_BUFLEN)
  1430. {
  1431. /* buffer overloaded, and return default size */
  1432. p_winsize->ws_col = 80;
  1433. p_winsize->ws_row = 24;
  1434. break;
  1435. }
  1436. /* interpreting data eg: "\033[8;1;15t" which means row is 1 and col is 15 (unit: size of ONE character) */
  1437. rt_memset(row_s, 0, 4);
  1438. rt_memset(col_s, 0, 4);
  1439. cnt1 = 0;
  1440. while (cnt1 < _TIO_BUFLEN && _tio_buf[cnt1] != ';')
  1441. {
  1442. cnt1++;
  1443. }
  1444. cnt2 = ++cnt1;
  1445. while (cnt2 < _TIO_BUFLEN && _tio_buf[cnt2] != ';')
  1446. {
  1447. cnt2++;
  1448. }
  1449. p = row_s;
  1450. while (cnt1 < cnt2)
  1451. {
  1452. *p++ = _tio_buf[cnt1++];
  1453. }
  1454. p = col_s;
  1455. cnt2++;
  1456. cnt3 = rt_strlen(_tio_buf) - 1;
  1457. while (cnt2 < cnt3)
  1458. {
  1459. *p++ = _tio_buf[cnt2++];
  1460. }
  1461. /* load the window size date */
  1462. p_winsize->ws_col = atoi(col_s);
  1463. p_winsize->ws_row = atoi(row_s);
  1464. #undef _TIO_BUFLEN
  1465. }
  1466. p_winsize->ws_xpixel = 0; /* unused */
  1467. p_winsize->ws_ypixel = 0; /* unused */
  1468. }
  1469. break;
  1470. case FIONREAD:
  1471. if (args == RT_NULL)
  1472. {
  1473. ret = -RT_EINVAL;
  1474. }
  1475. else
  1476. {
  1477. rt_ssize_t unread_bytes = 0;
  1478. ret = _serial_get_unread_bytes_count(serial, &unread_bytes);
  1479. if (ret == RT_EOK)
  1480. *(rt_size_t *)args = (rt_size_t)unread_bytes;
  1481. else
  1482. *(rt_size_t *)args = 0;
  1483. }
  1484. break;
  1485. #endif /* RT_USING_POSIX_STDIO */
  1486. default:
  1487. /* control device */
  1488. ret = serial->ops->control(serial, cmd, args);
  1489. break;
  1490. }
  1491. return ret;
  1492. }
  1493. #ifdef RT_USING_DEVICE_OPS
  1494. static rt_ssize_t rt_serial_read(struct rt_device *dev,
  1495. rt_off_t pos,
  1496. void *buffer,
  1497. rt_size_t size)
  1498. {
  1499. struct rt_serial_device *serial;
  1500. RT_ASSERT(dev != RT_NULL);
  1501. if (size == 0) return 0;
  1502. serial = (struct rt_serial_device *)dev;
  1503. if (serial->config.rx_bufsz)
  1504. {
  1505. return _serial_fifo_rx(dev, pos, buffer, size);
  1506. }
  1507. return _serial_poll_rx(dev, pos, buffer, size);
  1508. }
  1509. static rt_ssize_t rt_serial_write(struct rt_device *dev,
  1510. rt_off_t pos,
  1511. const void *buffer,
  1512. rt_size_t size)
  1513. {
  1514. struct rt_serial_device *serial;
  1515. struct rt_serial_tx_fifo *tx_fifo;
  1516. RT_ASSERT(dev != RT_NULL);
  1517. if (size == 0) return 0;
  1518. serial = (struct rt_serial_device *)dev;
  1519. RT_ASSERT((serial != RT_NULL) && (buffer != RT_NULL));
  1520. tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
  1521. if (serial->config.tx_bufsz == 0)
  1522. {
  1523. return _serial_poll_tx(dev, pos, buffer, size);
  1524. }
  1525. if (dev->open_flag & RT_SERIAL_TX_BLOCKING)
  1526. {
  1527. RT_ASSERT(tx_fifo != RT_NULL);
  1528. if ((tx_fifo->rb.buffer_ptr) == RT_NULL)
  1529. {
  1530. return _serial_fifo_tx_blocking_nbuf(dev, pos, buffer, size);
  1531. }
  1532. return _serial_fifo_tx_blocking_buf(dev, pos, buffer, size);
  1533. }
  1534. return _serial_fifo_tx_nonblocking(dev, pos, buffer, size);
  1535. }
  1536. const static struct rt_device_ops serial_ops =
  1537. {
  1538. rt_serial_init,
  1539. rt_serial_open,
  1540. rt_serial_close,
  1541. rt_serial_read,
  1542. rt_serial_write,
  1543. rt_serial_control};
  1544. #endif
  1545. /**
  1546. * @brief Register the serial device.
  1547. * @param serial RT-thread serial device.
  1548. * @param name The device driver's name
  1549. * @param flag The capabilities flag of device.
  1550. * @param data The device driver's data.
  1551. * @return Return the status of the operation.
  1552. */
  1553. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
  1554. const char *name,
  1555. rt_uint32_t flag,
  1556. void *data)
  1557. {
  1558. rt_err_t ret;
  1559. struct rt_device *device;
  1560. RT_ASSERT(serial != RT_NULL);
  1561. rt_spin_lock_init(&serial->spinlock);
  1562. device = &serial->parent;
  1563. device->type = RT_Device_Class_Char;
  1564. device->rx_indicate = RT_NULL;
  1565. device->tx_complete = RT_NULL;
  1566. #ifdef RT_USING_DEVICE_OPS
  1567. device->ops = &serial_ops;
  1568. #else
  1569. device->init = rt_serial_init;
  1570. device->open = rt_serial_open;
  1571. device->close = rt_serial_close;
  1572. device->read = RT_NULL;
  1573. device->write = RT_NULL;
  1574. device->control = rt_serial_control;
  1575. #endif
  1576. device->user_data = data;
  1577. /* register a character device */
  1578. ret = rt_device_register(device, name, flag);
  1579. #ifdef RT_USING_POSIX_STDIO
  1580. /* set fops */
  1581. device->fops = &_serial_fops;
  1582. #endif
  1583. return ret;
  1584. }
  1585. /**
  1586. * @brief ISR for serial interrupt
  1587. * @param serial RT-thread serial device.
  1588. * @param event ISR event type.
  1589. */
  1590. rt_err_t rt_hw_serial_control_isr(struct rt_serial_device *serial, int cmd, void *args)
  1591. {
  1592. RT_ASSERT(serial != RT_NULL);
  1593. rt_err_t ret = RT_EOK;
  1594. switch (cmd)
  1595. {
  1596. case RT_HW_SERIAL_CTRL_PUTC:
  1597. if (args == RT_NULL)
  1598. {
  1599. ret = -RT_EINVAL;
  1600. }
  1601. else
  1602. {
  1603. struct rt_serial_rx_fifo *rx_fifo = RT_NULL;
  1604. rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
  1605. RT_ASSERT(rx_fifo != RT_NULL);
  1606. #ifdef RT_SERIAL_BUF_STRATEGY_DROP
  1607. rt_ringbuffer_putchar(&rx_fifo->rb, *(rt_uint8_t *)args);
  1608. #else
  1609. rt_ringbuffer_putchar_force(&rx_fifo->rb, *(rt_uint8_t *)args);
  1610. #endif /* RT_SERIAL_BUF_STRATEGY_DROP */
  1611. }
  1612. break;
  1613. case RT_HW_SERIAL_CTRL_GETC:
  1614. if (args == RT_NULL)
  1615. {
  1616. ret = -RT_EINVAL;
  1617. }
  1618. else
  1619. {
  1620. struct rt_serial_tx_fifo *tx_fifo = RT_NULL;
  1621. tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
  1622. RT_ASSERT(tx_fifo != RT_NULL);
  1623. if (rt_ringbuffer_getchar(&tx_fifo->rb, (rt_uint8_t *)args) == 0)
  1624. {
  1625. ret = -RT_EEMPTY;
  1626. }
  1627. }
  1628. break;
  1629. #ifdef RT_SERIAL_USING_DMA
  1630. case RT_HW_SERIAL_CTRL_GET_DMA_PING_BUF:
  1631. if (args == RT_NULL)
  1632. {
  1633. ret = -RT_EINVAL;
  1634. }
  1635. else
  1636. {
  1637. struct rt_serial_rx_fifo *rx_fifo = RT_NULL;
  1638. rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
  1639. RT_ASSERT(rx_fifo != RT_NULL);
  1640. *(rt_uint8_t **)args = rx_fifo->dma_ping_rb.buffer_ptr;
  1641. }
  1642. break;
  1643. #endif
  1644. default:
  1645. ret = -RT_EINVAL;
  1646. break;
  1647. }
  1648. return ret;
  1649. }
  1650. /**
  1651. * @brief ISR for serial interrupt
  1652. * @param serial RT-thread serial device.
  1653. * @param event ISR event type.
  1654. */
  1655. void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
  1656. {
  1657. RT_ASSERT(serial != RT_NULL);
  1658. switch (event & 0xff)
  1659. {
  1660. /* Interrupt receive event */
  1661. case RT_SERIAL_EVENT_RX_IND:
  1662. case RT_SERIAL_EVENT_RX_DMADONE: {
  1663. struct rt_serial_rx_fifo *rx_fifo;
  1664. rt_size_t rx_length = 0;
  1665. rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
  1666. RT_ASSERT(rx_fifo != RT_NULL);
  1667. #ifdef RT_SERIAL_USING_DMA
  1668. rt_base_t level;
  1669. /* If the event is RT_SERIAL_EVENT_RX_IND, rx_length is equal to 0 */
  1670. rx_length = event >> 8;
  1671. /* RT_SERIAL_EVENT_RX_DMADONE MODE */
  1672. if (rx_length != 0)
  1673. {
  1674. #ifdef RT_SERIAL_BUF_STRATEGY_DROP
  1675. rt_uint8_t *ptr;
  1676. rt_size_t size;
  1677. rt_size_t space_len;
  1678. /* UART_IT_IDLE and dma isr */
  1679. level = rt_spin_lock_irqsave(&serial->spinlock);
  1680. rt_serial_update_write_index(&rx_fifo->dma_ping_rb, rx_length);
  1681. do
  1682. {
  1683. space_len = rt_ringbuffer_space_len(&rx_fifo->rb);
  1684. if (space_len == 0)
  1685. break;
  1686. size = rt_ringbuffer_peek(&rx_fifo->dma_ping_rb, &ptr);
  1687. if (size == 0)
  1688. break;
  1689. space_len -= rt_ringbuffer_put(&rx_fifo->rb, ptr, size);
  1690. if (space_len == 0)
  1691. break;
  1692. size = rt_ringbuffer_peek(&rx_fifo->dma_ping_rb, &ptr);
  1693. if (size == 0)
  1694. break;
  1695. rt_ringbuffer_put(&rx_fifo->rb, ptr, size);
  1696. } while (0);
  1697. if (space_len == 0)
  1698. rt_serial_update_read_index(&rx_fifo->dma_ping_rb, rt_ringbuffer_get_size(&rx_fifo->dma_ping_rb));
  1699. rt_spin_unlock_irqrestore(&serial->spinlock, level);
  1700. #else
  1701. rt_uint8_t *ptr;
  1702. rt_size_t size;
  1703. /* UART_IT_IDLE and dma isr */
  1704. level = rt_spin_lock_irqsave(&serial->spinlock);
  1705. rt_serial_update_write_index(&rx_fifo->dma_ping_rb, rx_length);
  1706. do
  1707. {
  1708. size = rt_ringbuffer_peek(&rx_fifo->dma_ping_rb, &ptr);
  1709. if (size == 0)
  1710. break;
  1711. rt_ringbuffer_put_force(&rx_fifo->rb, ptr, size);
  1712. size = rt_ringbuffer_peek(&rx_fifo->dma_ping_rb, &ptr);
  1713. if (size == 0)
  1714. break;
  1715. rt_ringbuffer_put_force(&rx_fifo->rb, ptr, size);
  1716. } while (0);
  1717. rt_spin_unlock_irqrestore(&serial->spinlock, level);
  1718. #endif /* RT_SERIAL_BUF_STRATEGY_DROP */
  1719. }
  1720. #endif /* RT_SERIAL_USING_DMA */
  1721. rx_length = rt_ringbuffer_data_len(&rx_fifo->rb);
  1722. if (rx_length == 0)
  1723. {
  1724. break;
  1725. }
  1726. if (serial->parent.open_flag & RT_SERIAL_RX_BLOCKING)
  1727. {
  1728. if (rx_fifo->rx_cpt_index && rx_length >= rx_fifo->rx_cpt_index)
  1729. {
  1730. rx_fifo->rx_cpt_index = 0;
  1731. rt_completion_done(&rx_fifo->rx_cpt);
  1732. }
  1733. }
  1734. /* Trigger the receiving completion callback */
  1735. if (serial->parent.rx_indicate != RT_NULL)
  1736. {
  1737. serial->parent.rx_indicate(&serial->parent, rx_length);
  1738. }
  1739. if (serial->rx_notify.notify != RT_NULL)
  1740. {
  1741. serial->rx_notify.notify(serial->rx_notify.dev);
  1742. }
  1743. break;
  1744. }
  1745. /* Interrupt transmit event */
  1746. case RT_SERIAL_EVENT_TX_DONE: {
  1747. struct rt_serial_tx_fifo *tx_fifo;
  1748. rt_size_t tx_length = 0;
  1749. tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
  1750. RT_ASSERT(tx_fifo != RT_NULL);
  1751. /* Get the length of the data from the ringbuffer */
  1752. tx_length = rt_ringbuffer_data_len(&tx_fifo->rb);
  1753. /* If there is no data in tx_ringbuffer,
  1754. * then the transmit completion callback is triggered*/
  1755. if (tx_length == 0)
  1756. {
  1757. rt_completion_done(&tx_fifo->tx_cpt);
  1758. /* Trigger the transmit completion callback */
  1759. if (serial->parent.tx_complete != RT_NULL)
  1760. {
  1761. serial->parent.tx_complete(&serial->parent, RT_NULL);
  1762. }
  1763. rt_atomic_flag_clear(&tx_fifo->activated);
  1764. break;
  1765. }
  1766. rt_atomic_flag_test_and_set(&tx_fifo->activated);
  1767. /* Call the transmit interface for transmission again */
  1768. /* Note that in interrupt mode, tx_fifo->buffer and tx_length
  1769. * are inactive parameters */
  1770. serial->ops->transmit(serial,
  1771. tx_fifo->rb.buffer_ptr,
  1772. tx_length,
  1773. serial->parent.open_flag & (RT_SERIAL_TX_BLOCKING | RT_SERIAL_TX_NON_BLOCKING));
  1774. break;
  1775. }
  1776. #ifdef RT_SERIAL_USING_DMA
  1777. case RT_SERIAL_EVENT_TX_DMADONE: {
  1778. struct rt_serial_tx_fifo *tx_fifo;
  1779. rt_size_t tx_length = 0;
  1780. tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
  1781. RT_ASSERT(tx_fifo != RT_NULL);
  1782. if ((serial->parent.open_flag & RT_SERIAL_TX_BLOCKING) != RT_SERIAL_TX_BLOCKING || rt_ringbuffer_get_size(&tx_fifo->rb) != 0)
  1783. {
  1784. // 每次进来中断就说明之前的put_size已经被发送完毕了
  1785. rt_serial_update_read_index(&tx_fifo->rb, tx_fifo->put_size);
  1786. /* Get the length of the data from the ringbuffer */
  1787. tx_length = rt_ringbuffer_data_len(&tx_fifo->rb);
  1788. if (tx_length != 0)
  1789. {
  1790. /* If there is some data in tx_ringbuffer,
  1791. * then call the transmit interface for transmission again */
  1792. rt_atomic_flag_test_and_set(&tx_fifo->activated);
  1793. rt_uint8_t *put_ptr = RT_NULL;
  1794. /* Get the linear length buffer from ringbuffer */
  1795. tx_fifo->put_size = rt_serial_get_linear_buffer(&tx_fifo->rb, &put_ptr);
  1796. /* Call the transmit interface for transmission again */
  1797. serial->ops->transmit(serial,
  1798. put_ptr,
  1799. tx_fifo->put_size,
  1800. RT_SERIAL_TX_NON_BLOCKING);
  1801. break;
  1802. }
  1803. }
  1804. rt_completion_done(&tx_fifo->tx_cpt);
  1805. /* Trigger the transmit completion callback */
  1806. if (serial->parent.tx_complete != RT_NULL)
  1807. {
  1808. serial->parent.tx_complete(&serial->parent, RT_NULL);
  1809. }
  1810. rt_atomic_flag_clear(&tx_fifo->activated);
  1811. break;
  1812. }
  1813. #endif /* RT_SERIAL_USING_DMA */
  1814. default:
  1815. break;
  1816. }
  1817. }