<?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>wake from GPIO using SENSE without a race condition</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/99599/wake-from-gpio-using-sense-without-a-race-condition</link><description>NCS2.3.0, NRF52833 
 I use a button to wake my product. The same button is used to turn off. The button is tied low with a 100K resistor and pulled high when pressed. 
 I found that very occasionally, when I turn the device off, it later fails to wake</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 12 May 2023 07:05:32 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/99599/wake-from-gpio-using-sense-without-a-race-condition" /><item><title>RE: wake from GPIO using SENSE without a race condition</title><link>https://devzone.nordicsemi.com/thread/425228?ContentTypeID=1</link><pubDate>Fri, 12 May 2023 07:05:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:41129a19-d8bc-4326-8c14-09ec6dfe7053</guid><dc:creator>Jason</dc:creator><description>&lt;p&gt;I think my PWM code caused this issue, so I&amp;#39;m going to close this support request.&lt;/p&gt;
&lt;p&gt;THANK YOU!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: wake from GPIO using SENSE without a race condition</title><link>https://devzone.nordicsemi.com/thread/425197?ContentTypeID=1</link><pubDate>Thu, 11 May 2023 19:18:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d44f1781-8b95-4163-9883-1b8acb9f990b</guid><dc:creator>Jason</dc:creator><description>&lt;div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;
&lt;div&gt;&lt;span&gt;I recently changed the function I use to stop PWM prior to shutdown. I think it could get stuck in the do-while loop waiting for&amp;nbsp;EVENTS_STOPPED but I&amp;#39;m not sure if, or how, this could happen. &lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;I&amp;#39;m not sure if this is the correct way to write this function at all. &lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;Could you take a look please?&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;void nrf_pwm_stop()
{
    if (NRF_PWM0-&amp;gt;ENABLE == (PWM_ENABLE_ENABLE_Enabled &amp;lt;&amp;lt; PWM_ENABLE_ENABLE_Pos))
    {
        NRF_PWM0-&amp;gt;TASKS_STOP = PWM_TASKS_STOP_TASKS_STOP_Trigger &amp;lt;&amp;lt; PWM_TASKS_STOP_TASKS_STOP_Pos;
        NRF_PWM1-&amp;gt;TASKS_STOP = PWM_TASKS_STOP_TASKS_STOP_Trigger &amp;lt;&amp;lt; PWM_TASKS_STOP_TASKS_STOP_Pos;
        NRF_PWM2-&amp;gt;TASKS_STOP = PWM_TASKS_STOP_TASKS_STOP_Trigger &amp;lt;&amp;lt; PWM_TASKS_STOP_TASKS_STOP_Pos;
        NRF_PWM3-&amp;gt;TASKS_STOP = PWM_TASKS_STOP_TASKS_STOP_Trigger &amp;lt;&amp;lt; PWM_TASKS_STOP_TASKS_STOP_Pos;

        NRF_PWM0-&amp;gt;EVENTS_STOPPED = 0;
        NRF_PWM1-&amp;gt;EVENTS_STOPPED = 0;
        NRF_PWM2-&amp;gt;EVENTS_STOPPED = 0;
        NRF_PWM3-&amp;gt;EVENTS_STOPPED = 0;

        do
        {

        } 
        while (
            (0 == NRF_PWM0-&amp;gt;EVENTS_STOPPED) ||
            (0 == NRF_PWM1-&amp;gt;EVENTS_STOPPED) ||
            (0 == NRF_PWM2-&amp;gt;EVENTS_STOPPED) ||
            (0 == NRF_PWM3-&amp;gt;EVENTS_STOPPED));
        
        for (int ii = 0; ii &amp;lt; 4; ii++)
        {
            NRF_PWM0-&amp;gt;PSEL.OUT[ii] = (PWM_PSEL_OUT_CONNECT_Disconnected &amp;lt;&amp;lt; PWM_PSEL_OUT_CONNECT_Pos);
            NRF_PWM1-&amp;gt;PSEL.OUT[ii] = (PWM_PSEL_OUT_CONNECT_Disconnected &amp;lt;&amp;lt; PWM_PSEL_OUT_CONNECT_Pos);
            NRF_PWM2-&amp;gt;PSEL.OUT[ii] = (PWM_PSEL_OUT_CONNECT_Disconnected &amp;lt;&amp;lt; PWM_PSEL_OUT_CONNECT_Pos);
            NRF_PWM3-&amp;gt;PSEL.OUT[ii] = (PWM_PSEL_OUT_CONNECT_Disconnected &amp;lt;&amp;lt; PWM_PSEL_OUT_CONNECT_Pos);
        }

        NRF_PWM0-&amp;gt;ENABLE = (PWM_ENABLE_ENABLE_Disabled &amp;lt;&amp;lt; PWM_ENABLE_ENABLE_Pos);
        NRF_PWM1-&amp;gt;ENABLE = (PWM_ENABLE_ENABLE_Disabled &amp;lt;&amp;lt; PWM_ENABLE_ENABLE_Pos);
        NRF_PWM2-&amp;gt;ENABLE = (PWM_ENABLE_ENABLE_Disabled &amp;lt;&amp;lt; PWM_ENABLE_ENABLE_Pos);
        NRF_PWM3-&amp;gt;ENABLE = (PWM_ENABLE_ENABLE_Disabled &amp;lt;&amp;lt; PWM_ENABLE_ENABLE_Pos);
    }

}&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;I&amp;#39;ve since changed it to do each channel individually, *EDIT* and I noticed that I should clear EVENTS_STOPPED flag *before* doing TASKS_STOP. I think this might have been the race condition. &lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;The bug happens so rarely that I&amp;#39;ve not had it all day, so I&amp;#39;m trying to guess what code could be the culprit.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;void nrf_pwm_stop_x(NRF_PWM_Type * PWMx)
{
    if (PWMx-&amp;gt;ENABLE == (PWM_ENABLE_ENABLE_Enabled &amp;lt;&amp;lt; PWM_ENABLE_ENABLE_Pos))
    {
        PWMx-&amp;gt;EVENTS_STOPPED = 0;   // Need to clear this *before* TASKS_STOP!
        PWMx-&amp;gt;TASKS_STOP = PWM_TASKS_STOP_TASKS_STOP_Trigger &amp;lt;&amp;lt; PWM_TASKS_STOP_TASKS_STOP_Pos;


        while (0 == PWMx-&amp;gt;EVENTS_STOPPED);

        for (int ii = 0; ii &amp;lt; 4; ii++)
        {
            PWMx-&amp;gt;PSEL.OUT[ii] = (PWM_PSEL_OUT_CONNECT_Disconnected &amp;lt;&amp;lt; PWM_PSEL_OUT_CONNECT_Pos);
        }

        PWMx-&amp;gt;ENABLE = (PWM_ENABLE_ENABLE_Disabled &amp;lt;&amp;lt; PWM_ENABLE_ENABLE_Pos);
    }
}

//--------------------------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------------------------

void nrf_pwm_stop()
{
    nrf_pwm_stop_x(NRF_PWM0);
    nrf_pwm_stop_x(NRF_PWM1);
    nrf_pwm_stop_x(NRF_PWM2);
    nrf_pwm_stop_x(NRF_PWM3);
}&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: wake from GPIO using SENSE without a race condition</title><link>https://devzone.nordicsemi.com/thread/425030?ContentTypeID=1</link><pubDate>Thu, 11 May 2023 08:06:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a1025329-d61b-47e3-9903-5df6dde50905</guid><dc:creator>Jason</dc:creator><description>&lt;p&gt;I&amp;#39;ve added an &amp;quot;attach&amp;quot; configuration so I&amp;#39;m trying to trigger the bug to see if I can get it in the debugger.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1683792352047v1.png" alt=" " /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: wake from GPIO using SENSE without a race condition</title><link>https://devzone.nordicsemi.com/thread/425028?ContentTypeID=1</link><pubDate>Thu, 11 May 2023 07:40:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:de8b5541-87ef-433b-80af-9acc8925a8c0</guid><dc:creator>Jason</dc:creator><description>&lt;p&gt;So the bug happened again last night, after changing my code back.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;void EnterSystemOff(void)
{
	LOG_INF(&amp;quot;Entering system off; press BUTTON to restart&amp;quot;);

// #ifdef thread0_id
	common.run_button_thread = false;
	// k_msleep(100);
	// k_thread_abort(thread0_id);
	// k_thread_abort(thread1_id);
// #endif

	LIS2DW12_power_down();

	ClearRGBLEDs();
	nrf_pwm_play_shutdown();
	
	nrf_pwm_stop();
	all_gpios_off();

	do
	{
		nrf_gpio_cfg_sense_set(MY_BUTTON_PIN, NRF_GPIO_PIN_SENSE_HIGH);
		pm_state_force(0u, &amp;amp;(struct pm_state_info){PM_STATE_SOFT_OFF, 0, 0});

		/* Now we need to go to sleep. This will let the idle thread run and
		* the pm subsystem will use the forced state. To confirm that the
		* forced state is used, lets set the same timeout used previously.
		*/
		k_sleep(K_SECONDS(2));

		LOG_INF(&amp;quot;ERROR: System off failed - resetting&amp;quot;);
		// We should never get here because the button should cause a reset which restarts the system.
		// Something serious has gone wrong. so lets try to reset the system. 
		sys_reboot(SYS_REBOOT_COLD);
		k_sleep(K_SECONDS(2));
		
		//keep trying 
	} 
	while (true);
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I had the power profiler attached so I could see it was ~700uA, the debugger was not hooked up (SWDIO and SWDCLK, Vdetect and GND). So it was not fully asleep, it should have been ~3uA. ~700uA normally means HFCLOCK was still running I think. And there was a slight increase when I pressed the button, which I think was the 100K resistor to GND.&lt;/p&gt;
&lt;p&gt;I wanted to attach the debugger but I don&amp;#39;t know how to do it without NCS erasing and reflashing the firmware. Do you know&amp;nbsp;how I can do this?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If I could attach the debugger when I see the problem then I might be able to figure out what&amp;#39;s going on.&lt;/p&gt;
&lt;p&gt;Can you tell me if this NCS option is the same as pressing F5 or choosing the&amp;nbsp;&lt;span style="font-family:inherit;"&gt;menu option&amp;nbsp;&lt;/span&gt;&lt;span style="font-family:inherit;"&gt;Run-&amp;gt;Start Debugger?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/ice_5F00_screenshot_5F00_20230511_2D00_092357.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;I think I need to make a new configuration but I don&amp;#39;t know how to do it.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I think it would be better if there was a checkbox to decide if it should erase and reflash when launching the debugger rather than always doing it.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/ice_5F00_screenshot_5F00_20230511_2D00_092546.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve also noticed that after I&amp;#39;ve used the debugger I see strange crashes. I think it might be the breakpoints persisting outside the debug session. This is with a Jlink. And it still happens if I erase the board, then reflash, then&amp;nbsp;reset the board. It still crashes. I have to run with the debugger again, clear the breakpoints and exit cleanly.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I am not sure&amp;nbsp;if this is to be expected but I don&amp;#39;t recall these problems with segger embedded Studio.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: wake from GPIO using SENSE without a race condition</title><link>https://devzone.nordicsemi.com/thread/424954?ContentTypeID=1</link><pubDate>Wed, 10 May 2023 16:03:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:611483ae-6996-4bc2-89a8-56c9e964ba1f</guid><dc:creator>Jason</dc:creator><description>&lt;p&gt;Thank you for clearing that up for me.&lt;/p&gt;
&lt;p&gt;I misunderstood this section on page 142 of the nrf52833 product spec.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1683734070988v2.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve noticed this bug only recently but it&amp;#39;s happened 3 times in 2 weeks. So when I reread the product spec section, I thought it might explain the failure to wake.I&amp;#39;ve seen it on several PCBs so it&amp;#39;s not appeared because of a hardware issue.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Until today, my code for&amp;nbsp;EnterSystemOff() did the same as the example you linked, it had a call to&amp;nbsp;nrf_gpio_cfg_sense_set()&amp;nbsp; immediately before calling&amp;nbsp;pm_state_force.&lt;/p&gt;
&lt;p&gt;My other guess was that the bug could be caused by zephyr initialising the button pin because of it&amp;#39;s mention in the .dts file and removing the SENSE configuration and that somehow resulting in the bug. So today, I removed the mention of the button from the .dts file and changed my code to only interact with buttons using the nrf_gpio_ functions.&lt;/p&gt;
&lt;p&gt;My dts file had his section before&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;	buttons {
		compatible = &amp;quot;gpio-keys&amp;quot;;
		button0: button_0 {
			gpios = &amp;lt;&amp;amp;gpio0 17 ( GPIO_ACTIVE_HIGH)&amp;gt;;
			label = &amp;quot;Push button switch 0&amp;quot;;
		};
	};&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#39;m not sure what else to suspect so maybe I should look through all the changes I&amp;#39;ve made recently to see if there&amp;#39;s any thing that might explain this.&lt;/p&gt;
&lt;p&gt;If you have any suggestions I&amp;#39;d greatly appreciate&amp;nbsp;it.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: wake from GPIO using SENSE without a race condition</title><link>https://devzone.nordicsemi.com/thread/424891?ContentTypeID=1</link><pubDate>Wed, 10 May 2023 13:35:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cd3e76e7-5671-427f-8acf-6691eec32f1a</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
[quote user=""]I read the product spec again and concluded that I must ensure the button is not pressed when I call pm_stat_force or the SENSE wont work. (please correct me if I&amp;#39;m wrong)[/quote]
&lt;p&gt;Where did you read this? There is no such fundamental limit in the IC, but there the&amp;nbsp;DK buttons and LEDs module disabled interrupt&amp;#39;s while a button is pressed, as described in&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/95303/ncs1-9-1-system-off-mode-abnormal-problem/407989"&gt;this post&lt;/a&gt;. I do not see any indication you are using that though?&lt;/p&gt;
[quote user=""]I can&amp;#39;t see how to configure SENSE on the gpio in the zephyr .dts file, so I use the&amp;nbsp;nrf_gpio functions instead and I don&amp;#39;t configure any of my gpios in the .dts file.[/quote]
&lt;p&gt;There is nothing wrong with that approach. Alternatively, you can use low-level function just to enable sense, as is done in the&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.3.0/zephyr/samples/boards/nrf/system_off/README.html"&gt;nRF5x System Off demo&lt;/a&gt;:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;	nrf_gpio_cfg_sense_set(NRF_DT_GPIOS_TO_PSEL(DT_ALIAS(sw0), gpios),
			       NRF_GPIO_PIN_SENSE_LOW);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;This demonstrate wake-up from a button, and that approach should work regardless if you are pressing the button when entering system OFF mode or not.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>