{
// Initialize NVS.
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK( ret );
ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
ret = esp_bt_controller_init(&bt_cfg);
if (ret)
{
ESP_LOGE(GATTC_TAG, "%s initialize controller failed: %s\n", __func__, esp_err_to_name(ret));
return;
}
ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
if (ret)
{
ESP_LOGE(GATTC_TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(ret));
return;
}
ret = esp_bluedroid_init();
if (ret)
{
ESP_LOGE(GATTC_TAG, "%s init bluetooth failed: %s\n", __func__, esp_err_to_name(ret));
return;
}
ret = esp_bluedroid_enable();
if (ret)
{
ESP_LOGE(GATTC_TAG, "%s enable bluetooth failed: %s\n", __func__, esp_err_to_name(ret));
return;
}
//register the callback function to the gap module
ret = esp_ble_gap_register_callback(esp_gap_cb);
if (ret)
{
ESP_LOGE(GATTC_TAG, "%s gap register failed, error code = %x\n", __func__, ret);
return;
}
//register the callback function to the gattc module
ret = esp_ble_gattc_register_callback(esp_gattc_cb);
if(ret)
{
ESP_LOGE(GATTC_TAG, "%s gattc register failed, error code = %x\n", __func__, ret);
return;
}
ret = esp_ble_gattc_app_register(PROFILE_A_APP_ID);
if (ret)
{
ESP_LOGE(GATTC_TAG, "%s gattc app register failed, error code = %x\n", __func__, ret);
}
esp_err_t local_mtu_ret = esp_ble_gatt_set_local_mtu(500);
if (local_mtu_ret)
{
ESP_LOGE(GATTC_TAG, "set local MTU failed, error code = %x", local_mtu_ret);
}
/*************************************************/
/* Configure the button GPIO as input, enable wakeup */
gpio_config_t io_conf;
//interrupt of rising edge
io_conf.intr_type = GPIO_INTR_POSEDGE;
//bit mask of the pins, use GPIO4/5 here
io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL;
//set as input mode
io_conf.mode = GPIO_MODE_INPUT;
//enable pull-up mode
io_conf.pull_up_en = 1;
gpio_config(&io_conf);
ESP_ERROR_CHECK(gpio_config(&io_conf));
Code: Select all
gpio_wakeup_enable(GPIO_NUM_4, GPIO_INTR_LOW_LEVEL);
gpio_wakeup_enable(GPIO_NUM_5, GPIO_INTR_LOW_LEVEL);
gpio_wakeup_enable(GPIO_NUM_6, GPIO_INTR_LOW_LEVEL);
gpio_wakeup_enable(GPIO_NUM_7, GPIO_INTR_LOW_LEVEL);
//change gpio intrrupt type for one pin
gpio_set_intr_type(GPIO_INPUT_IO_0, GPIO_INTR_ANYEDGE);
gpio_set_intr_type(GPIO_INPUT_IO_1, GPIO_INTR_ANYEDGE);
gpio_set_intr_type(GPIO_INPUT_IO_2, GPIO_INTR_ANYEDGE);
gpio_set_intr_type(GPIO_INPUT_IO_3, GPIO_INTR_ANYEDGE);
gpio_set_intr_type(GPIO_INPUT_IO_4, GPIO_INTR_ANYEDGE);
gpio_set_intr_type(GPIO_INPUT_IO_5, GPIO_INTR_ANYEDGE);
//create a queue to handle gpio event from isr
gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t));
//start gpio task
xTaskCreate(gpio_task_example, "gpio_task_example", 2048, NULL, 10, NULL);
//install gpio isr service
gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
//hook isr handler for specific gpio pin
gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void *)GPIO_INPUT_IO_0);
//hook isr handler for specific gpio pin
gpio_isr_handler_add(GPIO_INPUT_IO_1, gpio_isr_handler, (void *)GPIO_INPUT_IO_1);
//hook isr handler for specific gpio pin
gpio_isr_handler_add(GPIO_INPUT_IO_2, gpio_isr_handler, (void *)GPIO_INPUT_IO_2);
//hook isr handler for specific gpio pin
gpio_isr_handler_add(GPIO_INPUT_IO_3, gpio_isr_handler, (void *)GPIO_INPUT_IO_3);
//hook isr handler for specific gpio pin
gpio_isr_handler_add(GPIO_INPUT_IO_4, gpio_isr_handler, (void *)GPIO_INPUT_IO_4);
//hook isr handler for specific gpio pin
gpio_isr_handler_add(GPIO_INPUT_IO_5, gpio_isr_handler, (void *)GPIO_INPUT_IO_5);
printf("Minimum free heap size: %d bytes\n", esp_get_minimum_free_heap_size());
/*************************************************/
esp_sleep_enable_gpio_wakeup();
while(true)
{
printf("InterruptCounter: %d\n", InterruptCounter++);
printf("cnt0: %d\n", cnt0++);
vTaskDelay(1000 / portTICK_RATE_MS);
if( cnt0 >10 )
{
esp_light_sleep_start();
}
}
}