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  */
12 
13 #include <rtthread.h>
14 #include "compiler.h"
15 #include "pm.h"
16 #include "gpio.h"
17 #include "usart.h"
18 #include "intc.h"
19 #include "drv_uart.h"
20 #include "drv_gpio.h"
21 
22 /**
23  * System tick interrupt handler.
24  */
rt_hw_timer_handler(void)25 static void rt_hw_timer_handler(void)
26 {
27     // Clears the interrupt request.
28     Set_system_register(AVR32_COMPARE, FCPU / RT_TICK_PER_SECOND);
29 
30     rt_tick_increase();
31 }
32 
33 /**
34  * Initialize system clock and all peripherals.
35  */
peripherals_init(void)36 static void peripherals_init(void)
37 {
38     /*
39      * PM initialization: OSC0 = 12MHz XTAL, PLL0 = 60MHz System Clock
40      */
41     pm_freq_param_t pm_freq_param =
42     {
43         .cpu_f = FCPU,
44         .pba_f = FPBA,
45         .osc0_f = FOSC0,
46         .osc0_startup = AVR32_PM_OSCCTRL0_STARTUP_2048_RCOSC
47     };
48     pm_configure_clocks(&pm_freq_param);
49     INTC_init_interrupts();
50 }
51 
52 /**
53  * Initialize CPU cycle counter for system tick.
54  */
cpu_counter_init(void)55 static void cpu_counter_init(void)
56 {
57     INTC_register_interrupt(&rt_hw_timer_handler, AVR32_CORE_COMPARE_IRQ, AVR32_INTC_INT3);
58     Set_system_register(AVR32_COMPARE, FCPU / RT_TICK_PER_SECOND);
59     Set_system_register(AVR32_COUNT, 0);
60 }
61 
rt_hw_board_init(void)62 void rt_hw_board_init(void)
63 {
64     Disable_global_interrupt();
65 
66     peripherals_init();
67     cpu_counter_init();
68 
69 #ifdef RT_USING_COMPONENTS_INIT
70     rt_components_board_init();
71 #endif
72 
73 #ifdef RT_USING_SERIAL
74     rt_hw_uart_init();
75 #endif
76 
77 #ifdef RT_USING_PIN
78     rt_hw_gpio_init();
79 #endif
80 
81 #ifdef RT_USING_CONSOLE
82     rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
83 #endif
84 }
85