新手入门
- 积分
- 8
- 金钱
- 8
- 注册时间
- 2018-10-17
- 在线时间
- 1 小时
|
1金钱
本帖最后由 MrBigTree 于 2018-10-17 23:02 编辑
我在zynq-7000上跑freertos (基于lwip),我起了一个任务建立mqtt连接,又在其中起了另一个任务去初始化一个GPIO的中断。现在遇到的问题是,只要我启动这个中断,mqtt的连接就总会断,大概5秒之内吧;而如果不起中断,则连接很稳定。我在中断中只是每10ms发送一个信号量给其他任务。
建立mqtt连接的代码如下:[mw_shl_code=c,true]void mqtt_application_thread() {
xil_printf("Enter into mqtt_application_thread...\r\n");
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...\r\n");
u8_t state = mqtt_client_is_connected(client);
xil_printf("Current mqtt connection state is %d...\r\n", 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);
}
}
}
}[/mw_shl_code]初始化中断的代码如下:
[mw_shl_code=c,true]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;
}[/mw_shl_code]
中断处理函数如下:
[mw_shl_code=c,true]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 );
}[/mw_shl_code]
另一个奇怪的问题是,当我试图在mqtt的线程中重连时,通过mqtt_client_is_connected(client)读回来的连接状态永远是已连接,即使服务端都显示断开了也不会变。麻烦各位大神帮忙看看,谢谢。
|
|