flash.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * flash.c
  3. *
  4. * Created on: 2019. 7. 15.
  5. * Author: parkyj
  6. */
  7. #include "main.h"
  8. uint32_t GetSector(uint32_t);
  9. uint8_t flashinit = 0;
  10. uint32_t Address = FLASH_USER_START_ADDR;
  11. typedef void (*fptr)(void);
  12. fptr jump_to_app;
  13. uint32_t jump_addr;
  14. void Jump_App(void){
  15. __HAL_RCC_TIM6_CLK_DISABLE(); // 筌띲끉�뵥???�뜝�룞�삕�솒紐뚯삕?? ?�뜝�룞�삕�뜝占�??�뜝�룞�삕?�뜝�룞�삕?�뜝�룞�삕
  16. printf("boot loader start\n"); //筌롫뗄苑��뜝占�? �빊�뮆�젾
  17. jump_addr = *(__IO uint32_t*) (APPLICATION_ADDRESS + 4);
  18. jump_to_app = (fptr) jump_addr;
  19. /* init user app's sp */
  20. printf("jump!\n");
  21. __set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS);
  22. jump_to_app();
  23. }
  24. void FLASH_If_Init(void)
  25. {
  26. /* Unlock the Program memory */
  27. HAL_FLASH_Unlock();
  28. /* Clear all FLASH flags */
  29. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
  30. FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR);
  31. /* Unlock the Program memory */
  32. HAL_FLASH_Lock();
  33. }
  34. void Flash_InitRead(void) // ?�뜝�룞�삕疫꿸퀬釉�?�뜝�룞�삕
  35. {
  36. uint32_t Address = 0;
  37. Address = FLASH_USER_START_ADDR;
  38. for(uint32_t i = 0; i < 16; i++ ){
  39. printf("%08x : %02X \n",Address ,*(uint8_t*)Address);
  40. Address++;
  41. }
  42. #if 0 // PYJ.2019.03.27_BEGIN --
  43. for(uint32_t i = 0; i < 13848; i++ ){
  44. printf("%08x : %02X \n",Address ,*(uint8_t*)Address);
  45. Address++;
  46. }
  47. Address = StartAddr;
  48. for(uint32_t i = 0; i < 13848; i++ ){
  49. printf("%02X ",*(uint8_t*)Address);
  50. Address++;
  51. }
  52. #endif // PYJ.2019.03.27_END --
  53. }
  54. uint8_t Flash_RGB_Data_Write(uint8_t* data){
  55. uint16_t Firmdata = 0;
  56. uint8_t ret = 0;
  57. for(uint8_t i = 0; i < data[bluecell_length] - 2; i+=2){
  58. Firmdata = ((data[(bluecell_length + 1) + i]) & 0x00FF);
  59. Firmdata += ((data[(bluecell_length + 1) + (i + 1)] << 8) & 0xFF00);
  60. if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,Address , (uint16_t)Firmdata) != HAL_OK){
  61. printf("HAL NOT OK \n");
  62. ret = 1;
  63. }
  64. Address += 2;
  65. //if(!(i%FirmwareUpdateDelay))
  66. // HAL_Delay(1);
  67. }
  68. return ret;
  69. }
  70. HAL_StatusTypeDef EraseFlash()
  71. {
  72. uint32_t SectorError = 0;
  73. /* Unlock to control */
  74. HAL_FLASH_Unlock();
  75. /* Calculate sector index */
  76. uint32_t UserSector = GetSector(FLASH_USER_START_ADDR);
  77. uint32_t NbOfSectors = GetSector(FLASH_USER_END_ADDR) - UserSector + 1;
  78. /* Erase sectors */
  79. FLASH_EraseInitTypeDef EraseInitStruct;
  80. EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
  81. EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
  82. EraseInitStruct.Sector = UserSector;
  83. EraseInitStruct.NbSectors = NbOfSectors;
  84. if(HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK)
  85. {
  86. uint32_t errorcode = HAL_FLASH_GetError();
  87. return HAL_ERROR;
  88. }
  89. /* Clear cache for flash */
  90. __HAL_FLASH_DATA_CACHE_DISABLE();
  91. __HAL_FLASH_INSTRUCTION_CACHE_DISABLE();
  92. __HAL_FLASH_DATA_CACHE_RESET();
  93. __HAL_FLASH_INSTRUCTION_CACHE_RESET();
  94. __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
  95. __HAL_FLASH_DATA_CACHE_ENABLE();
  96. /* Lock flash control register */
  97. HAL_FLASH_Lock();
  98. return HAL_OK;
  99. }
  100. uint8_t Flash_write(uint8_t* data) // ?�뜝�룞�삕疫꿸퀬釉�?�뜝�룞�삕
  101. {
  102. uint32_t SectorError = 0;
  103. uint32_t UserSector = GetSector(FLASH_USER_START_ADDR);
  104. /*Variable used for Erase procedure*/
  105. static FLASH_EraseInitTypeDef EraseInitStruct;
  106. static uint32_t PAGEError = 0;
  107. uint8_t ret = 0;
  108. /* Fill EraseInit structure*/
  109. EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
  110. EraseInitStruct.Sector = FLASH_USER_START_ADDR;
  111. // EraseInitStruct.NbPages = (FLASH_USER_END_ADDR - FLASH_USER_START_ADDR) / FLASH_PAGE_SIZE;
  112. EraseInitStruct.NbSectors = GetSector(FLASH_USER_END_ADDR) - UserSector + 1;
  113. uint32_t NbOfSectors =
  114. __HAL_RCC_TIM6_CLK_DISABLE(); // 筌띲끉�뵥???�뜝�룞�삕�솒紐뚯삕?? ?�뜝�룞�삕�뜝占�??�뜝�룞�삕?�뜝�룞�삕?�뜝�룞�삕
  115. HAL_FLASH_Unlock(); // lock ??�뜝占�?
  116. if(flashinit == 0){
  117. flashinit= 1;
  118. //FLASH_PageErase(StartAddr);
  119. // if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK){
  120. // printf("Erase Failed \r\n");
  121. // }
  122. EraseFlash();
  123. }
  124. // FLASH_If_Erase();
  125. ret = Flash_RGB_Data_Write(&data[bluecell_stx]);
  126. HAL_FLASH_Lock(); // lock ?�뜝�룞�삕域밸㈇由�
  127. __HAL_RCC_TIM6_CLK_ENABLE(); // 筌띲끉�뵥???�뜝�룞�삕�솒紐뚯삕?? ?�뜝�룞�삕?�뜝�룞�삕?�뜝�룞�삕?�뜝�룞�삕?�뜝�룞�삕?�뜝�룞�삕
  128. return ret;
  129. }
  130. uint32_t GetSector(uint32_t Address)
  131. {
  132. uint32_t sector = 0;
  133. if((Address < FLASH_SECTOR_1) && (Address >= FLASH_SECTOR_0))
  134. {
  135. sector = FLASH_SECTOR_0;
  136. }
  137. else if((Address < FLASH_SECTOR_2) && (Address >= FLASH_SECTOR_1))
  138. {
  139. sector = FLASH_SECTOR_1;
  140. }
  141. else if((Address < FLASH_SECTOR_3) && (Address >= FLASH_SECTOR_2))
  142. {
  143. sector = FLASH_SECTOR_2;
  144. }
  145. else if((Address < FLASH_SECTOR_4) && (Address >= FLASH_SECTOR_3))
  146. {
  147. sector = FLASH_SECTOR_3;
  148. }
  149. else if((Address < FLASH_SECTOR_5) && (Address >= FLASH_SECTOR_4))
  150. {
  151. sector = FLASH_SECTOR_4;
  152. }
  153. else if((Address < FLASH_SECTOR_6) && (Address >= FLASH_SECTOR_5))
  154. {
  155. sector = FLASH_SECTOR_5;
  156. }
  157. else if((Address < FLASH_SECTOR_7) && (Address >= FLASH_SECTOR_6))
  158. {
  159. sector = FLASH_SECTOR_6;
  160. }
  161. else if((Address < FLASH_SECTOR_8) && (Address >= FLASH_SECTOR_7))
  162. {
  163. sector = FLASH_SECTOR_7;
  164. }
  165. else if((Address < FLASH_SECTOR_9) && (Address >= FLASH_SECTOR_8))
  166. {
  167. sector = FLASH_SECTOR_8;
  168. }
  169. else if((Address < FLASH_SECTOR_10) && (Address >= FLASH_SECTOR_9))
  170. {
  171. sector = FLASH_SECTOR_9;
  172. }
  173. else if((Address < FLASH_SECTOR_11) && (Address >= FLASH_SECTOR_10))
  174. {
  175. sector = FLASH_SECTOR_10;
  176. }
  177. else /* (Address < FLASH_END_ADDR) && (Address >= FLASH_SECTOR_11) */
  178. {
  179. sector = FLASH_SECTOR_11;
  180. }
  181. return sector;
  182. }
  183. uint32_t GetSectorSize(uint32_t Sector)
  184. {
  185. uint32_t sectorsize = 0x00;
  186. if((Sector == FLASH_SECTOR_0) || (Sector == FLASH_SECTOR_1) || (Sector == FLASH_SECTOR_2) || (Sector == FLASH_SECTOR_3))
  187. {
  188. sectorsize = 16 * 1024;
  189. }
  190. else if(Sector == FLASH_SECTOR_4)
  191. {
  192. sectorsize = 64 * 1024;
  193. }
  194. else
  195. {
  196. sectorsize = 128 * 1024;
  197. }
  198. return sectorsize;
  199. }