uart.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. */
  9. #include <rthw.h>
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "board.h"
  13. #include "uart.h"
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include "r_pdl_sci.h"
  17. /* General RPDL function definitions */
  18. #include "r_pdl_definitions.h"
  19. #include "intrinsics.h"
  20. #include "iorx62n.h"
  21. //#include <string.h>
  22. /* Clock selection control */
  23. #define SCI_CKS_MIN 0
  24. #define SCI_CKS_MAX 3
  25. #define SCI_CKS_STEP 1
  26. #define IPR_ADDRESS(a) ((volatile unsigned char *)&ICU.IPR[IPR_SCI0_ + a])
  27. //#define IER_ADDRESS(a) ((volatile unsigned char *)&(ICU.IER[IER_SCI0_ERI0 + a])/sizeof(unsigned char))
  28. #define ERI_ADDRESS(a) ((volatile unsigned char *)&ICU.IR[IR_SCI0_ERI0] + ((4 * a) / sizeof(unsigned char)) )
  29. #define IER_ADDRESS(a) ((volatile unsigned char *)&ICU.IER[IER_SCI0_ERI0] + ((4 * a) / sizeof(unsigned char)) )
  30. #define RXI_ADDRESS(a) ((volatile unsigned char *)&ICU.IR[IR_SCI0_RXI0] + ((4 * a) / sizeof(unsigned char)) )
  31. #define TXI_ADDRESS(a) ((volatile unsigned char *)&ICU.IR[IR_SCI0_TXI0] + ((4 * a) / sizeof(unsigned char)) )
  32. #define TEI_ADDRESS(a) ((volatile unsigned char *)&ICU.IR[IR_SCI0_TEI0] + ((4 * a) / sizeof(unsigned char)) )
  33. #define RXI_DTCER_ADDRESS(a) (( volatile unsigned char *)&ICU.DTCER[IR_SCI0_RXI0]+ ((4*a)/sizeof(unsigned char)))
  34. #define TXI_DTCER_ADDRESS(a) (( volatile unsigned char *)&ICU.DTCER[IR_SCI0_TXI0]+ ((4*a) / sizeof(unsigned char)))
  35. //#define SCI1_USE_B
  36. //#define SCI2_USE_B
  37. //#define SCI3_USE_B
  38. //#define SCI6_USE_B
  39. #define SourceClk 12000000
  40. #define rpdl_CGC_f_pclk SourceClk * 4
  41. /* Idle output options */
  42. #define SPACE 0
  43. #define MARK 1
  44. typedef int UART_ID_Type;
  45. typedef int IRQn_Type;
  46. #define SCI2_USE_B
  47. struct rx_uart
  48. {
  49. UART_ID_Type UART;
  50. volatile struct st_sci __sfr * sci;
  51. };
  52. static rt_err_t rx_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  53. {
  54. #if 1
  55. struct rx_uart *uart;
  56. unsigned char smr_copy;
  57. unsigned char semr_copy;
  58. unsigned char scr_copy;
  59. unsigned char scmr_copy;
  60. unsigned long brr_divider;
  61. unsigned long bit_interval_counter;
  62. RT_ASSERT(serial != RT_NULL);
  63. uart = (struct rx_uart *)serial->parent.user_data;
  64. if (uart->UART > 6) {
  65. return -RT_ERROR;
  66. }
  67. /* Initialise the working copies */
  68. smr_copy = 0x00u;
  69. scr_copy = 0x00u;
  70. semr_copy = 0x00u;
  71. scmr_copy = 0x72u;
  72. brr_divider = 0;
  73. switch (uart->UART) {
  74. case 0:
  75. SYSTEM.MSTPCRB.BIT.MSTPB31 = 0;
  76. /* Enable the input buffer */
  77. PORT2.ICR.BIT.B2 = 1;
  78. /* Ensure the pin is set to input */
  79. PORT2.DDR.BIT.B2 = 0;
  80. /* Disable the USB0_DRPD output */
  81. IOPORT.PFKUSB.BIT.USBE = 0;
  82. /* Disable the MTIOC3B-A output */
  83. MTU3.TIORH.BIT.IOB = 0;
  84. /* Disable the TMO0 output */
  85. TMR0.TCSR.BIT.OSA = 0;
  86. TMR0.TCSR.BIT.OSB = 0;
  87. /* Disable the MTIOC1A output */
  88. MTU1.TIOR.BIT.IOA = 0;
  89. /* Set the idle state direction */
  90. PORT2.DDR.BIT.B0 = 1;
  91. PORT2.DR.BIT.B0 = 1;
  92. PORT2.ICR.BIT.B0 = 1;
  93. PORT2.ICR.BIT.B1 = 1;
  94. break;
  95. case 1:
  96. SYSTEM.MSTPCRB.BIT.MSTPB30 = 0;
  97. /* Disable the CS6#-C output */
  98. IOPORT.PF0CSE.BIT.CS6E = 0;
  99. /* Disable the MOSIB-A output */
  100. IOPORT.PFHSPI.BIT.MOSIE = 0;
  101. /* Disable the MTIOC2A output */
  102. MTU2.TIOR.BIT.IOA = 0;
  103. /* Disable the TMO1 output */
  104. TMR1.TCSR.BYTE = 0xF0u;
  105. #ifdef SCI1_USE_B
  106. IOPORT.PFFSCI.BIT.SCI1S = 1;
  107. PORTF.DDR.BIT.B2 = 0;
  108. PORTF.ICR.BIT.B2 = 1;
  109. PORTF.DDR.BIT.B0 = 1;
  110. PORTF.DR.BIT.B0 = 1;
  111. #else
  112. IOPORT.PFFSCI.BIT.SCI1S = 0;
  113. /* Set the idle state direction */
  114. PORT2.DDR.BIT.B6 = 1;
  115. PORT2.DR.BIT.B6 = 1;
  116. // PORT2.DR.BIT.B6 = 1;
  117. /* Enable the input buffer */
  118. //PORT3.ICR.BIT.B0 = 1;
  119. /* Ensure the pin is set to input */
  120. PORT3.DDR.BIT.B0 = 0;
  121. #endif
  122. break;
  123. case 2:
  124. SYSTEM.MSTPCRB.BIT.MSTPB29 = 0;
  125. /* Disable the SSLB2-A output */
  126. IOPORT.PFHSPI.BIT.SSL2E = 0;
  127. #ifdef SCI2_USE_B
  128. IOPORT.PFFSCI.BIT.SCI2S = 1;
  129. PORT5.DDR.BIT.B0 = 1;
  130. PORT5.DR.BIT.B0 = 1;
  131. PORT5.DDR.BIT.B2 = 0;
  132. PORT5.ICR.BIT.B2 = 1;
  133. /* Disable the SSLB1-A output */
  134. IOPORT.PFHSPI.BIT.SSL1E = 0;
  135. #else
  136. IOPORT.PFFSCI.BIT.SCI2S = 0;
  137. /* Enable the input buffer */
  138. PORT1.ICR.BIT.B2 = 1;
  139. /* Ensure the pin is set to input */
  140. PORT1.DDR.BIT.B2 = 0;
  141. PORT1.DDR.BIT.B3 = 1;
  142. PORT1.DR.BIT.B3 = 1;
  143. /* Disable the TMO3 output */
  144. TMR3.TCSR.BYTE = 0xF0u;
  145. #endif
  146. break;
  147. case 3:
  148. SYSTEM.MSTPCRB.BIT.MSTPB28 = 0;
  149. #ifdef SCI3_USE_B
  150. IOPORT.PFFSCI.BIT.SCI3S = 1;
  151. PORT2.DDR.BIT.B5 = 0;
  152. PORT2.ICR.BIT.B5 = 1;
  153. PORT2.DDR.BIT.B3 = 1;
  154. PORT2.DR.BIT.B3 = 1;
  155. IOPORT.PF0CSE.BIT.CS4E = 0;
  156. /* Disable the USB0_VBUSEN-A output */
  157. IOPORT.PFKUSB.BIT.USBE = 0;
  158. /* Disable the MTIOC4A-A output */
  159. MTU4.TIORH.BIT.IOA = 0;
  160. /* Disable the USB0_DPUPE-A output */
  161. IOPORT.PFKUSB.BIT.USBE = 0;
  162. /* Disable the EDACK0-B output */
  163. EXDMAC0.EDMOMD.BIT.DACKE = 0;
  164. /* Disable the MTIOC3D-A output */
  165. MTU3.TIORL.BIT.IOD = 0;
  166. #else
  167. IOPORT.PFFSCI.BIT.SCI3S = 0;
  168. /* Disable the MTIOC0B output */
  169. MTU0.TIORH.BIT.IOB = 0;
  170. PORT1.DDR.BIT.B6 = 0;
  171. PORT1.ICR.BIT.B6 = 1;
  172. PORT1.DDR.BIT.B7 = 1;
  173. PORT1.DR.BIT.B7 = 1;
  174. MTU3.TIORH.BIT.IOA = 0;
  175. /* Set the idle state direction */
  176. #endif
  177. break;
  178. //case UartPort4:
  179. // SYSTEM.MSTPCRB.BIT.MSTPB27 = 0;
  180. // break;
  181. case 5:
  182. SYSTEM.MSTPCRB.BIT.MSTPB26 = 0;
  183. /* Enable the input buffer */
  184. PORTC.ICR.BIT.B1 = 1;
  185. /* Ensure the pin is set to input */
  186. PORTC.DDR.BIT.B1 = 0;
  187. PORTC.DDR.BIT.B3 = 1;
  188. PORTC.DR.BIT.B3 = 1;
  189. /* Disable the A17-A output */
  190. IOPORT.PF3BUS.BIT.A17E = 0;
  191. break;
  192. case 6:
  193. SYSTEM.MSTPCRB.BIT.MSTPB25 = 0;
  194. #ifdef SCI6_USE_B
  195. IOPORT.PFFSCI.BIT.SCI6S = 1;
  196. PORT3.DDR.BIT.B3 = 0;
  197. //PORT3.ICR.BIT.B3 = 1;
  198. PORT3.DDR.BIT.B2 = 1;
  199. PORT3.DR.BIT.B2 = 1;
  200. /* Disable the MTIOC0A output */
  201. MTU0.TIORH.BIT.IOA = 0;
  202. /* Disable the CTX0 output */
  203. IOPORT.PFJCAN.BIT.CAN0E = 0;
  204. /* Disable the MTIOC0C output */
  205. MTU0.TIORL.BIT.IOC = 0;
  206. #else
  207. IOPORT.PFFSCI.BIT.SCI6S = 0;
  208. PORT0.DDR.BIT.B0 = 1;
  209. PORT0.DR.BIT.B0 = 1;
  210. PORT0.ICR.BIT.B1 = 1;
  211. PORT0.DDR.BIT.B1 = 0;
  212. #endif
  213. break;
  214. default:
  215. break;
  216. }
  217. /*stop bit*/
  218. if (cfg->stop_bits == STOP_BITS_2) {
  219. smr_copy |= BIT_3;
  220. } else if (cfg->stop_bits != STOP_BITS_1) {
  221. return -RT_ERROR;
  222. }
  223. /*data bit*/
  224. if (cfg->data_bits == 7) {
  225. smr_copy |= BIT_6;
  226. } else if (cfg->data_bits != DATA_BITS_8) {
  227. return -RT_ERROR;
  228. }
  229. /*parity*/
  230. if (cfg->parity == PARITY_ODD)
  231. smr_copy |= BIT_5;
  232. else if (cfg->parity == PARITY_EVEN)
  233. smr_copy |= BIT_4 | BIT_5;
  234. brr_divider = rpdl_CGC_f_pclk / cfg->baud_rate;
  235. /* There is a fixed division by 2 */
  236. brr_divider /= 16;
  237. /* Select 8 base clock cycles (ABCS = 1) */
  238. semr_copy |= (unsigned char)BIT_4;
  239. //brr_divider /= 8;
  240. /* More division required? */
  241. if (brr_divider > 256) {
  242. /* Select 16 base clock cycles (ABCS = 0) */
  243. semr_copy &= (unsigned char)INV_BIT_4;
  244. brr_divider /= 2;
  245. }
  246. /* Load the BRR reset value */
  247. //brr_copy = 0xFFu;
  248. /* Ensure bits TIE, RIE, TE, RE and TEIE in the SCR are 0 */
  249. uart->sci->SCR.BYTE = 0x00;
  250. /* Configure the CKE & MPIE bits */
  251. uart->sci->SCR.BYTE = scr_copy & (BIT_0 | BIT_1 | BIT_3);
  252. /* Configure the SMR register */
  253. uart->sci->SMR.BYTE = smr_copy;
  254. /* Configure the SCMR register */
  255. uart->sci->SCMR.BYTE = scmr_copy;
  256. /* Configure the SEMR register */
  257. uart->sci->SEMR.BYTE = semr_copy;
  258. /* Configure the BRR register */
  259. uart->sci->BRR = brr_divider - 1;
  260. bit_interval_counter = rpdl_CGC_f_pclk / cfg->baud_rate;
  261. /* Wait for at least a 1-bit duration */
  262. do {
  263. bit_interval_counter--;
  264. }while (bit_interval_counter != 0);
  265. scr_copy = 0x00u;
  266. /*enable rx an tx*/
  267. scr_copy |= BIT_5 | BIT_4 ;
  268. uart->sci->SCR.BYTE &= 0x5B;
  269. uart->sci->SCR.BYTE |= scr_copy;
  270. *(IPR_ADDRESS(uart->UART)) = 5;
  271. uart->sci->SSR.BYTE = 0xC0;
  272. uart->sci->SSR.BYTE &= INV_BIT_5;
  273. while (uart->sci->SSR.BYTE & BIT_4);
  274. uart->sci->SSR.BYTE &= INV_BIT_3;
  275. #else
  276. struct rx_uart *uart;
  277. /* Declare error flag */
  278. bool err = true;
  279. uint32_t flag = 0;
  280. RT_ASSERT(serial != RT_NULL);
  281. uart = (struct rx_uart *)serial->parent.user_data;
  282. /* Configure the pin selection of SCI channel */
  283. err &= R_SCI_Set
  284. (
  285. PDL_SCI_PIN_SCI2_B
  286. );
  287. uart->sci->SCR.BYTE |= BIT_4 | BIT_5;
  288. switch (cfg->parity) {
  289. case PARITY_ODD:
  290. flag |= PDL_SCI_PARITY_ODD;
  291. break;
  292. case PARITY_EVEN:
  293. flag |= PDL_SCI_PARITY_EVEN;
  294. break;
  295. default:
  296. flag |= PDL_SCI_PARITY_NONE;
  297. break;
  298. }
  299. switch (cfg->data_bits) {
  300. case DATA_BITS_7:
  301. flag |= PDL_SCI_7_BIT_LENGTH;
  302. break;
  303. case DATA_BITS_8:
  304. flag |= PDL_SCI_8_BIT_LENGTH;
  305. break;
  306. }
  307. switch (cfg->stop_bits) {
  308. case STOP_BITS_1:
  309. flag |= PDL_SCI_STOP_1;
  310. break;
  311. case STOP_BITS_2:
  312. flag |= PDL_SCI_STOP_2;
  313. break;
  314. }
  315. flag |= PDL_SCI_ASYNC |
  316. PDL_SCI_TX_CONNECTED |
  317. PDL_SCI_RX_CONNECTED |
  318. PDL_SCI_CLK_INT_IO ;
  319. /* Configure the RS232 port */
  320. err &= R_SCI_Create(
  321. uart->UART,
  322. flag,
  323. cfg->baud_rate,
  324. 5);
  325. uart->sci->SCR.BYTE |= BIT_4|BIT_5;
  326. __enable_interrupt();
  327. #endif
  328. switch (uart->UART) {
  329. case 0:
  330. //ier_copy |= BIT_6 | BIT_7;
  331. ICU.IER[IER_SCI0_ERI0].BIT.IEN_SCI0_ERI0 = 1;
  332. ICU.IER[IER_SCI0_RXI0].BIT.IEN_SCI0_RXI0 = 1;
  333. ICU.IER[IER_SCI0_TEI0].BIT.IEN_SCI0_TEI0 = 1;
  334. ICU.IER[IER_SCI0_TXI0].BIT.IEN_SCI0_TXI0 = 1;
  335. break;
  336. case 1:
  337. ICU.IER[IER_SCI1_ERI1].BIT.IEN_SCI1_ERI1 = 1;
  338. ICU.IER[IER_SCI1_RXI1].BIT.IEN_SCI1_RXI1 = 1;
  339. //ICU.IER[IER_SCI1_TEI1].BIT.IEN_SCI1_TEI1 = 1;
  340. //ICU.IER[IER_SCI1_TXI1].BIT.IEN_SCI1_TXI1 = 1;
  341. break;
  342. case 2:
  343. ICU.IER[IER_SCI2_ERI2].BIT.IEN_SCI2_ERI2 = 1;
  344. ICU.IER[IER_SCI2_RXI2].BIT.IEN_SCI2_RXI2 = 1;
  345. ICU.IER[IER_SCI2_RXI2].BIT.IEN_SCI2_TEI2 = 0;
  346. ICU.IER[IER_SCI2_TXI2].BIT.IEN_SCI2_TXI2 = 0;
  347. break;
  348. case 3:
  349. ICU.IER[IER_SCI3_ERI3].BIT.IEN_SCI3_ERI3 = 1;
  350. ICU.IER[IER_SCI3_RXI3].BIT.IEN_SCI3_RXI3 = 1;
  351. ICU.IER[IER_SCI3_TEI3].BIT.IEN_SCI3_TEI3 = 1;
  352. ICU.IER[IER_SCI3_TXI3].BIT.IEN_SCI3_TXI3 = 1;
  353. break;
  354. case 5:
  355. ICU.IER[IER_SCI5_ERI5].BIT.IEN_SCI5_ERI5 = 1;
  356. ICU.IER[IER_SCI5_RXI5].BIT.IEN_SCI5_RXI5 = 1;
  357. ICU.IER[IER_SCI5_TEI5].BIT.IEN_SCI5_TEI5 = 1;
  358. ICU.IER[IER_SCI5_TXI5].BIT.IEN_SCI5_TXI5 = 1;
  359. break;
  360. case 6:
  361. ICU.IER[IER_SCI6_ERI6].BIT.IEN_SCI6_ERI6 = 1;
  362. ICU.IER[IER_SCI6_RXI6].BIT.IEN_SCI6_RXI6 = 1;
  363. ICU.IER[IER_SCI6_TEI6].BIT.IEN_SCI6_TEI6 = 1;
  364. ICU.IER[IER_SCI6_TXI6].BIT.IEN_SCI6_TXI6 = 1;
  365. break;
  366. }
  367. return RT_EOK;
  368. }
  369. static rt_err_t rx_control(struct rt_serial_device *serial, int cmd, void *arg)
  370. {
  371. struct rx_uart *uart;
  372. RT_ASSERT(serial != RT_NULL);
  373. uart = (struct rx_uart *)serial->parent.user_data;
  374. switch (cmd)
  375. {
  376. case RT_DEVICE_CTRL_CLR_INT:
  377. /* disable rx irq */
  378. uart->sci->SCR.BIT.RIE = 0;
  379. break;
  380. case RT_DEVICE_CTRL_SET_INT:
  381. /* enable rx irq */
  382. (void)(uart->sci->RDR);
  383. uart->sci->SCR.BIT.RIE = 1;
  384. break;
  385. }
  386. return RT_EOK;
  387. }
  388. static int rx_putc(struct rt_serial_device *serial, char c)
  389. {
  390. struct rx_uart *uart;
  391. uart = (struct rx_uart *)serial->parent.user_data;
  392. while (uart->sci->SSR.BIT.TDRE == 0);
  393. uart->sci->TDR = c;
  394. return 1;
  395. }
  396. static int rx_getc(struct rt_serial_device *serial)
  397. {
  398. struct rx_uart *uart;
  399. uart = (struct rx_uart *)serial->parent.user_data;
  400. if (uart->sci->SSR.BIT.RDRF)
  401. return (int) (uart->sci->RDR);
  402. return -1;
  403. }
  404. static const struct rt_uart_ops rx_uart_ops =
  405. {
  406. rx_configure,
  407. rx_control,
  408. rx_putc,
  409. rx_getc,
  410. };
  411. #if defined(RT_USING_UART2)
  412. /* UART0 device driver structure */
  413. struct rx_uart uart2 =
  414. {
  415. 2,
  416. &SCI2,
  417. };
  418. struct rt_serial_device serial2;
  419. #pragma vector = VECT_SCI2_ERI2
  420. __interrupt void Interrupt_SCI2_ERI2(void)
  421. {
  422. /* Will the user handle the errors? */
  423. /* Clear the error flags */
  424. SCI2.SSR.BYTE = (uint8_t)(BIT_7 | BIT_6);
  425. }
  426. #pragma vector = VECT_SCI2_RXI2
  427. __interrupt void Interrupt_SCI2_RXI2(void)
  428. {
  429. rt_interrupt_enter();
  430. rt_hw_serial_isr(&serial2, RT_SERIAL_EVENT_RX_IND);
  431. rt_interrupt_leave();
  432. }
  433. #endif
  434. void rt_hw_uart_init(void)
  435. {
  436. struct rx_uart *uart;
  437. struct serial_configure config;
  438. #ifdef RT_USING_UART2
  439. uart = &uart2;
  440. config.baud_rate = BAUD_RATE_38400;
  441. config.bit_order = BIT_ORDER_LSB;
  442. config.data_bits = DATA_BITS_8;
  443. config.parity = PARITY_NONE;
  444. config.stop_bits = STOP_BITS_1;
  445. config.invert = NRZ_NORMAL;
  446. config.bufsz = RT_SERIAL_RB_BUFSZ;
  447. serial2.ops = &rx_uart_ops;
  448. serial2.config = config;
  449. /* register UART1 device */
  450. rt_hw_serial_register(&serial2, "uart2",
  451. RT_DEVICE_FLAG_RDWR |
  452. RT_DEVICE_FLAG_INT_RX |
  453. RT_DEVICE_FLAG_STREAM,
  454. uart);
  455. #endif
  456. }