1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author          Notes
8  * 2010-03-30     Kyle            First version
9  * 2023-10-13     Raman Gopalan   Move UART specific code sections into the drv_uart files
10  * 2023-10-20     Raman Gopalan   Initialize GPIO sub-system
11  * 2023-12-01     Raman Gopalan   Initialize software I2C sub-system
12  */
13 
14 #include <rtthread.h>
15 #include "compiler.h"
16 #include "pm.h"
17 #include "gpio.h"
18 #include "usart.h"
19 #include "intc.h"
20 #include "drv_uart.h"
21 #include "drv_gpio.h"
22 #include "drv_soft_i2c.h"
23 
24 /**
25  * System tick interrupt handler.
26  */
rt_hw_timer_handler(void)27 static void rt_hw_timer_handler(void)
28 {
29     // Clears the interrupt request.
30     Set_system_register(AVR32_COMPARE, FCPU / RT_TICK_PER_SECOND);
31 
32     rt_tick_increase();
33 }
34 
35 /**
36  * Initialize system clock and all peripherals.
37  */
peripherals_init(void)38 static void peripherals_init(void)
39 {
40     /*
41      * PM initialization: OSC0 = 12MHz XTAL, PLL0 = 60MHz System Clock
42      */
43     pm_freq_param_t pm_freq_param =
44     {
45         .cpu_f = FCPU,
46         .pba_f = FPBA,
47         .osc0_f = FOSC0,
48         .osc0_startup = AVR32_PM_OSCCTRL0_STARTUP_2048_RCOSC
49     };
50     pm_configure_clocks(&pm_freq_param);
51     INTC_init_interrupts();
52 }
53 
54 /**
55  * Initialize CPU cycle counter for system tick.
56  */
cpu_counter_init(void)57 static void cpu_counter_init(void)
58 {
59     INTC_register_interrupt(&rt_hw_timer_handler, AVR32_CORE_COMPARE_IRQ, AVR32_INTC_INT3);
60     Set_system_register(AVR32_COMPARE, FCPU / RT_TICK_PER_SECOND);
61     Set_system_register(AVR32_COUNT, 0);
62 }
63 
rt_hw_board_init(void)64 void rt_hw_board_init(void)
65 {
66     Disable_global_interrupt();
67 
68     peripherals_init();
69     cpu_counter_init();
70 
71 #ifdef RT_USING_COMPONENTS_INIT
72     rt_components_board_init();
73 #endif
74 
75 #ifdef RT_USING_SERIAL
76     rt_hw_uart_init();
77 #endif
78 
79 #ifdef RT_USING_PIN
80     rt_hw_gpio_init();
81 #endif
82 
83 #ifdef BSP_USING_SOFT_I2C
84     rt_sw_i2c_init();
85 #endif
86 
87 #ifdef RT_USING_CONSOLE
88     rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
89 #endif
90 }
91