<?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>The finer points of PWM on nRF52 using two chained buffers</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/123249/the-finer-points-of-pwm-on-nrf52-using-two-chained-buffers</link><description>I&amp;#39;m looking into a rare issue on the BBC micro:bit V2 which uses the nRF52833 as its (application) microcontroller. I used to be familiar with some CPUs from the 80s but my low-level microcontroller knowledge is very minimal. 
 There&amp;#39;s a two buffer PWM</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 05 Aug 2025 08:13:14 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/123249/the-finer-points-of-pwm-on-nrf52-using-two-chained-buffers" /><item><title>RE: The finer points of PWM on nRF52 using two chained buffers</title><link>https://devzone.nordicsemi.com/thread/544566?ContentTypeID=1</link><pubDate>Tue, 05 Aug 2025 08:13:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a3cb2637-e711-47a5-a78f-5d28b1e4eb79</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Not sure I understand fully, but I guess you can check out the PWM hardware chapter on what may be possible:&lt;br /&gt;&lt;a href="https://docs.nordicsemi.com/bundle/ps_nrf52833/page/pwm.html"&gt;https://docs.nordicsemi.com/bundle/ps_nrf52833/page/pwm.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Kenneth&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: The finer points of PWM on nRF52 using two chained buffers</title><link>https://devzone.nordicsemi.com/thread/544367?ContentTypeID=1</link><pubDate>Fri, 01 Aug 2025 21:25:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:31dbde4c-f4fc-41e1-94e7-2da9b258805a</guid><dc:creator>kevinjwalters</dc:creator><description>&lt;p&gt;I&amp;#39;ve now got a problem where a few pulses pop out of a buffer after we would like it to have stopped. I think this is due to a late interrupt. Some of the timings I have seen show little variability and happen to match exactly the duration of both buffers. It seems like a very advanced feature but does the PWM peripheral inside the nRF52 chips have anything that can calculate an imminent second event (in this case 10us later) and then coalesce the two into one slightly later interrupt? This isn&amp;#39;t happening all the time, perhaps 1 in 2000 uses of the PWM.&lt;/p&gt;
&lt;p&gt;The comments in the code suggest there&amp;#39;s no way to do a reliable STOP after the current buffer has been processed when using &lt;code&gt;LOOP&lt;/code&gt; and &lt;code&gt;SHORTS&lt;/code&gt;. Is that true? Something like a &lt;code&gt;PWM.EVENT_STOP_AFTER_CURRENT_BUFFER = 1&lt;/code&gt; type thing?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: The finer points of PWM on nRF52 using two chained buffers</title><link>https://devzone.nordicsemi.com/thread/544241?ContentTypeID=1</link><pubDate>Thu, 31 Jul 2025 15:09:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e96f02a2-a6ee-43f0-8939-41bb2a480a7f</guid><dc:creator>kevinjwalters</dc:creator><description>&lt;p&gt;I&amp;#39;ve got rid of one issue but another one lurks. I&amp;#39;m getting an&amp;nbsp;&lt;code&gt;EVENT_SEQEND[0]&lt;/code&gt; interrupt as the PWM is started back up occasionally. That&amp;#39;s clearly bogus because there&amp;#39;s 128 samples to process at 800kHz so first one should arrive 160us later.&lt;/p&gt;
&lt;p&gt;I tried adding&amp;nbsp;&lt;code&gt;ENABLE = 0&lt;/code&gt;&amp;nbsp;to the stopping code and that didn&amp;#39;t help with this spurious interrupt. I think I may have to implement a workaround in the code to ignore interrupts which happen when the code doesn&amp;#39;t think they should.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: The finer points of PWM on nRF52 using two chained buffers</title><link>https://devzone.nordicsemi.com/thread/543963?ContentTypeID=1</link><pubDate>Tue, 29 Jul 2025 13:31:24 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0a628f9b-f18a-4393-84f9-b7fcde7b9109</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;It sounds like you&amp;nbsp;may be on the right track. Hopefully this is it. Thank you for the update.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: The finer points of PWM on nRF52 using two chained buffers</title><link>https://devzone.nordicsemi.com/thread/543958?ContentTypeID=1</link><pubDate>Tue, 29 Jul 2025 13:06:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:af68299b-f6a1-450c-897f-24cd2a860401</guid><dc:creator>kevinjwalters</dc:creator><description>&lt;p&gt;I&amp;#39;ve been working my way through a few things. A brief bit of testing of &lt;code&gt;PWM.ENABLE = 0&lt;/code&gt; after the stop code seems to do the job but the answer to this mystery may be in the SDK library code. I see the &lt;a href="https://github.com/NordicSemiconductor/nrfx/blob/db85149b1871aa09e8ff8ed240a177e8406ead58/drivers/src/nrfx_pwm.c#L443-L473"&gt;implementation&lt;/a&gt; of&amp;nbsp;&lt;code&gt;nrfx_pwm_stop()&lt;/code&gt;&amp;nbsp;includes a very relevant comment and additional hw register setting.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;    // Deactivate shortcuts before triggering the STOP task, otherwise the PWM
    // could be immediately started again if the LOOPSDONE event occurred in
    // the same peripheral clock cycle as the STOP task was triggered.
    nrfy_pwm_shorts_set(p_instance-&amp;gt;p_reg, 0);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I had already tried setting &lt;code&gt;SHORTS&lt;/code&gt; to 0 but I was doing it &lt;em&gt;after&lt;/em&gt; the &lt;code&gt;STOP&lt;/code&gt; setting. That improved things but wasn&amp;#39;t perfect. I&amp;#39;ll re-order them and do some more testing.&lt;/p&gt;
&lt;p&gt;The library code also does an &lt;code&gt;ENABLE = 0&lt;/code&gt; if waiting for completition is requested.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: The finer points of PWM on nRF52 using two chained buffers</title><link>https://devzone.nordicsemi.com/thread/543817?ContentTypeID=1</link><pubDate>Mon, 28 Jul 2025 17:45:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6c06bf34-8dc7-4f5f-a56d-8c120b2bcc05</guid><dc:creator>kevinjwalters</dc:creator><description>&lt;p&gt;This is on &lt;code&gt;PWM2&lt;/code&gt;, btw.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: The finer points of PWM on nRF52 using two chained buffers</title><link>https://devzone.nordicsemi.com/thread/543811?ContentTypeID=1</link><pubDate>Mon, 28 Jul 2025 17:30:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3227c39a-3f2b-458c-be7b-7b103c29e6a9</guid><dc:creator>kevinjwalters</dc:creator><description>&lt;p&gt;The code definitely passes the code that checks for the stop. From &lt;a href="https://github.com/lancaster-university/codal-microbit-v2/issues/475#issuecomment-3128046276"&gt;some testing today&lt;/a&gt; I&amp;#39;m become more certain that &lt;code&gt;PWM.STOPS = 1&lt;/code&gt; is failing very occasionally, perhaps 1 in 50000.&lt;/p&gt;
&lt;p&gt;The complete code is a bit hairy because it&amp;#39;s part of a streaming library but here&amp;#39;s the stop code:&amp;nbsp;&lt;a href="https://github.com/kevinjwalters/codal-nrf52/blob/6360d434874979218e49d0202d8bfd108ae47061/source/NRF52PWM.cpp#L271-L272"&gt;https://github.com/kevinjwalters/codal-nrf52/blob/6360d434874979218e49d0202d8bfd108ae47061/source/NRF52PWM.cpp#L271-L272&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;For the &lt;a href="https://github.com/lancaster-university/codal-microbit-v2/issues/475"&gt;bug&lt;/a&gt; I&amp;#39;m interested in its being used to generate the WS2812B protocol for 60 RGB pixels. I think there&amp;#39;s actually a few bugs which contribute to that which makes it more confusing.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: The finer points of PWM on nRF52 using two chained buffers</title><link>https://devzone.nordicsemi.com/thread/543732?ContentTypeID=1</link><pubDate>Mon, 28 Jul 2025 12:15:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:12fc696e-616c-4158-ad01-3c91305c3592</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;The PWM can be stopped at any time,&amp;nbsp;also in the middle of a sequence. From the same PWM chapter you linked to:&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/pastedimage1753704019581v1.png" alt=" " /&gt;&lt;/p&gt;
[quote user=""]&lt;p&gt;The &lt;a href="https://github.com/lancaster-university/codal-nrf52/blob/bdfa1eca85dbbd6fc8aaf5fc4c3fd00c1f65762d/source/NRF52PWM.cpp#L229"&gt;code&lt;/a&gt; I&amp;#39;m looking at waits for &lt;code&gt;PWM.EVENTS_STOPPED&lt;/code&gt; to be non zero to indicate the stop has taken effect.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;[/quote]
&lt;p&gt;Are you able to confirm if the program always exits this loop? What is the symptom of the problem you are seeing?&lt;/p&gt;
[quote user=""]Is that saying this &lt;em&gt;definitely only&lt;/em&gt; occurs for &lt;code&gt;SEQ[1].CNT = 1?&amp;nbsp;&lt;/code&gt;I am far from certain but I think this could explain some issues I&amp;#39;m seeing if it could occur for sequences of length 128 or 8.[/quote]
&lt;p&gt;Yes, that is correct, SEQ[1].CNT must be equal to 1 for the conditions of this&amp;nbsp;errata to be met so it does not appear to be relevant to your case.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Vidar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>