CANTask.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "bsp.h"
  2. #include "bsp_custom.h"
  3. #include "osasys.h"
  4. #include "ostask.h"
  5. #include "queue.h"
  6. #include "ps_event_callback.h"
  7. #include "cmisim.h"
  8. #include "cmimm.h"
  9. #include "cmips.h"
  10. #include "sockets.h"
  11. #include "psifevent.h"
  12. #include "ps_lib_api.h"
  13. #include "lwip/netdb.h"
  14. #include <cis_def.h>
  15. #include "debug_log.h"
  16. #include "slpman_ec616.h"
  17. #include "plat_config.h"
  18. #include "ec_tcpip_api.h"
  19. #include "hal_module_adapter.h"
  20. #include "UartTask.h"
  21. #include "MainTask.h"
  22. #include "app.h"
  23. #include "CANTask.h"
  24. #include "UDSService.h"
  25. extern osMutexId_t UartMutex;
  26. extern UINT32 Timer_count;
  27. extern volatile bool Sleep_flag;
  28. extern UartReadMsgType UartReadMsg;
  29. CAN_Msg_Type CanRxMsg[2];
  30. /*线程声明*/
  31. #define PROC_CAN_RX_TASK_STACK_SIZE (1024)
  32. static StaticTask_t gProcess_Can_Rx_Task_t;
  33. static UINT8 gProcess_Can_Rx_TaskStack[PROC_CAN_RX_TASK_STACK_SIZE];
  34. static process_CAN gProcess_CAN_Task = PROCESS_CAN_STATE_IDLE;
  35. #define PROC_CAN_STATE_SWITCH(a) (gProcess_CAN_Task = a)
  36. volatile bool CAN_Sleep_State = false;
  37. UINT8 CanSendSlpHandler = 0xfd;
  38. void CANTaskInit(void* arg)
  39. {
  40. osThreadAttr_t task_rx_attr,task_tx_attr;
  41. Can_InitType param;
  42. param.baudrate = CAN_250Kbps;
  43. param.mode = REQOP_NORMAL;
  44. param.packType = STD_PACK;
  45. memset(&task_rx_attr,0,sizeof(task_rx_attr));
  46. memset(gProcess_Can_Rx_TaskStack, 0, PROC_CAN_RX_TASK_STACK_SIZE);
  47. task_rx_attr.name = "Can_Task";
  48. task_rx_attr.stack_mem = gProcess_Can_Rx_TaskStack;
  49. task_rx_attr.stack_size = PROC_CAN_RX_TASK_STACK_SIZE;
  50. task_rx_attr.priority = osPriorityBelowNormal7;
  51. task_rx_attr.cb_mem = &gProcess_Can_Rx_Task_t;
  52. task_rx_attr.cb_size = sizeof(StaticTask_t);
  53. HAL_Can_Init(param);
  54. osDelay(1000);
  55. osThreadNew(Can_Receive, NULL, &task_rx_attr);
  56. }
  57. static void Can_Receive()
  58. {
  59. slpManSlpState_t State;
  60. uint8_t cnt,temp,udsFlag=0;
  61. UINT8 i = 0;
  62. CAN_Msg_Type CANSendBuffer ={0};
  63. while(true)
  64. {
  65. memset(CanRxMsg, 0, sizeof(CanRxMsg));
  66. switch(gProcess_CAN_Task)
  67. {
  68. case PROCESS_CAN_STATE_IDLE:
  69. CAN_Sleep_State = false;
  70. PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_WORK);
  71. break;
  72. case PROCESS_CAN_STATE_WORK:
  73. {
  74. if(Sleep_flag)
  75. {
  76. PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_SLEEP);
  77. }
  78. HAL_Can_Receive(CanRxMsg);
  79. for(i=0; i<2; i++)
  80. {
  81. if(CanRxMsg[i].Id == 0x7A0)
  82. {
  83. #ifdef USING_PRINTF1
  84. printf("ID = 0x%x\n",CanRxMsg[i].Id);
  85. for(UINT8 j = 0; j<8;j++)
  86. {
  87. printf("%x ",CanRxMsg[i].Data[j]);
  88. }
  89. printf("\n");
  90. #endif
  91. udsFlag = 1;
  92. UDSService[i] = CanRxMsg[i].Data[0];
  93. UDSSubService[i] = CanRxMsg[i].Data[1];
  94. UDSSubServiceActionCode[i] = CanRxMsg[i].Data[2];
  95. if(UDSService[i] == 0x10)
  96. {
  97. if(UDSSubService[i] == 0x02)
  98. {
  99. UDSSwitch = 1;
  100. }
  101. }
  102. }
  103. }
  104. if(udsFlag==1)
  105. {
  106. UDS_Service();
  107. udsFlag = 0;
  108. }
  109. break;
  110. }
  111. case PROCESS_CAN_STATE_SLEEP:
  112. {
  113. HAL_Can_Sleep();
  114. CAN_Sleep_State = true;
  115. while(true)
  116. {
  117. osDelay(60000/portTICK_PERIOD_MS);
  118. }
  119. break;
  120. }
  121. }
  122. osDelay(50);
  123. }
  124. }