<?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>How to have a beep after another in a loop with PWM?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/89737/how-to-have-a-beep-after-another-in-a-loop-with-pwm</link><description>Hi, 
 I&amp;#39;ve implemented a buzzer service with the following code: 
 
 m_top is 1880 such that I get a C note/tone at the buzzer. 
 I also use the following evt handler function: 
 
 
 The service is very simple, if I write a 0x01 to buzzer characteristic</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Sun, 18 Sep 2022 09:31:03 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/89737/how-to-have-a-beep-after-another-in-a-loop-with-pwm" /><item><title>RE: How to have a beep after another in a loop with PWM?</title><link>https://devzone.nordicsemi.com/thread/386744?ContentTypeID=1</link><pubDate>Sun, 18 Sep 2022 09:31:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5f2357df-4615-4de4-a0a1-17469c4136c8</guid><dc:creator>builder01</dc:creator><description>&lt;p&gt;I ended up using this library to solve my problem:&lt;br /&gt;&lt;a href="https://sourceforge.net/p/nrf52-pwm-audio/"&gt;sourceforge.net/.../&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to have a beep after another in a loop with PWM?</title><link>https://devzone.nordicsemi.com/thread/376526?ContentTypeID=1</link><pubDate>Tue, 12 Jul 2022 05:28:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3d6401f7-1ad7-40fe-974a-8e7b0364ab51</guid><dc:creator>builder01</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Based on your suggestion I attempted the following:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static void buzzer_write_handler(ble_buzzer_service_t * p_buzzer_service, ble_buzzer_new_status_t * new_buzzer_status)
{
    
    buzzer_new_status_t new_status = new_buzzer_status-&amp;gt;params_command.status_command_data;
    //ble_buzzer_service_t * p_buzz_serv = p_buzzer_service;
    //uint8_t const * p_data

    nrf_pwm_sequence_t const seq0 = 
    {
      .values.p_common = sequence_values,
      .length          = NRF_PWM_VALUES_LENGTH(sequence_values),
      .repeats         = 0x00,
      .end_delay       = 0,
    };

    NRF_LOG_INFO(&amp;quot;write handler&amp;quot;);
    while(new_status.p_data[0] == 0x01)
    {
      NRF_LOG_INFO(&amp;quot;Buzzing!&amp;quot;);
      (void)nrfx_pwm_simple_playback(&amp;amp;m_pwm0, &amp;amp;seq0, 3, NRFX_PWM_FLAG_STOP);
      nrf_delay_ms(500);
      
      //read p_data gain
      //new_status = new_buzzer_status-&amp;gt;params_command.status_command_data;
    }
    
    (void)nrfx_pwm_stop(&amp;amp;m_pwm0, false);
    NRF_LOG_INFO(&amp;quot;Quiet!&amp;quot;);
    
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;However, it doesn&amp;#39;t work because, at least within the scope of the while-loop, the new_status.pdata[0] will not change.&lt;/p&gt;
&lt;p&gt;The question is: how can I make the application pass the new value I&amp;#39;ve written to the characteristic when it is inside this while-loop playing the PWM tone?&lt;/p&gt;
&lt;p&gt;I tried calling the buzzer_write_handler() again within the while-loop, but that didn&amp;#39;t work either.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to have a beep after another in a loop with PWM?</title><link>https://devzone.nordicsemi.com/thread/376482?ContentTypeID=1</link><pubDate>Mon, 11 Jul 2022 15:05:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1f1b50f2-0f75-49a0-af6e-b4ab1269659d</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;I don&amp;#39;t see how your while(1) will ever exit? You need some logic to exit that while loop when you want to stop the pwm.&lt;/p&gt;
&lt;p&gt;Kenneth&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to have a beep after another in a loop with PWM?</title><link>https://devzone.nordicsemi.com/thread/376480?ContentTypeID=1</link><pubDate>Mon, 11 Jul 2022 15:00:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:79db2b9b-18f0-4db7-8bac-20da2b7c5076</guid><dc:creator>builder01</dc:creator><description>&lt;p&gt;Hi Kenneth,&lt;/p&gt;
&lt;p&gt;Thanks for your answer.&lt;/p&gt;
&lt;p&gt;Setting the NRF_LOG_DEFERRED to 0 made the NRF_LOG_INFO() call work.&lt;/p&gt;
&lt;p&gt;But also, as I expected, it&amp;#39;s basically printing the NRF_LOG_INFO(&amp;quot;Buzzing! within pwm_play&amp;quot;) calls within the while-loop. If I write 0x00 to the characteristic, the function pwm_stop() is not called, because the application is stuck in the while-loop of the pwm_play() function.&lt;/p&gt;
&lt;p&gt;It sounds to me that disabling the PWM won&amp;#39;t work as the application insists on staying within the pwm_play() function.&lt;/p&gt;
&lt;p&gt;Perhaps I&amp;#39;m misunderstanding something fundamental and this is the actual expected behavior.&lt;/p&gt;
&lt;p&gt;If that&amp;#39;s the case, do you care to suggest an alternative implementation to my code within the pwm_play function?&lt;br /&gt;&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;//..
  while(1){
    NRF_LOG_INFO(&amp;quot;Buzzing! within pwm_play&amp;quot;);
    (void)nrfx_pwm_simple_playback(&amp;amp;m_pwm0, &amp;amp;seq0, 3, NRFX_PWM_FLAG_STOP);
    nrf_delay_ms(500);
  }
//..&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;As I said, I know I can work around it by removing everything but the ..._simple_playback() function and use the NRFX_PWM_FLAG_LOOP, but that plays a constant tone (no silences in between beeps).&lt;/p&gt;
&lt;p&gt;Best,&lt;/p&gt;
&lt;p&gt;Ernesto&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to have a beep after another in a loop with PWM?</title><link>https://devzone.nordicsemi.com/thread/376360?ContentTypeID=1</link><pubDate>Mon, 11 Jul 2022 08:44:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f8dbe7c1-0913-4649-ba4f-d14c6620bcc9</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;It may be that you should change&amp;nbsp;NRF_LOG_DEFERRED to 0 in sdk_config.h to ensure that it will print log statement directly when calling NRF_LOG() api instead of waiting for idle. Hopefully you should then be able to&amp;nbsp;enable better&amp;nbsp;debugging for you to find the problem.&lt;/p&gt;
&lt;p&gt;Kenneth&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to have a beep after another in a loop with PWM?</title><link>https://devzone.nordicsemi.com/thread/376290?ContentTypeID=1</link><pubDate>Sat, 09 Jul 2022 11:13:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cdeedd16-1098-4a90-b60e-4044e436f9d2</guid><dc:creator>builder01</dc:creator><description>&lt;p&gt;HI Kenneth,&lt;/p&gt;
&lt;p&gt;What I mean with &amp;quot;it&amp;#39;s stuck in the for- or while-loop&amp;quot; is that it&amp;#39;s stuck in the loop I implemented within the pwm_play() function. In other wors, the server remains beeping forever according to this while loop and it&amp;#39;ll never stop, not even if I write 0x00 to the characteristic.&lt;/p&gt;
&lt;p&gt;Right now, I have it implemented like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt; //..
  while(1){
    NRF_LOG_INFO(&amp;quot;Buzzing! within pwm_play&amp;quot;);
    (void)nrfx_pwm_simple_playback(&amp;amp;m_pwm0, &amp;amp;seq0, 3, NRFX_PWM_FLAG_STOP);
    nrf_delay_ms(500);
  }
  //..&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;From debugging, all I see is that, after writing 0x01, the server starts buzzing, but and it goes to the breakpoint I set at the NRF_LOG_INFO() call. However, it never goes to the pwm_stop() function I implemented when I write 0x00.&lt;/p&gt;
&lt;p&gt;What I also find interesting is that the NRF_LOG_INFO doesn&amp;#39;t print when running the debugging mode. Could it be because it needs a delay after the NRF_LOG_INFO() call so that it&amp;#39;s able to print the text? I&amp;#39;ve tried 100ms and it still woudn&amp;#39;t print.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Ernesto&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to have a beep after another in a loop with PWM?</title><link>https://devzone.nordicsemi.com/thread/376124?ContentTypeID=1</link><pubDate>Fri, 08 Jul 2022 07:47:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ccf599d7-b2d6-445e-af62-84e32fb33d22</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;Hi Ernesto,&lt;/p&gt;
&lt;p&gt;It&amp;#39;s not clear to me where the code is hanging when you write &amp;quot;&lt;span&gt;Somehow, the sever application gets stuck in the for or while loop. I write 0x00 to the characteristic&lt;/span&gt;&amp;quot;, it should be possible to find where the code is hanging by debugging.&lt;/p&gt;
&lt;p&gt;If you just want to forcefully stop the PWM you can always disable it? E.g. by nrfx_pwm_uninit(). When disabled the pin used by PWM will fall back to the PIN_CNF[] for the pin in question.&lt;/p&gt;
&lt;p&gt;Alterntively you may look into disabling and enabling the PWM directly without using the nrfx_pwm driver. E.g. if you want to disable PWM, but still allow calling&amp;nbsp;nrfx_pwm_simple_playback(), but calling it should have no effect, then you may simply consider calling:&lt;/p&gt;
&lt;p&gt;NRF_PWM0-&amp;gt;ENABLE = 0; // stop PWM (calling&amp;nbsp;&lt;span&gt;nrfx_pwm_simple_playback() will have no effect)&lt;/span&gt;&lt;br /&gt;&lt;span&gt;NRF_PWM1-&amp;gt;ENABLE = 1; // start PWM (calling&amp;nbsp;nrfx_pwm_simple_playback() will have an effect)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;br /&gt;Kenneth&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to have a beep after another in a loop with PWM?</title><link>https://devzone.nordicsemi.com/thread/376102?ContentTypeID=1</link><pubDate>Fri, 08 Jul 2022 06:25:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b2321ab8-c569-47b6-808c-33640a07870e</guid><dc:creator>builder01</dc:creator><description>&lt;p&gt;Hi, I&amp;#39;m sorry I wasn&amp;#39;t clear.&lt;/p&gt;
&lt;p&gt;I also don&amp;#39;t understand how the forwarded ticket relates to my problem. I&amp;#39;m not using .values.p_individual = &amp;amp;m_demo1_seq_values but .p_common, so my sequence has a length &amp;gt; 1.&lt;/p&gt;
&lt;p&gt;Let me try to simplify the explanation of my problem:&lt;br /&gt;&lt;br /&gt;My aim is to implement the the pwm_play() function like this:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static void pwm_play(void){

//..

    for(int i=0;i&amp;lt;100;i++){
      (void)nrfx_pwm_simple_playback(&amp;amp;m_pwm0, &amp;amp;seq0, 3, NRFX_PWM_FLAG_STOP);
      nrf_delay_ms(500);
      }

}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Or, even better:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static void pwm_play(void)
{
    nrf_pwm_sequence_t const seq0 =
    {
        .values.p_common = sequence_values,
        .length          = NRF_PWM_VALUES_LENGTH(sequence_values),
        .repeats         = 0x00,
        .end_delay       = 0,
     };

    while(1){
        (void)nrfx_pwm_simple_playback(&amp;amp;m_pwm0, &amp;amp;seq0, 3, NRFX_PWM_FLAG_STOP);
        nrf_delay_ms(500);
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;When I write to my buzzer characteristic, the buzzer_write_handler() function works properly by beeping and having pauses in between beeps due to the nrf_delay_ms() function. However, when I write 0x00 to the characteristic, the buzzer_write_handler() function doesn&amp;#39;t work as it doesn&amp;#39;t stop the beeping.&lt;/p&gt;
&lt;p&gt;In other words, this part of the handler function code:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;//..
    else if (new_status.p_data[0] == 0x00)
    {
        pwm_stop();
        NRF_LOG_INFO(&amp;quot;Buzzer is quiet.&amp;quot;);
    }&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;is not executed, or is executed but doesn&amp;#39;t stop the beeping. Somehow, the sever application gets stuck in the for or while loop. I write 0x00 to the characteristic, and even read it back as well, and it says is 0x00. So, I, at least, know that the characteristic is being properly written to.&lt;/p&gt;
&lt;p&gt;Is there a proper way to implement the pwm_play() function such that it can be properly shut off by the buzzer_write_handler() when I write 0x00 to it.&lt;/p&gt;
&lt;p&gt;As I said earlier, the only way I could make it work (i.e. beeping stops when writing 0x00 to the char.) is by taking out the ..._simple_playback() function out of any loop, and using the NRFX_PWM_FLAG_LOOP flag. Unfortunately, this is not desired because the LOOP flag will keep playing the tone without any pauses, as I do them using the nrf_delay_ms() function.&lt;br /&gt;&lt;br /&gt;Let me know if anything is still not clear.&lt;/p&gt;
&lt;p&gt;Best,&lt;/p&gt;
&lt;p&gt;Ernesto&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to have a beep after another in a loop with PWM?</title><link>https://devzone.nordicsemi.com/thread/376003?ContentTypeID=1</link><pubDate>Thu, 07 Jul 2022 12:14:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d3460364-6660-41f5-959c-b36bfda62179</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;I don&amp;#39;t quite understand your problem, but I could find someone else reporting something similiar, so I am pointing you to this case and hopefully that will also help you:&amp;nbsp;&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/68522/pwm-stays-on-even-when-proper-stop-functions-are-called"&gt;Pwm Stays on even when proper stop functions are called&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kenneth&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>