flash_if.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /**
  2. ******************************************************************************
  3. * @file IAP/IAP_Main/Src/flash_if.c
  4. * @author MCD Application Team
  5. * @brief This file provides all the memory related operation functions.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics International N.V.
  10. * All rights reserved.</center></h2>
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted, provided that the following conditions are met:
  14. *
  15. * 1. Redistribution of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. Neither the name of STMicroelectronics nor the names of other
  21. * contributors to this software may be used to endorse or promote products
  22. * derived from this software without specific written permission.
  23. * 4. This software, including modifications and/or derivative works of this
  24. * software, must execute solely and exclusively on microcontroller or
  25. * microprocessor devices manufactured by or for STMicroelectronics.
  26. * 5. Redistribution and use of this software other than as permitted under
  27. * this license is void and will automatically terminate your rights under
  28. * this license.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  31. * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  33. * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  34. * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  35. * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  36. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  37. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  38. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  39. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  40. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  41. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. *
  43. ******************************************************************************
  44. */
  45. /** @addtogroup STM32F4xx_IAP_Main
  46. * @{
  47. */
  48. /* Includes ------------------------------------------------------------------*/
  49. #include "flash_if.h"
  50. #include "usart.h"
  51. /* Private typedef -----------------------------------------------------------*/
  52. /* Private define ------------------------------------------------------------*/
  53. /* Private macro -------------------------------------------------------------*/
  54. /* Private variables ---------------------------------------------------------*/
  55. /* Private function prototypes -----------------------------------------------*/
  56. static uint32_t GetSector(uint32_t Address);
  57. /* Private functions ---------------------------------------------------------*/
  58. /**
  59. * @brief Unlocks Flash for write access
  60. * @param None
  61. * @retval None
  62. */
  63. void FLASH_If_Init(void)
  64. {
  65. HAL_FLASH_Unlock();
  66. /* Clear pending flags (if any) */
  67. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
  68. FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
  69. }
  70. /**
  71. * @brief This function does an erase of all user flash area
  72. * @param StartSector: start of user flash area
  73. * @retval 0: user flash area successfully erased
  74. * 1: error occurred
  75. */
  76. uint32_t FLASH_If_Erase(uint32_t StartSector, uint32_t NumberOfSector)
  77. {
  78. uint32_t UserStartSector;
  79. uint32_t SectorError;
  80. FLASH_EraseInitTypeDef pEraseInit;
  81. /* Unlock the Flash to enable the flash control register access *************/
  82. FLASH_If_Init();
  83. /* Get the sector where start the user flash area */
  84. UserStartSector = GetSector(StartSector);
  85. pEraseInit.TypeErase = TYPEERASE_SECTORS;
  86. pEraseInit.Sector = UserStartSector;
  87. pEraseInit.NbSectors = NumberOfSector;
  88. pEraseInit.VoltageRange = VOLTAGE_RANGE_3;
  89. if (HAL_FLASHEx_Erase(&pEraseInit, &SectorError) != HAL_OK)
  90. {
  91. /* Error occurred while page erase */
  92. return (1);
  93. }
  94. return (0);
  95. }
  96. /**
  97. * @brief This function writes a data buffer in flash (data are 32-bit aligned).
  98. * @note After writing data buffer, the flash content is checked.
  99. * @param FlashAddress: start address for writing data buffer
  100. * @param Data: pointer on data buffer
  101. * @param DataLength: length of data buffer (unit is 32-bit word)
  102. * @retval 0: Data successfully written to Flash memory
  103. * 1: Error occurred while writing data in Flash memory
  104. * 2: Written Data in flash memory is different from expected one
  105. */
  106. uint32_t FLASH_If_Write(uint32_t FlashAddress, uint32_t* Data ,uint32_t DataLength)
  107. {
  108. uint32_t i = 0;
  109. for (i = 0; (i < DataLength) && (FlashAddress <= (USER_FLASH_END_ADDRESS-0)); i++)
  110. {
  111. /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
  112. be done by word */
  113. if (HAL_FLASH_Program(TYPEPROGRAM_WORD, FlashAddress, *(uint32_t*)(Data+i)) == HAL_OK)
  114. {
  115. /* Check the written value */
  116. if (*(uint32_t*)FlashAddress != *(uint32_t*)(Data+i))
  117. {
  118. /* Flash content doesn't match SRAM content */
  119. return(FLASHIF_WRITINGCTRL_ERROR);
  120. }
  121. /* Increment FLASH destination address */
  122. FlashAddress += 4;
  123. }
  124. else
  125. {
  126. /* Error occurred while writing data in Flash memory */
  127. return (FLASHIF_WRITING_ERROR);
  128. }
  129. }
  130. return (FLASHIF_OK);
  131. }
  132. /**
  133. * @brief Returns the write protection status of user flash area.
  134. * @param None
  135. * @retval 0: No write protected sectors inside the user flash area
  136. * 1: Some sectors inside the user flash area are write protected
  137. */
  138. uint16_t FLASH_If_GetWriteProtectionStatus(void)
  139. {
  140. uint32_t ProtectedSECTOR = 0xFFF;
  141. FLASH_OBProgramInitTypeDef OptionsBytesStruct;
  142. /* Unlock the Flash to enable the flash control register access *************/
  143. HAL_FLASH_Unlock();
  144. /* Check if there are write protected sectors inside the user flash area ****/
  145. HAL_FLASHEx_OBGetConfig(&OptionsBytesStruct);
  146. /* Lock the Flash to disable the flash control register access (recommended
  147. to protect the FLASH memory against possible unwanted operation) *********/
  148. HAL_FLASH_Lock();
  149. /* Get pages already write protected ****************************************/
  150. ProtectedSECTOR = ~(OptionsBytesStruct.WRPSector) & FLASH_SECTOR_TO_BE_PROTECTED;
  151. /* Check if desired pages are already write protected ***********************/
  152. if(ProtectedSECTOR != 0)
  153. {
  154. /* Some sectors inside the user flash area are write protected */
  155. return FLASHIF_PROTECTION_WRPENABLED;
  156. }
  157. else
  158. {
  159. /* No write protected sectors inside the user flash area */
  160. return FLASHIF_PROTECTION_NONE;
  161. }
  162. }
  163. /**
  164. * @brief Gets the sector of a given address
  165. * @param Address: Flash address
  166. * @retval The sector of a given address
  167. */
  168. static uint32_t GetSector(uint32_t Address)
  169. {
  170. uint32_t sector = 0;
  171. if((Address < ADDR_FLASH_SECTOR_1) && (Address >= ADDR_FLASH_SECTOR_0))
  172. {
  173. sector = FLASH_SECTOR_0;
  174. }
  175. else if((Address < ADDR_FLASH_SECTOR_2) && (Address >= ADDR_FLASH_SECTOR_1))
  176. {
  177. sector = FLASH_SECTOR_1;
  178. }
  179. else if((Address < ADDR_FLASH_SECTOR_3) && (Address >= ADDR_FLASH_SECTOR_2))
  180. {
  181. sector = FLASH_SECTOR_2;
  182. }
  183. else if((Address < ADDR_FLASH_SECTOR_4) && (Address >= ADDR_FLASH_SECTOR_3))
  184. {
  185. sector = FLASH_SECTOR_3;
  186. }
  187. else if((Address < ADDR_FLASH_SECTOR_5) && (Address >= ADDR_FLASH_SECTOR_4))
  188. {
  189. sector = FLASH_SECTOR_4;
  190. }
  191. else if((Address < ADDR_FLASH_SECTOR_6) && (Address >= ADDR_FLASH_SECTOR_5))
  192. {
  193. sector = FLASH_SECTOR_5;
  194. }
  195. else if((Address < ADDR_FLASH_SECTOR_7) && (Address >= ADDR_FLASH_SECTOR_6))
  196. {
  197. sector = FLASH_SECTOR_6;
  198. }
  199. else if((Address < ADDR_FLASH_SECTOR_8) && (Address >= ADDR_FLASH_SECTOR_7))
  200. {
  201. sector = FLASH_SECTOR_7;
  202. }
  203. else if((Address < ADDR_FLASH_SECTOR_9) && (Address >= ADDR_FLASH_SECTOR_8))
  204. {
  205. sector = FLASH_SECTOR_8;
  206. }
  207. else if((Address < ADDR_FLASH_SECTOR_10) && (Address >= ADDR_FLASH_SECTOR_9))
  208. {
  209. sector = FLASH_SECTOR_9;
  210. }
  211. else if((Address < ADDR_FLASH_SECTOR_11) && (Address >= ADDR_FLASH_SECTOR_10))
  212. {
  213. sector = FLASH_SECTOR_10;
  214. }
  215. else /*(Address < FLASH_END_ADDR) && (Address >= ADDR_FLASH_SECTOR_11))*/
  216. {
  217. sector = FLASH_SECTOR_11;
  218. }
  219. return sector;
  220. }
  221. /**
  222. * @brief Configure the write protection status of user flash area.
  223. * @param modifier DISABLE or ENABLE the protection
  224. * @retval HAL_StatusTypeDef HAL_OK if change is applied.
  225. */
  226. HAL_StatusTypeDef FLASH_If_WriteProtectionConfig(uint32_t modifier)
  227. {
  228. uint32_t ProtectedSECTOR = 0xFFF;
  229. FLASH_OBProgramInitTypeDef config_new, config_old;
  230. HAL_StatusTypeDef result = HAL_OK;
  231. /* Get pages write protection status ****************************************/
  232. HAL_FLASHEx_OBGetConfig(&config_old);
  233. /* The parameter says whether we turn the protection on or off */
  234. config_new.WRPState = modifier;
  235. /* We want to modify only the Write protection */
  236. config_new.OptionType = OPTIONBYTE_WRP;
  237. /* No read protection, keep BOR and reset settings */
  238. config_new.RDPLevel = OB_RDP_LEVEL_0;
  239. config_new.USERConfig = config_old.USERConfig;
  240. /* Get pages already write protected ****************************************/
  241. ProtectedSECTOR = config_old.WRPSector | FLASH_SECTOR_TO_BE_PROTECTED;
  242. /* Unlock the Flash to enable the flash control register access *************/
  243. HAL_FLASH_Unlock();
  244. /* Unlock the Options Bytes *************************************************/
  245. HAL_FLASH_OB_Unlock();
  246. config_new.WRPSector = ProtectedSECTOR;
  247. result = HAL_FLASHEx_OBProgram(&config_new);
  248. return result;
  249. }
  250. //================================================
  251. // Phihong IAP custom routine
  252. //================================================
  253. uint8_t PH_CalNewCodeChecksum(void)
  254. {
  255. uint32_t CheckSum=0;
  256. __IO uint32_t flashdestination = NEW_CODE_ADDRESS;
  257. for(uint32_t i=0;i<0x60000;i++)
  258. {
  259. CheckSum += (*(uint16_t *)(flashdestination+i));
  260. }
  261. #ifdef DEBUG
  262. DEBUG_INFO("\r\nChecksum = 0x%X", CheckSum);
  263. #endif
  264. if(CheckSum == *(uint16_t *)0x080fffff)
  265. {
  266. return(1);
  267. }
  268. else
  269. {
  270. return(0);
  271. }
  272. }
  273. uint8_t PH_isMoveOK(void)
  274. {
  275. uint8_t isSuccess = 1;
  276. //__IO uint32_t flashdestination = APPLICATION_ADDRESS;
  277. //__IO uint32_t newdestination = NEW_CODE_ADDRESS;
  278. uint32_t flashdestination = APPLICATION_ADDRESS;
  279. uint32_t newdestination = NEW_CODE_ADDRESS;
  280. for(uint32_t i=0;i<0x60000;i++)
  281. {
  282. HAL_IWDG_Refresh(&hiwdg);
  283. if((*((uint8_t *)newdestination++)) != (*((uint8_t *)flashdestination++)))
  284. {
  285. isSuccess = 0;
  286. }
  287. }
  288. #ifdef DEBUG
  289. if(isSuccess)
  290. DEBUG_INFO("Move code compare OK...\r\n");
  291. else
  292. DEBUG_INFO("Move code compare fail...\r\n");
  293. #endif
  294. return isSuccess;
  295. }
  296. void PH_ClearUpgradeReq(void)
  297. {
  298. FLASH_If_Erase(UPGRADE_REQ_ADDRESS, 1);
  299. #ifdef DEBUG
  300. DEBUG_INFO("Clean firmware upgrade request flag...\r\n");
  301. #endif
  302. }
  303. uint8_t PH_isUpgradeReq(void)
  304. {
  305. uint8_t isSuccess = 0;
  306. __IO uint32_t flashdestination = UPGRADE_REQ_ADDRESS;
  307. if(*((uint32_t *)flashdestination) == 0x0aa55aa55)
  308. {
  309. #ifdef DEBUG
  310. DEBUG_INFO("Firmware upgrade request detect...\r\n");
  311. #endif
  312. isSuccess = 1;
  313. }
  314. else if(*((uint32_t *)flashdestination) != 0x0ffffffff)
  315. {
  316. PH_ClearUpgradeReq();
  317. }
  318. else
  319. {
  320. #ifdef DEBUG
  321. DEBUG_INFO("Firmware upgrade request does not detect...\r\n");
  322. #endif
  323. }
  324. return isSuccess;
  325. }
  326. void PH_MoveCode2Ap(void)
  327. {
  328. uint32_t packet_length = 0x10000;
  329. uint8_t flag_flash_write_err = 0;
  330. //__IO uint32_t flashdestination = APPLICATION_ADDRESS;
  331. //__IO uint32_t newdestination = NEW_CODE_ADDRESS;
  332. uint32_t flashdestination = APPLICATION_ADDRESS;
  333. uint32_t newdestination = NEW_CODE_ADDRESS;
  334. FLASH_If_Erase(ADDR_FLASH_SECTOR_6, 3);
  335. #ifdef DEBUG
  336. DEBUG_INFO("AP flash(0x8040000 erase finish\r\n");
  337. DEBUG_INFO("Move flash data form 0x80A0000( length 0x60000) to 0x8040000");
  338. #endif
  339. for(uint32_t i=0;i<6;i++)
  340. {
  341. if ((FLASH_If_Write(flashdestination, (uint32_t*) newdestination, packet_length/4)) == FLASHIF_OK)
  342. {
  343. flashdestination += packet_length;
  344. newdestination += packet_length;
  345. #ifdef DEBUG
  346. DEBUG_INFO(".");
  347. if(i==5)
  348. DEBUG_INFO("\r\n");
  349. #endif
  350. }
  351. else
  352. {
  353. #ifdef DEBUG
  354. DEBUG_INFO("Fail\r\n");
  355. #endif
  356. flag_flash_write_err = 1;
  357. }
  358. }
  359. //CalNewCodeChecksum();
  360. if(!PH_isMoveOK())
  361. {
  362. flag_flash_write_err = 1;
  363. }
  364. if(!flag_flash_write_err)
  365. {
  366. PH_ClearUpgradeReq();
  367. #ifdef DEBUG
  368. DEBUG_INFO("Upgrade finish, MCU reboot...\r\n");
  369. #endif
  370. NVIC_SystemReset();
  371. }
  372. }
  373. /**
  374. * @}
  375. */
  376. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/