uart.c 14 KB

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