1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
/*
* Copyright (c) 2021-2025 HPMicro
* SPDX-License-Identifier: BSD-3-Clause
*
*/
#include "board.h"
#include "rtt_board.h"
#include "hpm_uart_drv.h"
#include "hpm_gpio_drv.h"
#include "hpm_pmp_drv.h"
#include "assert.h"
#include "hpm_clock_drv.h"
#include "hpm_sysctl_drv.h"
#include <rthw.h>
#include <rtthread.h>
#include "hpm_dma_mgr.h"
#include "hpm_l1c_drv.h"
#include "hpm_mchtmr_drv.h"
extern int rt_hw_uart_init(void);
void os_tick_config(void);
void rtt_board_init(void);
void os_tick_config(void)
{
sysctl_config_clock(HPM_SYSCTL, clock_node_mchtmr0, clock_source_osc0_clk0, 1);
sysctl_add_resource_to_cpu0(HPM_SYSCTL, sysctl_resource_mchtmr0);
mchtmr_set_compare_value(HPM_MCHTMR, BOARD_MCHTMR_FREQ_IN_HZ / RT_TICK_PER_SECOND);
enable_mchtmr_irq();
}
void rtt_os_tick_clock(void)
{
clock_add_to_group(clock_mchtmr0, 0);
}
void rtt_board_init(void)
{
rtt_os_tick_clock();
board_init_clock();
board_init_console();
board_init_pmp();
dma_mgr_init();
/* initialize memory system */
rt_system_heap_init(RT_HW_HEAP_BEGIN, RT_HW_HEAP_END);
/* Configure the OS Tick */
os_tick_config();
/* Initialize the UART driver first, because later driver initialization may require the rt_kprintf */
rt_hw_uart_init();
/* Set console device */
rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
}
void app_init_led_pins(void)
{
board_init_led_pins();
gpio_set_pin_output(APP_LED0_GPIO_CTRL, APP_LED0_GPIO_INDEX, APP_LED0_GPIO_PIN);
gpio_set_pin_output(APP_LED1_GPIO_CTRL, APP_LED1_GPIO_INDEX, APP_LED1_GPIO_PIN);
gpio_set_pin_output(APP_LED2_GPIO_CTRL, APP_LED2_GPIO_INDEX, APP_LED2_GPIO_PIN);
gpio_write_pin(APP_LED0_GPIO_CTRL, APP_LED0_GPIO_INDEX, APP_LED0_GPIO_PIN, APP_LED_OFF);
gpio_write_pin(APP_LED1_GPIO_CTRL, APP_LED1_GPIO_INDEX, APP_LED1_GPIO_PIN, APP_LED_OFF);
gpio_write_pin(APP_LED2_GPIO_CTRL, APP_LED2_GPIO_INDEX, APP_LED2_GPIO_PIN, APP_LED_OFF);
}
void app_led_write(uint32_t index, bool state)
{
switch (index)
{
case 0:
gpio_write_pin(APP_LED0_GPIO_CTRL, APP_LED0_GPIO_INDEX, APP_LED0_GPIO_PIN, state);
break;
case 1:
gpio_write_pin(APP_LED1_GPIO_CTRL, APP_LED1_GPIO_INDEX, APP_LED1_GPIO_PIN, state);
break;
case 2:
gpio_write_pin(APP_LED2_GPIO_CTRL, APP_LED2_GPIO_INDEX, APP_LED2_GPIO_PIN, state);
break;
default:
/* Suppress the toolchain warnings */
break;
}
}
void rt_hw_console_output(const char *str)
{
while (*str != '\0')
{
uart_send_byte(BOARD_APP_UART_BASE, *str++);
}
}
void app_init_usb_pins(void)
{
board_init_usb(HPM_USB0);
}
void rt_hw_cpu_reset(void)
{
HPM_PPOR->RESET_ENABLE |= (1UL << 31);
HPM_PPOR->RESET_HOT &= ~(1UL << 31);
HPM_PPOR->RESET_COLD |= (1UL << 31);
HPM_PPOR->SOFTWARE_RESET = 1000U;
while(1) {
}
}
MSH_CMD_EXPORT_ALIAS(rt_hw_cpu_reset, reset, reset the board);
#ifdef RT_USING_CACHE
void rt_hw_cpu_dcache_ops(int ops, void *addr, int size)
{
if (ops == RT_HW_CACHE_FLUSH) {
l1c_dc_flush((uint32_t)addr, size);
} else {
l1c_dc_invalidate((uint32_t)addr, size);
}
}
#endif
uint32_t rtt_board_init_adc12_clock(ADC12_Type *ptr, bool clk_src_ahb)
{
uint32_t freq = 0;
if (ptr == HPM_ADC0) {
if (clk_src_ahb) {
/* Configure the ADC clock from AHB (@200MHz by default)*/
clock_set_adc_source(clock_adc0, clk_adc_src_ahb0);
} else {
/* Configure the ADC clock from pll1_clk1 divided by 2 (@200MHz by default) */
clock_set_adc_source(clock_adc0, clk_adc_src_ana0);
clock_set_source_divider(clock_ana0, clk_src_pll1_clk1, 2U);
}
clock_add_to_group(clock_adc0, 0);
freq = clock_get_frequency(clock_adc0);
} else if (ptr == HPM_ADC1) {
if (clk_src_ahb) {
/* Configure the ADC clock from AHB (@200MHz by default)*/
clock_set_adc_source(clock_adc1, clk_adc_src_ahb0);
} else {
/* Configure the ADC clock from pll1_clk1 divided by 2 (@200MHz by default) */
clock_set_adc_source(clock_adc1, clk_adc_src_ana1);
clock_set_source_divider(clock_ana1, clk_src_pll1_clk1, 2U);
}
clock_add_to_group(clock_adc1, 0);
freq = clock_get_frequency(clock_adc1);
} else if (ptr == HPM_ADC2) {
if (clk_src_ahb) {
/* Configure the ADC clock from AHB (@200MHz by default)*/
clock_set_adc_source(clock_adc2, clk_adc_src_ahb0);
} else {
/* Configure the ADC clock from pll1_clk1 divided by 2 (@200MHz by default) */
clock_set_adc_source(clock_adc2, clk_adc_src_ana2);
clock_set_source_divider(clock_ana2, clk_src_pll1_clk1, 2U);
}
clock_add_to_group(clock_adc2, 0);
freq = clock_get_frequency(clock_adc2);
}
return freq;
}
uint32_t rtt_board_init_adc16_clock(ADC16_Type *ptr, bool clk_src_ahb)
{
uint32_t freq = 0;
if (ptr == HPM_ADC3) {
if (clk_src_ahb) {
/* Configure the ADC clock from AHB (@200MHz by default)*/
clock_set_adc_source(clock_adc3, clk_adc_src_ahb0);
} else {
/* Configure the ADC clock from pll1_clk1 divided by 2 (@200MHz by default) */
clock_set_adc_source(clock_adc3, clk_adc_src_ana2);
clock_set_source_divider(clock_ana2, clk_src_pll1_clk1, 2U);
}
clock_add_to_group(clock_adc3, 0);
freq = clock_get_frequency(clock_adc3);
}
return freq;
}
uint32_t rtt_board_init_i2s_clock(I2S_Type *ptr)
{
uint32_t freq = 0;
if (ptr == HPM_I2S0) {
clock_add_to_group(clock_i2s0, 0);
sysctl_config_clock(HPM_SYSCTL, clock_node_aud0, clock_source_pll3_clk0, 25);
sysctl_set_adc_i2s_clock_mux(HPM_SYSCTL, clock_node_i2s0, clock_source_i2s_aud0_clk);
freq = clock_get_frequency(clock_i2s0);
} else if (ptr == HPM_I2S1) {
clock_add_to_group(clock_i2s1, 0);
sysctl_config_clock(HPM_SYSCTL, clock_node_aud1, clock_source_pll3_clk0, 25);
sysctl_set_adc_i2s_clock_mux(HPM_SYSCTL, clock_node_i2s1, clock_source_i2s_aud1_clk);
freq = clock_get_frequency(clock_i2s1);
} else {
;
}
return freq;
}
uint32_t rtt_board_init_pwm_clock(PWM_Type *ptr)
{
uint32_t freq = 0;
if (ptr == HPM_PWM0) {
clock_add_to_group(clock_mot0, 0);
freq = clock_get_frequency(clock_mot0);
} else if (ptr == HPM_PWM1) {
clock_add_to_group(clock_mot1, 0);
freq = clock_get_frequency(clock_mot1);
} else if (ptr == HPM_PWM2) {
clock_add_to_group(clock_mot2, 0);
freq = clock_get_frequency(clock_mot2);
} else if (ptr == HPM_PWM3) {
clock_add_to_group(clock_mot3, 0);
freq = clock_get_frequency(clock_mot3);
} else {
}
return freq;
}
#ifdef CONFIG_CHERRYUSB_CUSTOM_IRQ_HANDLER
extern void hpm_isr_usb0(void);
RTT_DECLARE_EXT_ISR_M(IRQn_USB0, hpm_isr_usb0)
#ifdef HPM_USB1_BASE
extern void hpm_isr_usb1(void);
SDK_DECLARE_EXT_ISR_M(IRQn_USB1, hpm_isr_usb1)
#endif
#endif
void rt_hw_board_init(void)
{
rtt_board_init();
/* Call the RT-Thread Component Board Initialization */
rt_components_board_init();
}
ATTR_PLACE_AT(".isr_vector") void mchtmr_isr(void)
{
HPM_MCHTMR->MTIMECMP = HPM_MCHTMR->MTIME + BOARD_MCHTMR_FREQ_IN_HZ / RT_TICK_PER_SECOND;
rt_tick_increase();
}
|