fm3_uart.c 23 KB

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