1 /*
2  * Copyright (c) 2006-2022, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2017-07-24     Tanek        the first version
9  * 2018-11-12     Ernest Chen  modify copyright
10  */
11 
12 #include "board.h"
13 
14 #include <stdint.h>
15 #include "drv_usart.h"
16 #include "drv_gpio.h"
17 
18 #include <rthw.h>
19 #include <rtthread.h>
20 
21 
22 /* Updates the variable SystemCoreClock and must be called
23  * whenever the core clock is changed during program execution.
24  */
25 extern void SystemCoreClockUpdate(void);
26 
27 /* core clock */
28 extern uint32_t SystemCoreClock;
29 
30 volatile rt_uint32_t g_ticks=0;
31 
_SysTick_Config(rt_uint32_t ticks)32 static uint32_t _SysTick_Config(rt_uint32_t ticks)
33 {
34     g_ticks=ticks;
35     /* disable hardware pushing stack automatically and disable interrupt nesting */
36     PFIC->CFGR=0xFA050003;
37     NVIC_SetPriority(SysTicK_IRQn,0xf0);
38     NVIC_SetPriority(Software_IRQn,0xf0);
39     SysTick->CTLR=0;
40     SysTick->CNTL0=0;SysTick->CNTL1=0;SysTick->CNTL2=0;SysTick->CNTL3=0;
41     SysTick->CNTH0=0;SysTick->CNTH1=0;SysTick->CNTH2=0;SysTick->CNTH3=0;
42     SysTick->CMPLR0=(rt_uint8_t)(g_ticks-1);
43     SysTick->CMPLR1=(rt_uint8_t)((g_ticks-1)>>8);
44     SysTick->CMPLR2=(rt_uint8_t)((g_ticks-1)>>16);
45     SysTick->CMPLR3=(rt_uint8_t)((g_ticks-1)>>24);
46     SysTick->CMPHR0=0;SysTick->CMPHR1=0;SysTick->CMPHR2=0;SysTick->CMPHR3=0;
47     SysTick->CTLR=0x1;
48     NVIC_EnableIRQ(SysTicK_IRQn);
49     NVIC_EnableIRQ(Software_IRQn);
50     return 0;
51 }
52 
53 #if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
54 #define RT_HEAP_SIZE (1024)
55 /* heap default size: 4K(1024 * 4) */
56 static uint32_t rt_heap[RT_HEAP_SIZE];
rt_heap_begin_get(void)57 rt_weak void *rt_heap_begin_get(void)
58 {
59     return rt_heap;
60 }
61 
rt_heap_end_get(void)62 rt_weak void *rt_heap_end_get(void)
63 {
64     return rt_heap + RT_HEAP_SIZE;
65 }
66 #endif
67 
68 /**
69  * This function will initial your board.
70  */
rt_hw_board_init()71 void rt_hw_board_init()
72 {
73     /* System Clock Update */
74     SystemCoreClockUpdate();
75 
76     /* System Tick Configuration, systick clock is HCLK/8 */
77     _SysTick_Config(SystemCoreClock / 8 / RT_TICK_PER_SECOND);
78     /* Call components board initial (use INIT_BOARD_EXPORT()) */
79 #ifdef RT_USING_PIN
80     /* pin must initialized before i2c */
81     rt_hw_pin_init();
82 #endif
83 #ifdef RT_USING_COMPONENTS_INIT
84     rt_components_board_init();
85 #endif
86 #if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
87     rt_system_heap_init(rt_heap_begin_get(), rt_heap_end_get());
88 #endif
89 
90 #ifdef RT_USING_CONSOLE
91     rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
92 #endif
93 }
94 
95 
96 void SysTick_Handler(void) __attribute__((interrupt()));
SysTick_Handler(void)97 void SysTick_Handler(void)
98 {
99     GET_INT_SP();
100     /* enter interrupt */
101     rt_interrupt_enter();
102     SysTick->CTLR=0;
103     SysTick->CNTL0=0;SysTick->CNTL1=0;SysTick->CNTL2=0;SysTick->CNTL3=0;
104     SysTick->CNTH0=0;SysTick->CNTH1=0;SysTick->CNTH2=0;SysTick->CNTH3=0;
105     SysTick->CTLR=0x1;
106     rt_tick_increase();
107     /* leave interrupt */
108     rt_interrupt_leave();
109     FREE_INT_SP();
110 
111 }
112