我在zynq-7000上跑freertos (基于lwip),我起了一个任务建立mqtt连接,又在其中起了另一个任务去初始化一个GPIO的中断。现在遇到的问题是,只要我启动这个中断,mqtt的连接就总会断,大概5秒之内吧;而如果不起中断,则连接很稳定。我在中断中只是每10ms发送一个信号量给其他任务。
建立mqtt连接的代码如下:[C]
纯文本查看 复制代码
void mqtt_applica
tion_thread() { xil_printf("Enter into mqtt_application_thread...rn"); mqtt_client_t *client = mqtt_client_new(); if (client != NULL) { mqtt_example_do_connect(client, client_id_suffix); } xBinarySemaphore = xSemaphoreCreateBinary(); if (NULL != xBinarySemaphore) { //Init Gpio Interrupt xTaskCreate(Task_Gpio_Init, "Gpio_Init", 1024, NULL, DEFAULT_THREAD_PRIO, NULL); } for (;;) { xSemaphoreTake(xBinarySemaphore, portMAX_DELAY); xil_printf("In infinite mqtt_application_thread loop...rn"); u8_t state = mqtt_client_is_connected(client); xil_printf("Current mqtt connection state is %d...rn", state); //if mqtt disconnected, try to reconnect if (example_publish(client, mqtt_pub_request_cb) != 1) { free(client); client = NULL; client = mqtt_client_new(); client_id_suffix++; if (client != NULL) { mqtt_example_do_connect(client, client_id_suffix); } } }}初始化中断的代码如下:
[C]
纯文本查看 复制代码
int InterruptInitialize(u16 DeviceId, XGpio * GpioInstancePtr) { XScuGic_Config * IntcConfigPtr; int status; // Interrupt controller initialization IntcConfigPtr = XScuGic_LookupConfig(DeviceId); if ((XScuGic_Config *) NULL == IntcConfigPtr) return XST_FAILURE; status = XScuGic_CfgInitialize(&InterruptInst, IntcConfigPtr, IntcConfigPtr->CpuBaseAddress); if (status != XST_SUCCESS) return XST_FAILURE; // Call interrupt setup function status = InterruptSystemSetup(&InterruptInst); if (status != XST_SUCCESS) return XST_FAILURE; // Register GPIO interrupt handler status = XScuGic_Connect(&InterruptInst, INTC_GPIO_INT_ID, (Xil_ExceptionHandler) InterruptHandler, (void*) GpioInstancePtr); if (status != XST_SUCCESS) return XST_FAILURE; // Enable GPIO interrupts XGpio_InterruptEnable(GpioInstancePtr, GPIO_INT_MASK); XGpio_InterruptGlobalEnable(GpioInstancePtr); // Enable GPIO interrupts in the controller XScuGic_Enable(&InterruptInst, INTC_GPIO_INT_ID); return XST_SUCCESS;}
中断处理函数如下:
[C]
纯文本查看 复制代码
void InterruptHandler(void * InstancePtr) { BaseType_t xHigherPriorityTaskWoken; xHigherPriorityTaskWoken = pdFALSE; // Acknowledge GPIO interrupts (void) XGpio_InterruptClear(&GpioInst, GPIO_INT_MASK); // Enable GPIO interrupts XGpio_InterruptEnable(&GpioInst, GPIO_INT_MASK); xSemaphoreGiveFromISR( xBinarySemaphore, &xHigherPriorityTaskWoken ); portYIELD_FROM_ISR( xHigherPriorityTaskWoken );}
另一个奇怪的问题是,当我试图在mqtt的线程中重连时,通过mqtt_client_is_connected(client)读回来的连接状态永远是已连接,即使服务端都显示断开了也不会变。麻烦各位大神帮忙看看,谢谢。