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 
10 #include <rthw.h>
11 #include <rtthread.h>
12 #include "board.h"
13 #include "uart.h"
14 #include <stdint.h>
15 #include <stdbool.h>
16 #include "r_pdl_cgc.h"
17 /* General RPDL function definitions */
18 #include "r_pdl_definitions.h"
19 #include "intrinsics.h"
20 #include "iorx62n.h"
21 
22 
23 
24 /**
25  * This is the timer interrupt service routine.
26  *
27  */
28 #pragma vector = VECT_CMT0_CMI0
29 __interrupt
SysTick_Handler(void)30 void SysTick_Handler(void)
31 {
32    // __enable_interrupt();
33     /* enter interrupt */
34     rt_interrupt_enter();
35 
36     rt_tick_increase();
37 
38     /* leave interrupt */
39     rt_interrupt_leave();
40 }
41 
42 
rt_hw_systick_init(void)43 void  rt_hw_systick_init(void)
44 {
45     /* Enable compare match timer 0. */
46     MSTP( CMT0 ) = 0;
47 
48     /* Interrupt on compare match. */
49     CMT0.CMCR.BIT.CMIE = 1;
50 
51     /* Set the compare match value. */
52     CMT0.CMCOR = ( unsigned short ) (((XTAL_FREQUENCY * PCLK_MUL) / RT_TICK_PER_SECOND)/8 -1);
53 
54     /* Divide the PCLK by 128. */
55     CMT0.CMCR.BIT.CKS = 0;
56 
57     /* Enable the interrupt... */
58     _IEN( _CMT0_CMI0 ) = 1;
59 
60     /* ...and set its priority to the application defined kernel priority. */
61     _IPR( _CMT0_CMI0 ) = 4;
62 
63     /* Start the timer. */
64     CMT.CMSTR0.BIT.STR0 = 1;
65 }
66 
rt_hw_system_freq_init(void)67 void rt_hw_system_freq_init(void)
68 {
69    /* Declare error flag */
70   bool err = true;
71 
72   /* Modify the MCU clocks, all are based off Epson 12 MHz clock */
73   err &=     R_CGC_Set
74     (
75     12E6,
76     96E6,
77     48E6,
78     24E6,
79     PDL_NO_DATA
80     );
81   /*
82   Clock Description              Frequency
83   ----------------------------------------
84   Input Clock Frequency..............12MHz
85   Internal Clock Frequency...........96MHz
86   Peripheral Clock Frequency.........48MHz
87   External Bus Clock Frequency.......24MHz */
88 
89   /* Halt in while loop when RPDL errors detected */
90   while (!err);
91 }
92 
93 /**
94  * This function will initial rx62n board
95  */
rt_hw_board_init()96 void rt_hw_board_init()
97 {
98 
99     rt_hw_system_freq_init();
100     rt_hw_systick_init();
101     rt_hw_uart_init();
102 #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
103     rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
104 #endif
105 }
106