GD32VF103 的 1ms 延迟不正确

Incorrect 1ms delay at GD32VF103

我使用 GigaDevice 的 GD32VF103C_START 套件并尝试 LED 闪烁示例项目。我在网上找到了这个项目,它编译得很好(我只从 500ms 改为 1000ms=1s)。

main.c:


#include "gd32vf103.h"
#include "gd32vf103c_start.h"
#include "systick.h"

int main(void)
{  
    /* enable the LED clock */
    rcu_periph_clock_enable(RCU_GPIOA);
    /* configure LED GPIO port */
    gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_7);

    gpio_bit_reset(GPIOA, GPIO_PIN_7);
    
    while(1){
        /* insert 1s delay */
        delay_1ms(1000);

        /* toggle the LED */ 
        gpio_bit_write(GPIOA, GPIO_PIN_7, (bit_status)(1-gpio_input_bit_get(GPIOA, GPIO_PIN_7)));

        /* insert 1s delay */
        delay_1ms(1000);

        gpio_bit_write(GPIOA, GPIO_PIN_7, (bit_status)(1-gpio_input_bit_get(GPIOA, GPIO_PIN_7)));
    }
}

systick.c:

#include "gd32vf103.h"
#include "systick.h"

void delay_1ms(uint32_t count)
{
    uint64_t start_mtime, delta_mtime;

    /* don't start measuruing until we see an mtime tick */ 
    uint64_t tmp = get_timer_value();

    do{
        start_mtime = get_timer_value();
    }while(start_mtime == tmp);

    do{
        delta_mtime = get_timer_value() - start_mtime;
    }while(delta_mtime <(SystemCoreClock/4000.0 *count));
}

但不是延迟 1 秒,而是延迟 13 秒。哪里错了?

问题不在 main.c,也不在 systick.c,而是在 system_gd32vf103.c.

有:

/* system frequency define */
#define __IRC8M           (IRC8M_VALUE)            /* internal 8 MHz RC oscillator frequency */
#define __HXTAL           (HXTAL_VALUE)            /* high speed crystal oscillator frequency */
#define __SYS_OSC_CLK     (__IRC8M)                /* main oscillator frequency */

/* select a system clock by uncommenting the following line */
/* use IRC8M */
//#define __SYSTEM_CLOCK_48M_PLL_IRC8M            (uint32_t)(48000000)
//#define __SYSTEM_CLOCK_72M_PLL_IRC8M            (uint32_t)(72000000)
//#define __SYSTEM_CLOCK_108M_PLL_IRC8M           (uint32_t)(108000000)

/********************************************************************/
//#define __SYSTEM_CLOCK_HXTAL                    (HXTAL_VALUE)
//#define __SYSTEM_CLOCK_24M_PLL_HXTAL            (uint32_t)(24000000)
/********************************************************************/

//#define __SYSTEM_CLOCK_36M_PLL_HXTAL            (uint32_t)(36000000)
//#define __SYSTEM_CLOCK_48M_PLL_HXTAL            (uint32_t)(48000000)
//#define __SYSTEM_CLOCK_56M_PLL_HXTAL            (uint32_t)(56000000)
//#define __SYSTEM_CLOCK_72M_PLL_HXTAL            (uint32_t)(72000000)
//#define __SYSTEM_CLOCK_96M_PLL_HXTAL            (uint32_t)(96000000)
#define __SYSTEM_CLOCK_108M_PLL_HXTAL           (uint32_t)(108000000)

并且您需要取消注释 #define __SYSTEM_CLOCK_HXTAL (HXTAL_VALUE) 行。就是这样。