flash.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /*
  2. * flash.c
  3. *
  4. * Created on: 2019. 7. 15.
  5. * Author: parkyj
  6. */
  7. #include "flash.h"
  8. #include "MBIC_Bootloader.h"
  9. #include "bluecell_operate.h"
  10. uint8_t flashinit = 0;
  11. uint32_t Address = FLASH_USER_START_ADDR;
  12. volatile static uint32_t UserAddress;
  13. uint32_t MBIC_Address;
  14. typedef void (*fptr)(void);
  15. fptr jump_to_app;
  16. uint32_t jump_addr;
  17. void Jump_App(void){
  18. __HAL_RCC_TIM6_CLK_DISABLE(); // 留ㅼ씤???占쏙옙癒몌옙?? ?占쏙옙占�??占쏙옙?占쏙옙?占쏙옙
  19. printf("boot loader start\n"); //硫붿꽭占�? 異쒕젰
  20. jump_addr = *(__IO uint32_t*) (APPLICATION_ADDRESS + 4);
  21. jump_to_app = (fptr) jump_addr;
  22. /* init user app's sp */
  23. printf("jump!\n");
  24. __set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS);
  25. jump_to_app();
  26. }
  27. void Flash_InitRead() // ?占쏙옙湲고븿?占쏙옙
  28. {
  29. uint32_t CurrApiAddress = 0,Bank1Address=0,Bank2Address = 0;
  30. uint32_t i = 0;
  31. uint8_t ret = 0;
  32. CurrApiAddress = FLASH_MBICUSER_START_ADDR;
  33. Bank1Address = FLASH_USER_BANK1_START_ADDR;
  34. Bank2Address = FLASH_USER_BANK2_START_ADDR;
  35. uint8_t* Currdata = (uint8_t*)CurrApiAddress;
  36. uint8_t* Bank1data = (uint8_t*)Bank1Address;
  37. uint8_t* Bank2data = (uint8_t*)Bank2Address;
  38. printf("Flash Init \r\n");
  39. for(int i = 0; i < 6; i++){
  40. if(Currdata[MBIC_BOOT_CREATION_TIME + i] != Bank1data[MBIC_BOOT_CREATION_TIME + i]){
  41. ret = HFR_BANK2;
  42. printf("MBIC BANK2 Booting \r\n");
  43. break;
  44. }else{
  45. ret = HFR_BANK1;
  46. }
  47. }
  48. uint8_t* pdata;
  49. bluecell_Currdatastatus.CPU_Bank1_Image_BuildTime1 = Bank1data[MBIC_BOOT_CREATION_TIME + 0];
  50. bluecell_Currdatastatus.CPU_Bank1_Image_BuildTime2 = Bank1data[MBIC_BOOT_CREATION_TIME + 1];
  51. bluecell_Currdatastatus.CPU_Bank1_Image_BuildTime3 = Bank1data[MBIC_BOOT_CREATION_TIME + 2];
  52. bluecell_Currdatastatus.CPU_Bank1_Image_BuildTime4 = Bank1data[MBIC_BOOT_CREATION_TIME + 3];
  53. bluecell_Currdatastatus.CPU_Bank1_Image_BuildTime5 = Bank1data[MBIC_BOOT_CREATION_TIME + 4];
  54. bluecell_Currdatastatus.CPU_Bank1_Image_BuildTime6 = Bank1data[MBIC_BOOT_CREATION_TIME + 5];
  55. bluecell_Currdatastatus.CPU_Bank1_Image_Version1 = Bank1data[MBIC_BOOT_VERSION + 0];
  56. bluecell_Currdatastatus.CPU_Bank1_Image_Version2 = Bank1data[MBIC_BOOT_VERSION + 1];
  57. bluecell_Currdatastatus.CPU_Bank1_Image_Version3 = Bank1data[MBIC_BOOT_VERSION + 2];
  58. pdata = &bluecell_Currdatastatus.CPU_Bank1_Image_Name;
  59. printf("BANK1 IMAGE NAME : ");
  60. for(int i = 0 ; i< 32; i++){
  61. pdata[i] = Bank1data[MBIC_BOOT_FILENAME + i];
  62. printf("%c",pdata[i]);
  63. }
  64. printf("\r\n");
  65. bluecell_Currdatastatus.CPU_Bank2_Image_BuildTime1 = Bank2data[MBIC_BOOT_CREATION_TIME + 0];
  66. bluecell_Currdatastatus.CPU_Bank2_Image_BuildTime2 = Bank2data[MBIC_BOOT_CREATION_TIME + 1];
  67. bluecell_Currdatastatus.CPU_Bank2_Image_BuildTime3 = Bank2data[MBIC_BOOT_CREATION_TIME + 2];
  68. bluecell_Currdatastatus.CPU_Bank2_Image_BuildTime4 = Bank2data[MBIC_BOOT_CREATION_TIME + 3];
  69. bluecell_Currdatastatus.CPU_Bank2_Image_BuildTime5 = Bank2data[MBIC_BOOT_CREATION_TIME + 4];
  70. bluecell_Currdatastatus.CPU_Bank2_Image_BuildTime6 = Bank2data[MBIC_BOOT_CREATION_TIME + 5];
  71. bluecell_Currdatastatus.CPU_Bank2_Image_Version1 = Bank2data[MBIC_BOOT_VERSION + 0];
  72. bluecell_Currdatastatus.CPU_Bank2_Image_Version2 = Bank2data[MBIC_BOOT_VERSION + 1];
  73. bluecell_Currdatastatus.CPU_Bank2_Image_Version3 = Bank2data[MBIC_BOOT_VERSION + 2];
  74. pdata = & bluecell_Currdatastatus.CPU_Bank2_Image_Name;
  75. printf("BANK2 IMAGE NAME : ");
  76. for(int i = 0 ; i< 32; i++){
  77. pdata[i] = Bank2data[MBIC_BOOT_FILENAME + i];
  78. printf("%c",pdata[i]);
  79. }
  80. printf("\r\n");
  81. bluecell_Currdatastatus.CPUVERSION1 = Currdata[MBIC_BOOT_VERSION + 0];
  82. bluecell_Currdatastatus.CPUVERSION2 = Currdata[MBIC_BOOT_VERSION + 1];
  83. bluecell_Currdatastatus.CPUVERSION3 = Currdata[MBIC_BOOT_VERSION + 2];
  84. printf("20%d Y / %d M / %d D / %d H / %d M / %d S \r\n",
  85. Currdata[MBIC_BOOT_CREATION_TIME + 0],
  86. Currdata[MBIC_BOOT_CREATION_TIME + 1],
  87. Currdata[MBIC_BOOT_CREATION_TIME + 2],
  88. Currdata[MBIC_BOOT_CREATION_TIME + 3],
  89. Currdata[MBIC_BOOT_CREATION_TIME + 4],
  90. Currdata[MBIC_BOOT_CREATION_TIME + 5]
  91. );
  92. bluecell_Currdatastatus.CPU_Current_Bank = ret;
  93. printf("MBIC BANK %d Booting \r\n",bluecell_Currdatastatus.CPU_Current_Bank);
  94. #if 0 // PYJ.2020.06.25_BEGIN --
  95. for(i = 0; i < 128; i++ ){
  96. printf("Address : %08x Value : %02X \r\n",data+i ,data[i]);
  97. }
  98. #endif // PYJ.2020.06.25_END --
  99. #if 0 // PYJ.2020.06.25_BEGIN --
  100. for(i = 0; i < size; i++ ){
  101. printf("Address : %08x Value : %02X \r\n",Address ,*(uint8_t*)Address);
  102. Address++;
  103. }
  104. Address -= i;
  105. #endif // PYJ.2020.06.25_END --
  106. #if 0 // PYJ.2019.03.27_BEGIN --
  107. for(uint32_t i = 0; i < 13848; i++ ){
  108. printf("%08x : %02X \n",Address ,*(uint8_t*)Address);
  109. Address++;
  110. }
  111. Address = StartAddr;
  112. for(uint32_t i = 0; i < 13848; i++ ){
  113. printf("%02X ",*(uint8_t*)Address);
  114. Address++;
  115. }
  116. #endif // PYJ.2019.03.27_END --
  117. }
  118. uint8_t Flash_Data_Write(uint8_t* data){
  119. uint16_t Firmdata = 0;
  120. uint8_t ret = 0;
  121. int i = 0;
  122. static uint8_t Errcnt = 0;
  123. static uint32_t Curr_Download_DataIndex = 0;
  124. static uint32_t Prev_Download_DataIndex = 0;
  125. Curr_Download_DataIndex = data[MBIC_PAYLOADSTART + 4] << 24;
  126. Curr_Download_DataIndex += data[MBIC_PAYLOADSTART + 5] << 16;
  127. Curr_Download_DataIndex += data[MBIC_PAYLOADSTART + 6] << 8;
  128. Curr_Download_DataIndex += data[MBIC_PAYLOADSTART + 7];
  129. // data[MBIC_PAYLOADSTART + 12 +i];
  130. returntoFirst:
  131. for(i= 0; i <= Curr_Download_DataIndex - Prev_Download_DataIndex; i+=2){
  132. Firmdata = ((data[MBIC_PAYLOADSTART + 12 +i]) & 0x00FF);
  133. Firmdata += ((data[MBIC_PAYLOADSTART + 12 +i + 1] << 8) & 0xFF00);
  134. if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,UserAddress , (uint16_t)Firmdata) != HAL_OK)
  135. {
  136. printf("HAL NOT OK \n");
  137. ret = 1;
  138. HAL_Delay(1000);
  139. if(Errcnt == 0xFF)
  140. return;
  141. // Errcnt++;
  142. goto returntoFirst;
  143. }else{
  144. Errcnt = 0; //Err Cnt Init;
  145. UserAddress += 2;
  146. }
  147. }
  148. Prev_Download_DataIndex = Curr_Download_DataIndex + 1;
  149. return ret;
  150. }
  151. uint8_t API_Flash_Data_Write(uint8_t* data){
  152. uint16_t Firmdata = 0;
  153. uint8_t ret = 0;
  154. int i = 0;
  155. static uint8_t Errcnt = 0;
  156. static uint32_t Curr_Download_DataIndex = 0;
  157. static uint32_t Prev_Download_DataIndex = 0;
  158. Curr_Download_DataIndex = data[MBIC_PAYLOADSTART + 4] << 24;
  159. Curr_Download_DataIndex += data[MBIC_PAYLOADSTART + 5] << 16;
  160. Curr_Download_DataIndex += data[MBIC_PAYLOADSTART + 6] << 8;
  161. Curr_Download_DataIndex += data[MBIC_PAYLOADSTART + 7];
  162. // data[MBIC_PAYLOADSTART + 12 +i];
  163. returntoFirst:
  164. for(i= 0; i <= Curr_Download_DataIndex - Prev_Download_DataIndex; i+=2){
  165. Firmdata = ((data[MBIC_PAYLOADSTART + 12 +i]) & 0x00FF);
  166. Firmdata += ((data[MBIC_PAYLOADSTART + 12 +i + 1] << 8) & 0xFF00);
  167. if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,UserAddress , (uint16_t)Firmdata) != HAL_OK)
  168. {
  169. printf("HAL NOT OK \n");
  170. ret = 1;
  171. HAL_Delay(1000);
  172. if(Errcnt == 0xFF)
  173. return;
  174. Errcnt++;
  175. goto returntoFirst;
  176. }else{
  177. Errcnt = 0; //Err Cnt Init;
  178. UserAddress += 2;
  179. }
  180. }
  181. Prev_Download_DataIndex = Curr_Download_DataIndex + 1;
  182. return ret;
  183. }
  184. #if 1 // PYJ.2020.06.24_BEGIN --
  185. uint8_t Bank_Flash_write(uint8_t* data,uint32_t StartBankAddress) // ?占쏙옙湲고븿?占쏙옙
  186. {
  187. /*Variable used for Erase procedure*/
  188. static FLASH_EraseInitTypeDef EraseInitStruct;
  189. static uint32_t PAGEError = 0;
  190. uint8_t ret = 0;
  191. HAL_FLASH_Unlock(); // lock ??占�?
  192. #if 0 // PYJ.2020.06.24_BEGIN --
  193. if(flashinit == 0){
  194. flashinit= 1;
  195. /* Fill EraseInit structure*/
  196. switch(StartBankAddress){
  197. case FLASH_USER_START_ADDR:
  198. EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
  199. EraseInitStruct.PageAddress = FLASH_USER_START_ADDR - 128;
  200. EraseInitStruct.NbPages = (FLASH_USER_END_ADDR - FLASH_USER_START_ADDR) / FLASH_PAGE_SIZE;
  201. break;
  202. case FLASH_USER_BANK1_START_ADDR:
  203. EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
  204. EraseInitStruct.PageAddress = FLASH_USER_BANK1_START_ADDR - 128;
  205. EraseInitStruct.NbPages = (FLASH_USER_END_ADDR - FLASH_USER_BANK1_START_ADDR) / FLASH_PAGE_SIZE;
  206. break;
  207. case FLASH_USER_BANK2_START_ADDR:
  208. EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
  209. EraseInitStruct.PageAddress = FLASH_USER_BANK2_START_ADDR - 128;
  210. EraseInitStruct.NbPages = (FLASH_USER_END_ADDR - FLASH_USER_BANK2_START_ADDR) / FLASH_PAGE_SIZE;
  211. break;
  212. }
  213. UserAddress = EraseInitStruct.PageAddress;
  214. //FLASH_PageErase(StartAddr);
  215. if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK){
  216. printf("Erase Failed \r\n");
  217. }
  218. }
  219. #endif // PYJ.2020.06.24_END --
  220. ret = Flash_Data_Write(&data[MBIC_PREAMBLE_0]);
  221. HAL_FLASH_Lock(); // lock ?占쏙옙洹멸린
  222. return ret;
  223. }
  224. #endif // PYJ.2020.06.24_END --
  225. #define DATA_32 ((uint32_t)0x33333333)
  226. __IO bool EraseInit = false;
  227. #if 0 // PYJ.2020.06.24_BEGIN --
  228. void MBIC_APIFLASH_test(uint8_t* data,uint32_t size){
  229. static FLASH_EraseInitTypeDef EraseInitStruct;
  230. static uint32_t Address = 0, PAGEError = 0;
  231. static __IO uint32_t data32 = 0 , MemoryProgramStatus = 0;
  232. int dataindex = 0;
  233. uint32_t writedata = 0;
  234. HAL_FLASH_Unlock();
  235. /* Erase the user Flash area
  236. (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
  237. /* Fill EraseInit structure*/
  238. if(EraseInit == false){
  239. EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
  240. EraseInitStruct.PageAddress = FLASH_USER_BANK1_START_ADDR;
  241. EraseInitStruct.NbPages = ((FLASH_USER_BANK1_START_ADDR + ((uint32_t)0xFFFF)) - FLASH_USER_BANK1_START_ADDR) / FLASH_PAGE_SIZE;
  242. printf("Erase Start\r\n");
  243. if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK)
  244. {
  245. /*
  246. Error occurred while page erase.
  247. User can add here some code to deal with this error.
  248. PAGEError will contain the faulty page and then to know the code error on this page,
  249. user can call function 'HAL_FLASH_GetError()'
  250. */
  251. /* Infinite loop */
  252. while (1)
  253. {
  254. /* Make LED2 blink (100ms on, 2s off) to indicate error in Erase operation */
  255. printf("HAL_FLASHEx_Erase Error\r\n");
  256. HAL_Delay(2000);
  257. }
  258. }
  259. EraseInit = true;
  260. printf("Erase End\r\n");
  261. }
  262. /* Program the user Flash area word by word
  263. (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
  264. Address = FLASH_USER_BANK1_START_ADDR;
  265. printf("HAL_FLASH_Program Start\r\n");
  266. while (Address < ((FLASH_USER_BANK1_START_ADDR + size)))
  267. {
  268. writedata = data[dataindex + 0] ;
  269. writedata += data[dataindex + 1] << 8 ;
  270. writedata += data[dataindex + 2] << 16;
  271. writedata += data[dataindex + 3] << 24;
  272. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Address,writedata) == HAL_OK)
  273. {
  274. Address = Address + 4;
  275. dataindex += 4;
  276. }
  277. else
  278. {
  279. /* Error occurred while writing data in Flash memory.
  280. User can add here some code to deal with this error */
  281. while (1)
  282. {
  283. /* Make LED2 blink (100ms on, 2s off) to indicate error in Write operation */
  284. /* Make LED2 blink (100ms on, 2s off) to indicate error in Erase operation */
  285. printf("HAL_FLASH_Program Error\r\n");
  286. }
  287. }
  288. }
  289. printf("HAL_FLASH_Program END\r\n");
  290. /* Lock the Flash to disable the flash control register access (recommended
  291. to protect the FLASH memory against possible unwanted operation) *********/
  292. HAL_FLASH_Lock();
  293. /* Check if the programmed data is OK
  294. MemoryProgramStatus = 0: data programmed correctly
  295. MemoryProgramStatus != 0: number of words not programmed correctly ******/
  296. Address = FLASH_USER_BANK1_START_ADDR;
  297. MemoryProgramStatus = 0x0;
  298. // while (Address < ((FLASH_USER_BANK1_START_ADDR + 12)))
  299. // {
  300. // data32 = *(__IO uint32_t *)Address;
  301. // printf("%08x : %02X \n",Address ,*(uint8_t*)Address);
  302. // if (data32 != DATA_32)
  303. // {
  304. // MemoryProgramStatus++;
  305. // }
  306. // Address = Address + 4;
  307. // }
  308. //
  309. // /*Check if there is an issue to program data*/
  310. // if (MemoryProgramStatus == 0)
  311. // {
  312. // printf("DATA OK \r\n");
  313. // /* No error detected. Switch on LED2*/
  314. // }
  315. // else
  316. // {
  317. // /* Error detected. LED2 will blink with 1s period */
  318. // while (1)
  319. // {
  320. // printf("Read Error\r\n");
  321. // }
  322. // }
  323. }
  324. #else
  325. void MBIC_DataBankWrite(uint8_t BankNum){
  326. static FLASH_EraseInitTypeDef EraseInitStruct;
  327. static uint32_t PAGEError = 0;
  328. EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
  329. EraseInitStruct.PageAddress = FLASH_USER_BANK_NUM;
  330. EraseInitStruct.NbPages = ((FLASH_USER_BANK_NUM + ((uint32_t)1)) - FLASH_USER_BANK_NUM) / FLASH_PAGE_SIZE;
  331. printf("Erase Start BANK NUM\r\n");
  332. if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK)
  333. {
  334. while (1)
  335. {
  336. HAL_Delay(2000);
  337. }
  338. }
  339. printf("Erase End\r\n");
  340. HAL_FLASH_Unlock();
  341. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, FLASH_USER_BANK_NUM,BankNum) == HAL_OK)
  342. {
  343. printf("Bank%d Write Success \r\n",BankNum);
  344. }else{
  345. printf("Bank%d Write Failed \r\n",BankNum);
  346. }
  347. HAL_FLASH_Lock();
  348. }
  349. void MBIC_DataErase_Func(uint8_t bank_num){
  350. static FLASH_EraseInitTypeDef EraseInitStruct;
  351. static uint32_t PAGEError = 0;
  352. if(EraseInit == false){
  353. #if 0 // PYJ.2020.06.24_BEGIN --
  354. EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
  355. EraseInitStruct.PageAddress = FLASH_USER_BANK1_START_ADDR;
  356. printf("PageAddress : %x \r\n",FLASH_USER_BANK1_START_ADDR);
  357. // EraseInitStruct.NbPages = ((FLASH_USER_BANK1_START_ADDR + ((uint32_t)0xFFFF)) - FLASH_USER_BANK1_START_ADDR) / FLASH_PAGE_SIZE;
  358. EraseInitStruct.NbPages = (FLASH_USER_BANK1_START_ADDR - ((uint32_t)0xFFFF)) / FLASH_PAGE_SIZE;
  359. #else
  360. if(bank_num == HFR_AUTO_SEL){
  361. if(bluecell_Currdatastatus.CPU_Current_Bank == HFR_BANK1){
  362. bank_num = HFR_BANK2;
  363. printf("Curr Bank BANK 1 I will download Bank2 \r\n");
  364. }
  365. else{
  366. bank_num = HFR_BANK1;
  367. printf("Curr Bank BANK 2 I will download Bank1 \r\n");
  368. }
  369. }
  370. if(bank_num == HFR_BANK2){
  371. EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
  372. EraseInitStruct.PageAddress = FLASH_USER_BANK2_START_ADDR;
  373. EraseInitStruct.NbPages = (FLASH_USER_BANK2_END_ADDR - FLASH_USER_BANK2_START_ADDR) / FLASH_PAGE_SIZE;
  374. MBIC_Address = FLASH_USER_BANK2_START_ADDR;
  375. printf("I will download Bank2 \r\n");
  376. printf("Erase BANK 2 \r\n");
  377. }else{
  378. EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
  379. EraseInitStruct.PageAddress = FLASH_USER_BANK1_START_ADDR;
  380. EraseInitStruct.NbPages = (FLASH_USER_BANK1_END_ADDR - FLASH_USER_BANK1_START_ADDR) / FLASH_PAGE_SIZE;
  381. MBIC_Address = FLASH_USER_BANK1_START_ADDR;
  382. printf("I will download Bank1 \r\n");
  383. printf("Erase BANK 1 \r\n");
  384. }
  385. EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
  386. EraseInitStruct.PageAddress = FLASH_USER_BANK2_START_ADDR;
  387. EraseInitStruct.NbPages = (FLASH_USER_BANK2_END_ADDR - FLASH_USER_BANK2_START_ADDR) / FLASH_PAGE_SIZE;
  388. MBIC_Address = FLASH_USER_BANK2_START_ADDR;
  389. #endif // PYJ.2020.06.24_END --
  390. printf("NbPages : %x \r\n",EraseInitStruct.NbPages );
  391. printf("Erase Start\r\n");
  392. if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK)
  393. {
  394. /*
  395. Error occurred while page erase.
  396. User can add here some code to deal with this error.
  397. PAGEError will contain the faulty page and then to know the code error on this page,
  398. user can call function 'HAL_FLASH_GetError()'
  399. */
  400. /* Infinite loop */
  401. while (1)
  402. {
  403. /* Make LED2 blink (100ms on, 2s off) to indicate error in Erase operation */
  404. printf("HAL_FLASHEx_Erase Error\r\n");
  405. HAL_Delay(2000);
  406. }
  407. }
  408. EraseInit = true;
  409. printf("Erase End\r\n");
  410. }
  411. }
  412. void MBIC_APIFLASH_Func(uint8_t* data,uint32_t size){
  413. static uint32_t PAGEError = 0;
  414. static uint32_t DownloadIndex;
  415. static __IO uint32_t data32 = 0 , MemoryProgramStatus = 0;
  416. int dataindex = 0;
  417. uint32_t writedata = 0;
  418. HAL_FLASH_Unlock();
  419. /* Erase the user Flash area
  420. (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
  421. /* Fill EraseInit structure*/
  422. #if 0 // PYJ.2020.06.24_BEGIN --
  423. if(EraseInit == false){
  424. EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
  425. EraseInitStruct.PageAddress = FLASH_USER_BANK1_START_ADDR;
  426. EraseInitStruct.NbPages = ((FLASH_USER_BANK1_START_ADDR + ((uint32_t)0xFFFF)) - FLASH_USER_BANK1_START_ADDR) / FLASH_PAGE_SIZE;
  427. printf("Erase Start\r\n");
  428. if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK)
  429. {
  430. /*
  431. Error occurred while page erase.
  432. User can add here some code to deal with this error.
  433. PAGEError will contain the faulty page and then to know the code error on this page,
  434. user can call function 'HAL_FLASH_GetError()'
  435. */
  436. /* Infinite loop */
  437. while (1)
  438. {
  439. /* Make LED2 blink (100ms on, 2s off) to indicate error in Erase operation */
  440. printf("HAL_FLASHEx_Erase Error\r\n");
  441. HAL_Delay(2000);
  442. }
  443. }
  444. EraseInit = true;
  445. printf("Erase End\r\n");
  446. }
  447. #endif // PYJ.2020.06.24_END --
  448. /* Program the user Flash area word by word
  449. (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
  450. // MBIC_Address = FLASH_USER_BANK1_START_ADDR;
  451. printf("HAL_FLASH_Program Start\r\n");
  452. DownloadIndex += size;
  453. #if 0 // PYJ.2020.06.24_BEGIN --
  454. while (MBIC_Address <= ((FLASH_USER_BANK1_START_ADDR + DownloadIndex)))
  455. {
  456. writedata = data[dataindex + 0] ;
  457. writedata += data[dataindex + 1] << 8 ;
  458. writedata += data[dataindex + 2] << 16;
  459. writedata += data[dataindex + 3] << 24;
  460. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, MBIC_Address,writedata) == HAL_OK)
  461. {
  462. MBIC_Address += 4;
  463. dataindex += 4;
  464. }
  465. else
  466. {
  467. /* Error occurred while writing data in Flash memory.
  468. User can add here some code to deal with this error */
  469. while (1)
  470. {
  471. printf("HAL_FLASH_Program Error\r\n");
  472. }
  473. }
  474. }
  475. #else
  476. printf("MBIC_Address : %x \r\n",MBIC_Address);
  477. for(int downindex = 0; downindex < size; downindex+=4)
  478. {
  479. writedata = data[downindex + 0] ;
  480. writedata += data[downindex + 1] << 8 ;
  481. writedata += data[downindex + 2] << 16;
  482. writedata += data[downindex + 3] << 24;
  483. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, MBIC_Address,writedata) == HAL_OK)
  484. {
  485. MBIC_Address += 4;
  486. }
  487. else
  488. {
  489. while (1)
  490. {
  491. printf("HAL_FLASH_Program Error\r\n");
  492. }
  493. }
  494. }
  495. #endif // PYJ.2020.06.24_END --
  496. printf("HAL_FLASH_Program END\r\n");
  497. /* Lock the Flash to disable the flash control register access (recommended
  498. to protect the FLASH memory against possible unwanted operation) *********/
  499. HAL_FLASH_Lock();
  500. /* Check if the programmed data is OK
  501. MemoryProgramStatus = 0: data programmed correctly
  502. MemoryProgramStatus != 0: number of words not programmed correctly ******/
  503. }
  504. #endif // PYJ.2020.06.24_END --