MCU Resets on bt_le_adv_update_data in GPIO Interrup

Hi,

I have used course's example for GPIO interrupt for toggling LED and used to update BLE adv parameters without using DK buttons library. But, as soon as, I update BLE adv parameters the MCU resets. I know the DK library is quite comprehensive but want to know what is the issue in GPIO interrupt which leads to this reset.

This is my main code.

#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/pwm.h>

#include "remote.h"

#define         SLEEP_TIME_MS           1000
#define         LED0_NODE               DT_ALIAS(led0)
#define         SW0_NODE                DT_ALIAS(sw0)

bool int_flag;

static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
static const struct gpio_dt_spec sw = GPIO_DT_SPEC_GET(SW0_NODE, gpios);

static const struct device *gpio_ct_dev = DEVICE_DT_GET(DT_NODELABEL(gpio0));

static const struct pwm_dt_spec pwm_led0 = PWM_DT_SPEC_GET(DT_ALIAS(pwmled0));

static struct gpio_callback button_cb_data;

void button_pressed (const struct device *dev, struct gpio_callback *cb, uint32_t pins) {
        //gpio_pin_toggle_dt(&led);
        
        gpio_pin_toggle(gpio_ct_dev, 13);
        bt_update_ad();
}

int main(void)
{

	int ret, err;
        bool val;

        if (!device_is_ready(led.port))
        {
                return;
        }

        if (!device_is_ready(pwm_led0.dev))
        {
                return 0;
        }


        ret = gpio_pin_configure(gpio_ct_dev, 13, GPIO_OUTPUT);
        ret = gpio_pin_configure(gpio_ct_dev, 11, (GPIO_INPUT | GPIO_PULL_UP | GPIO_ACTIVE_LOW));

        gpio_pin_configure_dt(&led,GPIO_OUTPUT_ACTIVE);
        gpio_pin_configure_dt(&sw, GPIO_INPUT);

        gpio_pin_interrupt_configure(gpio_ct_dev, 11, GPIO_INT_EDGE_TO_ACTIVE);

        gpio_init_callback(&button_cb_data, &button_pressed, BIT(11));

        gpio_add_callback(gpio_ct_dev, &button_cb_data);

        err = bluetooth_init();
        if (err) {
                return -1;
        }

        for (;;) {

        }
        
}

This is my remote.c:

#include "remote.h"

#define DEVICE_NAME CONFIG_BT_DEVICE_NAME
#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)

#define COMPANY_ID_CODE            0x0059 // Nordic Semiconductor company

typedef struct adv_mfg_data {
	uint16_t company_code;	    /* Company Identifier Code. */
	uint16_t number_press;      /* Number of times Button 1 is pressed*/
} adv_mfg_data_type;

static adv_mfg_data_type adv_mfg_data = {COMPANY_ID_CODE,0x00};

static unsigned char url_data[] ={0x17,'/','/','a','c','a','d','e','m','y','.',
                                 'n','o','r','d','i','c','s','e','m','i','.',
                                 'c','o','m'};

static struct bt_le_adv_param *adv_param =
	BT_LE_ADV_PARAM(BT_LE_ADV_OPT_NONE,
	800,
	801,
	NULL);

static const struct bt_data ad[] = {
	/* STEP 4.1.2 - Set the advertising flags */
    BT_DATA_BYTES(BT_DATA_FLAGS, BT_LE_AD_NO_BREDR),
	/* STEP 4.1.3 - Set the advertising packet data  */
    BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
    BT_DATA(BT_DATA_MANUFACTURER_DATA,(unsigned char *)&adv_mfg_data, sizeof(adv_mfg_data)),
};

static const struct bt_data sd[] = {
        /* 4.2.3 Include the URL data in the scan response packet*/
    BT_DATA(BT_DATA_URI, url_data,sizeof(url_data)),
};

int bluetooth_init(void)
{
    int err;

    err = err = bt_enable(NULL);
    if (err) {
        
        return -1;
    }
    
    err = bt_le_adv_start(adv_param, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
    if (err) {
	
	return -1;
    }

    return err;
}

void bt_update_ad(void)
{
    adv_mfg_data.number_press += 1;
    bt_le_adv_update_data(ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
}

As soon as bt_update_ad() is called in button_pressed which GPIO callback, MCU resets.

Please guide what should be implemented in this raw GPIO interrupt callback to avoid MCU reset to update adv parameters.

Regards

Related