Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

PWM disable doesn't work. Can't turn off PWM.

Hi! I am using an nRF51 DK board and the app_pwm library. I am using SDK 12.3.0.

Why doesn't

app_pwm_disable(&PWM1);

stop the PWM? I call it right after initialization:

// initialization in main.c
static int pwm_value = 50;

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	(3822L, 25);

// in main() function:
    NRF_LOG_INFO("Initializing PWM For Buzzer...\r\n");
	
		err_code = app_pwm_init(&PWM1, &pwm1_cfg, pwm_complete_callback); 
		APP_ERROR_CHECK(err_code);
		while (app_pwm_channel_duty_set(&PWM1, 0, pwm_value) == NRF_ERROR_BUSY);
		
		// !!! Why doesn't this stop PWM?
		app_pwm_disable(&PWM1);
		
    // Start execution.
    NRF_LOG_INFO("Blinky Start!\r\n");
    advertising_start();

    // Enter main loop.
    for (;;)
    {
        if (NRF_LOG_PROCESS() == false)
        {
            power_manage();
        }
    }

... as well as in a button handler, and neither stop PWM from happening.

I am using it to control a buzzer, so it doesn't matter whether it stops high or low. It just needs to stop.

None of the solutions on other threads worked, and they were all for older versions of the SDK <10.

Parents
  • Hi Jonathan,

    To be honest I am a bit confused what your issue is here. Do you get unwanted PWM behaviour even if it's not enabled ?  Does your "app_pwm_init" return something other than "NRF_SUCCESS"  if you set a breakpoint?

    I have tested this with the "pwm_library" example in the SDK, by just playing with the disable/enable functions which that turns on and off the PWM.  

     

    What happens if you do not initialize the PWM at all? Do you still have unwanted behaviour?

    Are you sure you have connected the buzzer correctly? 

    Is it constant giving out sound? 

    Could you please describe your setup a bit more and send me your whole main.c file.

     

  • Here's my entire main.c. It is based on the BLE Blinky example (which was SUPER helpful btw):

    
    #include <stdint.h>
    #include <string.h>
    #include "app_pwm.h"
    #include "nordic_common.h"
    #include "nrf.h"
    #include "app_error.h"
    #include "ble.h"
    #include "ble_hci.h"
    #include "ble_srv_common.h"
    #include "ble_advdata.h"
    #include "ble_conn_params.h"
    #include "softdevice_handler.h"
    #include "app_timer.h"
    #include "app_button.h"
    #include "ble_lbs.h"
    #include "bsp.h"
    #include "ble_gap.h"
    
    #define NRF_LOG_MODULE_NAME "APP"
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    
    #define CENTRAL_LINK_COUNT              0                                           /**< Number of central links used by the application. When changing this number remember to adjust the RAM settings*/
    #define PERIPHERAL_LINK_COUNT           1                                           /**< 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                       /**< 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        /**< Reply when unsupported features are requested. */
    
    #define ADVERTISING_LED_PIN             BSP_BOARD_LED_0                             /**< Is on when device is advertising. */
    #define CONNECTED_LED_PIN               BSP_BOARD_LED_1                             /**< Is on when device has connected. */
    
    #define LEDBUTTON_LED_PIN               BSP_BOARD_LED_2                             /**< LED to be toggled with the help of the LED Button Service. */
    #define LEDBUTTON_BUTTON_PIN            BSP_BUTTON_0                                /**< Button that will trigger the notification event with the LED Button Service */
    
    #define DEVICE_NAME                     "Nordic_Blinky"                             /**< Name of device. Will be included in the advertising data. */
    
    #define APP_ADV_INTERVAL                64                                          /**< 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       /**< The advertising time-out (in units of seconds). When set to 0, we will never time out. */
    
    #define APP_TIMER_PRESCALER             0                                           /**< Value of the RTC1 PRESCALER register. */
    #define APP_TIMER_MAX_TIMERS            6                                           /**< Maximum number of simultaneously created timers. */
    #define APP_TIMER_OP_QUEUE_SIZE         4                                           /**< Size of timer operation queues. */
    
    #define MIN_CONN_INTERVAL               MSEC_TO_UNITS(100, UNIT_1_25_MS)            /**< Minimum acceptable connection interval (0.5 seconds). */
    #define MAX_CONN_INTERVAL               MSEC_TO_UNITS(200, UNIT_1_25_MS)            /**< Maximum acceptable connection interval (1 second). */
    #define SLAVE_LATENCY                   0                                           /**< Slave latency. */
    #define CONN_SUP_TIMEOUT                MSEC_TO_UNITS(4000, UNIT_10_MS)             /**< Connection supervisory time-out (4 seconds). */
    #define FIRST_CONN_PARAMS_UPDATE_DELAY  APP_TIMER_TICKS(20000, APP_TIMER_PRESCALER) /**< 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)  /**< Time between each call to sd_ble_gap_conn_param_update after the first call (5 seconds). */
    #define MAX_CONN_PARAMS_UPDATE_COUNT    3                                           /**< Number of attempts before giving up the connection parameter negotiation. */
    
    #define APP_GPIOTE_MAX_USERS            1                                           /**< Maximum number of users of the GPIOTE handler. */
    #define BUTTON_DETECTION_DELAY          APP_TIMER_TICKS(50, APP_TIMER_PRESCALER)    /**< Delay from a GPIOTE event until a button is reported as pushed (in number of timer ticks). */
    
    #define DEAD_BEEF                       0xDEADBEEF                                  /**< 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;    /**< Handle of the current connection. */
    static ble_lbs_t                        m_lbs;                                      /**< 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(&PWM1);	// Does nothing :(
    	//while (app_pwm_channel_duty_set(&PWM1, 0, 100) == NRF_ERROR_BUSY);  // This works :)
    }
    
    void buzzer_on() {
    	app_pwm_enable(&PWM1); 
    	//while (app_pwm_channel_duty_set(&PWM1, 0, 50) == NRF_ERROR_BUSY); // This works :)
    }
    
    void buzzer_init() {
    	ret_code_t err_code;
    	NRF_LOG_INFO("Initializing PWM For Buzzer...\r\n");
    	
    	err_code = app_pwm_init(&PWM1, &pwm1_cfg, pwm_complete_callback); 
    	APP_ERROR_CHECK(err_code);
    	app_pwm_enable(&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(&sec_mode);
    
        err_code = sd_ble_gap_device_name_set(&sec_mode,
                                              (const uint8_t *)DEVICE_NAME,
                                              strlen(DEVICE_NAME));
        APP_ERROR_CHECK(err_code);
    
        memset(&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(&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(&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(&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(&advdata, &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("Received LED ON!\r\n");
    				child_present = true;
        }
        else
        {
            bsp_board_led_off(LEDBUTTON_LED_PIN);
            NRF_LOG_INFO("Received LED OFF!\r\n");
    				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(&m_lbs, &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->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(&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(&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(&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(&adv_params);
        APP_ERROR_CHECK(err_code);
        bsp_board_led_on(ADVERTISING_LED_PIN);
    }
    
    
    /**@brief Function for handling the Application'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->header.evt_id)
        {
            case BLE_GAP_EVT_CONNECTED:
                NRF_LOG_INFO("Connected\r\n");
                bsp_board_led_on(CONNECTED_LED_PIN);
                bsp_board_led_off(ADVERTISING_LED_PIN);
                m_conn_handle = p_ble_evt->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("Disconnected\r\n");
                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("GATT Client Timeout.\r\n");
                err_code = sd_ble_gap_disconnect(p_ble_evt->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("GATT Server Timeout.\r\n");
                err_code = sd_ble_gap_disconnect(p_ble_evt->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->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->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->evt.gatts_evt.conn_handle,
                                                                   &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->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(&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(&clock_lf_cfg, NULL);
    
        ble_enable_params_t ble_enable_params;
        err_code = softdevice_enable_get_default_config(CENTRAL_LINK_COUNT,
                                                        PERIPHERAL_LINK_COUNT,
                                                        &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(&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("Send button state change.\r\n");            
                err_code = ble_lbs_on_button_change(&m_lbs, button_action);
                if (err_code != NRF_SUCCESS &&
                    err_code != BLE_ERROR_INVALID_CONN_HANDLE &&
                    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("Blinky Start!\r\n");
        advertising_start();
    
        // Enter main loop.
        for (;;)
        {
            if (NRF_LOG_PROCESS() == false)
            {
                power_manage();
            }
        }
    }
    
    
    /**
     * @}
     */
    

    Pin 25 is hooked to a PIezzo Buzzer, and it buzzes really loud! Here are the relevant snippets from main.c:

    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(&PWM1);	// Does nothing :(
    	//while (app_pwm_channel_duty_set(&PWM1, 0, 100) == NRF_ERROR_BUSY);  // This works :)
    }
    
    void buzzer_on() {
    	app_pwm_enable(&PWM1); 
    	//while (app_pwm_channel_duty_set(&PWM1, 0, 50) == NRF_ERROR_BUSY); // This works :)
    }
    
    void buzzer_init() {
    	ret_code_t err_code;
    	NRF_LOG_INFO("Initializing PWM For Buzzer...\r\n");
    	
    	err_code = app_pwm_init(&PWM1, &pwm1_cfg, pwm_complete_callback); 
    	APP_ERROR_CHECK(err_code);
    	app_pwm_enable(&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("Send button state change.\r\n");            
                err_code = ble_lbs_on_button_change(&m_lbs, button_action);
                if (err_code != NRF_SUCCESS &&
                    err_code != BLE_ERROR_INVALID_CONN_HANDLE &&
                    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("Blinky Start!\r\n");
        advertising_start();
    
        // Enter main loop.
        for (;;)
        {
            if (NRF_LOG_PROCESS() == false)
            {
                power_manage();
            }
        }
    }

    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?

  • Hello Jonathan, 

    Sorry for the delay, I have been on vacation.

    Have you made any progress and/or updates to this case?

     

  • No, I haven'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.

  • 'app_pwm_disable(') is still dosen't work.
    sdk is 16. 
    Is there a solution?

Reply Children
Related