背景:
IDF版本: ESP-IDF v4.0-beta1-134-gef17d8bba-dirty
硬件:ESP32 WROVER-B 8Mflaash
GPIO39硬件电路已外接上拉电阻,如果注释掉BLE部分代码,GPIO39中断触发是正常的。
关键代码:
在demo例程ble_spp_server的基础上添加GPIO39的GPIO初始化和中断初始化函数
- static void IRAM_ATTR gpio_isr_handler(void* arg)
- {
- uint32_t gpio_num = (uint32_t) arg;
- BaseType_t xHigherPriorityTaskWoken;
- xHigherPriorityTaskWoken = pdFALSE;
- ets_printf("GPIO39 interrupt %d \r\n",gpio_num);
- }
- void gpio39_test(void)
- {
- gpio_config_t io_conf;
- //input
- //disable interrupt
- io_conf.intr_type = GPIO_PIN_INTR_DISABLE;
- //bit mask of the pins, use GPIO4/5 here
- io_conf.pin_bit_mask = 1ULL<<39;
- //set as input mode
- io_conf.mode = GPIO_MODE_INPUT;
- //disable pull-down mode
- io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
- //disable pull-up mode
- io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
- gpio_config(&io_conf);
- gpio_set_intr_type(39, GPIO_INTR_ANYEDGE);
- gpio_install_isr_service(0);
- //hook isr handler for HALL gpio pin
- gpio_isr_handler_add(39, gpio_isr_handler, (void*) 39);
- }
- void app_main(void)
- {
- gpio39_test();
- esp_err_t ret;
- esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
- // Initialize NVS
- 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));
- ret = esp_bt_controller_init(&bt_cfg);
- if (ret) {
- ESP_LOGE(GATTS_TABLE_TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(ret));
- return;
- }
- ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
- if (ret) {
- ESP_LOGE(GATTS_TABLE_TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(ret));
- return;
- }
- ESP_LOGI(GATTS_TABLE_TAG, "%s init bluetooth\n", __func__);
- ret = esp_bluedroid_init();
- if (ret) {
- ESP_LOGE(GATTS_TABLE_TAG, "%s init bluetooth failed: %s\n", __func__, esp_err_to_name(ret));
- return;
- }
- ret = esp_bluedroid_enable();
- if (ret) {
- ESP_LOGE(GATTS_TABLE_TAG, "%s enable bluetooth failed: %s\n", __func__, esp_err_to_name(ret));
- return;
- }
- esp_ble_gatts_register_callback(gatts_event_handler);
- esp_ble_gap_register_callback(gap_event_handler);
- esp_ble_gatts_app_register(ESP_SPP_APP_ID);
- spp_task_init();
- return;
- }