<?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>Max PWM frequency with 50% duty cycle on nrf52840</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/108813/max-pwm-frequency-with-50-duty-cycle-on-nrf52840</link><description>Hi, 
 What is the maximum PWM frequency one can achieve on the nrf52840 with 50% duty cycle? Sample .dts and c-code in NRF Connect SDK would be appreciated! 
 
 In practice I just want to achieve the highest possible frequency on a GPIO as possible. Is</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 14 Jun 2024 13:55:00 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/108813/max-pwm-frequency-with-50-duty-cycle-on-nrf52840" /><item><title>RE: Max PWM frequency with 50% duty cycle on nrf52840</title><link>https://devzone.nordicsemi.com/thread/488894?ContentTypeID=1</link><pubDate>Fri, 14 Jun 2024 13:55:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8e95dce3-5baf-4fc8-9d6d-9c59bd02f34a</guid><dc:creator>Jonathan E</dc:creator><description>&lt;p&gt;Thank you very much!&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Btw, I got a stable 4 MHz using this earlier through Zephyr&amp;#39;s API:&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&lt;span&gt;#define&lt;/span&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;span&gt;PWM_PERIOD_4MHZ&lt;/span&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;span&gt;PWM_KHZ&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;4000&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;&lt;span&gt;ret&lt;/span&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;span&gt;pwm_set_dt&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;pwm_led0&lt;/span&gt;&lt;span&gt;,&amp;nbsp;&lt;/span&gt;&lt;span&gt;PWM_PERIOD_4MHZ&lt;/span&gt;&lt;span&gt;,&amp;nbsp;&lt;/span&gt;&lt;span&gt;PWM_PERIOD_4MHZ&lt;/span&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;span&gt;/&lt;/span&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;span&gt;2U&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Max PWM frequency with 50% duty cycle on nrf52840</title><link>https://devzone.nordicsemi.com/thread/488878?ContentTypeID=1</link><pubDate>Fri, 14 Jun 2024 13:08:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8fcd061c-0de2-4e06-b8fd-bbc2dbc73f46</guid><dc:creator>Jared</dc:creator><description>&lt;p&gt;Using the Zephyr API I struggle to get anything stable over 1MHz, but using the PWM peripheral directly I&amp;#39;m able to get about 5.33 MHz with 50% duty cycle easily:&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/2538.pastedimage1718370422309v1.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;Code used:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#define PWM_PIN (13UL)


int16_t buf[] = {(1 &amp;lt;&amp;lt; 15) | 1}; // Inverse polarity (bit 15), 

int main(void)
{
  // Start accurate HFCLK (XOSC)
  NRF_CLOCK-&amp;gt;TASKS_HFCLKSTART = 1;
  while (NRF_CLOCK-&amp;gt;EVENTS_HFCLKSTARTED == 0) ;
  NRF_CLOCK-&amp;gt;EVENTS_HFCLKSTARTED = 0;
  
  // Configure PWM_PIN as output, and set it to 0
  NRF_GPIO-&amp;gt;DIRSET = (1 &amp;lt;&amp;lt; PWM_PIN);
  NRF_GPIO-&amp;gt;OUTCLR = (1 &amp;lt;&amp;lt; PWM_PIN);
  
  
  NRF_PWM0-&amp;gt;PRESCALER   = PWM_PRESCALER_PRESCALER_DIV_1; // 1 us
  NRF_PWM0-&amp;gt;PSEL.OUT[0] = PWM_PIN;
  NRF_PWM0-&amp;gt;MODE        = (PWM_MODE_UPDOWN_Up &amp;lt;&amp;lt; PWM_MODE_UPDOWN_Pos);
  NRF_PWM0-&amp;gt;DECODER     = (PWM_DECODER_LOAD_Common       &amp;lt;&amp;lt; PWM_DECODER_LOAD_Pos) | 
                          (PWM_DECODER_MODE_RefreshCount &amp;lt;&amp;lt; PWM_DECODER_MODE_Pos);
  NRF_PWM0-&amp;gt;LOOP        = (PWM_LOOP_CNT_Disabled &amp;lt;&amp;lt; PWM_LOOP_CNT_Pos);
  
  NRF_PWM0-&amp;gt;COUNTERTOP = 3; // 
  
  
  NRF_PWM0-&amp;gt;SEQ[0].CNT = ((sizeof(buf) / sizeof(uint16_t)) &amp;lt;&amp;lt; PWM_SEQ_CNT_CNT_Pos);
  NRF_PWM0-&amp;gt;SEQ[0].ENDDELAY = 0;
  NRF_PWM0-&amp;gt;SEQ[0].PTR = (uint32_t)&amp;amp;buf[0];
  NRF_PWM0-&amp;gt;SEQ[0].REFRESH = 0;
  NRF_PWM0-&amp;gt;SHORTS = 0;
  
  NRF_PWM0-&amp;gt;ENABLE = 1;
  NRF_PWM0-&amp;gt;TASKS_SEQSTART[0] = 1;
  
  while (1) 
  {
  }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;regards&lt;/p&gt;
&lt;p&gt;Jared&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Max PWM frequency with 50% duty cycle on nrf52840</title><link>https://devzone.nordicsemi.com/thread/488715?ContentTypeID=1</link><pubDate>Thu, 13 Jun 2024 14:36:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:607ab877-139e-443c-8e85-2785a41a4a26</guid><dc:creator>Jonathan E</dc:creator><description>&lt;p&gt;5.333 MHz as that is the highest possible&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Max PWM frequency with 50% duty cycle on nrf52840</title><link>https://devzone.nordicsemi.com/thread/488529?ContentTypeID=1</link><pubDate>Wed, 12 Jun 2024 14:06:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1f7efe9f-3e15-4566-bdc4-36aaef965e86</guid><dc:creator>Jared</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Which exact frequency are you trying to set the PWM signal to with the Zephyr API?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;5 MHz?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Max PWM frequency with 50% duty cycle on nrf52840</title><link>https://devzone.nordicsemi.com/thread/488037?ContentTypeID=1</link><pubDate>Mon, 10 Jun 2024 08:01:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2fbcb0e2-66da-4034-b5b9-23a879a0aad1</guid><dc:creator>Jonathan E</dc:creator><description>&lt;p&gt;Is it possible to obtain this with the zephyr API?&lt;/p&gt;
&lt;p&gt;It works for 4 MHz, but not 5,333&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&lt;span&gt;#define&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;PWM_PERIOD_4MHZ&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;PWM_KHZ&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;4000&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;&lt;span&gt;ret&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;pwm_set_dt&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;pwm_led0&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;PWM_PERIOD_4MHZ&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;PWM_PERIOD_4MHZ&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;/&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;2U&lt;/span&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;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;span&gt;I have tried this which doesn&amp;#39;t work:&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;div&gt;
&lt;div&gt;&lt;span&gt;#define&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;PWM_PERIOD_5333KHZ&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;PWM_KHZ&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;16000&lt;/span&gt;&lt;span&gt;/&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;span&gt;ret =&amp;nbsp;&lt;/span&gt;&lt;/span&gt;
&lt;div&gt;
&lt;div&gt;&lt;span&gt;pwm_set_dt&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;pwm_led0&lt;/span&gt;&lt;span&gt;, PWM_PERIOD_5333KHZ&lt;/span&gt;&lt;span&gt;, PWM_PERIOD_5333KHZ&amp;nbsp;&lt;/span&gt;&lt;span&gt;/&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;2U&lt;/span&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;But it does not work&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Max PWM frequency with 50% duty cycle on nrf52840</title><link>https://devzone.nordicsemi.com/thread/472240?ContentTypeID=1</link><pubDate>Tue, 05 Mar 2024 13:42:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f09e07c7-f9a8-4d19-963a-e0a99542f263</guid><dc:creator>Jared</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;You can find code for using the PWM peripheral with nrfx &lt;a href="https://github.com/zephyrproject-rtos/hal_nordic/tree/master/nrfx/samples/src/nrfx_pwm/common_mode"&gt;here&lt;/a&gt;,&lt;/p&gt;
&lt;p&gt;regards&lt;br /&gt;Jared&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Max PWM frequency with 50% duty cycle on nrf52840</title><link>https://devzone.nordicsemi.com/thread/472225?ContentTypeID=1</link><pubDate>Tue, 05 Mar 2024 12:41:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dc061cca-5641-4f6b-b65d-8132232cba74</guid><dc:creator>Torsten Robitzki</dc:creator><description>&lt;p&gt;No, but I would try to have an SPI transfer running all the time.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Max PWM frequency with 50% duty cycle on nrf52840</title><link>https://devzone.nordicsemi.com/thread/472218?ContentTypeID=1</link><pubDate>Tue, 05 Mar 2024 12:06:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8f093093-6406-4809-87b6-28953019349b</guid><dc:creator>Jonathan E</dc:creator><description>&lt;p&gt;Good suggestion. Do you have any idea how this can be achieved?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Max PWM frequency with 50% duty cycle on nrf52840</title><link>https://devzone.nordicsemi.com/thread/472217?ContentTypeID=1</link><pubDate>Tue, 05 Mar 2024 11:55:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7646d82f-4fe4-4b28-908d-ef27a48a92bd</guid><dc:creator>Jonathan E</dc:creator><description>&lt;p&gt;Do you have the code for this?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Max PWM frequency with 50% duty cycle on nrf52840</title><link>https://devzone.nordicsemi.com/thread/471769?ContentTypeID=1</link><pubDate>Fri, 01 Mar 2024 11:56:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:98449bd3-4ff5-4cf9-9694-f5c1f6a8fff8</guid><dc:creator>Jared</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Without testing, I would expect 5 MHz to be possible as the PWM peripheral runs at base clock 16 MHz and the countertop has to be minimum 3 -&amp;gt; 16/3 ~ 5,&lt;/p&gt;
&lt;p&gt;regards&lt;/p&gt;
&lt;p&gt;Jared&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Max PWM frequency with 50% duty cycle on nrf52840</title><link>https://devzone.nordicsemi.com/thread/471566?ContentTypeID=1</link><pubDate>Thu, 29 Feb 2024 14:13:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b5616921-a6e6-4ec0-9a03-c931d249f390</guid><dc:creator>Torsten Robitzki</dc:creator><description>&lt;p&gt;(mis-)using the QSPI CLK line would&amp;nbsp;give you a frequency of 96MHz. Maybe, you can find a way to have a SPI transaction running all the time.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>