fm3_uart.c 22 KB

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