Skip to main content
Visitor II
September 3, 2025
Question

Not able to enable SYSTICK

  • September 3, 2025
  • 3 replies
  • 757 views

Hi,

I am working with the STM32F103RCT6. I am writing a bare metal code for delay function using SYSTICK timer. The problem I am facing is that I am not able to write to the ARM cortex registers. Below is my code for your reference - 
This is the stm32f103xx.h file

#define SYSCLK 8000000UL // System clock frequency in Hz
#define SYSTICK_BASE 0xE000E010UL

// SysTick Structure definition
typedef struct
{
 volatile uint32_t CTRL; /* SysTick control and status register offset: 0x00 */
 volatile uint32_t LOAD; /* SysTick reload value register offset: 0x04 */
 volatile uint32_t VAL; /* SysTick current value register offset: 0x08 */
 volatile uint32_t CALIB; /* SysTick calibration value register offset: 0x0C */
} SysTick_TypeDef;

#define SYSTICK ((SysTick_TypeDef *) SYSTICK_BASE)


This is the function in my main.c

void SystemClock_Config(void)
{
 __disable_irq(); // Disable all interrupts

 // 1. Enable HSI
 RCC->CR |= (1 << 0); // HSION = 1

 // 2. Wait until HSI is ready
 while (!(RCC->CR & (1 << 1))); // HSIRDY = 1

 // 3. Select HSI as system clock
 RCC->CFGR &= ~(0x3 << 0); // clear SW[1:0]
 RCC->CFGR |= (0x0 << 0); // select HSI (00)

 // 4. Wait until HSI is used as system clock
 while ((RCC->CFGR & (0x3 << 2)) != (0x0 <<2) );

 SYSTICK->LOAD = (uint32_t)((SYSCLK / 1000) - 1); // 1ms reload
 SYSTICK->VAL = 0; // reset counter
 SYSTICK->CTRL = (1U << 2); // processor clock enable
 SYSTICK->CTRL |= (1U << 1); // enable interrupt
 SYSTICK->CTRL |= (1U << 0); // enable SysTick

 __enable_irq(); // Re-enable all interrupts
 
}

This is the implementation of the SysTick_Handler . I debugged my code and the current_tick variable was not incrementing. 

volatile uint32_t current_tick = 0;

void SysTick_Handler(void)
{
 current_tick +=1;
}


Below is the screenshot of the cube programmer window after I flashed the code - 
Screenshot from 2025-09-03 17-52-58.png

As you can see, the SYSTICK registers do not have the value I tried to write. My code only contains this piece of code which I shared. Is my code missing something that needs to be added or some other register that needs to be configured? 

    This topic has been closed for replies.

    3 replies

    Graduate II
    September 3, 2025

    Is the program only flashed or is it started and running?

    Visitor II
    September 3, 2025

    I flashed the code but it didn't work. Then I debugged the code and saw that the system ticks are not incrementing. I flashed the code and then checked the register contents on cube programmer. The SYSTICK register was not set as you can see in the screenshot.

    Graduate II
    September 3, 2025

    I'm not sure of the efficacy of using STM32 Cube Programmer in this manner.

    Can't you use the debugger, and do a "peripheral view", or for that matter using a serial port to dump the register content of interest to the terminal?

    You shouldn't need to start HSI, the MCU will implicitly by running from the HSI when it first starts.

    Check BOOTx pins as to what code starts.

    Check SCB->VTOR points to your vector table, and that it's correct.

    Double check what the NVIC is doing.

    Super User
    September 3, 2025

    Are you calling those functions? Attach your main.c file.

    Maybe only do a single write to CTRL.

    Visitor II
    September 4, 2025

    Sure. Here is my main.c file - 

    #include <main.h>
    #include <gpio.h>
    
    /** @note DONT ASSIGN ANY PIN FUNCTIONS TO PA13 AND PA14. THEY ARE SWD PINS*/
    
    void SystemClock_Config(void);
    
    //BLDCMotor(pole_pairs, resistance, kv_rating, inductance)
    BLDCMotor motor = BLDCMotor(7,12.9,260,2);
    
    //BLDCDriver3PWM(pwmA, pwmB, pwmC)
    BLDCDriver3PWM driver = BLDCDriver3PWM(PB1,PB0,PA7);
    
    int main(int argc, char *argv[])
    {
     SystemClock_Config();
     driver.voltage_power_supply = 12.0f; // power supply voltage [V]
     driver.voltage_limit = 7.0f;
     driver.init();
     motor.linkDriver(&driver);
     motor.voltage_limit = 3.0f;
     motor.init();
     
     while(1){
     motor.move(10);
     }
     
     return 0;
    }
    
    
    /**
     * @brief initialize system clock to 8MHz HSI
     */
    void SystemClock_Config(void)
    {
     __disable_irq(); // Disable all interrupts
    
     // 1. Enable HSI
     RCC->CR |= (1 << 0); // HSION = 1
    
     // 2. Wait until HSI is ready
     while (!(RCC->CR & (1 << 1))); // HSIRDY = 1
    
     // 3. Select HSI as system clock
     RCC->CFGR &= ~(0x3 << 0); // clear SW[1:0]
     RCC->CFGR |= (0x0 << 0); // select HSI (00)
    
     // 4. Wait until HSI is used as system clock
     while ((RCC->CFGR & (0x3 << 2)) != (0x0 <<2) );
    
     SYSTICK->LOAD = (uint32_t)((SYSCLK / 1000) - 1); // 1ms reload
     SYSTICK->VAL = 0; // reset counter
     SYSTICK->CTRL = (1U << 2); // processor clock enable
     SYSTICK->CTRL |= (1U << 1); // enable interrupt
     SYSTICK->CTRL |= (1U << 0); // enable SysTick
    
     __enable_irq(); // Re-enable all interrupts
     
    }
    Visitor II
    September 4, 2025

    Hi people,
    I just observed a strange behavior. When I use STMCubeIDE to make and flash my project then it works fine. But when I use VS Code then it doesn't work. I have double checked my CmakeList.txt and it is correct. So what could be wrong? Your suggestions would be really helpful.