<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Strange interaction between SysTick and Sleep</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/52975/strange-interaction-between-systick-and-sleep</link><description>I am currently using SysTick to count milliseconds in my device. This works fine, however with it enabled I can&amp;#39;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</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 11 Oct 2019 12:38:45 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/52975/strange-interaction-between-systick-and-sleep" /><item><title>RE: Strange interaction between SysTick and Sleep</title><link>https://devzone.nordicsemi.com/thread/214606?ContentTypeID=1</link><pubDate>Fri, 11 Oct 2019 12:38:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b3c0ecf9-1dbb-40a3-b07a-1df00c3b9292</guid><dc:creator>Xenoamor</dc:creator><description>&lt;p&gt;Thanks H&amp;aring;kon, that&amp;#39;s exactly what&amp;#39;s happening. Any periodic interrupt that occurs just at the right time can cause this to happen. It just so happens that my code is of that perfect timing to cause this to always happen.&lt;/p&gt;
&lt;p&gt;What&amp;#39;s happening is the SysTick event is pending just as WFE is called, this means WFE is effectively treated as a NOP. The next time through the loop the same happens at exactly the same time&lt;/p&gt;
&lt;p&gt;The fixes for this are:&lt;/p&gt;
&lt;p&gt;a) Use SEV, WFE, WFE. This is risky as stated by Radoslaw Koppel in the thread you linked&lt;br /&gt;b) Randomise when you call WFE, SEV, WFE. This removes the &amp;quot;perfect storm&amp;quot; timing issue&lt;br /&gt;c) Don&amp;#39;t use periodic interrupts&lt;/p&gt;
&lt;p&gt;I am going to use C as a solution. I can emulate the SysTick&amp;#39;s functionality I need by using a Timer and a Counter joined with PPI. This prevents me needing to generate any interrupts at all. I assume A is not a solution if you use the softdevice as I imagine that internally this uses the WFE, SEV, WFE sequence&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Strange interaction between SysTick and Sleep</title><link>https://devzone.nordicsemi.com/thread/214539?ContentTypeID=1</link><pubDate>Fri, 11 Oct 2019 09:06:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ff9e20ea-b930-45e6-b382-43eabb4c2618</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;I think the ordering of your sleep routine is the reason why you&amp;#39;re seeing this behavior.&lt;/p&gt;
&lt;p&gt;Try setting sev/wfe/wfe instead.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Read this thread for more detailed info:&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/10424/nrf51422-won-t-sleep/38735#38735"&gt;https://devzone.nordicsemi.com/f/nordic-q-a/10424/nrf51422-won-t-sleep/38735#38735&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Strange interaction between SysTick and Sleep</title><link>https://devzone.nordicsemi.com/thread/214428?ContentTypeID=1</link><pubDate>Thu, 10 Oct 2019 14:23:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2fecb811-0020-4002-8db0-7b582efa709f</guid><dc:creator>Xenoamor</dc:creator><description>&lt;p&gt;I have attached two variants that cause this behaviour. One is based on SysTick and the other on a Timer. It seems to be timing related as tweaking NOP_COUNT determines if the device sleeps properly or not&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/main_5F00_timer.c"&gt;devzone.nordicsemi.com/.../main_5F00_timer.c&lt;/a&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/main_5F00_systick.c"&gt;devzone.nordicsemi.com/.../main_5F00_systick.c&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Strange interaction between SysTick and Sleep</title><link>https://devzone.nordicsemi.com/thread/214168?ContentTypeID=1</link><pubDate>Wed, 09 Oct 2019 12:50:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:185502f8-3544-4670-a8d1-a59fbd831faf</guid><dc:creator>Xenoamor</dc:creator><description>&lt;p&gt;Hi Roy, unfortunately the results are the same&lt;/p&gt;
&lt;p&gt;In some cases I have seen the debugger mess with the sleep modes but this code seems to fail every time with or without it.&lt;br /&gt;&lt;br /&gt;Increasing the timer period to 1000ms from the current 100ms seems to resolve the issue but I can&amp;#39;t imagine why. Unfortunately that&amp;#39;s not a fix I can use for my application&lt;br /&gt;&lt;br /&gt;EDIT: I&amp;#39;ve tested the following interrupts and none appear to be being entered that shouldn&amp;#39;t. The device is also definitely not resetting&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;Reset_Handler &amp;lt;- Not sure as the startup file handles this one
TIMER3_IRQHandler &amp;lt;- Does get entered

// None of the below are entered
NMI_Handler
HardFault_Handler
MemoryManagement_Handler
BusFault_Handler
UsageFault_Handler
SVC_Handler
DebugMon_Handler
PendSV_Handler
SysTick_Handler
POWER_CLOCK_IRQHandler
RADIO_IRQHandler
UARTE0_UART0_IRQHandler
SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQHandler
SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1_IRQHandler
NFCT_IRQHandler
GPIOTE_IRQHandler
SAADC_IRQHandler
TIMER0_IRQHandler
TIMER1_IRQHandler
TIMER2_IRQHandler
RTC0_IRQHandler
TEMP_IRQHandler
RNG_IRQHandler
ECB_IRQHandler
CCM_AAR_IRQHandler
WDT_IRQHandler
RTC1_IRQHandler
QDEC_IRQHandler
COMP_LPCOMP_IRQHandler
SWI0_EGU0_IRQHandler
SWI1_EGU1_IRQHandler
SWI2_EGU2_IRQHandler
SWI3_EGU3_IRQHandler
SWI4_EGU4_IRQHandler
SWI5_EGU5_IRQHandler
TIMER4_IRQHandler
PWM0_IRQHandler
PDM_IRQHandler
MWU_IRQHandler
PWM1_IRQHandler
PWM2_IRQHandler
SPIM2_SPIS2_SPI2_IRQHandler
RTC2_IRQHandler
I2S_IRQHandler
FPU_IRQHandler
USBD_IRQHandler
UARTE1_IRQHandler
QSPI_IRQHandler
CRYPTOCELL_IRQHandler
PWM3_IRQHandler
SPIM3_IRQHandler&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;EDIT2: I&amp;#39;ve tried this on two of our custom boards and also on the PCA10059 Dongle&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Strange interaction between SysTick and Sleep</title><link>https://devzone.nordicsemi.com/thread/214167?ContentTypeID=1</link><pubDate>Wed, 09 Oct 2019 12:36:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ef8a8571-96b6-4bbf-b71d-ef2c95d7c2dc</guid><dc:creator>RoyCreemers</dc:creator><description>&lt;p&gt;Could you try rebooting your device with the debugger detached?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Strange interaction between SysTick and Sleep</title><link>https://devzone.nordicsemi.com/thread/214150?ContentTypeID=1</link><pubDate>Wed, 09 Oct 2019 11:56:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ea24172d-695c-41d8-8a0a-9c209d3cdfb7</guid><dc:creator>Xenoamor</dc:creator><description>&lt;p&gt;Hi H&amp;aring;kon,&lt;br /&gt;&lt;br /&gt;Thanks for the input. I&amp;#39;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)&lt;br /&gt;&lt;br /&gt;I&amp;#39;ve created a simplified, one file variant with a timer and still see the same issue:&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;quot;nrfx_timer.h&amp;quot;
#include &amp;quot;nrf_gpio.h&amp;quot;
#include &amp;quot;nrf_pwr_mgmt.h&amp;quot;

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 &amp;lt; 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(&amp;amp;timer_inst, &amp;amp;timer_config, timer_irq);
    nrfx_timer_extended_compare(&amp;amp;timer_inst, NRF_TIMER_CC_CHANNEL0, 125 * 100, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true); // 100 ms
    nrfx_timer_enable(&amp;amp;timer_inst);
    
    for (;;)
    {
        delay();

        nrfx_timer_disable(&amp;amp;timer_inst); // Disable the timer
        
        nrf_pwr_mgmt_run(); // We would expect to sleep here indefinitely

        nrfx_timer_enable(&amp;amp;timer_inst);

        delay();
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#39;m going to try a completely new build with the latest SDK as this must be an issue with the startup file or something.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;EDIT: I&amp;#39;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&lt;br /&gt;&lt;br /&gt;EDIT2: I&amp;#39;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&amp;#39;ve checked the assembly code for `main()` and this appears to be correct&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Strange interaction between SysTick and Sleep</title><link>https://devzone.nordicsemi.com/thread/214128?ContentTypeID=1</link><pubDate>Wed, 09 Oct 2019 11:02:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a411db71-4c00-492f-8264-d1260c266712</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;The SysTick is suspended when you&amp;#39;re in sleep, so there must be something else that is waking up the&amp;nbsp;CPU. Is that the only component you have in your application now?&lt;/p&gt;
&lt;p&gt;How often&amp;nbsp;do you expect the systick vs. how often it actually occurs?&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Strange interaction between SysTick and Sleep</title><link>https://devzone.nordicsemi.com/thread/213941?ContentTypeID=1</link><pubDate>Tue, 08 Oct 2019 13:27:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c0236c3e-a688-4f39-ae38-865ab9eab2fd</guid><dc:creator>Xenoamor</dc:creator><description>&lt;p&gt;The SysTick stopping when sleeping is actually the functionality I&amp;#39;m after. My issue is more surrounding the fact that SysTick appears to be waking my device from sleep&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Strange interaction between SysTick and Sleep</title><link>https://devzone.nordicsemi.com/thread/213815?ContentTypeID=1</link><pubDate>Tue, 08 Oct 2019 08:28:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e25fd75b-d277-41d7-8f9f-4c9dbd0b4d1b</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The SysTick requires that the CPU is running on nRF52 devices, so when you enter sleep (WFE/WFI), the systick is suspended.&lt;/p&gt;
&lt;p&gt;Using RTC is a&amp;nbsp;recommended option.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>