#include "board.h" #include "hpm_gptmr_drv.h" #include "hard_system_time.h" #include "test.h" // 需要修改分频配置 #define SYSTEM_TIMER_0 HPM_GPTMR0 #define SYSTEM_TIMER_0_CLK_NAME clock_gptmr0 #define SYSTEM_TIMER_0_CH 0 #define SYSTEM_TIMER_0_IRQ IRQn_GPTMR0 #define SYSTEM_TIMER_0_TICK_US (1) #define SYSTEM_TIMER_0_RELOAD 0xFFFFFFFF void system_time_init(void) { uint32_t gptmr_freq; gptmr_channel_config_t config; clock_set_source_divider(SYSTEM_TIMER_0_CLK_NAME, clk_src_osc24m, 24); // 1Mhz clock_add_to_group(SYSTEM_TIMER_0_CLK_NAME, 0); // 添加到组 gptmr_freq = clock_get_frequency(SYSTEM_TIMER_0_CLK_NAME); printf("timer0 fre: %d\r\n", gptmr_freq); gptmr_channel_get_default_config(SYSTEM_TIMER_0, &config); config.reload = SYSTEM_TIMER_0_RELOAD; // 32bit timer0 71.58min gptmr_channel_config(SYSTEM_TIMER_0, SYSTEM_TIMER_0_CH, &config, false); gptmr_start_counter(SYSTEM_TIMER_0, SYSTEM_TIMER_0_CH); gptmr_enable_irq(SYSTEM_TIMER_0, GPTMR_CH_RLD_IRQ_MASK(SYSTEM_TIMER_0_CH)); intc_m_enable_irq_with_priority(SYSTEM_TIMER_0_IRQ, 1); } SDK_DECLARE_EXT_ISR_M(SYSTEM_TIMER_0_IRQ, timer0_isr) void timer0_isr(void) { if (gptmr_check_status(SYSTEM_TIMER_0, GPTMR_CH_RLD_STAT_MASK(SYSTEM_TIMER_0_CH))) { gptmr_clear_status(SYSTEM_TIMER_0, GPTMR_CH_RLD_STAT_MASK(SYSTEM_TIMER_0_CH)); //time_hookfunction(); // printf("timer test\r\n"); } } unsigned int hard_micros(void) { unsigned int ret; // 返回当前定时器通道计数值 ret = gptmr_channel_get_counter(SYSTEM_TIMER_0,SYSTEM_TIMER_0_CH, gptmr_counter_type_normal); return ret; } /** * @brief 微秒延时 */ static void hard_delay_us(unsigned int us) { unsigned int start = hard_micros(); while ((hard_micros() - start) < us) { // 忙等待 } } /** * @brief 毫秒延时 */ static void hard_delay_ms(unsigned int ms) { hard_delay_us(ms * 1000); } /*timer0 test 2026/3/14 测试通过*/ #ifdef TIMER0_TEST #include "bsp_V8M_YY_led.h" void timer0_test(void) { system_time_init(); v8m_yy_led_init(); while(1) { hard_delay_ms(1000); v8m_yy_led_toggle(V8M_YY_LED_G); printf("1000ms \r\n"); } } #endif