flash.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. uint8_t flashinit = 0;
  10. uint32_t Address = FLASH_USER_START_ADDR;
  11. volatile static uint32_t UserAddress;
  12. typedef void (*fptr)(void);
  13. fptr jump_to_app;
  14. uint32_t jump_addr;
  15. void Jump_App(void){
  16. __HAL_RCC_TIM6_CLK_DISABLE(); // 留ㅼ씤???占쏙옙癒몌옙?? ?占쏙옙占�??占쏙옙?占쏙옙?占쏙옙
  17. printf("boot loader start\n"); //硫붿꽭占�? 異쒕젰
  18. jump_addr = *(__IO uint32_t*) (APPLICATION_ADDRESS + 4);
  19. jump_to_app = (fptr) jump_addr;
  20. /* init user app's sp */
  21. printf("jump!\n");
  22. __set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS);
  23. jump_to_app();
  24. }
  25. void Flash_InitRead(void) // ?占쏙옙湲고븿?占쏙옙
  26. {
  27. uint32_t Address = 0;
  28. Address = FLASH_USER_TEMPBANK_START_ADDR;
  29. for(uint32_t i = 0; i < 300; i++ ){
  30. printf("%08x : %02X \n",Address ,*(uint8_t*)Address);
  31. Address++;
  32. }
  33. #if 0 // PYJ.2019.03.27_BEGIN --
  34. for(uint32_t i = 0; i < 13848; i++ ){
  35. printf("%08x : %02X \n",Address ,*(uint8_t*)Address);
  36. Address++;
  37. }
  38. Address = StartAddr;
  39. for(uint32_t i = 0; i < 13848; i++ ){
  40. printf("%02X ",*(uint8_t*)Address);
  41. Address++;
  42. }
  43. #endif // PYJ.2019.03.27_END --
  44. }
  45. uint8_t Flash_Data_Write(uint8_t* data){
  46. uint16_t Firmdata = 0;
  47. uint8_t ret = 0;
  48. int i = 0;
  49. static uint32_t Curr_Download_DataIndex = 0;
  50. static uint32_t Prev_Download_DataIndex = 0;
  51. Curr_Download_DataIndex = data[MBIC_PAYLOADSTART + 4] << 24;
  52. Curr_Download_DataIndex += data[MBIC_PAYLOADSTART + 5] << 16;
  53. Curr_Download_DataIndex += data[MBIC_PAYLOADSTART + 6] << 8;
  54. Curr_Download_DataIndex += data[MBIC_PAYLOADSTART + 7];
  55. // data[MBIC_PAYLOADSTART + 12 +i];
  56. returntoFirst:
  57. UserAddress -= i;
  58. for(i= 0; i <= Curr_Download_DataIndex - Prev_Download_DataIndex; i+=2){
  59. Firmdata = ((data[MBIC_PAYLOADSTART + 12 +i]) & 0x00FF);
  60. Firmdata += ((data[MBIC_PAYLOADSTART + 12 +i + 1] << 8) & 0xFF00);
  61. if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,UserAddress , (uint16_t)Firmdata) != HAL_OK){
  62. printf("HAL NOT OK \n");
  63. ret = 1;
  64. HAL_Delay(1000);
  65. goto returntoFirst;
  66. }else{
  67. UserAddress += 2;
  68. }
  69. }
  70. Prev_Download_DataIndex = Curr_Download_DataIndex + 1;
  71. return ret;
  72. }
  73. uint8_t Bank_Flash_write(uint8_t* data,uint32_t StartBankAddress) // ?占쏙옙湲고븿?占쏙옙
  74. {
  75. /*Variable used for Erase procedure*/
  76. static FLASH_EraseInitTypeDef EraseInitStruct;
  77. static uint32_t PAGEError = 0;
  78. uint8_t ret = 0;
  79. HAL_FLASH_Unlock(); // lock ??占�?
  80. if(flashinit == 0){
  81. flashinit= 1;
  82. /* Fill EraseInit structure*/
  83. switch(StartBankAddress){
  84. case FLASH_USER_START_ADDR:
  85. EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
  86. EraseInitStruct.PageAddress = FLASH_USER_START_ADDR - 128;
  87. EraseInitStruct.NbPages = (FLASH_USER_END_ADDR - FLASH_USER_START_ADDR) / FLASH_PAGE_SIZE;
  88. break;
  89. case FLASH_USER_BANK1_START_ADDR:
  90. EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
  91. EraseInitStruct.PageAddress = FLASH_USER_BANK1_START_ADDR - 128;
  92. EraseInitStruct.NbPages = (FLASH_USER_END_ADDR - FLASH_USER_BANK1_START_ADDR) / FLASH_PAGE_SIZE;
  93. break;
  94. case FLASH_USER_BANK2_START_ADDR:
  95. EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
  96. EraseInitStruct.PageAddress = FLASH_USER_BANK2_START_ADDR - 128;
  97. EraseInitStruct.NbPages = (FLASH_USER_END_ADDR - FLASH_USER_BANK2_START_ADDR) / FLASH_PAGE_SIZE;
  98. break;
  99. case FLASH_USER_TEMPBANK_START_ADDR:
  100. EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
  101. EraseInitStruct.PageAddress = FLASH_USER_TEMPBANK_START_ADDR - 128;
  102. EraseInitStruct.NbPages = (FLASH_USER_END_ADDR - FLASH_USER_TEMPBANK_START_ADDR) / FLASH_PAGE_SIZE;
  103. break;
  104. }
  105. UserAddress = EraseInitStruct.PageAddress;
  106. //FLASH_PageErase(StartAddr);
  107. if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK){
  108. printf("Erase Failed \r\n");
  109. }
  110. }
  111. ret = Flash_Data_Write(&data[MBIC_PREAMBLE_0]);
  112. HAL_FLASH_Lock(); // lock ?占쏙옙洹멸린
  113. return ret;
  114. }
  115. #define DATA_32 ((uint32_t)0x12345678)
  116. __IO bool EraseInit = false;
  117. void MBIC_APIFLASH_test(){
  118. static FLASH_EraseInitTypeDef EraseInitStruct;
  119. static uint32_t Address = 0, PAGEError = 0;
  120. static __IO uint32_t data32 = 0 , MemoryProgramStatus = 0;
  121. HAL_FLASH_Unlock();
  122. /* Erase the user Flash area
  123. (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
  124. /* Fill EraseInit structure*/
  125. if(EraseInit == false){
  126. EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
  127. EraseInitStruct.PageAddress = FLASH_USER_TEMPBANK_START_ADDR;
  128. EraseInitStruct.NbPages = ((FLASH_USER_TEMPBANK_START_ADDR + ((uint32_t)0x000FFFF)) - FLASH_USER_TEMPBANK_START_ADDR) / FLASH_PAGE_SIZE;
  129. printf("Erase Start\r\n");
  130. if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK)
  131. {
  132. /*
  133. Error occurred while page erase.
  134. User can add here some code to deal with this error.
  135. PAGEError will contain the faulty page and then to know the code error on this page,
  136. user can call function 'HAL_FLASH_GetError()'
  137. */
  138. /* Infinite loop */
  139. while (1)
  140. {
  141. /* Make LED2 blink (100ms on, 2s off) to indicate error in Erase operation */
  142. printf("HAL_FLASHEx_Erase Error\r\n");
  143. HAL_Delay(2000);
  144. }
  145. }
  146. EraseInit = true;
  147. printf("Erase End\r\n");
  148. }
  149. /* Program the user Flash area word by word
  150. (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
  151. Address = FLASH_USER_TEMPBANK_START_ADDR;
  152. printf("HAL_FLASH_Program Start\r\n");
  153. while (Address < ((FLASH_USER_TEMPBANK_START_ADDR + ((uint32_t)0x000FFFF))))
  154. {
  155. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, Address, DATA_32) == HAL_OK)
  156. {
  157. Address = Address + 2;
  158. }
  159. else
  160. {
  161. /* Error occurred while writing data in Flash memory.
  162. User can add here some code to deal with this error */
  163. while (1)
  164. {
  165. /* Make LED2 blink (100ms on, 2s off) to indicate error in Write operation */
  166. /* Make LED2 blink (100ms on, 2s off) to indicate error in Erase operation */
  167. printf("HAL_FLASH_Program Error\r\n");
  168. }
  169. }
  170. }
  171. printf("HAL_FLASH_Program END\r\n");
  172. /* Lock the Flash to disable the flash control register access (recommended
  173. to protect the FLASH memory against possible unwanted operation) *********/
  174. HAL_FLASH_Lock();
  175. /* Check if the programmed data is OK
  176. MemoryProgramStatus = 0: data programmed correctly
  177. MemoryProgramStatus != 0: number of words not programmed correctly ******/
  178. Address = FLASH_USER_TEMPBANK_START_ADDR;
  179. MemoryProgramStatus = 0x0;
  180. while (Address < ((FLASH_USER_TEMPBANK_START_ADDR + ((uint32_t)0x000FFFF))))
  181. {
  182. data32 = *(__IO uint32_t *)Address;
  183. printf("%08x : %02X \n",Address ,*(uint8_t*)Address);
  184. if (data32 != DATA_32)
  185. {
  186. MemoryProgramStatus++;
  187. }
  188. Address = Address + 4;
  189. }
  190. /*Check if there is an issue to program data*/
  191. if (MemoryProgramStatus == 0)
  192. {
  193. printf("DATA OK \r\n");
  194. /* No error detected. Switch on LED2*/
  195. }
  196. else
  197. {
  198. /* Error detected. LED2 will blink with 1s period */
  199. while (1)
  200. {
  201. printf("Read Error\r\n");
  202. }
  203. }
  204. }