cancel
Showing results for 
Search instead for 
Did you mean: 

Not able to enable SYSTICK

Akshat_codes_
Associate II

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? 

8 REPLIES 8
mfgkw
Associate III

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

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.

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.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

I haven't written any code pertaining to NVIC. This is the only code I have written in the empty project and flashing on the MCU. The boot0 pin is connected to GND. Also, I enabled HSI just for readability and specifically showing in the code that HSI is being used. So, should I remove the HSI code ?

TDK
Super User

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

Maybe only do a single write to CTRL.

If you feel a post has answered your question, please click "Accept as Solution".

I'm just saying to inspect the relevant plumbing to understand why the interrupt is not occurring.

You need to follow the code flow, not sure if the code is running at the point you're inspecting the Cortex registers, would perhaps read a couple of the registers rather than a block of 0x400 bytes.

Generally better to do in the debugger, and whilst stepping through the code.

Perhaps check also with working HAL code, to see what the register look like in working cases.

 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

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
  
}
Akshat_codes_
Associate II

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.