fm3_uart.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2012-05-15 lgnq first version.
  9. * 2012-05-28 heyuanjie87 change interfaces
  10. */
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include "fm3_uart.h"
  14. #if (defined(RT_USING_UART0_0) || defined(RT_USING_UART0_1))
  15. /* UART0 device driver structure */
  16. struct uart03_device uart0 =
  17. {
  18. FM3_MFS0_UART,
  19. MFS0RX_IRQn,
  20. MFS0TX_IRQn,
  21. };
  22. struct rt_serial_device serial0;
  23. void MFS0RX_IRQHandler(void)
  24. {
  25. /* enter interrupt */
  26. rt_interrupt_enter();
  27. rt_hw_serial_isr(&serial0, RT_SERIAL_EVENT_RX_IND);
  28. /* leave interrupt */
  29. rt_interrupt_leave();
  30. }
  31. #endif
  32. #if (defined(RT_USING_UART1_0) || defined(RT_USING_UART1_1))
  33. /* UART1 device driver structure */
  34. struct uart03_device uart1 =
  35. {
  36. FM3_MFS1_UART,
  37. MFS1RX_IRQn,
  38. MFS1TX_IRQn,
  39. };
  40. struct rt_serial_device serial1;
  41. void MFS1RX_IRQHandler(void)
  42. {
  43. /* enter interrupt */
  44. rt_interrupt_enter();
  45. rt_hw_serial_isr(&serial1, RT_SERIAL_EVENT_RX_IND);
  46. /* leave interrupt */
  47. rt_interrupt_leave();
  48. }
  49. #endif
  50. #if (defined(RT_USING_UART2_0) || defined(RT_USING_UART2_1) || defined(RT_USING_UART2_2))
  51. /* UART2 device driver structure */
  52. struct uart03_device uart2 =
  53. {
  54. FM3_MFS2_UART,
  55. MFS2RX_IRQn,
  56. MFS2TX_IRQn,
  57. };
  58. struct rt_serial_device serial2;
  59. void MFS2RX_IRQHandler(void)
  60. {
  61. /* enter interrupt */
  62. rt_interrupt_enter();
  63. rt_hw_serial_isr(&serial2, RT_SERIAL_EVENT_RX_IND);
  64. /* leave interrupt */
  65. rt_interrupt_leave();
  66. }
  67. #endif
  68. #if (defined(RT_USING_UART3_0) || defined(RT_USING_UART3_1) || defined(RT_USING_UART3_2))
  69. /* UART3 device driver structure */
  70. struct uart03_device uart3 =
  71. {
  72. FM3_MFS3_UART,
  73. MFS3RX_IRQn,
  74. MFS3TX_IRQn,
  75. };
  76. struct rt_serial_device serial3;
  77. void MFS3RX_IRQHandler(void)
  78. {
  79. /* enter interrupt */
  80. rt_interrupt_enter();
  81. rt_hw_serial_isr(&serial3, RT_SERIAL_EVENT_RX_IND);
  82. /* leave interrupt */
  83. rt_interrupt_leave();
  84. }
  85. #endif
  86. #if (defined(RT_USING_UART4_0) || defined(RT_USING_UART4_1) || defined(RT_USING_UART4_2))
  87. /* UART4 device driver structure */
  88. struct uart47_device uart4 =
  89. {
  90. FM3_MFS4_UART,
  91. MFS4RX_IRQn,
  92. MFS4TX_IRQn,
  93. FIFO_SIZE,
  94. };
  95. struct rt_serial_device serial4;
  96. void MFS4RX_IRQHandler(void)
  97. {
  98. /* enter interrupt */
  99. rt_interrupt_enter();
  100. rt_hw_serial_isr(&serial4, RT_SERIAL_EVENT_RX_IND);
  101. /* leave interrupt */
  102. rt_interrupt_leave();
  103. }
  104. #endif
  105. #if (defined(RT_USING_UART5_0) || defined(RT_USING_UART5_1) || defined(RT_USING_UART5_2))
  106. /* UART5 device driver structure */
  107. struct uart47_device uart5 =
  108. {
  109. FM3_MFS5_UART,
  110. MFS5RX_IRQn,
  111. MFS5TX_IRQn,
  112. FIFO_SIZE,
  113. };
  114. struct rt_serial_device serial5;
  115. void MFS5RX_IRQHandler(void)
  116. {
  117. /* enter interrupt */
  118. rt_interrupt_enter();
  119. rt_hw_serial_isr(&serial5, RT_SERIAL_EVENT_RX_IND);
  120. /* leave interrupt */
  121. rt_interrupt_leave();
  122. }
  123. #endif
  124. #if (defined(RT_USING_UART6_0) || defined(RT_USING_UART6_1))
  125. /* UART6 device driver structure */
  126. struct uart47_device uart6 =
  127. {
  128. FM3_MFS6_UART,
  129. MFS6RX_IRQn,
  130. MFS6TX_IRQn,
  131. FIFO_SIZE,
  132. };
  133. struct rt_serial_device serial6;
  134. void MFS6RX_IRQHandler(void)
  135. {
  136. /* enter interrupt */
  137. rt_interrupt_enter();
  138. rt_hw_serial_isr(&serial6, RT_SERIAL_EVENT_RX_IND);
  139. /* leave interrupt */
  140. rt_interrupt_leave();
  141. }
  142. #endif
  143. #if (defined(RT_USING_UART7_0) || defined(RT_USING_UART7_1))
  144. /* UART7 device driver structure */
  145. struct uart47_device uart7 =
  146. {
  147. FM3_MFS7_UART,
  148. MFS7RX_IRQn,
  149. MFS7TX_IRQn,
  150. FIFO_SIZE,
  151. };
  152. struct rt_serial_device serial7;
  153. void MFS7RX_IRQHandler(void)
  154. {
  155. /* enter interrupt */
  156. rt_interrupt_enter();
  157. rt_hw_serial_isr(&serial7, RT_SERIAL_EVENT_RX_IND);
  158. /* leave interrupt */
  159. rt_interrupt_leave();
  160. }
  161. #endif
  162. void uart_pin_setup(void)
  163. {
  164. #if defined(RT_USING_UART0_0)
  165. /* Set UART Ch0 Port, SIN0_0(P21), SOT0_0(P22) */
  166. FM3_GPIO->PFR2_f.P1 = 1;
  167. FM3_GPIO->PFR2_f.P2 = 1;
  168. FM3_GPIO->EPFR07_f.SIN0S0 = 1;
  169. FM3_GPIO->EPFR07_f.SIN0S1 = 0;
  170. FM3_GPIO->EPFR07_f.SOT0B0 = 1;
  171. FM3_GPIO->EPFR07_f.SOT0B1 = 0;
  172. #elif defined(RT_USING_UART0_1)
  173. /* Set UART Ch0 Port, SIN0_1(P14), SOT0_1(P15) */
  174. FM3_GPIO->PFR1_f.P4 = 1;
  175. FM3_GPIO->PFR1_f.P5 = 1;
  176. FM3_GPIO->EPFR07_f.SIN0S0 = 0;
  177. FM3_GPIO->EPFR07_f.SIN0S1 = 1;
  178. FM3_GPIO->EPFR07_f.SOT0B0 = 0;
  179. FM3_GPIO->EPFR07_f.SOT0B1 = 1;
  180. #endif
  181. #if defined(RT_USING_UART1_0)
  182. /* Set UART Ch1 Port, SIN1_0(P56), SOT1_0(P57) */
  183. FM3_GPIO->PFR5_f.P6 = 1;
  184. FM3_GPIO->PFR5_f.P7 = 1;
  185. FM3_GPIO->EPFR07_f.SIN1S0 = 1;
  186. FM3_GPIO->EPFR07_f.SIN1S1 = 0;
  187. FM3_GPIO->EPFR07_f.SOT1B0 = 1;
  188. FM3_GPIO->EPFR07_f.SOT1B1 = 0;
  189. #elif defined(RT_USING_UART1_1)
  190. /* Set UART Ch1 Port, SIN1_1(P11), SOT1_1(P12) */
  191. FM3_GPIO->PFR1_f.P1 = 1;
  192. FM3_GPIO->PFR1_f.P2 = 1;
  193. FM3_GPIO->EPFR07_f.SIN1S0 = 0;
  194. FM3_GPIO->EPFR07_f.SIN1S1 = 1;
  195. FM3_GPIO->EPFR07_f.SOT1B0 = 0;
  196. FM3_GPIO->EPFR07_f.SOT1B1 = 1;
  197. #endif
  198. #if defined(RT_USING_UART2_0)
  199. /* Set UART Ch2 Port, SIN2_0(P72), SOT2_0(P73) */
  200. FM3_GPIO->PFR7_f.P2 = 1;
  201. FM3_GPIO->PFR7_f.P3 = 1;
  202. FM3_GPIO->EPFR07_f.SIN2S0 = 1;
  203. FM3_GPIO->EPFR07_f.SIN2S1 = 0;
  204. FM3_GPIO->EPFR07_f.SOT2B0 = 1;
  205. FM3_GPIO->EPFR07_f.SOT2B1 = 0;
  206. #elif defined(RT_USING_UART2_1)
  207. /* Set UART Ch2 Port, SIN2_1(P24), SOT2_1(P25) */
  208. FM3_GPIO->PFR2_f.P4 = 1;
  209. FM3_GPIO->PFR2_f.P5 = 1;
  210. FM3_GPIO->EPFR07_f.SIN2S0 = 0;
  211. FM3_GPIO->EPFR07_f.SIN2S1 = 1;
  212. FM3_GPIO->EPFR07_f.SOT2B0 = 0;
  213. FM3_GPIO->EPFR07_f.SOT2B1 = 1;
  214. #elif defined(RT_USING_UART2_2)
  215. /* Set UART Ch2 Port, SIN2_2(P17), SOT2_2(P18) */
  216. FM3_GPIO->PFR1_f.P7 = 1;
  217. FM3_GPIO->PFR1_f.P8 = 1;
  218. FM3_GPIO->EPFR07_f.SIN2S0 = 1;
  219. FM3_GPIO->EPFR07_f.SIN2S1 = 1;
  220. FM3_GPIO->EPFR07_f.SOT2B0 = 1;
  221. FM3_GPIO->EPFR07_f.SOT2B1 = 1;
  222. #endif
  223. #if defined(RT_USING_UART3_0)
  224. /* Set UART Ch3 Port, SIN3_0(P66), SOT3_0(P67) */
  225. FM3_GPIO->PFR6_f.P6 = 1;
  226. FM3_GPIO->PFR6_f.P7 = 1;
  227. FM3_GPIO->EPFR07_f.SIN3S0 = 1;
  228. FM3_GPIO->EPFR07_f.SIN3S1 = 0;
  229. FM3_GPIO->EPFR07_f.SOT3B0 = 1;
  230. FM3_GPIO->EPFR07_f.SOT3B1 = 0;
  231. #elif defined(RT_USING_UART3_1)
  232. /* Set UART Ch3 Port, SIN3_1(P50), SOT3_1(P51) */
  233. FM3_GPIO->PFR5_f.P0 = 1;
  234. FM3_GPIO->PFR5_f.P1 = 1;
  235. FM3_GPIO->EPFR07_f.SIN3S0 = 0;
  236. FM3_GPIO->EPFR07_f.SIN3S1 = 1;
  237. FM3_GPIO->EPFR07_f.SOT3B0 = 0;
  238. FM3_GPIO->EPFR07_f.SOT3B1 = 1;
  239. #elif defined(RT_USING_UART3_2)
  240. /* Set UART Ch3 Port, SIN3_2(P48), SOT3_2(P49) */
  241. FM3_GPIO->PFR4_f.P8 = 1;
  242. FM3_GPIO->PFR4_f.P9 = 1;
  243. FM3_GPIO->EPFR07_f.SIN3S0 = 1;
  244. FM3_GPIO->EPFR07_f.SIN3S1 = 1;
  245. FM3_GPIO->EPFR07_f.SOT3B0 = 1;
  246. FM3_GPIO->EPFR07_f.SOT3B1 = 1;
  247. #endif
  248. #if defined(RT_USING_UART4_0)
  249. /* Set UART Ch4 Port, SIN4_0(P0A), SOT4_0(P0B), CTS4_0(P0E), RTS4_0(P0D) */
  250. FM3_GPIO->PFR0_f.PA = 1;
  251. FM3_GPIO->PFR0_f.PB = 1;
  252. FM3_GPIO->PFR0_f.PD = 1;
  253. FM3_GPIO->PFR0_f.PE = 1;
  254. FM3_GPIO->EPFR08_f.SIN4S0 = 1;
  255. FM3_GPIO->EPFR08_f.SIN4S1 = 0;
  256. FM3_GPIO->EPFR08_f.SOT4B0 = 1;
  257. FM3_GPIO->EPFR08_f.SOT4B1 = 0;
  258. FM3_GPIO->EPFR08_f.CTS4S0 = 1;
  259. FM3_GPIO->EPFR08_f.CTS4S1 = 0;
  260. FM3_GPIO->EPFR08_f.RTS4E0 = 1;
  261. FM3_GPIO->EPFR08_f.RTS4E1 = 0;
  262. #elif defined(RT_USING_UART4_1)
  263. /* Set UART Ch4 Port, SIN4_1(P1A), SOT4_1(P1B), CTS4_1(P1D), RTS4_1(P1E) */
  264. FM3_GPIO->PFR1_f.PA = 1;
  265. FM3_GPIO->PFR1_f.PB = 1;
  266. FM3_GPIO->PFR1_f.PD = 1;
  267. FM3_GPIO->PFR1_f.PE = 1;
  268. FM3_GPIO->EPFR08_f.SIN4S0 = 0;
  269. FM3_GPIO->EPFR08_f.SIN4S1 = 1;
  270. FM3_GPIO->EPFR08_f.SOT4B0 = 0;
  271. FM3_GPIO->EPFR08_f.SOT4B1 = 1;
  272. FM3_GPIO->EPFR08_f.CTS4S0 = 0;
  273. FM3_GPIO->EPFR08_f.CTS4S1 = 1;
  274. FM3_GPIO->EPFR08_f.RTS4E0 = 0;
  275. FM3_GPIO->EPFR08_f.RTS4E1 = 1;
  276. #elif defined(RT_USING_UART4_2)
  277. /* Set UART Ch4 Port, SIN4_2(P05), SOT4_2(P06), CTS4_2(P08), RTS4_2(P09)*/
  278. FM3_GPIO->PFR0_f.P5 = 1;
  279. FM3_GPIO->PFR0_f.P6 = 1;
  280. FM3_GPIO->PFR0_f.P8 = 1;
  281. FM3_GPIO->PFR0_f.P9 = 1;
  282. FM3_GPIO->EPFR08_f.SIN4S0 = 1;
  283. FM3_GPIO->EPFR08_f.SIN4S1 = 1;
  284. FM3_GPIO->EPFR08_f.SOT4B0 = 1;
  285. FM3_GPIO->EPFR08_f.SOT4B1 = 1;
  286. FM3_GPIO->EPFR08_f.CTS4S0 = 1;
  287. FM3_GPIO->EPFR08_f.CTS4S1 = 1;
  288. FM3_GPIO->EPFR08_f.RTS4E0 = 1;
  289. FM3_GPIO->EPFR08_f.RTS4E1 = 1;
  290. #endif
  291. #if defined(RT_USING_UART5_0)
  292. /* Set UART Ch5 Port, SIN5_0(P60), SOT5_0(P61) */
  293. FM3_GPIO->PFR6_f.P0 = 1;
  294. FM3_GPIO->PFR6_f.P1 = 1;
  295. FM3_GPIO->EPFR08_f.SIN5S0 = 1;
  296. FM3_GPIO->EPFR08_f.SIN5S1 = 0;
  297. FM3_GPIO->EPFR08_f.SOT5B0 = 1;
  298. FM3_GPIO->EPFR08_f.SOT5B1 = 0;
  299. #elif defined(RT_USING_UART5_1)
  300. /* Set UART Ch5 Port, SIN5_1(P63), SOT5_1(P64) */
  301. FM3_GPIO->PFR6_f.P3 = 1;
  302. FM3_GPIO->PFR6_f.P4 = 1;
  303. FM3_GPIO->EPFR08_f.SIN5S0 = 0;
  304. FM3_GPIO->EPFR08_f.SIN5S1 = 1;
  305. FM3_GPIO->EPFR08_f.SOT5B0 = 0;
  306. FM3_GPIO->EPFR08_f.SOT5B1 = 1;
  307. #elif defined(RT_USING_UART5_2)
  308. /* Set UART Ch5 Port, SIN5_2(P36), SOT5_2(P37) */
  309. FM3_GPIO->PFR3_f.P6 = 1;
  310. FM3_GPIO->PFR3_f.P7 = 1;
  311. FM3_GPIO->EPFR08_f.SIN5S0 = 1;
  312. FM3_GPIO->EPFR08_f.SIN5S1 = 1;
  313. FM3_GPIO->EPFR08_f.SOT5B0 = 1;
  314. FM3_GPIO->EPFR08_f.SOT5B1 = 1;
  315. #endif
  316. #if defined(RT_USING_UART6_0)
  317. /* Set UART Ch6 Port, SIN6_0(P53), SOT6_0(P54) */
  318. FM3_GPIO->PFR5_f.P3 = 1;
  319. FM3_GPIO->PFR5_f.P4 = 1;
  320. FM3_GPIO->EPFR08_f.SIN6S0 = 1;
  321. FM3_GPIO->EPFR08_f.SIN6S1 = 0;
  322. FM3_GPIO->EPFR08_f.SOT6B0 = 1;
  323. FM3_GPIO->EPFR08_f.SOT6B1 = 0;
  324. #elif defined(RT_USING_UART6_1)
  325. /* Set UART Ch6 Port, SIN6_1(P33), SOT6_1(P32) */
  326. FM3_GPIO->PFR3_f.P2 = 1;
  327. FM3_GPIO->PFR3_f.P3 = 1;
  328. FM3_GPIO->EPFR08_f.SIN6S0 = 0;
  329. FM3_GPIO->EPFR08_f.SIN6S1 = 1;
  330. FM3_GPIO->EPFR08_f.SOT6B0 = 0;
  331. FM3_GPIO->EPFR08_f.SOT6B1 = 1;
  332. #endif
  333. #if defined(RT_USING_UART7_0)
  334. /* Set UART Ch7 Port, SIN7_0(P59), SOT7_0(P5A) */
  335. FM3_GPIO->PFR5_f.P9 = 1;
  336. FM3_GPIO->PFR5_f.PA = 1;
  337. FM3_GPIO->EPFR08_f.SIN7S0 = 1;
  338. FM3_GPIO->EPFR08_f.SIN7S1 = 0;
  339. FM3_GPIO->EPFR08_f.SOT7B0 = 1;
  340. FM3_GPIO->EPFR08_f.SOT7B1 = 0;
  341. #elif defined(RT_USING_UART7_1)
  342. /* Set UART Ch7 Port, SIN7_1(P4E), SOT7_1(P4D) */
  343. FM3_GPIO->PFR4_f.PD = 1;
  344. FM3_GPIO->PFR4_f.PE = 1;
  345. FM3_GPIO->EPFR08_f.SIN7S0 = 0;
  346. FM3_GPIO->EPFR08_f.SIN7S1 = 1;
  347. FM3_GPIO->EPFR08_f.SOT7B0 = 0;
  348. FM3_GPIO->EPFR08_f.SOT7B1 = 1;
  349. #endif
  350. }
  351. static rt_err_t uart03_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  352. {
  353. struct uart03_device *uart;
  354. RT_ASSERT(serial != RT_NULL);
  355. uart = (struct uart03_device *)serial->parent.user_data;
  356. uart->uart_regs->SMR = SMR_MD_UART | SMR_SOE;
  357. /* set baudreate */
  358. uart->uart_regs->BGR = (40000000UL + (cfg->baud_rate/2))/cfg->baud_rate - 1;
  359. /* set stop bits */
  360. switch (cfg->stop_bits)
  361. {
  362. case STOP_BITS_1:
  363. uart->uart_regs->SMR_f.SBL = 0;
  364. uart->uart_regs->ESCR_f.ESBL = 0;
  365. break;
  366. case STOP_BITS_2:
  367. uart->uart_regs->SMR_f.SBL = 1;
  368. uart->uart_regs->ESCR_f.ESBL = 0;
  369. break;
  370. case STOP_BITS_3:
  371. uart->uart_regs->SMR_f.SBL = 0;
  372. uart->uart_regs->ESCR_f.ESBL = 1;
  373. break;
  374. case STOP_BITS_4:
  375. uart->uart_regs->SMR_f.SBL = 1;
  376. uart->uart_regs->ESCR_f.ESBL = 1;
  377. break;
  378. default:
  379. return RT_ERROR;
  380. }
  381. /* set data bits */
  382. switch (cfg->data_bits)
  383. {
  384. case DATA_BITS_5:
  385. uart->uart_regs->ESCR_f.L0 = 1;
  386. uart->uart_regs->ESCR_f.L1 = 0;
  387. uart->uart_regs->ESCR_f.L2 = 0;
  388. break;
  389. case DATA_BITS_6:
  390. uart->uart_regs->ESCR_f.L0 = 0;
  391. uart->uart_regs->ESCR_f.L1 = 1;
  392. uart->uart_regs->ESCR_f.L2 = 0;
  393. break;
  394. case DATA_BITS_7:
  395. uart->uart_regs->ESCR_f.L0 = 1;
  396. uart->uart_regs->ESCR_f.L1 = 1;
  397. uart->uart_regs->ESCR_f.L2 = 0;
  398. break;
  399. case DATA_BITS_8:
  400. uart->uart_regs->ESCR_f.L0 = 0;
  401. uart->uart_regs->ESCR_f.L1 = 0;
  402. uart->uart_regs->ESCR_f.L2 = 0;
  403. break;
  404. case DATA_BITS_9:
  405. uart->uart_regs->ESCR_f.L0 = 0;
  406. uart->uart_regs->ESCR_f.L1 = 0;
  407. uart->uart_regs->ESCR_f.L2 = 1;
  408. break;
  409. default:
  410. return RT_ERROR;
  411. }
  412. /* set parity */
  413. switch (cfg->parity)
  414. {
  415. case PARITY_NONE:
  416. uart->uart_regs->ESCR_f.PEN = 0;
  417. break;
  418. case PARITY_EVEN:
  419. uart->uart_regs->ESCR_f.PEN = 1;
  420. uart->uart_regs->ESCR_f.P = 0;
  421. break;
  422. case PARITY_ODD:
  423. uart->uart_regs->ESCR_f.PEN = 1;
  424. uart->uart_regs->ESCR_f.P = 1;
  425. break;
  426. default:
  427. return RT_ERROR;
  428. }
  429. /* set bit order */
  430. switch (cfg->bit_order)
  431. {
  432. case BIT_ORDER_LSB:
  433. uart->uart_regs->SMR_f.BDS = 0;
  434. break;
  435. case BIT_ORDER_MSB:
  436. uart->uart_regs->SMR_f.BDS = 1;
  437. break;
  438. default:
  439. return RT_ERROR;
  440. }
  441. /* set NRZ mode */
  442. switch (cfg->invert)
  443. {
  444. case NRZ_NORMAL:
  445. uart->uart_regs->ESCR_f.INV = 0;
  446. break;
  447. case NRZ_INVERTED:
  448. uart->uart_regs->ESCR_f.INV = 1;
  449. break;
  450. default:
  451. return RT_ERROR;
  452. }
  453. uart->uart_regs->SCR = SCR_RXE | SCR_TXE | SCR_RIE;
  454. return RT_EOK;
  455. }
  456. static rt_err_t uart03_control(struct rt_serial_device *serial, int cmd, void *arg)
  457. {
  458. struct uart03_device *uart;
  459. RT_ASSERT(serial != RT_NULL);
  460. uart = (struct uart03_device *)serial->parent.user_data;
  461. switch (cmd)
  462. {
  463. case RT_DEVICE_CTRL_CLR_INT:
  464. /* disable rx irq */
  465. UART_DISABLE_IRQ(uart->rx_irq);
  466. break;
  467. case RT_DEVICE_CTRL_SET_INT:
  468. /* enable rx irq */
  469. UART_ENABLE_IRQ(uart->rx_irq);
  470. break;
  471. }
  472. return (RT_EOK);
  473. }
  474. static int uart03_putc(struct rt_serial_device *serial, char c)
  475. {
  476. struct uart03_device *uart;
  477. RT_ASSERT(serial != RT_NULL);
  478. uart = (struct uart03_device *)serial->parent.user_data;
  479. /* while send buffer is empty */
  480. while (!(uart->uart_regs->SSR & SSR_TDRE));
  481. /* write to send buffer */
  482. uart->uart_regs->TDR = c;
  483. return (1);
  484. }
  485. static int uart03_getc(struct rt_serial_device *serial)
  486. {
  487. struct uart03_device *uart;
  488. int ch;
  489. RT_ASSERT(serial != RT_NULL);
  490. uart = (struct uart03_device *)serial->parent.user_data;
  491. /* receive buffer is full */
  492. if (uart->uart_regs->SSR & SSR_RDRF)
  493. {
  494. ch = uart->uart_regs->RDR & 0xff;
  495. return (ch);
  496. }
  497. else
  498. return (-1);
  499. }
  500. static struct rt_uart_ops uart03_ops =
  501. {
  502. uart03_configure,
  503. uart03_control,
  504. uart03_putc,
  505. uart03_getc,
  506. };
  507. static rt_err_t uart47_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  508. {
  509. struct uart47_device *uart;
  510. RT_ASSERT(serial != RT_NULL);
  511. uart = (struct uart47_device *)serial->parent.user_data;
  512. uart->uart_regs->SMR = SMR_MD_UART | SMR_SOE;
  513. /* set baudreate */
  514. uart->uart_regs->BGR = (40000000UL + (cfg->baud_rate/2))/cfg->baud_rate - 1;
  515. /* set stop bits */
  516. switch (cfg->stop_bits)
  517. {
  518. case STOP_BITS_1:
  519. uart->uart_regs->SMR_f.SBL = 0;
  520. uart->uart_regs->ESCR_f.ESBL = 0;
  521. break;
  522. case STOP_BITS_2:
  523. uart->uart_regs->SMR_f.SBL = 1;
  524. uart->uart_regs->ESCR_f.ESBL = 0;
  525. break;
  526. case STOP_BITS_3:
  527. uart->uart_regs->SMR_f.SBL = 0;
  528. uart->uart_regs->ESCR_f.ESBL = 1;
  529. break;
  530. case STOP_BITS_4:
  531. uart->uart_regs->SMR_f.SBL = 1;
  532. uart->uart_regs->ESCR_f.ESBL = 1;
  533. break;
  534. default:
  535. return RT_ERROR;
  536. }
  537. /* set data bits */
  538. switch (cfg->data_bits)
  539. {
  540. case DATA_BITS_5:
  541. uart->uart_regs->ESCR_f.L0 = 1;
  542. uart->uart_regs->ESCR_f.L1 = 0;
  543. uart->uart_regs->ESCR_f.L2 = 0;
  544. break;
  545. case DATA_BITS_6:
  546. uart->uart_regs->ESCR_f.L0 = 0;
  547. uart->uart_regs->ESCR_f.L1 = 1;
  548. uart->uart_regs->ESCR_f.L2 = 0;
  549. break;
  550. case DATA_BITS_7:
  551. uart->uart_regs->ESCR_f.L0 = 1;
  552. uart->uart_regs->ESCR_f.L1 = 1;
  553. uart->uart_regs->ESCR_f.L2 = 0;
  554. break;
  555. case DATA_BITS_8:
  556. uart->uart_regs->ESCR_f.L0 = 0;
  557. uart->uart_regs->ESCR_f.L1 = 0;
  558. uart->uart_regs->ESCR_f.L2 = 0;
  559. break;
  560. case DATA_BITS_9:
  561. uart->uart_regs->ESCR_f.L0 = 0;
  562. uart->uart_regs->ESCR_f.L1 = 0;
  563. uart->uart_regs->ESCR_f.L2 = 1;
  564. break;
  565. default:
  566. return RT_ERROR;
  567. }
  568. /* set parity */
  569. switch (cfg->parity)
  570. {
  571. case PARITY_NONE:
  572. uart->uart_regs->ESCR_f.PEN = 0;
  573. break;
  574. case PARITY_EVEN:
  575. uart->uart_regs->ESCR_f.PEN = 1;
  576. uart->uart_regs->ESCR_f.P = 0;
  577. break;
  578. case PARITY_ODD:
  579. uart->uart_regs->ESCR_f.PEN = 1;
  580. uart->uart_regs->ESCR_f.P = 1;
  581. break;
  582. default:
  583. return RT_ERROR;
  584. }
  585. /* set bit order */
  586. switch (cfg->bit_order)
  587. {
  588. case BIT_ORDER_LSB:
  589. uart->uart_regs->SMR_f.BDS = 0;
  590. break;
  591. case BIT_ORDER_MSB:
  592. uart->uart_regs->SMR_f.BDS = 1;
  593. break;
  594. default:
  595. return RT_ERROR;
  596. }
  597. /* set NRZ mode */
  598. switch (cfg->invert)
  599. {
  600. case NRZ_NORMAL:
  601. uart->uart_regs->ESCR_f.INV = 0;
  602. break;
  603. case NRZ_INVERTED:
  604. uart->uart_regs->ESCR_f.INV = 1;
  605. break;
  606. default:
  607. return RT_ERROR;
  608. }
  609. /* configure fifo */
  610. /* Disable the Data Lost detection */
  611. uart->uart_regs->FCR1_f.FLSTE = 0;
  612. /* Enable the received FIFO idle detection */
  613. uart->uart_regs->FCR1_f.FRIE = 1;
  614. /* Requests for the transmit FIFO data */
  615. uart->uart_regs->FCR1_f.FDRQ = 1;
  616. /* Disable the transmit FIFO interrupt */
  617. uart->uart_regs->FCR1_f.FTIE = 0;
  618. /* Transmit FIFO:FIFO1; Received FIFO:FIFO2 */
  619. uart->uart_regs->FCR1_f.FSEL = 0;
  620. /* Transfer data count */
  621. uart->uart_regs->FBYTE1 = 0;
  622. /* Set the data count to generate a received interrupt */
  623. uart->uart_regs->FBYTE2 = uart->fifo_size;
  624. /* FIFO pointer Not reloaded */
  625. uart->uart_regs->FCR0_f.FLD = 0;
  626. /* FIFO pointer Not saved */
  627. uart->uart_regs->FCR0_f.FSET = 0;
  628. /* FIFO2 is reset */
  629. uart->uart_regs->FCR0_f.FCL2 = 1;
  630. /* FIFO1 is reset */
  631. uart->uart_regs->FCR0_f.FCL1 = 1;
  632. /* Enables the FIFO2 operation */
  633. uart->uart_regs->FCR0_f.FE2 = 1;
  634. /* Enables the FIFO1 operation */
  635. uart->uart_regs->FCR0_f.FE1 = 1;
  636. /* enable receive and send */
  637. uart->uart_regs->SCR = SCR_RXE | SCR_TXE | SCR_RIE;
  638. return RT_EOK;
  639. }
  640. static rt_err_t uart47_control(struct rt_serial_device *serial, int cmd, void *arg)
  641. {
  642. struct uart47_device *uart;
  643. RT_ASSERT(serial != RT_NULL);
  644. uart = (struct uart47_device *)serial->parent.user_data;
  645. switch (cmd)
  646. {
  647. case RT_DEVICE_CTRL_CLR_INT:
  648. /* disable rx irq */
  649. UART_DISABLE_IRQ(uart->rx_irq);
  650. break;
  651. case RT_DEVICE_CTRL_SET_INT:
  652. /* enable rx irq */
  653. UART_ENABLE_IRQ(uart->rx_irq);
  654. break;
  655. }
  656. return (RT_EOK);
  657. }
  658. static int uart47_putc(struct rt_serial_device *serial, char c)
  659. {
  660. struct uart47_device *uart;
  661. RT_ASSERT(serial != RT_NULL);
  662. uart = (struct uart47_device *)serial->parent.user_data;
  663. /* while send fifo is empty */
  664. while (!(uart->uart_regs->SSR & SSR_TDRE));
  665. /* write to fifo */
  666. uart->uart_regs->TDR = c;
  667. return (1);
  668. }
  669. static int uart47_getc(struct rt_serial_device *serial)
  670. {
  671. int ch;
  672. struct uart47_device *uart;
  673. RT_ASSERT(serial != RT_NULL);
  674. uart = (struct uart47_device *)serial->parent.user_data;
  675. /* receive is disabled */
  676. if (!(uart->uart_regs->SCR & SCR_RXE))
  677. return (-1);
  678. /* receive fifo is not full */
  679. if ((uart->uart_regs->SSR & SSR_RDRF) == 0)
  680. return (-1);
  681. /* read char */
  682. ch = uart->uart_regs->RDR & 0xff;
  683. return (ch);
  684. }
  685. static struct rt_uart_ops uart47_ops =
  686. {
  687. uart47_configure,
  688. uart47_control,
  689. uart47_putc,
  690. uart47_getc,
  691. };
  692. void rt_hw_serial_init(void)
  693. {
  694. struct serial_configure config;
  695. uart_pin_setup();
  696. #if (defined(RT_USING_UART0_0) || defined(RT_USING_UART0_1))
  697. config.baud_rate = BAUD_RATE_115200;
  698. config.bit_order = BIT_ORDER_LSB;
  699. config.data_bits = DATA_BITS_8;
  700. config.parity = PARITY_NONE;
  701. config.stop_bits = STOP_BITS_1;
  702. config.invert = NRZ_NORMAL;
  703. config.bufsz = RT_SERIAL_RB_BUFSZ;
  704. serial0.ops = &uart03_ops;
  705. serial0.config = config;
  706. /* register UART0 device */
  707. rt_hw_serial_register(&serial0, "uart0",
  708. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  709. &uart0);
  710. #endif
  711. #if (defined(RT_USING_UART1_0) || defined(RT_USING_UART1_1))
  712. config.baud_rate = BAUD_RATE_115200;
  713. config.bit_order = BIT_ORDER_LSB;
  714. config.data_bits = DATA_BITS_8;
  715. config.parity = PARITY_NONE;
  716. config.stop_bits = STOP_BITS_1;
  717. config.invert = NRZ_NORMAL;
  718. config.bufsz = RT_SERIAL_RB_BUFSZ;
  719. serial1.ops = &uart03_ops;
  720. serial1.config = config;
  721. /* register UART1 device */
  722. rt_hw_serial_register(&serial1,
  723. "uart1",
  724. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  725. &uart1);
  726. #endif
  727. #if (defined(RT_USING_UART2_0) || defined(RT_USING_UART2_1) || defined(RT_USING_UART2_2))
  728. config.baud_rate = BAUD_RATE_115200;
  729. config.bit_order = BIT_ORDER_LSB;
  730. config.data_bits = DATA_BITS_8;
  731. config.parity = PARITY_NONE;
  732. config.stop_bits = STOP_BITS_1;
  733. config.invert = NRZ_NORMAL;
  734. config.bufsz = RT_SERIAL_RB_BUFSZ;
  735. serial2.ops = &uart03_ops;
  736. serial2.config = config;
  737. /* register UART2 device */
  738. rt_hw_serial_register(&serial2,
  739. "uart2",
  740. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  741. &uart2);
  742. #endif
  743. #if (defined(RT_USING_UART3_0) || defined(RT_USING_UART3_1) || defined(RT_USING_UART3_2))
  744. config.baud_rate = BAUD_RATE_115200;
  745. config.bit_order = BIT_ORDER_LSB;
  746. config.data_bits = DATA_BITS_8;
  747. config.parity = PARITY_NONE;
  748. config.stop_bits = STOP_BITS_1;
  749. config.invert = NRZ_NORMAL;
  750. config.bufsz = RT_SERIAL_RB_BUFSZ;
  751. serial3.ops = &uart03_ops;
  752. serial3.config = config;
  753. /* register UART3 device */
  754. rt_hw_serial_register(&serial3,
  755. "uart3",
  756. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  757. &uart3);
  758. #endif
  759. #if (defined(RT_USING_UART4_0) || defined(RT_USING_UART4_1) || defined(RT_USING_UART4_2))
  760. config.baud_rate = BAUD_RATE_115200;
  761. config.bit_order = BIT_ORDER_LSB;
  762. config.data_bits = DATA_BITS_8;
  763. config.parity = PARITY_NONE;
  764. config.stop_bits = STOP_BITS_1;
  765. config.invert = NRZ_NORMAL;
  766. config.bufsz = RT_SERIAL_RB_BUFSZ;
  767. serial4.ops = &uart47_ops;
  768. serial4.config = config;
  769. /* register UART4 device */
  770. rt_hw_serial_register(&serial4,
  771. "uart4",
  772. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  773. &uart4);
  774. #endif
  775. #if (defined(RT_USING_UART5_0) || defined(RT_USING_UART5_1) || defined(RT_USING_UART5_2))
  776. config.baud_rate = BAUD_RATE_115200;
  777. config.bit_order = BIT_ORDER_LSB;
  778. config.data_bits = DATA_BITS_8;
  779. config.parity = PARITY_NONE;
  780. config.stop_bits = STOP_BITS_1;
  781. config.invert = NRZ_NORMAL;
  782. config.bufsz = RT_SERIAL_RB_BUFSZ;
  783. serial5.ops = &uart47_ops;
  784. serial5.config = config;
  785. /* register UART5 device */
  786. rt_hw_serial_register(&serial5,
  787. "uart5",
  788. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  789. &uart5);
  790. #endif
  791. #if (defined(RT_USING_UART6_0) || defined(RT_USING_UART6_1))
  792. config.baud_rate = BAUD_RATE_115200;
  793. config.bit_order = BIT_ORDER_LSB;
  794. config.data_bits = DATA_BITS_8;
  795. config.parity = PARITY_NONE;
  796. config.stop_bits = STOP_BITS_1;
  797. config.invert = NRZ_NORMAL;
  798. config.bufsz = RT_SERIAL_RB_BUFSZ;
  799. serial6.ops = &uart47_ops;
  800. serial6.config = config;
  801. /* register UART6 device */
  802. rt_hw_serial_register(&serial6,
  803. "uart6",
  804. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  805. &uart6);
  806. #endif
  807. #if (defined(RT_USING_UART7_0) || defined(RT_USING_UART7_1))
  808. config.baud_rate = BAUD_RATE_115200;
  809. config.bit_order = BIT_ORDER_LSB;
  810. config.data_bits = DATA_BITS_8;
  811. config.parity = PARITY_NONE;
  812. config.stop_bits = STOP_BITS_1;
  813. config.invert = NRZ_NORMAL;
  814. config.bufsz = RT_SERIAL_RB_BUFSZ;
  815. serial7.ops = &uart47_ops;
  816. serial7.config = config;
  817. /* register UART7 device */
  818. rt_hw_serial_register(&serial7,
  819. "uart7",
  820. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  821. &uart7);
  822. #endif
  823. }