<?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 disable doesn&amp;#39;t work. Can&amp;#39;t turn off PWM.</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/32572/pwm-disable-doesn-t-work-can-t-turn-off-pwm</link><description>Hi! I am using an nRF51 DK board and the app_pwm library. I am using SDK 12.3.0. 
 
 Why doesn&amp;#39;t 
 
 
 stop the PWM? I call it right after initialization: 
 
 ... as well as in a button handler, and neither stop PWM from happening. 
 I am using it to</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 23 Sep 2020 11:30:33 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/32572/pwm-disable-doesn-t-work-can-t-turn-off-pwm" /><item><title>RE: PWM disable doesn't work. Can't turn off PWM.</title><link>https://devzone.nordicsemi.com/thread/271056?ContentTypeID=1</link><pubDate>Wed, 23 Sep 2020 11:30:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0f0c844a-b664-476e-96fa-465f6722fff3</guid><dc:creator>Martin Lesund</dc:creator><description>&lt;p&gt;Hi hj.lee,&lt;br /&gt;This case is over 2 years old.&lt;br /&gt;Please create a new ticket.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM disable doesn't work. Can't turn off PWM.</title><link>https://devzone.nordicsemi.com/thread/270707?ContentTypeID=1</link><pubDate>Tue, 22 Sep 2020 01:39:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8425cb90-13ab-4292-bd08-9d181f572f84</guid><dc:creator>hj.lee</dc:creator><description>&lt;p&gt;&lt;span&gt;&amp;#39;app_pwm_disable(&amp;#39;) is still dosen&amp;#39;t work.&lt;br /&gt;sdk is 16.&amp;nbsp;&lt;/span&gt;&lt;span&gt;Is there a solution?&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM disable doesn't work. Can't turn off PWM.</title><link>https://devzone.nordicsemi.com/thread/127233?ContentTypeID=1</link><pubDate>Fri, 06 Apr 2018 13:37:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c2d21348-8d39-4a3b-9286-8228b81c347f</guid><dc:creator>Martin Lesund</dc:creator><description>&lt;p&gt;Have you stepped through your code and confirmed that the&amp;nbsp;&lt;em&gt;app_pwm_disable&lt;/em&gt; function actually gets reached ?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM disable doesn't work. Can't turn off PWM.</title><link>https://devzone.nordicsemi.com/thread/127082?ContentTypeID=1</link><pubDate>Thu, 05 Apr 2018 14:17:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:820416b0-ccac-423c-9440-7950d874ae87</guid><dc:creator>Jonathan Hale</dc:creator><description>&lt;p&gt;No, I haven&amp;#39;t made any progress. Using app_pwm_channel_duty set and setting the duty cycle to 100% is technically turning off the PWM, but I am concerned that it is not a power-optimized method of disabling PWM. Also, I am puzzled/troubled that calling app_pwm_disable does not stop the PWM.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM disable doesn't work. Can't turn off PWM.</title><link>https://devzone.nordicsemi.com/thread/127045?ContentTypeID=1</link><pubDate>Thu, 05 Apr 2018 12:48:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b0a93043-d2c9-4006-8dc1-74077fd7216c</guid><dc:creator>Martin Lesund</dc:creator><description>&lt;p&gt;Hello Jonathan,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Sorry for the delay, &lt;em&gt;I have been on vacation.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Have you made any progress and/or updates to this case?&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM disable doesn't work. Can't turn off PWM.</title><link>https://devzone.nordicsemi.com/thread/125604?ContentTypeID=1</link><pubDate>Thu, 22 Mar 2018 18:13:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:433b7b3a-02bf-424e-9734-684a2c7f9bd4</guid><dc:creator>Jonathan Hale</dc:creator><description>&lt;p&gt;Here&amp;#39;s my entire main.c. It is based on the BLE Blinky example (which was SUPER helpful btw):&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;

#include &amp;lt;stdint.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &amp;quot;app_pwm.h&amp;quot;
#include &amp;quot;nordic_common.h&amp;quot;
#include &amp;quot;nrf.h&amp;quot;
#include &amp;quot;app_error.h&amp;quot;
#include &amp;quot;ble.h&amp;quot;
#include &amp;quot;ble_hci.h&amp;quot;
#include &amp;quot;ble_srv_common.h&amp;quot;
#include &amp;quot;ble_advdata.h&amp;quot;
#include &amp;quot;ble_conn_params.h&amp;quot;
#include &amp;quot;softdevice_handler.h&amp;quot;
#include &amp;quot;app_timer.h&amp;quot;
#include &amp;quot;app_button.h&amp;quot;
#include &amp;quot;ble_lbs.h&amp;quot;
#include &amp;quot;bsp.h&amp;quot;
#include &amp;quot;ble_gap.h&amp;quot;

#define NRF_LOG_MODULE_NAME &amp;quot;APP&amp;quot;
#include &amp;quot;nrf_log.h&amp;quot;
#include &amp;quot;nrf_log_ctrl.h&amp;quot;

#define CENTRAL_LINK_COUNT              0                                           /**&amp;lt; Number of central links used by the application. When changing this number remember to adjust the RAM settings*/
#define PERIPHERAL_LINK_COUNT           1                                           /**&amp;lt; Number of peripheral links used by the application. When changing this number remember to adjust the RAM settings*/

#if (NRF_SD_BLE_API_VERSION == 3)
#define NRF_BLE_MAX_MTU_SIZE            GATT_MTU_SIZE_DEFAULT                       /**&amp;lt; MTU size used in the softdevice enabling and to reply to a BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST event. */
#endif

#define APP_FEATURE_NOT_SUPPORTED       BLE_GATT_STATUS_ATTERR_APP_BEGIN + 2        /**&amp;lt; Reply when unsupported features are requested. */

#define ADVERTISING_LED_PIN             BSP_BOARD_LED_0                             /**&amp;lt; Is on when device is advertising. */
#define CONNECTED_LED_PIN               BSP_BOARD_LED_1                             /**&amp;lt; Is on when device has connected. */

#define LEDBUTTON_LED_PIN               BSP_BOARD_LED_2                             /**&amp;lt; LED to be toggled with the help of the LED Button Service. */
#define LEDBUTTON_BUTTON_PIN            BSP_BUTTON_0                                /**&amp;lt; Button that will trigger the notification event with the LED Button Service */

#define DEVICE_NAME                     &amp;quot;Nordic_Blinky&amp;quot;                             /**&amp;lt; Name of device. Will be included in the advertising data. */

#define APP_ADV_INTERVAL                64                                          /**&amp;lt; The advertising interval (in units of 0.625 ms; this value corresponds to 40 ms). */
#define APP_ADV_TIMEOUT_IN_SECONDS      BLE_GAP_ADV_TIMEOUT_GENERAL_UNLIMITED       /**&amp;lt; The advertising time-out (in units of seconds). When set to 0, we will never time out. */

#define APP_TIMER_PRESCALER             0                                           /**&amp;lt; Value of the RTC1 PRESCALER register. */
#define APP_TIMER_MAX_TIMERS            6                                           /**&amp;lt; Maximum number of simultaneously created timers. */
#define APP_TIMER_OP_QUEUE_SIZE         4                                           /**&amp;lt; Size of timer operation queues. */

#define MIN_CONN_INTERVAL               MSEC_TO_UNITS(100, UNIT_1_25_MS)            /**&amp;lt; Minimum acceptable connection interval (0.5 seconds). */
#define MAX_CONN_INTERVAL               MSEC_TO_UNITS(200, UNIT_1_25_MS)            /**&amp;lt; Maximum acceptable connection interval (1 second). */
#define SLAVE_LATENCY                   0                                           /**&amp;lt; Slave latency. */
#define CONN_SUP_TIMEOUT                MSEC_TO_UNITS(4000, UNIT_10_MS)             /**&amp;lt; Connection supervisory time-out (4 seconds). */
#define FIRST_CONN_PARAMS_UPDATE_DELAY  APP_TIMER_TICKS(20000, APP_TIMER_PRESCALER) /**&amp;lt; Time from initiating event (connect or start of notification) to first time sd_ble_gap_conn_param_update is called (15 seconds). */
#define NEXT_CONN_PARAMS_UPDATE_DELAY   APP_TIMER_TICKS(5000, APP_TIMER_PRESCALER)  /**&amp;lt; Time between each call to sd_ble_gap_conn_param_update after the first call (5 seconds). */
#define MAX_CONN_PARAMS_UPDATE_COUNT    3                                           /**&amp;lt; Number of attempts before giving up the connection parameter negotiation. */

#define APP_GPIOTE_MAX_USERS            1                                           /**&amp;lt; Maximum number of users of the GPIOTE handler. */
#define BUTTON_DETECTION_DELAY          APP_TIMER_TICKS(50, APP_TIMER_PRESCALER)    /**&amp;lt; Delay from a GPIOTE event until a button is reported as pushed (in number of timer ticks). */

#define DEAD_BEEF                       0xDEADBEEF                                  /**&amp;lt; Value used as error code on stack dump, can be used to identify stack location on stack unwind. */

static uint16_t                         m_conn_handle = BLE_CONN_HANDLE_INVALID;    /**&amp;lt; Handle of the current connection. */
static ble_lbs_t                        m_lbs;                                      /**&amp;lt; LED Button Service instance. */

static bool child_present = false;

void pwm_complete_callback(uint32_t pwm_instance_index) {
}
APP_PWM_INSTANCE(PWM1,1); 
app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH	(500L, 25);

void buzzer_off() {
	app_pwm_disable(&amp;amp;PWM1);	// Does nothing :(
	//while (app_pwm_channel_duty_set(&amp;amp;PWM1, 0, 100) == NRF_ERROR_BUSY);  // This works :)
}

void buzzer_on() {
	app_pwm_enable(&amp;amp;PWM1); 
	//while (app_pwm_channel_duty_set(&amp;amp;PWM1, 0, 50) == NRF_ERROR_BUSY); // This works :)
}

void buzzer_init() {
	ret_code_t err_code;
	NRF_LOG_INFO(&amp;quot;Initializing PWM For Buzzer...\r\n&amp;quot;);
	
	err_code = app_pwm_init(&amp;amp;PWM1, &amp;amp;pwm1_cfg, pwm_complete_callback); 
	APP_ERROR_CHECK(err_code);
	app_pwm_enable(&amp;amp;PWM1);
	buzzer_off();
}

/**@brief Function for assert macro callback.
 *
 * @details This function will be called in case of an assert in the SoftDevice.
 *
 * @warning This handler is an example only and does not fit a final product. You need to analyze
 *          how your product is supposed to react in case of Assert.
 * @warning On assert from the SoftDevice, the system can only recover on reset.
 *
 * @param[in] line_num    Line number of the failing ASSERT call.
 * @param[in] p_file_name File name of the failing ASSERT call.
 */
void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name)
{
    app_error_handler(DEAD_BEEF, line_num, p_file_name);
}


/**@brief Function for the LEDs initialization.
 *
 * @details Initializes all LEDs used by the application.
 */
static void leds_init(void)
{
    bsp_board_leds_init();
}


/**@brief Function for the Timer initialization.
 *
 * @details Initializes the timer module.
 */
static void timers_init(void)
{
    // Initialize timer module, making it use the scheduler
    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
}


/**@brief Function for the GAP initialization.
 *
 * @details This function sets up all the necessary GAP (Generic Access Profile) parameters of the
 *          device including the device name, appearance, and the preferred connection parameters.
 */
static void gap_params_init(void)
{
    uint32_t                err_code;
    ble_gap_conn_params_t   gap_conn_params;
    ble_gap_conn_sec_mode_t sec_mode;

    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&amp;amp;sec_mode);

    err_code = sd_ble_gap_device_name_set(&amp;amp;sec_mode,
                                          (const uint8_t *)DEVICE_NAME,
                                          strlen(DEVICE_NAME));
    APP_ERROR_CHECK(err_code);

    memset(&amp;amp;gap_conn_params, 0, sizeof(gap_conn_params));

    gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL;
    gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL;
    gap_conn_params.slave_latency     = SLAVE_LATENCY;
    gap_conn_params.conn_sup_timeout  = CONN_SUP_TIMEOUT;

    err_code = sd_ble_gap_ppcp_set(&amp;amp;gap_conn_params);
    APP_ERROR_CHECK(err_code);
}


/**@brief Function for initializing the Advertising functionality.
 *
 * @details Encodes the required advertising data and passes it to the stack.
 *          Also builds a structure to be passed to the stack when starting advertising.
 */
static void advertising_init(void)
{
    uint32_t      err_code;
    ble_advdata_t advdata;
    ble_advdata_t scanrsp;

    ble_uuid_t adv_uuids[] = {{LBS_UUID_SERVICE, m_lbs.uuid_type}};

    // Build and set advertising data
    memset(&amp;amp;advdata, 0, sizeof(advdata));

    advdata.name_type          = BLE_ADVDATA_FULL_NAME;
    advdata.include_appearance = true;
    advdata.flags              = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;


    memset(&amp;amp;scanrsp, 0, sizeof(scanrsp));
    scanrsp.uuids_complete.uuid_cnt = sizeof(adv_uuids) / sizeof(adv_uuids[0]);
    scanrsp.uuids_complete.p_uuids  = adv_uuids;

    err_code = ble_advdata_set(&amp;amp;advdata, &amp;amp;scanrsp);
    APP_ERROR_CHECK(err_code);
}


/**@brief Function for handling write events to the LED characteristic.
 *
 * @param[in] p_lbs     Instance of LED Button Service to which the write applies.
 * @param[in] led_state Written/desired state of the LED.
 */
static void led_write_handler(ble_lbs_t * p_lbs, uint8_t led_state)
{
    if (led_state)
    {
        bsp_board_led_on(LEDBUTTON_LED_PIN);
        NRF_LOG_INFO(&amp;quot;Received LED ON!\r\n&amp;quot;);
				child_present = true;
    }
    else
    {
        bsp_board_led_off(LEDBUTTON_LED_PIN);
        NRF_LOG_INFO(&amp;quot;Received LED OFF!\r\n&amp;quot;);
				child_present = false;
    }
}


/**@brief Function for initializing services that will be used by the application.
 */
static void services_init(void)
{
    uint32_t       err_code;
    ble_lbs_init_t init;

    init.led_write_handler = led_write_handler;

    err_code = ble_lbs_init(&amp;amp;m_lbs, &amp;amp;init);
    APP_ERROR_CHECK(err_code);
}


/**@brief Function for handling the Connection Parameters Module.
 *
 * @details This function will be called for all events in the Connection Parameters Module that
 *          are passed to the application.
 *
 * @note All this function does is to disconnect. This could have been done by simply
 *       setting the disconnect_on_fail config parameter, but instead we use the event
 *       handler mechanism to demonstrate its use.
 *
 * @param[in] p_evt  Event received from the Connection Parameters Module.
 */
static void on_conn_params_evt(ble_conn_params_evt_t * p_evt)
{
    uint32_t err_code;

    if (p_evt-&amp;gt;evt_type == BLE_CONN_PARAMS_EVT_FAILED)
    {
        err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_CONN_INTERVAL_UNACCEPTABLE);
        APP_ERROR_CHECK(err_code);
    }
}


/**@brief Function for handling a Connection Parameters error.
 *
 * @param[in] nrf_error  Error code containing information about what went wrong.
 */
static void conn_params_error_handler(uint32_t nrf_error)
{
    APP_ERROR_HANDLER(nrf_error);
}


/**@brief Function for initializing the Connection Parameters module.
 */
static void conn_params_init(void)
{
    uint32_t               err_code;
    ble_conn_params_init_t cp_init;

    memset(&amp;amp;cp_init, 0, sizeof(cp_init));

    cp_init.p_conn_params                  = NULL;
    cp_init.first_conn_params_update_delay = FIRST_CONN_PARAMS_UPDATE_DELAY;
    cp_init.next_conn_params_update_delay  = NEXT_CONN_PARAMS_UPDATE_DELAY;
    cp_init.max_conn_params_update_count   = MAX_CONN_PARAMS_UPDATE_COUNT;
    cp_init.start_on_notify_cccd_handle    = BLE_GATT_HANDLE_INVALID;
    cp_init.disconnect_on_fail             = false;
    cp_init.evt_handler                    = on_conn_params_evt;
    cp_init.error_handler                  = conn_params_error_handler;

    err_code = ble_conn_params_init(&amp;amp;cp_init);
    APP_ERROR_CHECK(err_code);
}


/**@brief Function for starting advertising.
 */
static void advertising_start(void)
{
    uint32_t             err_code;
    ble_gap_adv_params_t adv_params;

    // Start advertising
    memset(&amp;amp;adv_params, 0, sizeof(adv_params));

    adv_params.type        = BLE_GAP_ADV_TYPE_ADV_IND;
    adv_params.p_peer_addr = NULL;
    adv_params.fp          = BLE_GAP_ADV_FP_ANY;
    adv_params.interval    = APP_ADV_INTERVAL;
    adv_params.timeout     = APP_ADV_TIMEOUT_IN_SECONDS;

    err_code = sd_ble_gap_adv_start(&amp;amp;adv_params);
    APP_ERROR_CHECK(err_code);
    bsp_board_led_on(ADVERTISING_LED_PIN);
}


/**@brief Function for handling the Application&amp;#39;s BLE stack events.
 *
 * @param[in] p_ble_evt  Bluetooth stack event.
 */
static void on_ble_evt(ble_evt_t * p_ble_evt)
{
    uint32_t err_code;

    switch (p_ble_evt-&amp;gt;header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            NRF_LOG_INFO(&amp;quot;Connected\r\n&amp;quot;);
            bsp_board_led_on(CONNECTED_LED_PIN);
            bsp_board_led_off(ADVERTISING_LED_PIN);
            m_conn_handle = p_ble_evt-&amp;gt;evt.gap_evt.conn_handle;

            err_code = app_button_enable();
            APP_ERROR_CHECK(err_code);
				
						buzzer_off();
						
            break; // BLE_GAP_EVT_CONNECTED

        case BLE_GAP_EVT_DISCONNECTED:
            NRF_LOG_INFO(&amp;quot;Disconnected\r\n&amp;quot;);
            bsp_board_led_off(CONNECTED_LED_PIN);
            m_conn_handle = BLE_CONN_HANDLE_INVALID;

            err_code = app_button_disable();
            APP_ERROR_CHECK(err_code);

            advertising_start();
				
						if (child_present) {
							buzzer_on();
						}
            break; // BLE_GAP_EVT_DISCONNECTED

        case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
            // Pairing not supported
            err_code = sd_ble_gap_sec_params_reply(m_conn_handle,
                                                   BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP,
                                                   NULL,
                                                   NULL);
            APP_ERROR_CHECK(err_code);
            break; // BLE_GAP_EVT_SEC_PARAMS_REQUEST

        case BLE_GATTS_EVT_SYS_ATTR_MISSING:
            // No system attributes have been stored.
            err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0, 0);
            APP_ERROR_CHECK(err_code);
            break; // BLE_GATTS_EVT_SYS_ATTR_MISSING

        case BLE_GATTC_EVT_TIMEOUT:
            // Disconnect on GATT Client timeout event.
            NRF_LOG_DEBUG(&amp;quot;GATT Client Timeout.\r\n&amp;quot;);
            err_code = sd_ble_gap_disconnect(p_ble_evt-&amp;gt;evt.gattc_evt.conn_handle,
                                             BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
            APP_ERROR_CHECK(err_code);
            break; // BLE_GATTC_EVT_TIMEOUT

        case BLE_GATTS_EVT_TIMEOUT:
            // Disconnect on GATT Server timeout event.
            NRF_LOG_DEBUG(&amp;quot;GATT Server Timeout.\r\n&amp;quot;);
            err_code = sd_ble_gap_disconnect(p_ble_evt-&amp;gt;evt.gatts_evt.conn_handle,
                                             BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
            APP_ERROR_CHECK(err_code);
            break; // BLE_GATTS_EVT_TIMEOUT

        case BLE_EVT_USER_MEM_REQUEST:
            err_code = sd_ble_user_mem_reply(p_ble_evt-&amp;gt;evt.gattc_evt.conn_handle, NULL);
            APP_ERROR_CHECK(err_code);
            break; // BLE_EVT_USER_MEM_REQUEST

        case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
        {
            ble_gatts_evt_rw_authorize_request_t  req;
            ble_gatts_rw_authorize_reply_params_t auth_reply;

            req = p_ble_evt-&amp;gt;evt.gatts_evt.params.authorize_request;

            if (req.type != BLE_GATTS_AUTHORIZE_TYPE_INVALID)
            {
                if ((req.request.write.op == BLE_GATTS_OP_PREP_WRITE_REQ)     ||
                    (req.request.write.op == BLE_GATTS_OP_EXEC_WRITE_REQ_NOW) ||
                    (req.request.write.op == BLE_GATTS_OP_EXEC_WRITE_REQ_CANCEL))
                {
                    if (req.type == BLE_GATTS_AUTHORIZE_TYPE_WRITE)
                    {
                        auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE;
                    }
                    else
                    {
                        auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_READ;
                    }
                    auth_reply.params.write.gatt_status = APP_FEATURE_NOT_SUPPORTED;
                    err_code = sd_ble_gatts_rw_authorize_reply(p_ble_evt-&amp;gt;evt.gatts_evt.conn_handle,
                                                               &amp;amp;auth_reply);
                    APP_ERROR_CHECK(err_code);
                }
            }
        } break; // BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST

#if (NRF_SD_BLE_API_VERSION == 3)
        case BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST:
            err_code = sd_ble_gatts_exchange_mtu_reply(p_ble_evt-&amp;gt;evt.gatts_evt.conn_handle,
                                                       NRF_BLE_MAX_MTU_SIZE);
            APP_ERROR_CHECK(err_code);
            break; // BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST
#endif

        default:
            // No implementation needed.
            break;
    }
}


/**@brief Function for dispatching a BLE stack event to all modules with a BLE stack event handler.
 *
 * @details This function is called from the scheduler in the main loop after a BLE stack
 *          event has been received.
 *
 * @param[in] p_ble_evt  Bluetooth stack event.
 */
static void ble_evt_dispatch(ble_evt_t * p_ble_evt)
{
    on_ble_evt(p_ble_evt);
    ble_conn_params_on_ble_evt(p_ble_evt);
    ble_lbs_on_ble_evt(&amp;amp;m_lbs, p_ble_evt);
}


/**@brief Function for initializing the BLE stack.
 *
 * @details Initializes the SoftDevice and the BLE event interrupt.
 */
static void ble_stack_init(void)
{
    uint32_t err_code;

    nrf_clock_lf_cfg_t clock_lf_cfg = NRF_CLOCK_LFCLKSRC;

    // Initialize the SoftDevice handler module.
    SOFTDEVICE_HANDLER_INIT(&amp;amp;clock_lf_cfg, NULL);

    ble_enable_params_t ble_enable_params;
    err_code = softdevice_enable_get_default_config(CENTRAL_LINK_COUNT,
                                                    PERIPHERAL_LINK_COUNT,
                                                    &amp;amp;ble_enable_params);
    APP_ERROR_CHECK(err_code);

    //Check the ram settings against the used number of links
    CHECK_RAM_START_ADDR(CENTRAL_LINK_COUNT, PERIPHERAL_LINK_COUNT);

    // Enable BLE stack.
#if (NRF_SD_BLE_API_VERSION == 3)
    ble_enable_params.gatt_enable_params.att_mtu = NRF_BLE_MAX_MTU_SIZE;
#endif
    err_code = softdevice_enable(&amp;amp;ble_enable_params);
    APP_ERROR_CHECK(err_code);

    // Subscribe for BLE events.
    err_code = softdevice_ble_evt_handler_set(ble_evt_dispatch);
    APP_ERROR_CHECK(err_code);
}


/**@brief Function for handling events from the button handler module.
 *
 * @param[in] pin_no        The pin that the event applies to.
 * @param[in] button_action The button action (press/release).
 */
static void button_event_handler(uint8_t pin_no, uint8_t button_action)
{
    uint32_t err_code;

    switch (pin_no)
    {
        case LEDBUTTON_BUTTON_PIN:
            NRF_LOG_INFO(&amp;quot;Send button state change.\r\n&amp;quot;);            
            err_code = ble_lbs_on_button_change(&amp;amp;m_lbs, button_action);
            if (err_code != NRF_SUCCESS &amp;amp;&amp;amp;
                err_code != BLE_ERROR_INVALID_CONN_HANDLE &amp;amp;&amp;amp;
                err_code != NRF_ERROR_INVALID_STATE)
            {
                APP_ERROR_CHECK(err_code);
            }
            break;
				
				case BSP_BUTTON_1:
					
					if (button_action == BSP_BUTTON_ACTION_PUSH) {
						buzzer_on();
					} else {
						buzzer_off();
					}
	
					break;
				case BSP_BUTTON_2:
						
					break;
        default:
            APP_ERROR_HANDLER(pin_no);
            break;
    }
}


/**@brief Function for initializing the button handler module.
 */
static void buttons_init(void)
{
    uint32_t err_code;

    //The array must be static because a pointer to it will be saved in the button handler module.
    static app_button_cfg_t buttons[] =
    {
        {LEDBUTTON_BUTTON_PIN, false, BUTTON_PULL, button_event_handler},
				{BSP_BUTTON_1, false, BUTTON_PULL, button_event_handler},
				{BSP_BUTTON_2, false, BUTTON_PULL, button_event_handler},
				{BSP_BUTTON_3, false, BUTTON_PULL, button_event_handler}
    };

    err_code = app_button_init(buttons, sizeof(buttons) / sizeof(buttons[0]),
                               BUTTON_DETECTION_DELAY);
    APP_ERROR_CHECK(err_code);
}


/**@brief Function for the Power Manager.
 */
static void power_manage(void)
{
    uint32_t err_code = sd_app_evt_wait();

    APP_ERROR_CHECK(err_code);
}




/**@brief Function for application main entry.
 */
int main(void)
{
    ret_code_t err_code;


    // Initialize.
    leds_init();
    timers_init();
    err_code = NRF_LOG_INIT(NULL);
    APP_ERROR_CHECK(err_code);
    buttons_init();
    ble_stack_init();
    gap_params_init();
    services_init();
    advertising_init();
    conn_params_init();
		buzzer_init();
		sd_ble_gap_tx_power_set(-30);

    // Start execution.
    NRF_LOG_INFO(&amp;quot;Blinky Start!\r\n&amp;quot;);
    advertising_start();

    // Enter main loop.
    for (;;)
    {
        if (NRF_LOG_PROCESS() == false)
        {
            power_manage();
        }
    }
}


/**
 * @}
 */
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Pin 25 is hooked to a PIezzo Buzzer, and it buzzes really loud! Here are the relevant snippets from main.c:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;void pwm_complete_callback(uint32_t pwm_instance_index) {
}
APP_PWM_INSTANCE(PWM1,1); 
app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH	(500L, 25);

void buzzer_off() {
	app_pwm_disable(&amp;amp;PWM1);	// Does nothing :(
	//while (app_pwm_channel_duty_set(&amp;amp;PWM1, 0, 100) == NRF_ERROR_BUSY);  // This works :)
}

void buzzer_on() {
	app_pwm_enable(&amp;amp;PWM1); 
	//while (app_pwm_channel_duty_set(&amp;amp;PWM1, 0, 50) == NRF_ERROR_BUSY); // This works :)
}

void buzzer_init() {
	ret_code_t err_code;
	NRF_LOG_INFO(&amp;quot;Initializing PWM For Buzzer...\r\n&amp;quot;);
	
	err_code = app_pwm_init(&amp;amp;PWM1, &amp;amp;pwm1_cfg, pwm_complete_callback); 
	APP_ERROR_CHECK(err_code);
	app_pwm_enable(&amp;amp;PWM1);
	buzzer_off();
}

/**@brief Function for handling events from the button handler module.
 *
 * @param[in] pin_no        The pin that the event applies to.
 * @param[in] button_action The button action (press/release).
 */
static void button_event_handler(uint8_t pin_no, uint8_t button_action)
{
    uint32_t err_code;

    switch (pin_no)
    {
        case LEDBUTTON_BUTTON_PIN:
            NRF_LOG_INFO(&amp;quot;Send button state change.\r\n&amp;quot;);            
            err_code = ble_lbs_on_button_change(&amp;amp;m_lbs, button_action);
            if (err_code != NRF_SUCCESS &amp;amp;&amp;amp;
                err_code != BLE_ERROR_INVALID_CONN_HANDLE &amp;amp;&amp;amp;
                err_code != NRF_ERROR_INVALID_STATE)
            {
                APP_ERROR_CHECK(err_code);
            }
            break;
				
				case BSP_BUTTON_1:
					
					if (button_action == BSP_BUTTON_ACTION_PUSH) {
						buzzer_on();
					} else {
						buzzer_off();
					}
	
					break;
				case BSP_BUTTON_2:
						
					break;
        default:
            APP_ERROR_HANDLER(pin_no);
            break;
    }
}

    uint32_t err_code = sd_app_evt_wait();

    APP_ERROR_CHECK(err_code);
}




/**@brief Function for application main entry.
 */
int main(void)
{
    ret_code_t err_code;


    // Initialize.
    leds_init();
    timers_init();
    err_code = NRF_LOG_INIT(NULL);
    APP_ERROR_CHECK(err_code);
    buttons_init();
    ble_stack_init();
    gap_params_init();
    services_init();
    advertising_init();
    conn_params_init();
		buzzer_init();
		sd_ble_gap_tx_power_set(-30);

    // Start execution.
    NRF_LOG_INFO(&amp;quot;Blinky Start!\r\n&amp;quot;);
    advertising_start();

    // Enter main loop.
    for (;;)
    {
        if (NRF_LOG_PROCESS() == false)
        {
            power_manage();
        }
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;This program buzzes forever, from startup on, despite calling app_pwm_enable and then app_pwm_disable at startup. Pressing the button does not stop the buzzer, either. Why does app_pwm_disable() fail to stop the PWM and therefore the buzzing?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM disable doesn't work. Can't turn off PWM.</title><link>https://devzone.nordicsemi.com/thread/125529?ContentTypeID=1</link><pubDate>Thu, 22 Mar 2018 12:37:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:795c443a-73b4-49c7-9c5e-a631cfc304e8</guid><dc:creator>Martin Lesund</dc:creator><description>&lt;p&gt;Hi Jonathan,&lt;/p&gt;
&lt;p&gt;To be honest I am a bit confused what your issue is here. Do you get unwanted PWM behaviour even if it&amp;#39;s not enabled ?&amp;nbsp; Does your &amp;quot;app_pwm_init&amp;quot; return something other than &amp;quot;NRF_SUCCESS&amp;quot;&amp;nbsp; if you set a &lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/10729/my-device-is-freezing-and-restarting" target="_blank" rel="noopener noreferrer"&gt;breakpoint&lt;/a&gt;?&lt;/p&gt;
&lt;p&gt;I have tested this with the &amp;quot;pwm_library&amp;quot; example in the SDK, by just playing with the disable/enable functions which that turns on and off the PWM.&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;What happens if you do not initialize the PWM at all? Do you still have unwanted behaviour?&lt;/p&gt;
&lt;p&gt;Are you sure you have connected the buzzer correctly?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Is it constant giving out sound?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Could you please describe your setup a bit more and send me your whole main.c file.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM disable doesn't work. Can't turn off PWM.</title><link>https://devzone.nordicsemi.com/thread/125385?ContentTypeID=1</link><pubDate>Wed, 21 Mar 2018 14:48:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e26f3b5b-ab16-4d5b-8a06-bcab7ca2989d</guid><dc:creator>Jonathan Hale</dc:creator><description>&lt;p&gt;Thanks Martin for the detailed response! I still have a few questions.&lt;/p&gt;
&lt;p&gt;The PWM Library example and nRF52 peripheral tutorials you gave never disable the PWM instance, they just set the Duty Cycle to 0. This is the temporary solution I have been using as well. Would a PWM of 0 prevent interrupts and power consumption in the same way that disabling PWM should?&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&amp;quot;A quick look at your code shows that you do no&amp;nbsp;&lt;/span&gt;&lt;strong&gt;&lt;a href="http://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk5.v14.2.0%2Fgroup__app__pwm.html&amp;amp;cp=4_0_0_6_11_33_16&amp;amp;anchor=ga94f5d824afec86aff163f7cccedaa436" rel="noopener noreferrer" target="_blank"&gt;enable&lt;/a&gt;&lt;/strong&gt;&lt;span&gt;&amp;nbsp;the PWM instance after you initialize it. Which could be the reason why you cannot&amp;nbsp;&lt;/span&gt;&lt;strong&gt;&lt;a href="http://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk5.v14.2.0%2Fgroup__app__pwm.html&amp;amp;cp=4_0_0_6_11_33_15&amp;amp;anchor=gab496bf7cd3acaec3a1fafdc75442d503" rel="noopener noreferrer" target="_blank"&gt;disable&lt;/a&gt;&lt;/strong&gt;&lt;span&gt;&amp;nbsp;an instance that is not yet enabled.&amp;quot;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;First, I tried enabling the PWM immediately before disabling PWM, and it still did not stop the PWM.&amp;nbsp;Similarly, enabling the PWM and later disabling it on a button press also fails to stop the PWM.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Second, doesn&amp;#39;t your suggestion seem a little strange to you? If I never enabled PWM, then why&amp;nbsp;would unwanted PWM be occurring? Why would I need to enable something in order to disable it?&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: PWM disable doesn't work. Can't turn off PWM.</title><link>https://devzone.nordicsemi.com/thread/125325?ContentTypeID=1</link><pubDate>Wed, 21 Mar 2018 10:01:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:45b08884-3b62-4f80-88c3-ac5d179843ba</guid><dc:creator>Martin Lesund</dc:creator><description>&lt;p&gt;Hello Jonathan,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; I would recommend you to take a look at the &amp;quot;&lt;a href="http://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk5.v14.2.0%2Fpwm_example.html&amp;amp;cp=4_0_0_4_6_23" rel="noopener noreferrer" target="_blank"&gt;PWM Library&amp;quot; example&lt;/a&gt;&amp;nbsp;in the SDK and look at how it&amp;#39;s done there.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;A quick look at your code shows that you do no&amp;nbsp;&lt;strong&gt;&lt;a href="http://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk5.v14.2.0%2Fgroup__app__pwm.html&amp;amp;cp=4_0_0_6_11_33_16&amp;amp;anchor=ga94f5d824afec86aff163f7cccedaa436" rel="noopener noreferrer" target="_blank"&gt;enable&lt;/a&gt;&lt;/strong&gt; the PWM instance after you initialize it. Which could be the reason why you cannot&amp;nbsp;&lt;strong&gt;&lt;a href="http://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk5.v14.2.0%2Fgroup__app__pwm.html&amp;amp;cp=4_0_0_6_11_33_15&amp;amp;anchor=gab496bf7cd3acaec3a1fafdc75442d503" rel="noopener noreferrer" target="_blank"&gt;disable&lt;/a&gt;&lt;/strong&gt;&amp;nbsp;an instance that is not yet enabled.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I would also recommend you to look at the &lt;a href="https://github.com/bjornspockeli/nRF52_peripherals_tutorial" rel="noopener noreferrer" target="_blank"&gt;nRF52 peripheral tutorial&lt;/a&gt;&amp;nbsp;made by &lt;a href="https://devzone.nordicsemi.com/members/bjorn_2d00_spockeli" rel="noopener noreferrer" target="_blank"&gt;Bjorn&lt;/a&gt;. In step 4 you can see how you can use the PWM library to control a servo.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>