This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Strange interaction between SysTick and Sleep

I am currently using SysTick to count milliseconds in my device. This works fine, however with it enabled I can't sleep the device as it wakes up instantly.

I have tried to disable it, however it does not work. The following code blinks the LED unless I remove the references to SysTick_Config

int main(void)
{
    SysTick_Config(SystemCoreClock / 1000);

    syshal_gpio_init(GPIO_LED_GREEN);

    for (;;)
    {
        syshal_gpio_set_output_high(GPIO_LED_GREEN);

        for (uint32_t i = 0; i < 1000000; ++i)
            __NOP();

        SysTick->CTRL = 0;

        nrf_pwr_mgmt_run();

        syshal_gpio_set_output_low(GPIO_LED_GREEN);

        for (uint32_t i = 0; i < 1000000; ++i)
            __NOP();

        SysTick_Config(SystemCoreClock / 1000);
    }
}


I'm almost certainly going to switch to using an RTC but it would be nice to understand why this does not work.

I am using the nRF52840 with no softdevice

  • Hi,

     

    The SysTick requires that the CPU is running on nRF52 devices, so when you enter sleep (WFE/WFI), the systick is suspended.

    Using RTC is a recommended option.

     

    Kind regards,

    Håkon

  • The SysTick stopping when sleeping is actually the functionality I'm after. My issue is more surrounding the fact that SysTick appears to be waking my device from sleep

  • The SysTick is suspended when you're in sleep, so there must be something else that is waking up the CPU. Is that the only component you have in your application now?

    How often do you expect the systick vs. how often it actually occurs?

     

    Kind regards,

    Håkon

  • Hi Håkon,

    Thanks for the input. I'm starting to arrive at the same conclusion, something else is waking the device. I am not running any other peripherals or code on my device (no bootloader/softdevice)

    I've created a simplified, one file variant with a timer and still see the same issue:

    #include "nrfx_timer.h"
    #include "nrf_gpio.h"
    #include "nrf_pwr_mgmt.h"
    
    const nrfx_timer_t timer_inst = NRFX_TIMER_INSTANCE(3);
    
    void timer_irq(nrf_timer_event_t event_type, void* p_context)
    {
        nrf_gpio_pin_toggle(NRF_GPIO_PIN_MAP(1, 10)); // Toggle the LED
    }
    
    void delay(void)
    {
        for (uint32_t i = 0; i < 1000000; ++i)
            __NOP();
    }
    
    int main(void)
    {
        nrf_pwr_mgmt_init();
    
        const nrfx_timer_config_t timer_config =
        {
            .frequency          = NRF_TIMER_FREQ_125kHz,
            .mode               = NRF_TIMER_MODE_TIMER,
            .bit_width          = NRF_TIMER_BIT_WIDTH_32,
            .interrupt_priority = 6,
            .p_context          = NULL
        };
    
        nrf_gpio_cfg_output(NRF_GPIO_PIN_MAP(1, 10));
    
        nrfx_timer_init(&timer_inst, &timer_config, timer_irq);
        nrfx_timer_extended_compare(&timer_inst, NRF_TIMER_CC_CHANNEL0, 125 * 100, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true); // 100 ms
        nrfx_timer_enable(&timer_inst);
        
        for (;;)
        {
            delay();
    
            nrfx_timer_disable(&timer_inst); // Disable the timer
            
            nrf_pwr_mgmt_run(); // We would expect to sleep here indefinitely
    
            nrfx_timer_enable(&timer_inst);
    
            delay();
        }
    }

    I'm going to try a completely new build with the latest SDK as this must be an issue with the startup file or something.

    EDIT: I've tried it with the 15.3.0 SDK by copying the above code into the blinky project. I had to make a few tweaks to the linker and makefile to add in what was missing but this has resulted in the exact same issue

    EDIT2: I've tried both gcc-arm-none-eabi-7-2018-q2-update-win32 and gcc-arm-none-eabi-8-2018-q4-major-win32 and they both act the same. I've checked the assembly code for `main()` and this appears to be correct

  • Could you try rebooting your device with the debugger detached?

Related