乐鑫技术交流
直播中

刘慧

7年用户 1649经验值
私信 关注
[问答]

想实现一个按键发送MQTT消息功能,按键按下后无法在任务函数中发送MQTT消息怎么解决?

想实现一个按键发送MQTT消息功能,MQTT收发测试已经没有问题,按键按下后中断函数也可以正常打印,但是无法再按键按下后任务函数中发送MQTT消息,一直报错

IO中断部分代码

/**

* GPIO中断部分

*/

static xQueueHandle gpio_evt_queue = NULL;

static void IRAM_ATTR gpio_isr_handler(void* arg)

{

    uint32_t gpio_num = (uint32_t) arg;

    xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL);

}





/**

* 按键处理函数

*/

int last_time = 0;

static void key_task(void* arg)

{

    uint32_t io_num;

    for(;;) {

        if(xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) {

            // printf("GPIO[%d] intr, val: %dn", io_num, gpio_get_level(io_num));



            int now = xTaskGetTickCountFromISR();

            if(gpio_get_level(io_num) == 1){ //高电平 判断短按还是长按

                //获取系统当前运行的时钟节拍数,此函数用于在中断服务程序里面调用, 如果在任务里面调用的话,需要使用函数 xTaskGetTickCount,这两个函数切不可混用

                if (now - last_time > MIN_DELAY) { //长按

                    printf("long %d %dn", now, last_time);

                    gpio_set_level(3, 0);

                    //长按业务逻辑

                }else if (now - last_time < 5){ //消抖 无操作

                    

                }else{ //短按

                    printf("short %d %dn", now, last_time);

                    gpio_set_level(3, 1);

                    //短按业务逻辑 上报

                    // char topic_init[64];

                    // sprintf(topic_init, "/c/%s/send", mqtt_user);

                    if(client){

                        esp_mqtt_client_publish(client, '/test', "find", 0, 1, 0); //这行有问题,执行后重启

                        printf("send okn");

                    }else{

                        printf("no client");

                    }

                    // ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);

                }

            }else{ //低电平 按下按键

               

            }

            last_time = now;

        }

    }

}





错误信息

assert failed: xQueueTakeMutexRecursive queue.c:673 (pxMutex)

Stack dump detected

Core  0 register dump:

MEPC    : 0x4038061e  RA      : 0x403879ec  SP      : 0x3fcbe1d0  GP      : 0x3fc904f8  

0x4038061e: panic_abort at C:/Users/micro/Desktop/esp-idf/components/esp_system/panic.c:404



0x403879ec: rtos_int_enter at C:/Users/micro/Desktop/esp-idf/components/freertos/port/riscv/portasm.S:35



TP      : 0x3fc7f014  T0      : 0x37363534  T1      : 0x7271706f  T2      : 0x33323130

S0/FP   : 0x0000008a  S1      : 0x00000001  A0      : 0x3fcbe20c  A1      : 0x3fc91ed1

A2      : 0x00000001  A3      : 0x00000029  A4      : 0x00000001  A5      : 0x3fc9c000

A6      : 0x7a797877  A7      : 0x76757473  S2      : 0x00000009  S3      : 0x3fcbe31d

S4      : 0x3fc91ed0  S5      : 0x74657374  S6      : 0x00000000  S7      : 0x00000000

S8      : 0x00000000  S9      : 0x00000000  S10     : 0x00000000  S11     : 0x00000000

T3      : 0x6e6d6c6b  T4      : 0x6a696867  T5      : 0x66656463  T6      : 0x62613938  

MSTATUS : 0x00001881  MTVEC   : 0x40380001  MCAUSE  : 0x00000007  MTVAL   : 0x00000000  

0x40380001: _vector_table at ??:?



MHARTID : 0x00000000



Failed to run gdb_panic_server.py script: Command '['riscv32-esp-elf-gdb', '--batch', '-n', 'c:\code\esp\template-app\build\template-app.elf', '-ex', 'target remote | "C:\Users\micro\.espressif\python_env\idf4.3_py3.8_env\Scripts\python.exe" "C:/Users/micro/Desktop/esp-idf/tools\..\tools\gdb_panic_server.py" --target esp32c3 "C:\Users\micro\AppData\Local\Temp\tmp8kn1j2ve"', '-ex', 'bt']' returned non-zero exit status 1.

b'C:\Users\micro\.espressif\tools\riscv32-esp-elf\esp-2021r2-8.4.0\riscv32-esp-elf\bin\riscv32-esp-elf-gdb.exe: warning: Couldn't determine a path for the index cache directory.rnerror starting child process '"C:\Users\micro\.espressif\python_env\idf4.3_py3.8_env\Scripts\python.exe" "C:/Users/micro/Desktop/esp-idf/tools\..\tools\gdb_panic_server.py" --target esp32c3 "C:\Users\micro\AppData\Local\Temp\tmp8kn1j2ve"': CreateProcess: No such file or directoryrnNo stack.rn'





Core  0 register dump:

MEPC    : 0x4038061e  RA      : 0x403879ec  SP      : 0x3fcbe1d0  GP      : 0x3fc904f8

TP      : 0x3fc7f014  T0      : 0x37363534  T1      : 0x7271706f  T2      : 0x33323130

S0/FP   : 0x0000008a  S1      : 0x00000001  A0      : 0x3fcbe20c  A1      : 0x3fc91ed1

A2      : 0x00000001  A3      : 0x00000029  A4      : 0x00000001  A5      : 0x3fc9c000

A6      : 0x7a797877  A7      : 0x76757473  S2      : 0x00000009  S3      : 0x3fcbe31d

S4      : 0x3fc91ed0  S5      : 0x74657374  S6      : 0x00000000  S7      : 0x00000000

S8      : 0x00000000  S9      : 0x00000000  S10     : 0x00000000  S11     : 0x00000000

T3      : 0x6e6d6c6b  T4      : 0x6a696867  T5      : 0x66656463  T6      : 0x62613938

MSTATUS : 0x00001881  MTVEC   : 0x40380001  MCAUSE  : 0x00000007  MTVAL   : 0x00000000

MHARTID : 0x00000000

更多回帖

发帖
×
20
完善资料,
赚取积分