使用管道时,gpio中断事件进不去

ChiShaoJun
Posts: 26
Joined: Mon Sep 17, 2018 3:24 am

使用管道时,gpio中断事件进不去

Postby ChiShaoJun » Fri Mar 15, 2019 8:11 am

这边gpio中断部分代码使用的是esp-idf的gpio代码,我这边使用管道时,gpio中断事件没法触发,请问应该怎么处理。
  1. /* Example of Voice Activity Detection (VAD)
  2.  
  3.    This example code is in the Public Domain (or CC0 licensed, at your option.)
  4.  
  5.    Unless required by applicable law or agreed to in writing, this
  6.    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  7.    CONDITIONS OF ANY KIND, either express or implied.
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include "freertos/FreeRTOS.h"
  14. #include "esp_log.h"
  15. #include "audio_hal.h"
  16. #include "zl38063.h"
  17. #include "audio_common.h"
  18. #include "audio_pipeline.h"
  19. #include "i2s_stream.h"
  20. #include "raw_stream.h"
  21. #include "filter_resample.h"
  22. #include "esp_vad.h"
  23.  
  24. #include "esp_peripherals.h"
  25. #include "periph_touch.h"
  26.  
  27. static const char *TAG = "EXAMPLE-VAD";
  28.  
  29. #define VAD_SAMPLE_RATE_HZ 16000
  30. #define VAD_FRAME_LENGTH_MS 30
  31. #define VAD_BUFFER_LENGTH (VAD_FRAME_LENGTH_MS * VAD_SAMPLE_RATE_HZ / 1000)
  32.  
  33. #define LYRAT_TOUCH_SET     TOUCH_PAD_NUM9
  34. #define LYRAT_TOUCH_PLAY    TOUCH_PAD_NUM8
  35. #define LYRAT_TOUCH_VOLUP   TOUCH_PAD_NUM7
  36. #define LYRAT_TOUCH_VOLDWN  TOUCH_PAD_NUM4
  37.  
  38. #define GPIO_OUTPUT_IO_0    5
  39. #define GPIO_OUTPUT_PIN_SEL  ((1ULL<<GPIO_OUTPUT_IO_0) )
  40. #define GPIO_INPUT_IO_0     25
  41. #define GPIO_INPUT_PIN_SEL  ((1ULL<<GPIO_INPUT_IO_0) )
  42. #define ESP_INTR_FLAG_DEFAULT 0
  43.  
  44. /* Initialize one of supported audio codecs
  45.  * basing on board selection in menuconfig
  46.  */
  47. audio_hal_handle_t init_audio_codec()
  48. {
  49. #if (CONFIG_ESP_LYRAT_V4_3_BOARD || CONFIG_ESP_LYRAT_V4_2_BOARD)
  50.     audio_hal_codec_config_t audio_hal_codec_cfg = AUDIO_HAL_ES8388_DEFAULT();
  51.     audio_hal_codec_cfg.adc_input = AUDIO_HAL_ADC_INPUT_LINE2;
  52.     return audio_hal_init(&audio_hal_codec_cfg, 0);
  53. #endif
  54.  
  55. #if (CONFIG_ESP_LYRATD_MSC_V2_1_BOARD || CONFIG_ESP_LYRATD_MSC_V2_2_BOARD)
  56.     audio_hal_codec_config_t audio_hal_codec_cfg = AUDIO_HAL_ZL38063_DEFAULT();
  57.     return audio_hal_init(&audio_hal_codec_cfg, 2);
  58. #endif
  59. }
  60.  
  61. static xQueueHandle gpio_evt_queue = NULL;
  62.  
  63. static void IRAM_ATTR gpio_isr_handler(void* arg)
  64. {
  65.     uint32_t gpio_num = (uint32_t) arg;
  66.     xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL);
  67. }
  68.  
  69. static void gpio_task_example(void* arg)
  70. {
  71.     uint32_t io_num;
  72.     for(;;) {
  73.         if(xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) {
  74.             printf("GPIO[%d] intr, val: %d\n", io_num, gpio_get_level(io_num));
  75.         }
  76.     }
  77. }
  78.  
  79. static void gpio_task_change(void* arg)
  80. {
  81.     int cnt = 0;
  82.     while(1) {
  83.         printf("cnt: %d\n", cnt++);
  84.         vTaskDelay(1000 / portTICK_RATE_MS);
  85.         gpio_set_level(GPIO_OUTPUT_IO_0, cnt % 2);
  86.         //gpio_set_level(GPIO_OUTPUT_IO_1, cnt % 2);
  87.     }
  88. }
  89.  
  90. void gpio_init(void)
  91. {
  92.     gpio_config_t io_conf;
  93.     //disable interrupt
  94.     io_conf.intr_type = GPIO_PIN_INTR_DISABLE;
  95.     //set as output mode
  96.     io_conf.mode = GPIO_MODE_OUTPUT;
  97.     //bit mask of the pins that you want to set,e.g.GPIO18/19
  98.     io_conf.pin_bit_mask = GPIO_OUTPUT_PIN_SEL;
  99.     //disable pull-down mode
  100.     io_conf.pull_down_en = 0;
  101.     //disable pull-up mode
  102.     io_conf.pull_up_en = 0;
  103.     //configure GPIO with the given settings
  104.     gpio_config(&io_conf);
  105.  
  106.     //interrupt of rising edge
  107.     io_conf.intr_type = GPIO_PIN_INTR_POSEDGE;
  108.     //bit mask of the pins, use GPIO4/5 here
  109.     io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL;
  110.     //set as input mode
  111.     io_conf.mode = GPIO_MODE_INPUT;
  112.     //enable pull-up mode
  113.     io_conf.pull_up_en = 1;
  114.     gpio_config(&io_conf);
  115.  
  116.     //change gpio intrrupt type for one pin
  117.     gpio_set_intr_type(GPIO_INPUT_IO_0, GPIO_INTR_ANYEDGE);
  118.  
  119.     //create a queue to handle gpio event from isr
  120.     gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t));
  121.     //start gpio task
  122.     xTaskCreate(gpio_task_example, "gpio_task_example", 2048, NULL, 10, NULL);
  123.  
  124.     //install gpio isr service
  125.     gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
  126.     //hook isr handler for specific gpio pin
  127.     gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void*) GPIO_INPUT_IO_0);
  128.     //hook isr handler for specific gpio pin
  129.     //gpio_isr_handler_add(GPIO_INPUT_IO_1, gpio_isr_handler, (void*) GPIO_INPUT_IO_1);
  130.  
  131.     //remove isr handler for gpio number.
  132.     gpio_isr_handler_remove(GPIO_INPUT_IO_0);
  133.     //hook isr handler for specific gpio pin again
  134.     gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void*) GPIO_INPUT_IO_0);
  135. }
  136.  
  137. void app_main()
  138. {
  139.     esp_log_level_set("*", ESP_LOG_WARN);
  140.     esp_log_level_set(TAG, ESP_LOG_INFO);
  141.  
  142.     ESP_LOGI(TAG, "[ 1 ] Start codec chip");
  143.     audio_hal_ctrl_codec(init_audio_codec(), AUDIO_HAL_CODEC_MODE_BOTH, AUDIO_HAL_CTRL_START);
  144.  
  145.     audio_pipeline_handle_t pipeline;
  146.     audio_element_handle_t i2s_stream_reader, i2s_stream_writer;
  147.  
  148.     ESP_LOGI(TAG, "[ 2 ] Create audio pipeline for recording");
  149.     audio_pipeline_cfg_t pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG();
  150.     pipeline = audio_pipeline_init(&pipeline_cfg);
  151.     mem_assert(pipeline);
  152.  
  153.     ESP_LOGI(TAG, "[2.1] Create i2s stream to read audio data from codec chip");
  154.     i2s_stream_cfg_t i2s_r_cfg = I2S_STREAM_CFG_DEFAULT();
  155.     i2s_r_cfg.type = AUDIO_STREAM_READER;
  156.     i2s_stream_reader = i2s_stream_init(&i2s_r_cfg);
  157.  
  158.     ESP_LOGI(TAG, "[2.2] Create i2s stream to write data to codec chip");
  159.     i2s_stream_cfg_t i2s_w_cfg = I2S_STREAM_CFG_DEFAULT();
  160.     i2s_w_cfg.type = AUDIO_STREAM_WRITER;
  161.     i2s_w_cfg.i2s_config.sample_rate = 48000;
  162.     i2s_stream_writer = i2s_stream_init(&i2s_w_cfg);
  163.  
  164.     ESP_LOGI(TAG, "[ 3 ] Register all elements to audio pipeline");
  165.     audio_pipeline_register(pipeline, i2s_stream_reader, "i2sr");
  166.     audio_pipeline_register(pipeline, i2s_stream_writer, "i2sw");
  167.  
  168.     ESP_LOGI(TAG, "[ 4 ] Link elements together");
  169.     audio_pipeline_link(pipeline, (const char *[]) {"i2sr", "i2sw"}, 2);
  170.  
  171.     //5.初始化外围设备
  172.     ESP_LOGI(TAG, "[ 5 ] Initialize peripherals");
  173.     esp_periph_config_t periph_cfg = { 0 };
  174.     esp_periph_init(&periph_cfg);
  175.  
  176.     //5.1.初始化Touch外围设备
  177.     ESP_LOGI(TAG, "[5.1] Initialize Touch peripheral");
  178.     periph_touch_cfg_t touch_cfg = {
  179.         .touch_mask = TOUCH_SEL_SET | TOUCH_SEL_PLAY | TOUCH_SEL_VOLUP | TOUCH_SEL_VOLDWN,
  180.         .tap_threshold_percent = 70,
  181.     };
  182.     esp_periph_handle_t touch_periph = periph_touch_init(&touch_cfg);
  183.  
  184.     //5.2.启动所有外围设备
  185.     ESP_LOGI(TAG, "[5.2] Start all peripherals");
  186.     esp_periph_start(touch_periph);
  187.  
  188.     ESP_LOGI(TAG, "[6] Setup event listener");
  189.     audio_event_iface_cfg_t evt_cfg = AUDIO_EVENT_IFACE_DEFAULT_CFG();
  190.     audio_event_iface_handle_t evt = audio_event_iface_init(&evt_cfg);
  191.  
  192.     //6.1.来自管道的所有元素的监听事件
  193.     ESP_LOGI(TAG, "[6.1] Listening event from all elements of pipeline");
  194.     audio_pipeline_set_listener(pipeline, evt);
  195.  
  196.     //6.2.外围设备的监听事件
  197.     ESP_LOGI(TAG, "[6.2] Listening event from peripherals");
  198.     audio_event_iface_set_listener(esp_periph_get_event_iface(), evt);
  199.  
  200.     //初始化gpio事件
  201.     gpio_init();
  202.     xTaskCreate(gpio_task_change, "gpio_task_change", 2048, NULL, 10, NULL);
  203.  
  204.     //7.启动audio_pipeline
  205.     ESP_LOGI(TAG, "[ 7 ] Start audio_pipeline");
  206.     audio_pipeline_run(pipeline);
  207.  
  208.     //8.监听所有管道事件
  209.     ESP_LOGI(TAG, "[ 8 ] Listen for all pipeline events");
  210.     while (1) {
  211.         vTaskDelay(3000 / portTICK_RATE_MS);
  212.         audio_event_iface_msg_t msg;
  213.         esp_err_t ret = audio_event_iface_listen(evt, &msg, portMAX_DELAY);
  214.         if (ret != ESP_OK) {
  215.             ESP_LOGE(TAG, "[ * ] Event interface error : %d", ret);
  216.             continue;
  217.         }
  218.  
  219.         if (msg.cmd == AEL_MSG_CMD_ERROR) {
  220.             ESP_LOGE(TAG, "[ * ] Action command error: src_type:%d, source:%p cmd:%d, data:%p, data_len:%d",
  221.                      msg.source_type, msg.source, msg.cmd, msg.data, msg.data_len);
  222.         }
  223.  
  224.         if (msg.source_type == PERIPH_ID_TOUCH
  225.             && msg.cmd == PERIPH_TOUCH_TAP
  226.             && msg.source == (void *)touch_periph) {
  227.  
  228.             if ((int) msg.data == TOUCH_PLAY) {
  229.                 ESP_LOGI(TAG, "[ * ] [Play] touch tap event");
  230.             } else if ((int) msg.data == TOUCH_SET) {
  231.                 ESP_LOGI(TAG, "[ * ] [Set] touch tap event");
  232.             } else if ((int) msg.data == TOUCH_VOLUP) {
  233.                 ESP_LOGI(TAG, "[ * ] [Vol+] touch tap event");
  234.             } else if ((int) msg.data == TOUCH_VOLDWN) {
  235.                 ESP_LOGI(TAG, "[ * ] [Vol-] touch tap event");
  236.             }
  237.         }
  238.         /* Stop when the last pipeline element (i2s_stream_writer in this case) receives stop event */
  239.         if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg.source == (void *) i2s_stream_writer
  240.             && msg.cmd == AEL_MSG_CMD_REPORT_STATUS && (int) msg.data == AEL_STATUS_STATE_STOPPED) {
  241.             ESP_LOGW(TAG, "[ * ] Stop event received");
  242.             break;
  243.         }
  244.     }
  245.  
  246.     ESP_LOGI(TAG, "[ 9 ] Stop audio_pipeline and release all resources");
  247.     audio_pipeline_terminate(pipeline);
  248.  
  249.     /* Terminate the pipeline before removing the listener */
  250.     audio_pipeline_remove_listener(pipeline);
  251.  
  252.     audio_pipeline_unregister(pipeline, i2s_stream_reader);
  253.     audio_pipeline_unregister(pipeline, i2s_stream_writer);
  254.  
  255.     //在删除侦听器之前停止所有外围设备
  256.     /* Stop all peripherals before removing the listener */
  257.     esp_periph_stop_all();
  258.     audio_event_iface_remove_listener(esp_periph_get_event_iface(), evt);
  259.  
  260.     //确保在销毁event_iface之前调用audio_pipeline_remove_listener和audio_event_iface_remove_listener
  261.     /* Make sure audio_pipeline_remove_listener & audio_event_iface_remove_listener are called before destroying event_iface */
  262.     audio_event_iface_destroy(evt);
  263.  
  264.     /* Release all resources */
  265.     audio_pipeline_deinit(pipeline);
  266.     audio_element_deinit(i2s_stream_reader);
  267.     audio_element_deinit(i2s_stream_writer);
  268.     esp_periph_destroy();
  269.  
  270. }

Who is online

Users browsing this forum: arunbm123 and 2 guests