<?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>I would like to know how to change the PWM frequency.</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/66772/i-would-like-to-know-how-to-change-the-pwm-frequency</link><description>Hello 
 I&amp;#39;m trying to print out several sounds with one buzzer. To do that, I need to be able to change the frequency. 
 PWM was initialized and reset using code. An attempt was made to change the frequency depending on how the button is entered button</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 15 Oct 2020 07:29:08 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/66772/i-would-like-to-know-how-to-change-the-pwm-frequency" /><item><title>RE: I would like to know how to change the PWM frequency.</title><link>https://devzone.nordicsemi.com/thread/274990?ContentTypeID=1</link><pubDate>Thu, 15 Oct 2020 07:29:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6508df1a-cb4e-418b-85ee-50fca1d9b4f0</guid><dc:creator>schosdas</dc:creator><description>&lt;p&gt;Hello, Edvin!&lt;br /&gt;Sorry for the late reply.&lt;/p&gt;
&lt;p&gt;Resetting PWM has been resolved. I thought I was turning off initialization with pwm_disable, but there was a function called pwm_uninit.&amp;nbsp; This function has solved everything.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;div class="edit_box___1KtZ3 active___3VPGL" id="txtTarget"&gt;&lt;span&gt;Thank you for your help.&lt;/span&gt;&lt;/div&gt;
&lt;div class="edit_box___1KtZ3 active___3VPGL"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class="edit_box___1KtZ3 active___3VPGL"&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;void pwm_init(void) 
{
.
.
.
}


void pwm_change_frequency(uint16_t period_us) // ex) pwm_change_frequency(500);
{
    ret_code_t err_code;

    app_pwm_disable(&amp;amp;PWM1); //disable PWM for change PWM
    app_pwm_uninit(&amp;amp;PWM1); //uninitializing a PWM

    /* 1-channel PWM, 200Hz, output on DK LED pins. */
    app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(period_us, BUZZER_PIN); //original 5000L
    
    /* Switch the polarity of the second channel. */
    pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;
    
    /* Initialize and enable PWM. */
    err_code = app_pwm_init(&amp;amp;PWM1,&amp;amp;pwm1_cfg,pwm_ready_callback);
    APP_ERROR_CHECK(err_code);
    app_pwm_enable(&amp;amp;PWM1);
}&lt;/pre&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: I would like to know how to change the PWM frequency.</title><link>https://devzone.nordicsemi.com/thread/273377?ContentTypeID=1</link><pubDate>Wed, 07 Oct 2020 07:55:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b3cb2da2-c928-4f76-ab08-c65e2d32c62a</guid><dc:creator>Edvin</dc:creator><description>[quote user="schosdas"]NRF_LOG_INFO is not printed on the serial terminal.[/quote]
&lt;p&gt;&amp;nbsp;What does your log_init() look like? And what does the idle_state_handle() look like?&lt;/p&gt;
&lt;p&gt;Please compare them with the idle_state_handler of e.g. the ble_app_uart example:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static void idle_state_handle(void)
{
    if (NRF_LOG_PROCESS() == false)
    {
        nrf_pwr_mgmt_run();
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Also, please make sure that the sdk_config.h define:&lt;/p&gt;
&lt;p&gt;NRF_LOG_ENABLED is set to 1&lt;/p&gt;
&lt;p&gt;NRF_LOG_BACKEND_RTT_ENABLED is set to 1&lt;/p&gt;
&lt;p&gt;NRF_FPRINTF_FLAG_AUTOMATIC_CR_ON_LF_ENABLED is set to 0.&lt;/p&gt;
&lt;p&gt;these look good, but this is why I wanted to check your sdk_config.h file.&lt;/p&gt;
&lt;p&gt;However, you do get log information from your terminal, according to your last screenshot. It is the lines saying softdevice assertion failed.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;The reason you see the &amp;quot;SOFTDEVICE: ASSERTION FAILED&amp;quot; when you continue to debug is because the softdevice misses timing critical events when you break. After you break, remove the breakpoint, try to set a breakpoint another place, and reset the application:&amp;nbsp;&lt;/p&gt;
[quote user="Edvin Holmseth"] If you are doing BLE activity you need to reset the application after this.&amp;nbsp;[/quote]
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;If all you see is that the PWM signals stop, you need to check if your application reaches the place that you expect it to do. Is the&amp;nbsp;&amp;nbsp;app_button_event_generator() reached?&lt;/p&gt;
&lt;p&gt;Is &amp;quot;pwm_change_frequency(5000); //change PWM&amp;quot; reached?&lt;/p&gt;
&lt;p&gt;is&lt;/p&gt;
&lt;p&gt;nrf_delay_ms(duration);&lt;br /&gt; app_pwm_disable(&amp;amp;PWM1);&lt;/p&gt;
&lt;p&gt;reached?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;If all of the above is &amp;quot;yes&amp;quot;. Is there a way for me to reproduce this on a DK? You can zip the project folder containing your project. Please try to unzip it in an unmodified DK to check that it compiles without modifications to the rest of the SDK.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Where is the app_pwm_enable() called that is supposed to start the PWM, but doesn&amp;#39;t trigger the PWM?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: I would like to know how to change the PWM frequency.</title><link>https://devzone.nordicsemi.com/thread/273342?ContentTypeID=1</link><pubDate>Wed, 07 Oct 2020 02:36:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e07a4165-5771-4a9f-9642-73dfacb417c5</guid><dc:creator>schosdas</dc:creator><description>&lt;p&gt;&lt;span style="vertical-align:inherit;"&gt;&lt;span style="vertical-align:inherit;"&gt;&lt;span&gt;Hello, Edvin!&lt;/span&gt;&lt;br /&gt;&lt;span&gt;Thank you for your help.&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;I am working with Segger and SDK v17.0.0 and s132.&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;NRF_LOG_INFO is not printed on the serial terminal. So I used printf instead.&lt;/span&gt;&lt;br /&gt;&lt;span&gt;Added more code for app_button and attached sdk_config. If this code is executed, the BLE communication will be fine until the button is pressed.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#include &amp;lt;stdint.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &amp;lt;stdbool.h&amp;gt;
#include &amp;quot;nordic_common.h&amp;quot;
#include &amp;quot;nrf.h&amp;quot;
#include &amp;quot;ble_hci.h&amp;quot;
#include &amp;quot;ble_advdata.h&amp;quot;
#include &amp;quot;ble_advertising.h&amp;quot;
#include &amp;quot;ble_conn_params.h&amp;quot;
#include &amp;quot;nrf_sdh.h&amp;quot;
#include &amp;quot;nrf_sdh_soc.h&amp;quot;
#include &amp;quot;nrf_sdh_ble.h&amp;quot;
#include &amp;quot;nrf_ble_gatt.h&amp;quot;
#include &amp;quot;nrf_ble_lesc.h&amp;quot; 
#include &amp;quot;nrf_ble_qwr.h&amp;quot;
#include &amp;quot;app_timer.h&amp;quot;
#include &amp;quot;ble_nus.h&amp;quot;
#include &amp;quot;app_uart.h&amp;quot;
#include &amp;quot;app_util_platform.h&amp;quot;
#include &amp;quot;bsp_btn_ble.h&amp;quot;
#include &amp;quot;nrf_pwr_mgmt.h&amp;quot;
#include &amp;quot;nrf_drv_twi.h&amp;quot;

#include &amp;quot;nrf_uart.h&amp;quot;
#include &amp;quot;nrf_uarte.h&amp;quot;

#include &amp;quot;nrf_log.h&amp;quot;
#include &amp;quot;nrf_log_ctrl.h&amp;quot;
#include &amp;quot;nrf_log_default_backends.h&amp;quot;

#include &amp;quot;app_button.h&amp;quot; 
#include &amp;quot;nrf_delay.h&amp;quot; 
#include &amp;quot;peer_manager.h&amp;quot; 
#include &amp;quot;peer_manager_handler.h&amp;quot; 
#include &amp;quot;fds.h&amp;quot; 

#include &amp;quot;app_error.h&amp;quot;
#include &amp;quot;bsp.h&amp;quot; 
#include &amp;quot;app_pwm.h&amp;quot; 

#include &amp;quot;nrf_drv_saadc.h&amp;quot; //ADC
#include &amp;quot;nrf_drv_ppi.h&amp;quot;
#include &amp;quot;nrf_drv_timer.h&amp;quot;




APP_PWM_INSTANCE(PWM1,1);                   // Create the instance &amp;quot;PWM1&amp;quot; using TIMER1  (name, NRF_DRV_TIMER_INSTANCE(num)).
static volatile bool ready_flag;            // A flag indicating PWM status.


void pwm_play_buzzer_duration(uint32_t duration) //button buzzer
{
    uint8_t i;
    uint32_t duty = 50;

    NRF_LOG_INFO(&amp;quot;pwm_play_buzzer_duration =&amp;gt;&amp;quot;);    
    ready_flag = false;

    //Set the duty cycle - keep trying until PWM is ready... 
    while (app_pwm_channel_duty_set(&amp;amp;PWM1, 0, duty) == NRF_ERROR_BUSY);

    // ... or wait for callback. 
    //while (!ready_flag);
    //APP_ERROR_CHECK(app_pwm_channel_duty_set(&amp;amp;PWM1, 1, duty));
    nrf_delay_ms(duration);
    app_pwm_disable(&amp;amp;PWM1);
}


void pwm_ready_callback(uint32_t pwm_id)    // PWM callback function
{
    NRF_LOG_INFO(&amp;quot;pwm_ready_callback =&amp;gt;&amp;quot;);
    ready_flag = true;
}


/*
 init, Once executed
 */
//buzzer PWM test
void pwm_init(void) 
{
    ret_code_t err_code;

    /* 1-channel PWM, 200Hz, output on DK LED pins. */
    app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(1500, BUZZER_PIN); //original 5000L
    
    /* Switch the polarity of the second channel. */
    pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;
    
    /* Initialize and enable PWM. */
    err_code = app_pwm_init(&amp;amp;PWM1,&amp;amp;pwm1_cfg,pwm_ready_callback);
    APP_ERROR_CHECK(err_code);
    app_pwm_enable(&amp;amp;PWM1);
}


void pwm_change_frequency(uint16_t period_us) //reset PWM
{
    ret_code_t err_code;

    app_pwm_disable(&amp;amp;PWM1); //disable PWM for change PWM

    /* 1-channel PWM, 200Hz, output on DK LED pins. */
    app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(period_us, BUZZER_PIN); //original 5000L
    
    /* Switch the polarity of the second channel. */
    pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;
    
    /* Initialize and enable PWM. */
    err_code = app_pwm_init(&amp;amp;PWM1,&amp;amp;pwm1_cfg,pwm_ready_callback);
    APP_ERROR_CHECK(err_code);
    app_pwm_enable(&amp;amp;PWM1);
}


static void do_play_buzzer(void)
{
  pwm_play_buzzer_duration(300); //delay
  app_pwm_enable(&amp;amp;PWM1);
}


static uint32_t pressed_time, released_time, pressed_duration = 0;
static bool pressed_cnt= false;
static bool over5sec_cnt = true; //When at first, trying to connect.


static uint32_t app_button_duration(void)
{
  pressed_duration = (released_time - pressed_time)*2; //ms
  return pressed_duration;
}


static void app_button_init_time_variable(void)
{
  pressed_duration = 0;
  //released_time = 0;
  //pressed_time = 0;
}


static void app_button_event_generator(void)
{
  ret_code_t err_code;

  //if ((pressed_duration &amp;gt;= 2000) &amp;amp;&amp;amp; (pressed_duration &amp;lt; 5000) ){ //over 2sec
  if ((pressed_duration &amp;gt;= 3000) &amp;amp;&amp;amp; (pressed_duration &amp;lt; 10000000)) //over 3sec
  { 
    //nrf_gpio_pin_clear(APP_LED_2); //ledt led on, timer test
    NRF_LOG_INFO(&amp;quot;Button pressed for 3sec...&amp;quot;);
    printf(&amp;quot;Long button\n&amp;quot;);
    pwm_change_frequency(5000); //change PWM
    do_play_buzzer();

    //do_play_buzzer_2();

    if(m_conn_handle == BLE_CONN_HANDLE_INVALID) //in disconnect pairing
    {
      printf(&amp;quot;Not yet pairing!\n&amp;quot;);
    }

    else //in connect pairing
    {
      printf(&amp;quot;Disconnected pairing!\n&amp;quot;);
      err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); //disconnect pairing
      APP_ERROR_CHECK(err_code);
    }
  }

  app_button_init_time_variable();
}


static void app_button_event_handler(uint8_t pin_no, uint8_t button_action)
{
    ret_code_t err_code;

    switch (pin_no)
    {
      case APP_BTN_1: //button1
        switch (button_action)
        {
          case APP_BUTTON_PUSH:
          {
            NRF_LOG_INFO(&amp;quot;Button pressed...&amp;quot;);
            printf(&amp;quot;Button pressed...&amp;quot;);
            nrf_gpio_pin_clear(APP_LED_3); //led on
            pwm_change_frequency(1500);
            do_play_buzzer();
            //nrf_gpio_pin_clear(LED_2); //add(led test)
            //LeftLED_timers_start(); //timer test

            //get pressed time
            pressed_time = app_timer_cnt_get()*1000/32768;//milli-sec
            NRF_LOG_INFO(&amp;quot;pressed_time: %d&amp;quot;, (int)pressed_time);
            printf(&amp;quot;pressed_time: %d\n&amp;quot;, (int)pressed_time);
            pressed_cnt = true;
        
          } break;

           case APP_BUTTON_RELEASE:
           {  
              NRF_LOG_INFO(&amp;quot;Button releaed...&amp;quot;);
              nrf_gpio_pin_set(APP_LED_3); //led off
              //nrf_gpio_pin_set(LED_2);
              if (pressed_cnt)
              {
                released_time = app_timer_cnt_get()*1000/32768; //milli-sec
                NRF_LOG_INFO(&amp;quot;released_time: %d&amp;quot;, (int)released_time);
                printf(&amp;quot;released_time: %d\n&amp;quot;, (int)released_time);
              }

              pressed_cnt = false;
                
           } break;
        } 
      break;

      default:
         APP_ERROR_HANDLER(pin_no);
      break;
    }

    if(app_button_duration())
    {
      app_button_event_generator();
    }

    app_button_init_time_variable();
    
}


static void app_buttons_init(void)
{
  uint32_t err_code;

  static const app_button_cfg_t app_buttons[BUTTONS_NUMBER] =
  {
#if (1)
    #ifdef APP_BTN_1 //button1 (custom pca10040)
    {APP_BTN_1, false, BUTTON_PULL, app_button_event_handler},
    #endif // APP_BTN_1
#else
    #ifdef BSP_BUTTON_0 //button1
    {BSP_BUTTON_0, false, BUTTON_PULL, bsp_button_event_handler},
    #endif // BUTTON_0
#endif
  };

  err_code = app_button_init((app_button_cfg_t *)app_buttons,
                                       BUTTONS_NUMBER,        // 1
                                       APP_TIMER_TICKS(50)); //debounce
  APP_ERROR_CHECK(err_code);
}



int main(void)
{
    uint32_t err_code;
    bool erase_bonds = false;

    // Initialize.
    gpio_init(); 
    uart_init();
    log_init();
    timers_init();
    app_buttons_init(); //buttons_leds_init(&amp;amp;erase_bonds);
    power_management_init();
        
    ble_stack_init();
    gap_params_init();
    //set_static_passkey(); //Central display 
    gatt_init();
    services_init();
    advertising_init();
    conn_params_init();
    peer_manager_init();

    err_code = app_button_enable();
    //err_code = app_button_disable();
    APP_ERROR_CHECK(err_code);

    //adc_configure(); baterry value &amp;amp; LED
    pwm_init();
    

    // Start execution.
    printf(&amp;quot;\r\nUART started.\r\n&amp;quot;);
    NRF_LOG_INFO(&amp;quot;Debug logging for UART over RTT started.&amp;quot;);
    
    //CenterLED_timers_start();
    advertising_start(erase_bonds); //false

    for(;;)
    {
        idle_state_handle(); //sleep mode
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;When the button is pressed, the text output and buzzer must be output. However, the buzzer does not output and the text output stops halfway.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;(Press the button and the text will not be output to the end and will stop halfway.)&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/pastedimage1602037392266v3.png" /&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/1348.sdk_5F00_config.h"&gt;devzone.nordicsemi.com/.../1348.sdk_5F00_config.h&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;If I continue the debugging line by line, the error codes &amp;#39;NRF_BREAKPOINT_COND&amp;#39; and &amp;#39;error&amp;#39; app: SOFTDEVICE: ASSERTION FAILED&amp;#39; will be printed.&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/pastedimage1602037738540v4.png" /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: I would like to know how to change the PWM frequency.</title><link>https://devzone.nordicsemi.com/thread/273286?ContentTypeID=1</link><pubDate>Tue, 06 Oct 2020 14:32:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7854cd34-24b8-4e25-bc38-15b74832a148</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;What SDK version are you using?&lt;/p&gt;
&lt;p&gt;Do you see any of the logging information in the log, such as the line:&lt;/p&gt;
&lt;p&gt;NRF_LOG_INFO(&amp;quot;Button pressed for 3sec...&amp;quot;);&lt;/p&gt;
&lt;p&gt;Or any of the other NRF_LOG_INFO(&amp;quot;&amp;quot;); lines?&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t see how you generate the&amp;nbsp;app_button_event_generator() callbacks. Where is it triggered? I&amp;nbsp; assume it is one of the parts that you removed inside&amp;nbsp;app_button_event_handler().&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;If you have connected a debugger (or you are using a DK), are you able to debug and step through code? It is not trivial once the softdevice is started (in ble_stack_init()), but you should be able to set breakpoints and see whether they are hit or not. If you are doing BLE activity you need to reset the application after this.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Typically if the application stops, it is because of one of the APP_ERROR_CHECK(err_code); If you are using one of the later SDKs, the log should print something like &amp;quot;Fatal error.&amp;quot; Do you see that? If so, try to define DEBUG in your preprocessor definitions, and check what the log says then. Let me know what IDE you are using if you are not sure how to set preprocessor definitions. Also, let me know what SDK you are using if you don&amp;#39;t see any error handling in the log, but you do see the log. If you don&amp;#39;t see any logs at all, let me know what IDE and SDK version you are using, and also, please show me your sdk_config.h file.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>