/**************************************************************************** * * Copy right: 2021-, Copyrigths of RLWL Ltd.Zhen C * File name: AppTaskCan.h * Description: App Task Can H file * History: Rev3.0 2021-07-06 * ****************************************************************************/ #include "AppTaskCan.h" static void CanTask(void); static StaticTask_t gProcess_Can_Rx_Task_t; static UINT8 gProcess_Can_Rx_TaskStack[PROC_CAN_RX_TASK_STACK_SIZE]; static process_CAN gProcess_CAN_Task = PROCESS_CAN_STATE_IDLE; #define PROC_CAN_STATE_SWITCH(a) (gProcess_CAN_Task = a) void AppTaskCanInit(void *arg) { osThreadAttr_t task_rx_attr; memset(&task_rx_attr, 0, sizeof(task_rx_attr)); memset(gProcess_Can_Rx_TaskStack, 0, PROC_CAN_RX_TASK_STACK_SIZE); task_rx_attr.name = "Can_Task"; task_rx_attr.stack_mem = gProcess_Can_Rx_TaskStack; task_rx_attr.stack_size = PROC_CAN_RX_TASK_STACK_SIZE; task_rx_attr.priority = osPriorityBelowNormal7; task_rx_attr.cb_mem = &gProcess_Can_Rx_Task_t; task_rx_attr.cb_size = sizeof(StaticTask_t); osDelay(1000); osThreadNew(CanTask, NULL, &task_rx_attr); } static void CanTask() { Can_InitType param; UINT8 udsFlag, msgFlag = 0; UINT8 i = 0; UINT16 delayconuter = 0; UINT8 UDSService[2]; UINT8 UDSSubService[2]; UINT8 UDSSubServiceActionCode[2]; CAN_Msg_Type CanRxMsg[2]; UINT8 ConvertCounter = 0; PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_INIT); while (TRUE) { memset(CanRxMsg, 0, sizeof(CanRxMsg)); switch (gProcess_CAN_Task) { case PROCESS_CAN_STATE_INIT: { param.baudrate = CAN_250Kbps; param.mode = REQOP_NORMAL; param.packType = STD_PACK; HAL_Can_Init(param); osDelay(100); PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_WORK); break; } case PROCESS_CAN_STATE_IDLE: { ConvertCounter = 0; while (TRUE) { osDelay(100); ConvertCounter++; if (gProcess_app != WORK) { PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_SLEEP); break; } if (getbit(PadInterrupt, 0) == 1 || ConvertCounter >= 10) { PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_WORK); break; } } break; } case PROCESS_CAN_STATE_WORK: { if (HAL_Can_Receive(CanRxMsg) > 0) { delayconuter = 0; for (i = 0; i < 2; i++) { if (CanRxMsg[i].Id == 0x7A0) { udsFlag = 1; UDSService[i] = CanRxMsg[i].Data[0]; UDSSubService[i] = CanRxMsg[i].Data[1]; UDSSubServiceActionCode[i] = CanRxMsg[i].Data[2]; if (UDSService[i] == 0x10) { if (UDSSubService[i] == 0x02) { UDSSwitch = 1; } } } else if (CanRxMsg[i].Id == 0x7A1) { if (CanRxMsg[i].Data[0] != 0x00) { msgFlag = 1; } else { msgFlag = 0; } } } if (udsFlag == 1) { UDS_Service(CanRxMsg); udsFlag = 0; } } else { delayconuter++; osDelay(10); } if (msgFlag == 1) { CanMsgTx1000ms(); } if (delayconuter >= 500 && msgFlag == 0) { delayconuter = 0; clrbit(PadInterrupt, 0); PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_IDLE); if (RelayForceControl == 1) { RelayForceControl = 2; } } break; } case PROCESS_CAN_STATE_SLEEP: { HAL_Can_Sleep(); while (TRUE) { if (getbit(PadInterrupt, 0) == 1 || (slpManGetWakeupPinValue() & 0x1) == 0x00) //判断是否产生中断,产生中断则退出延时 { PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_INIT); break; } osDelay(2000); } break; } } } }