Hal_Fls.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * Hal_Fls.c
  3. *
  4. * Created on:
  5. * Author: Zhengchao
  6. */
  7. #include "Hal_Fls.h"
  8. #include "math.h"
  9. /* Application can used space */
  10. const BlockInfo_t gs_astBlockNumB[] =
  11. {
  12. {APP_B_START_ADDR, APP_B_END_ADDR}, /* Block logical B */
  13. };
  14. /* Application flash status, this status info should be saved into flash */
  15. static tAppFlashStatus gs_stAppFlashStatus;
  16. #define GetAppStatusPtr() (&gs_stAppFlashStatus)
  17. static tAppDownloadType gs_stAppDownloadInfo = {APP_VECTOR_TABLE_OFFSET,0,0};
  18. #define SetFlashEreaseStatus(bIsFlashEraseSuccessful) \
  19. do{\
  20. gs_stAppFlashStatus.isFlashErasedSuccessfull = bIsFlashEraseSuccessful;\
  21. }while(0u)
  22. #define SetFlashProgramStatus(bIsFlashProgramSuccessful) \
  23. do{\
  24. gs_stAppFlashStatus.isFlashProgramSuccessfull = bIsFlashProgramSuccessful;\
  25. }while(0u)
  26. #define SetAppUpdateControllerName(bControolerName) \
  27. do{\
  28. gs_stAppFlashStatus.controllerName = bControolerName;\
  29. }while(0u)
  30. #define SetAppConter(appCounter)\
  31. {\
  32. gs_stAppFlashStatus.appCnt = appCounter;\
  33. }
  34. #define SaveAppResetHandlerAddr(resetHandlerAddr, resetHnadlerAddrLen) \
  35. do{\
  36. gs_stAppFlashStatus.appStartAddr = resetHandlerAddr;\
  37. gs_stAppFlashStatus.appStartAddrLen = resetHnadlerAddrLen;\
  38. }while(0u)
  39. #define SaveAppStatusCrc(xCrc)\
  40. do{\
  41. gs_stAppFlashStatus.crc = xCrc;\
  42. }while(0u)
  43. /* Create APP status CRC and save */
  44. #define CreateAndSaveAppStatusCrc(o_pCrc) \
  45. do{\
  46. CreateAppStatusCrc(o_pCrc);\
  47. SaveAppStatusCrc(*o_pCrc);\
  48. }while(0u)
  49. #define CreateAppStatusCrc(o_pCrc) CRC_HAL_CreatSoftwareCrc((uint8 *)&gs_stAppFlashStatus, sizeof(gs_stAppFlashStatus) - 4u, o_pCrc)
  50. #define IsFlashEraseSuccessful() (gs_stAppFlashStatus.isFlashErasedSuccessfull)
  51. #define IsFlashProgramSuccessful() (gs_stAppFlashStatus.isFlashProgramSuccessfull)
  52. static void Hal_FlsSaveFingerPrint(const uint8 *i_pFingerPrint, const uint8 i_FingerPrintLen);
  53. #define InitStAppFlashStatus() memset(&gs_stAppFlashStatus,0xFF,sizeof(gs_stAppFlashStatus))
  54. #define SetFlashStructStatus(bIsAppFlashStructValid) \
  55. do{\
  56. gs_stAppFlashStatus.isFlashStructValid = bIsAppFlashStructValid;\
  57. }while(0u)
  58. #define SetDownloadAppLength(bAppLength) \
  59. do{\
  60. gs_stAppDownloadInfo.appLength = bAppLength;\
  61. gs_stAppFlashStatus.appLength = bAppLength;\
  62. }while(0u)
  63. #define SetDownloadAppPackCRC(bAppReceivedCRC) \
  64. do{\
  65. gs_stAppDownloadInfo.receivedCRC = bAppReceivedCRC;\
  66. }while(0u)
  67. //#define SetAPPStatus(bIsFlashEraseSuccessful, bIsFlashProgramSuccessful, bIsAppFlashStructValid) \
  68. // do{\
  69. // SetFlashEreaseStatus(bIsFlashEraseSuccessful);\
  70. // SetFlashProgramStatus(bIsFlashProgramSuccessful);\
  71. // SetFlashStructStatus(bIsAppFlashStructValid);\
  72. // }while(0u)
  73. MemIf_JobResultType Hal_FlsWrite(Fls_AddressType u32TargetAddress, uint8 * pSourceAddressPtr, Fls_LengthType u32Length, uint32 timeOutMs)
  74. {
  75. MemIf_JobResultType ret = MEMIF_JOB_FAILED;
  76. if(TRUE == IsFlashEraseSuccessful())
  77. {
  78. Fls_Write(u32TargetAddress,pSourceAddressPtr,u32Length);
  79. do
  80. {
  81. Fls_MainFunction();
  82. vTaskDelay(pdMS_TO_TICKS(1));
  83. timeOutMs--;
  84. }while(MEMIF_IDLE != Fls_GetStatus() && timeOutMs>0);
  85. ret = Fls_GetJobResult();
  86. }
  87. return ret;
  88. }
  89. MemIf_JobResultType Hal_FlsRead(Fls_AddressType u32TargetAddress, uint8 * pSourceAddressPtr, Fls_LengthType u32Length, uint32 timeOutMs)
  90. {
  91. MemIf_JobResultType ret = MEMIF_JOB_FAILED;
  92. Fls_Read(u32TargetAddress,pSourceAddressPtr,u32Length);
  93. while(MEMIF_IDLE != Fls_GetStatus() && timeOutMs>0)
  94. {
  95. Fls_MainFunction();
  96. vTaskDelay(pdMS_TO_TICKS(1));
  97. timeOutMs--;
  98. }
  99. ret = Fls_GetJobResult();
  100. return ret;
  101. }
  102. MemIf_JobResultType Hal_FlsErase(Fls_AddressType u32TargetAddress, uint32 eraseInternalLength, uint32 timeOutMs)
  103. {
  104. MemIf_JobResultType ret = MEMIF_JOB_FAILED;
  105. Fls_LengthType u32Length = 0;
  106. if(eraseInternalLength > NUMBER_OF_INTERNAL_SECTOR * INTERNAL_SECTOR_SIZE)
  107. {
  108. u32Length = NUMBER_OF_INTERNAL_SECTOR * INTERNAL_SECTOR_SIZE;
  109. }
  110. else
  111. {
  112. u32Length = ceil((double)(eraseInternalLength/(double)(INTERNAL_SECTOR_SIZE))) * INTERNAL_SECTOR_SIZE;
  113. }
  114. Fls_Erase(u32TargetAddress,u32Length);
  115. do
  116. {
  117. Fls_MainFunction();
  118. vTaskDelay(pdMS_TO_TICKS(1));
  119. timeOutMs--;
  120. }while(MEMIF_IDLE != Fls_GetStatus() && timeOutMs>0);
  121. ret = Fls_GetJobResult();
  122. if(ret == MEMIF_JOB_OK)
  123. {
  124. SetFlashEreaseStatus(TRUE);
  125. }
  126. else
  127. {
  128. SetFlashEreaseStatus(FALSE);
  129. }
  130. return ret;
  131. }
  132. void Hal_SetAppInfo(uint32 appLength, uint32 appReceviedCRC,ControllerType controllerName)
  133. {
  134. SetDownloadAppLength(appLength);
  135. SetDownloadAppPackCRC(appReceviedCRC);
  136. SetAppUpdateControllerName(controllerName);
  137. uint8 appCounter = 0;
  138. SetAppConter(appCounter);
  139. uint32 fingerPrint = 0x5555;
  140. Hal_FlsSaveFingerPrint(&fingerPrint, 2); //save finger print
  141. }
  142. /* Save FingerPrint */
  143. static void Hal_FlsSaveFingerPrint(const uint8 *i_pFingerPrint, const uint8 i_FingerPrintLen)
  144. {
  145. uint8 FingerPrintLen = 0u;
  146. // tCrc crc = 0u;
  147. // ASSERT(NULL_PTR == i_pFingerPrint);
  148. if (i_FingerPrintLen > FL_FINGER_PRINT_LENGTH)
  149. {
  150. FingerPrintLen = FL_FINGER_PRINT_LENGTH;
  151. }
  152. else
  153. {
  154. FingerPrintLen = (uint8)i_FingerPrintLen;
  155. }
  156. memcpy((void *) gs_stAppFlashStatus.aFingerPrint, (const void *) i_pFingerPrint,
  157. FingerPrintLen);
  158. // CreateAndSaveAppStatusCrc(&crc);
  159. }
  160. /* Get storage reset handler information */
  161. static uint32 Hal_FlsGetStorageRestHandlerAddr(void)
  162. {
  163. return APP_VECTOR_TABLE_OFFSET + RESET_HANDLER_OFFSET;
  164. }
  165. /* Get reset handler addr length */
  166. static uint32 Hal_FlsGetResetHandlerLen(void)
  167. {
  168. return RESET_HANDLER_ADDR_LEN;
  169. }
  170. static boolean Hal_GetAPPFlsInfo(uint32 *o_pAppInfoStartAddr, uint32 *o_pBlockSize)
  171. {
  172. boolean result = FALSE;
  173. *o_pAppInfoStartAddr = gs_astBlockNumB[0u].xBlockStartLogicalAddr;
  174. *o_pBlockSize = gs_astBlockNumB[0u].xBlockEndLogicalAddr - gs_astBlockNumB[0u].xBlockStartLogicalAddr;
  175. result = TRUE;
  176. return result;
  177. }
  178. /* Flash check sum */
  179. static uint8 FlashChecksum(void)
  180. {
  181. uint32 xCountCrc = 0u;
  182. CRC_HAL_CreatSoftwareCrc((const uint8 *)(gs_stAppDownloadInfo.appVectoTableStartAddr + APP_FLAST_START_PHY_ADDR), gs_stAppDownloadInfo.appLength, &xCountCrc);
  183. if(gs_stAppDownloadInfo.receivedCRC == xCountCrc)
  184. {
  185. return TRUE;
  186. }
  187. else
  188. {
  189. return FALSE;
  190. }
  191. }
  192. static void Hal_FlsGetResetHandlerInfo(uint32 *o_pResetHandlerOffset, uint32 *o_pResetHandlerLength)
  193. {
  194. if(NULL_PTR != o_pResetHandlerOffset && NULL_PTR != o_pResetHandlerLength)
  195. {
  196. *o_pResetHandlerOffset = Hal_FlsGetStorageRestHandlerAddr();
  197. *o_pResetHandlerLength = Hal_FlsGetResetHandlerLen();
  198. }
  199. }
  200. void Hal_FlsGetAppVectorTableStartAddr(uint32 *appVectorTableStartAddr)
  201. {
  202. *appVectorTableStartAddr = gs_stAppDownloadInfo.appVectoTableStartAddr;
  203. }
  204. static uint8 Hal_FlsWriteFlashAppInfo(void)
  205. {
  206. uint8 result = FALSE;
  207. MemIf_JobResultType flsRet = MEMIF_JOB_FAILED;
  208. tAppFlashStatus *pAppStatusPtr = NULL_PTR;
  209. uint32 appInfoStartAddr = 0u;
  210. uint32 appInfoLen = 0u;
  211. uint32 resetHandlerOffset = 0u;
  212. uint32 resetHandlerLength = 0u;
  213. uint32 resetHandleLogicalAddr = 0u;
  214. uint8 resetHandleAddrTemp[RESET_HANDLER_ADDR_LEN];
  215. uint32 resetHandlePhyAddr = 0u;
  216. uint32 crc = 0u;
  217. /* Write data information in flash */
  218. pAppStatusPtr = GetAppStatusPtr();
  219. Hal_GetAPPFlsInfo(&appInfoStartAddr, &appInfoLen);
  220. Hal_FlsGetResetHandlerInfo(&resetHandlerOffset,&resetHandlerLength);
  221. resetHandleLogicalAddr = appInfoStartAddr + resetHandlerOffset;
  222. Hal_FlsRead(resetHandleLogicalAddr,resetHandleAddrTemp,RESET_HANDLER_ADDR_LEN,500);
  223. for(uint8 i=0; i<RESET_HANDLER_ADDR_LEN;i++)
  224. {
  225. resetHandlePhyAddr |= (resetHandleAddrTemp[i]<<(8*i));
  226. }
  227. SaveAppResetHandlerAddr(resetHandlePhyAddr, resetHandlerLength);
  228. CreateAndSaveAppStatusCrc(&crc);
  229. if(pAppStatusPtr != NULL_PTR)
  230. {
  231. flsRet = Hal_FlsWrite(appInfoStartAddr,(uint8 *)pAppStatusPtr,(sizeof(tAppFlashStatus)+7)/8*8,500);
  232. }
  233. if(flsRet == MEMIF_JOB_OK)
  234. {
  235. result = TRUE;
  236. }
  237. else
  238. {
  239. result = FALSE;
  240. }
  241. return result;
  242. }
  243. uint8 Hal_FlsCheckIsTransferSucceed(void)
  244. {
  245. uint8 ret = FALSE;
  246. if(TRUE == FlashChecksum())
  247. {
  248. SetFlashProgramStatus(TRUE);
  249. ret = TRUE;
  250. }
  251. else
  252. {
  253. SetFlashProgramStatus(FALSE);
  254. ret = FALSE;
  255. }
  256. SetFlashStructStatus(TRUE);
  257. Hal_FlsWriteFlashAppInfo();
  258. Hal_SetAppInfo(0,0xFFFFFFFF,CONTROLLER_SELF);
  259. SetFlashEreaseStatus(FALSE);
  260. return ret;
  261. }
  262. void Hal_OTAFlashAppInfoInit(void)
  263. {
  264. InitStAppFlashStatus();
  265. }