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     Email                   Notes
8  * 2023-09-16  luhuadong  luhuadong@163.com       First Release
9  */
10 
11 #include <string.h>
12 
13 #include <atmel_start.h>
14 #include "peripheral_clk_config.h"
15 
16 #include <rtthread.h>
17 #include "board.h"
18 
19 #ifdef RT_USING_SERIAL
20 extern int rt_hw_uart_init(void);
21 #endif
22 
23 static struct io_descriptor* g_stdio;
24 static uint8_t board_info[32] = "* Adafruit Metro M4 Express\r\n"; //Microchip SAMD51
25 
rt_hw_console_output(const char * str)26 void rt_hw_console_output(const char *str)
27 {
28     io_write(g_stdio, (uint8_t *)str, strlen(str));
29     while (TARGET_IO.stat != 0);
30 }
31 RTM_EXPORT(rt_hw_console_output);
32 
hw_board_init_usart(void)33 static inline void hw_board_init_usart(void)
34 {
35     usart_async_get_io_descriptor(&TARGET_IO, &g_stdio);
36     usart_async_enable(&TARGET_IO);
37     io_write(g_stdio, board_info, strlen(board_info));
38 }
39 
40 /**
41  * This is the timer interrupt service routine.
42  *
43  */
SysTick_Handler(void)44 void SysTick_Handler(void)
45 {
46     /* enter interrupt */
47     rt_interrupt_enter();
48 
49     rt_tick_increase();
50 
51     /* leave interrupt */
52     rt_interrupt_leave();
53 }
54 
55 /**
56  * This function will initial SAMD51 board.
57  */
rt_hw_board_init(void)58 void rt_hw_board_init(void)
59 {
60     /* Initializes MCU, drivers and middleware */
61     atmel_start_init();
62 
63     /* enable USART stdout module */
64     hw_board_init_usart();
65 
66     /* UART driver initialization is open by default */
67 #ifdef RT_USING_SERIAL
68     rt_hw_uart_init();
69 #endif
70 
71     /* init systick */
72     SysTick_Config(CONF_CPU_FREQUENCY / RT_TICK_PER_SECOND);
73 
74     /* set pend exception priority */
75     NVIC_SetPriority(PendSV_IRQn, (1 << __NVIC_PRIO_BITS) - 1);
76 
77 #ifdef RT_USING_HEAP
78     #if defined(__ARMCC_VERSION)
79         rt_system_heap_init((void*)&Image$$RW_IRAM1$$ZI$$Limit, (void*)HEAP_END);
80     #elif __ICCARM__
81         rt_system_heap_init((void*)HEAP_BEGIN, (void*)HEAP_END);
82     #else
83         /* init memory system */
84         rt_system_heap_init((void*)&__bss_end, (void*)HEAP_END);
85     #endif
86 #endif
87 
88     /* Set the shell console output device */
89 #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
90     rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
91 #endif
92 
93 #ifdef RT_USING_COMPONENTS_INIT
94     rt_components_board_init();
95 #endif
96 }
97