<?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>PWM base clock frequency periodically changing</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/99726/pwm-base-clock-frequency-periodically-changing</link><description>I have an Arduino Nano 33 BLE using the nRF52840, and I&amp;#39;m trying to use PWM to output audio. As a test I have the PWM configured to output a 440 Hz sine wave using no prescaler (so 16 MHz clock) and a CLOCKTOP of 400 resulting in a PWM frequency of 40kHz</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 22 May 2023 13:40:03 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/99726/pwm-base-clock-frequency-periodically-changing" /><item><title>RE: PWM base clock frequency periodically changing</title><link>https://devzone.nordicsemi.com/thread/426605?ContentTypeID=1</link><pubDate>Mon, 22 May 2023 13:40:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:be4dc3bf-db84-4180-81fd-cff477a5aa75</guid><dc:creator>Jared</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Ok so it seems like you were able to resolve the issue in the end.&amp;nbsp;&lt;/p&gt;
[quote user="Pi Man"]and the last two values of the previous frame get reused before the buffer is swapped.&amp;nbsp;[/quote]
&lt;p&gt;This might be related to the limitations of the PWM peripheral, from the documentation:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;a href="https://infocenter.nordicsemi.com/topic/ps_nrf52840/pwm.html?cp=5_0_0_5_16_2#concept_pnx_tpw_nr"&gt;Previous compare value is repeated if the PWM period is shorter than the time it takes for the EasyDMA to retrieve from RAM and update the internal compare registers. This is to ensure a glitch-free operation even for very short PWM periods.&lt;/a&gt;&lt;/em&gt;&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: PWM base clock frequency periodically changing</title><link>https://devzone.nordicsemi.com/thread/426399?ContentTypeID=1</link><pubDate>Sun, 21 May 2023 21:16:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7bdae0aa-6bc3-458f-b2f3-3e228a4bb800</guid><dc:creator>Pi Man</dc:creator><description>&lt;p&gt;turns out it probably was being called, but having Serial inside the IRQ freezes the system so I thought it wasn&amp;#39;t being called, so IRQ is working now.&amp;nbsp; I was also able to get rid of the buzzing caused by the buffer not being refreshed fast enough, but I needed to use a tripple buffer and I don&amp;#39;t know why (double buffer got rid of every other jump)&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;void swap_buffer() {
  nrf_pwm_seq_ptr_set(NRF_PWM0, 1, pwm_duties[buffer_id]);
  buffer_id = (buffer_id + 1) % NUM_BUFFERS;
  buffer_flag = true;
}

void pwm_irq_handler(void) {
  nrf_pwm_event_clear(NRF_PWM0, NRF_PWM_EVENT_SEQEND1);
  swap_buffer();
  while(NRF_PWM0-&amp;gt;EVENTS_SEQEND[1]);
}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;here is my code for the tripple buffering, NUM_BUFFERS needs to be set to 3 to remove the buzzing.&amp;nbsp; (the data is filled into the buffer outside of the IRQ)&lt;br /&gt;&lt;br /&gt;though I just realized that this is still basically double buffering because the 3rd buffer is just sitting idle with old data, so now I&amp;#39;m even more confused about why 3 buffers work and 2 don&amp;#39;t.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM base clock frequency periodically changing</title><link>https://devzone.nordicsemi.com/thread/426395?ContentTypeID=1</link><pubDate>Sun, 21 May 2023 03:55:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b6b78636-6196-43e6-965c-05afc2ec1069</guid><dc:creator>Pi Man</dc:creator><description>&lt;p&gt;it seems my code can&amp;#39;t swap the buffer fast enough and the last two values of the previous frame get reused before the buffer is swapped.&amp;nbsp; Unfortunately I can&amp;#39;t seem to get PWM interrupts to work in order to swap the buffer fast enough.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;void pwm_handler(nrfx_pwm_evt_type_t event_type) {
  Serial.println(event_type);
  switch (event_type) {
    case NRFX_PWM_EVT_END_SEQ0:
    case NRFX_PWM_EVT_END_SEQ1:
      get_data();
      break;
  }
}

...

  nrfx_err_t err = nrfx_pwm_init(&amp;amp;p_instance, &amp;amp;p_config, pwm_handler);
  Serial.println(err);
  nrfx_pwm_simple_playback(&amp;amp;p_instance, &amp;amp;p_sequence, 1, NRFX_PWM_FLAG_LOOP | NRFX_PWM_FLAG_SIGNAL_END_SEQ0 | NRFX_PWM_FLAG_SIGNAL_END_SEQ1);&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;`pwm_handler` is never called&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM base clock frequency periodically changing</title><link>https://devzone.nordicsemi.com/thread/426394?ContentTypeID=1</link><pubDate>Sat, 20 May 2023 20:34:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:46c2c9d5-7a63-4c8d-ada4-12f644de347a</guid><dc:creator>Pi Man</dc:creator><description>&lt;p&gt;For the buzzing, I set the sin wave frequency to 400Hz to get exactly 100 PWM cycles per sin wave cycle and I set the buffer to 25 duties to get exactly 4 buffer refreshes per sine wave cycle.&amp;nbsp; The result is what is in the attached image, you can very clearly see a voltage jump every time the PWM loop restarts.&amp;nbsp; &lt;br /&gt;(edit: I also was only using sequence 1 and not sequence 0 in this test)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;img style="max-height:240px;max-width:180px;" alt="Oscilloscope picture showing a voltage jump every buffer refresh" src="https://devzone.nordicsemi.com/resized-image/__size/360x480/__key/communityserver-discussions-components-files/4/20230520_5F00_132556_5B00_1_5D00_.jpg" /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM base clock frequency periodically changing</title><link>https://devzone.nordicsemi.com/thread/426393?ContentTypeID=1</link><pubDate>Sat, 20 May 2023 19:43:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f023a4a2-957f-4d37-a3a6-99229bb84d10</guid><dc:creator>Pi Man</dc:creator><description>&lt;p&gt;turns out the frequency shifting was being caused by floating point error in my time tracking, added the line `if (t &amp;gt; 1) t--;` right underneath `t += dt;` and the frequency shifting went away.&amp;nbsp; for the buzzing, setting the PWM duty cycle buffer size to 4096(*4) removed the buzzing, but that puts an audio delay of 0.1 seconds which is more than I would like.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM base clock frequency periodically changing</title><link>https://devzone.nordicsemi.com/thread/426013?ContentTypeID=1</link><pubDate>Wed, 17 May 2023 00:43:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3a9ef9a1-ae0b-41d5-8bde-4d2e7e307850</guid><dc:creator>Pi Man</dc:creator><description>&lt;p&gt;I am not using the bluetooth or wifi, I&amp;#39;m not sure what the &amp;quot;Softdevice&amp;quot; is, what I posted is my entire code.&lt;br /&gt;&lt;br /&gt;the only other peripheral I plan to use later is the ADC&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM base clock frequency periodically changing</title><link>https://devzone.nordicsemi.com/thread/425563?ContentTypeID=1</link><pubDate>Mon, 15 May 2023 10:21:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d2250151-e7a2-4a52-af0c-f7c580b7215a</guid><dc:creator>Jared</dc:creator><description>&lt;p&gt;Hi there,&lt;/p&gt;
&lt;p&gt;Are you using the Softdevice or radio as well? Is any other peripheral used in your application?&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></channel></rss>