AppTaskCan.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /****************************************************************************
  2. *
  3. * Copy right: 2021-, Copyrigths of RLWL Ltd.Zhen C
  4. * File name: AppTaskCan.h
  5. * Description: App Task Can H file
  6. * History: Rev3.0 2021-07-06
  7. *
  8. ****************************************************************************/
  9. #include "AppTaskCan.h"
  10. static void CanTask(void);
  11. static StaticTask_t gProcess_Can_Rx_Task_t;
  12. static UINT8 gProcess_Can_Rx_TaskStack[PROC_CAN_RX_TASK_STACK_SIZE];
  13. static process_CAN gProcess_CAN_Task = PROCESS_CAN_STATE_IDLE;
  14. #define PROC_CAN_STATE_SWITCH(a) (gProcess_CAN_Task = a)
  15. void AppTaskCanInit(void *arg)
  16. {
  17. osThreadAttr_t task_rx_attr;
  18. memset(&task_rx_attr, 0, sizeof(task_rx_attr));
  19. memset(gProcess_Can_Rx_TaskStack, 0, PROC_CAN_RX_TASK_STACK_SIZE);
  20. task_rx_attr.name = "Can_Task";
  21. task_rx_attr.stack_mem = gProcess_Can_Rx_TaskStack;
  22. task_rx_attr.stack_size = PROC_CAN_RX_TASK_STACK_SIZE;
  23. task_rx_attr.priority = osPriorityBelowNormal7;
  24. task_rx_attr.cb_mem = &gProcess_Can_Rx_Task_t;
  25. task_rx_attr.cb_size = sizeof(StaticTask_t);
  26. osDelay(1000);
  27. osThreadNew(CanTask, NULL, &task_rx_attr);
  28. }
  29. static void CanTask()
  30. {
  31. Can_InitType param;
  32. UINT8 udsFlag, msgFlag = 0;
  33. UINT8 i = 0;
  34. UINT16 delayconuter = 0;
  35. UINT8 UDSService[2];
  36. UINT8 UDSSubService[2];
  37. UINT8 UDSSubServiceActionCode[2];
  38. CAN_Msg_Type CanRxMsg[2];
  39. UINT8 ConvertCounter = 0;
  40. PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_INIT);
  41. while (TRUE)
  42. {
  43. memset(CanRxMsg, 0, sizeof(CanRxMsg));
  44. switch (gProcess_CAN_Task)
  45. {
  46. case PROCESS_CAN_STATE_INIT:
  47. {
  48. param.baudrate = CAN_250Kbps;
  49. param.mode = REQOP_NORMAL;
  50. param.packType = STD_PACK;
  51. HAL_Can_Init(param);
  52. osDelay(100);
  53. PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_WORK);
  54. break;
  55. }
  56. case PROCESS_CAN_STATE_IDLE:
  57. {
  58. ConvertCounter = 0;
  59. while (TRUE)
  60. {
  61. osDelay(100);
  62. ConvertCounter++;
  63. if (gProcess_app != WORK)
  64. {
  65. PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_SLEEP);
  66. break;
  67. }
  68. if (getbit(PadInterrupt, 0) == 1 || ConvertCounter >= 10)
  69. {
  70. PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_WORK);
  71. break;
  72. }
  73. }
  74. break;
  75. }
  76. case PROCESS_CAN_STATE_WORK:
  77. {
  78. if (HAL_Can_Receive(CanRxMsg) > 0)
  79. {
  80. delayconuter = 0;
  81. for (i = 0; i < 2; i++)
  82. {
  83. if (CanRxMsg[i].Id == 0x7A0)
  84. {
  85. udsFlag = 1;
  86. UDSService[i] = CanRxMsg[i].Data[0];
  87. UDSSubService[i] = CanRxMsg[i].Data[1];
  88. UDSSubServiceActionCode[i] = CanRxMsg[i].Data[2];
  89. if (UDSService[i] == 0x10)
  90. {
  91. if (UDSSubService[i] == 0x02)
  92. {
  93. UDSSwitch = 1;
  94. }
  95. }
  96. }
  97. else if (CanRxMsg[i].Id == 0x7A1)
  98. {
  99. if (CanRxMsg[i].Data[0] != 0x00)
  100. {
  101. msgFlag = 1;
  102. }
  103. else
  104. {
  105. msgFlag = 0;
  106. }
  107. }
  108. }
  109. if (udsFlag == 1)
  110. {
  111. UDS_Service(CanRxMsg);
  112. udsFlag = 0;
  113. }
  114. }
  115. else
  116. {
  117. delayconuter++;
  118. osDelay(10);
  119. }
  120. if (msgFlag == 1)
  121. {
  122. CanMsgTx1000ms();
  123. }
  124. if (delayconuter >= 500 && msgFlag == 0)
  125. {
  126. delayconuter = 0;
  127. clrbit(PadInterrupt, 0);
  128. PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_IDLE);
  129. if (RelayForceControl == 1)
  130. {
  131. RelayForceControl = 2;
  132. }
  133. }
  134. break;
  135. }
  136. case PROCESS_CAN_STATE_SLEEP:
  137. {
  138. HAL_Can_Sleep();
  139. while (TRUE)
  140. {
  141. if (getbit(PadInterrupt, 0) == 1 || (slpManGetWakeupPinValue() & 0x1) == 0x00) //判断是否产生中断,产生中断则退出延时
  142. {
  143. PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_INIT);
  144. break;
  145. }
  146. osDelay(2000);
  147. }
  148. break;
  149. }
  150. }
  151. }
  152. }