UDSTask.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * UDSTask.c
  3. *
  4. * Created on: 2022Äê3ÔÂ14ÈÕ
  5. * Author: Zhengchao
  6. */
  7. #include "UDSTask.h"
  8. typedef struct
  9. {
  10. uint8 infoDataLen; /* Exchange information length must be N * 4 */
  11. uint8 requestEnterBootloader; /* Request enter bootloader mode flag */
  12. uint8 downloadAPPSuccessful; /* Download APP successful flag */
  13. uint32 infoStartAddr; /* Exchange information start address */
  14. uint32 requestEnterBootloaderAddr; /* Request enter bootloader flag address */
  15. uint32 downloadAppSuccessfulAddr; /* Download APP successful flag address */
  16. } tBootInfo;
  17. static const tBootInfo gs_stBootInfo =
  18. {
  19. 16u,
  20. 0x5Au,
  21. 0xA5u,
  22. INFO_START_ADDR,
  23. REQUEST_ENTER_BOOTLOADER_ADDR,
  24. DOWNLOAD_APP_SUCCESSFUL_ADDR,
  25. };
  26. static const uint16 g_dnpCrcTable[256u] =
  27. {
  28. 0x0000, 0x365E, 0x6CBC, 0x5AE2, 0xD978, 0xEF26, 0xB5C4, 0x839A,
  29. 0xFF89, 0xC9D7, 0x9335, 0xA56B, 0x26F1, 0x10AF, 0x4A4D, 0x7C13,
  30. 0xB26B, 0x8435, 0xDED7, 0xE889, 0x6B13, 0x5D4D, 0x07AF, 0x31F1,
  31. 0x4DE2, 0x7BBC, 0x215E, 0x1700, 0x949A, 0xA2C4, 0xF826, 0xCE78,
  32. 0x29AF, 0x1FF1, 0x4513, 0x734D, 0xF0D7, 0xC689, 0x9C6B, 0xAA35,
  33. 0xD626, 0xE078, 0xBA9A, 0x8CC4, 0x0F5E, 0x3900, 0x63E2, 0x55BC,
  34. 0x9BC4, 0xAD9A, 0xF778, 0xC126, 0x42BC, 0x74E2, 0x2E00, 0x185E,
  35. 0x644D, 0x5213, 0x08F1, 0x3EAF, 0xBD35, 0x8B6B, 0xD189, 0xE7D7,
  36. 0x535E, 0x6500, 0x3FE2, 0x09BC, 0x8A26, 0xBC78, 0xE69A, 0xD0C4,
  37. 0xACD7, 0x9A89, 0xC06B, 0xF635, 0x75AF, 0x43F1, 0x1913, 0x2F4D,
  38. 0xE135, 0xD76B, 0x8D89, 0xBBD7, 0x384D, 0x0E13, 0x54F1, 0x62AF,
  39. 0x1EBC, 0x28E2, 0x7200, 0x445E, 0xC7C4, 0xF19A, 0xAB78, 0x9D26,
  40. 0x7AF1, 0x4CAF, 0x164D, 0x2013, 0xA389, 0x95D7, 0xCF35, 0xF96B,
  41. 0x8578, 0xB326, 0xE9C4, 0xDF9A, 0x5C00, 0x6A5E, 0x30BC, 0x06E2,
  42. 0xC89A, 0xFEC4, 0xA426, 0x9278, 0x11E2, 0x27BC, 0x7D5E, 0x4B00,
  43. 0x3713, 0x014D, 0x5BAF, 0x6DF1, 0xEE6B, 0xD835, 0x82D7, 0xB489,
  44. 0xA6BC, 0x90E2, 0xCA00, 0xFC5E, 0x7FC4, 0x499A, 0x1378, 0x2526,
  45. 0x5935, 0x6F6B, 0x3589, 0x03D7, 0x804D, 0xB613, 0xECF1, 0xDAAF,
  46. 0x14D7, 0x2289, 0x786B, 0x4E35, 0xCDAF, 0xFBF1, 0xA113, 0x974D,
  47. 0xEB5E, 0xDD00, 0x87E2, 0xB1BC, 0x3226, 0x0478, 0x5E9A, 0x68C4,
  48. 0x8F13, 0xB94D, 0xE3AF, 0xD5F1, 0x566B, 0x6035, 0x3AD7, 0x0C89,
  49. 0x709A, 0x46C4, 0x1C26, 0x2A78, 0xA9E2, 0x9FBC, 0xC55E, 0xF300,
  50. 0x3D78, 0x0B26, 0x51C4, 0x679A, 0xE400, 0xD25E, 0x88BC, 0xBEE2,
  51. 0xC2F1, 0xF4AF, 0xAE4D, 0x9813, 0x1B89, 0x2DD7, 0x7735, 0x416B,
  52. 0xF5E2, 0xC3BC, 0x995E, 0xAF00, 0x2C9A, 0x1AC4, 0x4026, 0x7678,
  53. 0x0A6B, 0x3C35, 0x66D7, 0x5089, 0xD313, 0xE54D, 0xBFAF, 0x89F1,
  54. 0x4789, 0x71D7, 0x2B35, 0x1D6B, 0x9EF1, 0xA8AF, 0xF24D, 0xC413,
  55. 0xB800, 0x8E5E, 0xD4BC, 0xE2E2, 0x6178, 0x5726, 0x0DC4, 0x3B9A,
  56. 0xDC4D, 0xEA13, 0xB0F1, 0x86AF, 0x0535, 0x336B, 0x6989, 0x5FD7,
  57. 0x23C4, 0x159A, 0x4F78, 0x7926, 0xFABC, 0xCCE2, 0x9600, 0xA05E,
  58. 0x6E26, 0x5878, 0x029A, 0x34C4, 0xB75E, 0x8100, 0xDBE2, 0xEDBC,
  59. 0x91AF, 0xA7F1, 0xFD13, 0xCB4D, 0x48D7, 0x7E89, 0x246B, 0x1235,
  60. };
  61. /* Get information storage CRC */
  62. #define GetInfoStorageCRC() (*(uint16 *)(gs_stBootInfo.infoStartAddr + 14))
  63. /* Set information CRC */
  64. #define SetInforCRC(xCrc) ((*(uint16 *)(gs_stBootInfo.infoStartAddr + 14)) = (uint16)(xCrc))
  65. void CreatSoftwareCrc16(const uint8 *i_pDataBuf, const uint32 i_dataLen, uint32 *m_pCurCrc)
  66. {
  67. uint16 crc = 0u;
  68. uint32 index = 0u;
  69. #if (defined FALSH_ADDRESS_CONTINUE) && (FALSH_ADDRESS_CONTINUE == TRUE)
  70. crc = *m_pCurCrc;
  71. #endif
  72. for (index = 0u; index < i_dataLen; index++)
  73. {
  74. crc = ( crc >> 8 ) ^ g_dnpCrcTable[( crc ^ i_pDataBuf[index]) & 0x00ff] ;
  75. }
  76. *m_pCurCrc = (uint32)((~crc) & 0xFFFFu);
  77. }
  78. void CRC_HAL_CreatSoftwareCrc(const uint8_t *i_pucDataBuf, const uint32_t i_ulDataLen, uint32_t *m_pCurCrc)
  79. {
  80. #ifdef EN_CRC_SOFTWARE
  81. CreatSoftwareCrc16(i_pucDataBuf, i_ulDataLen, m_pCurCrc);
  82. #endif
  83. }
  84. /* Calculate information CRC */
  85. static uint16 Boot_CalculateInfoCRC(void)
  86. {
  87. uint32 infoCrc = 0u;
  88. CRC_HAL_CreatSoftwareCrc((const uint8 *)gs_stBootInfo.infoStartAddr, gs_stBootInfo.infoDataLen - 2u, &infoCrc);
  89. return (uint16)infoCrc;
  90. }
  91. /* Is information valid? */
  92. static boolean Boot_IsInfoValid(void)
  93. {
  94. uint16 infoCrc = 0u;
  95. uint16 storageCrc = 0u;
  96. boolean result = FALSE;
  97. infoCrc = Boot_CalculateInfoCRC();
  98. storageCrc = GetInfoStorageCRC();
  99. if (storageCrc == infoCrc)
  100. {
  101. result = TRUE;
  102. }
  103. return result;
  104. }
  105. void RequestEnterBootloader(void)
  106. {
  107. uint16 infoCrc = 0u;
  108. *((uint8 *)gs_stBootInfo.requestEnterBootloaderAddr) = gs_stBootInfo.requestEnterBootloader;
  109. infoCrc = Boot_CalculateInfoCRC();
  110. SetInforCRC(infoCrc);
  111. }
  112. /* Is download APP successful? */
  113. boolean IsDownloadAPPSccessful(void)
  114. {
  115. boolean result = FALSE;
  116. if (TRUE == Boot_IsInfoValid())
  117. {
  118. if (gs_stBootInfo.downloadAPPSuccessful == *((uint8 *)gs_stBootInfo.downloadAppSuccessfulAddr))
  119. {
  120. result = TRUE;
  121. }
  122. }
  123. return result;
  124. }
  125. /* Clear download APP successful flag */
  126. void ClearDownloadAPPSuccessfulFlag(void)
  127. {
  128. uint16 infoCrc = 0u;
  129. *((uint8 *)gs_stBootInfo.downloadAppSuccessfulAddr) = 0u;
  130. infoCrc = Boot_CalculateInfoCRC();
  131. SetInforCRC(infoCrc);
  132. }
  133. static boolean UDS_IsReceivedMsgIDValid(const uint32 i_receiveMsgID)
  134. {
  135. boolean result = FALSE;
  136. if ((i_receiveMsgID == RX_FUN_ADDR_ID)
  137. || (i_receiveMsgID == RX_PHY_ADDR_ID))
  138. {
  139. result = TRUE;
  140. }
  141. return result;
  142. }
  143. boolean IsJumptoBootloader(uint32 CanID, uint8 Data[])
  144. {
  145. boolean result = FALSE;
  146. if(UDS_IsReceivedMsgIDValid(CanID))
  147. {
  148. if(Data[0] == 0x02 && Data[1] == 0x10 && Data[2] == 0x02)
  149. {
  150. result = TRUE;
  151. }
  152. }
  153. return result;
  154. }
  155. void DoRoutinePositiveAnswer(void)
  156. {
  157. uint8 AnsData[8]={0x02,0x50,0x02,0x00,0x00,0x00,0x00,0x00};
  158. Can_Msg_Type CanTxMsg;
  159. CanTxMsg.id = TX_RESP_ADDR_ID;
  160. CanTxMsg.idFrame = CAN_EXTENDED_ID_TYPE;
  161. CanTxMsg.sdu = AnsData;
  162. CanTxMsg.length = 8;
  163. CanIf_SendMessage(1, CanTxMsg);
  164. }
  165. boolean Boot_CheckDownlaodAPPStatus(void)
  166. {
  167. boolean ret = FALSE;
  168. if (TRUE == IsDownloadAPPSccessful())
  169. {
  170. ClearDownloadAPPSuccessfulFlag();
  171. }
  172. return ret;
  173. }