flash.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * flash.c
  3. *
  4. * Created on: 2019. 7. 15.
  5. * Author: parkyj
  6. */
  7. #include <stdio.h>
  8. #include "flash.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(); // Flash Timer Disable
  17. printf("boot loader start\n"); //BootLoader Jump Function Test Print
  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. bool EraseInit = false;
  26. void DataErase_Func(uint32_t User_Address,uint32_t size){
  27. static FLASH_EraseInitTypeDef EraseInitStruct;
  28. static uint32_t PAGEError = 0;
  29. HAL_FLASH_Unlock();
  30. EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
  31. EraseInitStruct.PageAddress = FLASH_USER_USE_START_ADDR;
  32. EraseInitStruct.NbPages = ((FLASH_USER_END_ADDR - FLASH_USER_USE_START_ADDR) / FLASH_PAGE_SIZE) + 1;
  33. UserAddress = User_Address;
  34. printf("NbPages : %x \r\n",EraseInitStruct.NbPages );
  35. printf("EraseInitStruct.PageAddress : %x \r\n",EraseInitStruct.PageAddress);
  36. printf("Erase Start\r\n");
  37. if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK)
  38. {
  39. /*
  40. Error occurred while page erase.
  41. User can add here some code to deal with this error.
  42. PAGEError will contain the faulty page and then to know the code error on this page,
  43. user can call function 'HAL_FLASH_GetError()'
  44. */
  45. /* Infinite loop */
  46. while (1)
  47. {
  48. /* Make LED2 blink (100ms on, 2s off) to indicate error in Erase operation */
  49. printf("HAL_FLASHEx_Erase Error\r\n");
  50. HAL_Delay(2000);
  51. }
  52. }
  53. EraseInit = true;
  54. printf("Erase End\r\n");
  55. HAL_FLASH_Lock();
  56. }
  57. uint8_t FLASH_Write_Func(uint32_t User_Address,uint8_t* data,uint32_t size){
  58. //static FLASH_EraseInitTypeDef EraseInitStruct;
  59. //static uint32_t PAGEError = 0;
  60. static uint32_t DownloadIndex;
  61. static __IO uint32_t data32 = 0 , MemoryProgramStatus = 0;
  62. int dataindex = 0;
  63. uint32_t writedata = 0;
  64. uint32_t CurrApiAddress = 0;
  65. uint8_t ret = 0;
  66. CurrApiAddress = User_Address;
  67. uint8_t* Currdata = (uint8_t*)CurrApiAddress;
  68. printf("HAL_FLASH_Program Start\r\n");
  69. DownloadIndex += size;
  70. printf("User_Address : %x \r\n",UserAddress);
  71. HAL_FLASH_Unlock();
  72. for(int downindex = 0; downindex < size; downindex+=4)
  73. {
  74. writedata = data[downindex + 0] ;
  75. writedata += data[downindex + 1] << 8 ;
  76. writedata += data[downindex + 2] << 16;
  77. writedata += data[downindex + 3] << 24;
  78. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, UserAddress,writedata) == HAL_OK)
  79. {
  80. UserAddress += 4;
  81. }
  82. else
  83. {
  84. printf("HAL_FLASH_Program Error\r\n");
  85. printf("Flash Failed %x \r\n",UserAddress);
  86. }
  87. }
  88. printf("HAL_FLASH_Program END %x \r\n",UserAddress);
  89. /* Lock the Flash to disable the flash control register access (recommended
  90. to protect the FLASH memory against possible unwanted operation) *********/
  91. HAL_FLASH_Lock();
  92. return 0;
  93. /* Check if the programmed data is OK
  94. MemoryProgramStatus = 0: data programmed correctly
  95. MemoryProgramStatus != 0: number of words not programmed correctly ******/
  96. }
  97. void FLASH_Read_Func(uint32_t User_Address,uint8_t* dst,uint32_t size){
  98. uint32_t CurrApiAddress = 0;
  99. uint32_t i = 0;
  100. //uint8_t ret = 0;
  101. CurrApiAddress = User_Address;
  102. uint8_t* Currdata = (uint8_t*)CurrApiAddress;
  103. printf("Flash Read size : %d \r\n",size);
  104. for(int i = 0; i < size; i++){
  105. dst[i] = Currdata[i];
  106. printf("%02x ",dst[i]);
  107. }
  108. }
  109. void Flash_InitRead() // ?占쏙옙湲고븿?占쏙옙
  110. {
  111. uint32_t CurrApiAddress = 0;
  112. uint32_t i = 0;
  113. //uint8_t ret = 0;
  114. CurrApiAddress = FLASH_USER_USE_START_ADDR;
  115. uint8_t* Currdata = (uint8_t*)CurrApiAddress;
  116. printf("Flash Init \r\n");
  117. uint8_t* pdata;
  118. for(int i = 0; i < 200; i++){
  119. printf("%02x ",Currdata[i]);
  120. }
  121. }