cembeddedclockmcu

Incorrect 1ms delay at GD32VF103


I use GD32VF103C_START kit from GigaDevice and try LED blinking sample project. I found this project in the Internet and it compiles just fine (I've only changed from 500ms to 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));
}

But instead of 1s delay it delays like for 13s. Where is mistake?


Solution

  • Problem is non in main.c, neither in systick.c, it's in system_gd32vf103.c.

    There are:

    /* 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)
    

    and you need uncomment #define __SYSTEM_CLOCK_HXTAL (HXTAL_VALUE) line. That's it.