首页 > 其他分享 >bootloader

bootloader

时间:2022-11-12 14:44:35浏览次数:34  
标签:u8 system len crc u32 bootloader config

  1 /* Includes ------------------------------------------------------------------*/
  2 #include "main.h"
  3 #include "declare.h"
  4 #include "user_flash.h"
  5 #include "uart.h"
  6 
  7 u16 g_MsCnt;
  8 u32 g_Ms;
  9 u32 tick_us = 64;     // 1 us tick count,根据SysTick时钟源设定
 10 u8 databuf[FMC_PAGE_SIZE]={0};
 11 
 12 
 13 /*SysTick 是24位递减定时器,当递减至0时产生中断,同时加载Reload值重新递减*/
 14 void Sys_HardDelayUs(u32 us)//us< 1000
 15 {
 16     u32 old_tick;
 17     u32 delta_tick;
 18     if(us>900)return;
 19     old_tick = SysTick->VAL;
 20     while(1)
 21     {
 22         if(SysTick->VAL <= old_tick)
 23         {
 24             delta_tick = old_tick - SysTick->VAL;
 25         }
 26         else
 27         {
 28             delta_tick = SysTick->LOAD - (SysTick->VAL-old_tick);
 29         }
 30 
 31         if(delta_tick >=(tick_us*us))break;
 32     }
 33 }
 34 
 35 
 36 /* USER CODE END PFP */
 37 
 38 /* Private user code ---------------------------------------------------------*/
 39 /* USER CODE BEGIN 0 */
 40 
 41 u8 crc_caculate(u8 *pdata,u16 len)
 42 {
 43     u8 crc = 0;
 44     while(len)
 45     {
 46         crc += (*pdata);
 47         pdata++;
 48         len--;
 49     }
 50     return crc;
 51 }
 52 
 53 void print_hex(const char*p,u8 *pdata,u16 len)
 54 {
 55 #if 1
 56 
 57     SYSTEM_DEBUG("%s len=%d:",p,len);
 58     while(len)
 59     {
 60         printf("%02x ",*pdata);
 61         pdata++;
 62         len--;
 63     }
 64     printf("\r\n");
 65 
 66 #endif
 67 }
 68 
 69 CONFIG_t system_config;
 70 
 71 void App_Config_Save(void)
 72 {
 73     system_config.crc = crc_caculate((u8*)&system_config,sizeof(system_config)-1);
 74     User_Flash_Write(USER_FLASH_CONFIG_ADDR,(u8*)&system_config,sizeof(system_config));
 75     //print_hex(__FUNCTION__,(u8*)&system_config,sizeof(system_config));
 76 }
 77 
 78 void App_Config_Read(void)
 79 {
 80     u8 crc=0;
 81     User_Flash_Read(USER_FLASH_CONFIG_ADDR,(u8*)&system_config,sizeof(system_config));
 82     crc = crc_caculate((u8*)&system_config,sizeof(system_config)-1);
 83     if(crc != system_config.crc)
 84     {
 85         SYSTEM_DEBUG("Error! system_config.crc=%x , crc=%x %d\r\n",system_config.crc,crc,sizeof(system_config));
 86         system_config.crc = crc_caculate((u8*)&system_config,sizeof(system_config)-1);
 87         User_Flash_Write(USER_FLASH_CONFIG_ADDR,(u8*)&system_config,sizeof(system_config));
 88     }
 89     //print_hex(__FUNCTION__,(u8*)&system_config,sizeof(system_config));
 90 }
 91 typedef  void (*pFunction)(void);
 92 u32 JumpAddress;
 93 pFunction JumpToApplication;
 94 
 95 void JumpToApp(u32 address)
 96 {
 97     u32 result=0;
 98     u32 address_value=0;
 99     address_value = (*(__IO u32*)(address));
100     result = (((*(__IO u32*)address) & 0x2FFE0000 ));
101     //SYSTEM_DEBUG("*address = %#X,result = %#X\n",address_value,result);
102     //return;
103     if(result==0x20000000)
104     {
105         SYSTEM_DEBUG("start jump to app(0x%0x)....\r\n",address);
106         /* Jump to user application */
107         //debug("jump to application\r\n");
108         JumpAddress = *(__IO u32*) (address + 4);
109 
110         /* Initialize user application's Stack Pointer */
111         __set_MSP(*(__IO u32*) address);
112         JumpToApplication = (pFunction) JumpAddress;
113         JumpToApplication();
114     }
115 }
116 
117 #define CONFIG_CRC32_POLY           0x04C11DB7
118 #define CONFIG_CRC32_INIT_VALUE     0xFFFFFFFF
119 #define CONFIG_CRC32_OUT_XOR        0x00000000
120 #define CONFIG_CRC32_REFLECTION_IN  0
121 #define CONFIG_CRC32_REFLECTION_OUT 0
122 
123 u32 crc32_customized(u32 crc_poly,u32 crc_init_value,u32 out_xor_value,u8 *source,u32 length)
124 {
125     u32 crc_value = crc_init_value;
126 
127     for (int i =0; i < length; i++)
128     {
129         for (int j = 0; j < 8; j++)
130         {
131             u8 get_bit_value = ((source[i] >> (7 - j) & 1) == 1);
132             u8 get_value = ((crc_value >> 31 & 1) == 1);
133 
134             crc_value <<= 1;
135 
136             if (get_value ^ get_bit_value)
137             {
138                 crc_value ^= crc_poly;
139             }
140         }
141     }
142 
143     crc_value &= 0xFFFFFFFF;
144 
145     return (crc_value ^= out_xor_value);
146 }
147 
148 u32 soft_crc32(u8* buf,u32 len)
149 {
150     return crc32_customized(CONFIG_CRC32_POLY,CONFIG_CRC32_INIT_VALUE,CONFIG_CRC32_OUT_XOR,buf,len);
151 }
152 void copyflash(void)
153 {
154     u32 crc32=0;
155     u32 len=0;
156     u8 page_num=0;
157     u8 j=0;
158     App_Config_Read();
159     if(system_config.ota.have_new!=1)
160     {
161         SYSTEM_DEBUG("no new firmware!\r\n ");
162         if(system_config.ota.crc_result != CRC_NoNeed)
163         {
164             system_config.ota.crc_result = CRC_NoNeed;
165             App_Config_Save();
166         }
167         return;
168     }
169 
170     crc32 = soft_crc32((u8*)APP_BACK_ADDR_START,system_config.ota.newFirmware_size);
171 
172     SYSTEM_DEBUG("crc  failed newFirmware_crc=%#X ,crc32=%#X ,size=%d\r\n ",system_config.ota.newFirmware_crc,crc32,system_config.ota.newFirmware_size);
173     system_config.ota.have_new = 0;
174     if(system_config.ota.newFirmware_crc == crc32)
175     {
176         len = system_config.ota.newFirmware_size;
177         while(len>0)
178         {
179             if(len>=FMC_PAGE_SIZE)
180             {
181                 User_Flash_Read(APP_BACK_ADDR_START + FMC_PAGE_SIZE*page_num , databuf , FMC_PAGE_SIZE);
182                 User_Flash_Write(APP_ENTRY_ADDRESS + FMC_PAGE_SIZE*page_num , databuf , FMC_PAGE_SIZE);
183                 page_num++;
184                 len -= FMC_PAGE_SIZE;
185             }
186             else
187             {
188                 User_Flash_Read(APP_BACK_ADDR_START + FMC_PAGE_SIZE*page_num , databuf , len);
189                 j=len%8;
190                 if(j==0)j=8;
191                 memset(databuf+len,0xFF,8-j);
192                 User_Flash_Write(APP_ENTRY_ADDRESS + FMC_PAGE_SIZE*page_num , databuf , len+8-j);
193                 len= 0;
194             }
195         }
196         SYSTEM_DEBUG("update new firmware  scucess \r\n ");
197         system_config.ota.crc_result = CRC_SCUCESS;
198     }
199     else
200     {
201         SYSTEM_DEBUG("crc  failed newFirmware_crc=%#X ,crc32=%#X ,size=%d\r\n ",system_config.ota.newFirmware_crc,crc32,system_config.ota.newFirmware_size);
202         system_config.ota.crc_result = CRC_FAIL;
203     }
204     App_Config_Save();
205     return ;
206 
207 }
208 
209 int main(void)
210 {
211     u8 i=0;
212     u32 WRPR_Value;
213     #ifdef SYSTEM_DEBUG_EN
214     USART1_Init();
215     #endif
216     //RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_CRC, ENABLE);
217     //WRPR_Value = FLASH_GetWriteProtectionOB();
218     SYSTEM_DEBUG("$$ bootloader for <GD32 Chip> Sxx $$\r\n");
219     //SYSTEM_DEBUG("FLASH->WRP=%08X Read_L1=%d,Read_L2=%d,OBT->USER_RDP=%#x\n",WRPR_Value,FLASH_GetReadOutProtectionSTS(),FLASH_GetReadOutProtectionL2STS(),OBT->USER_RDP);
220     //User_Write_Protect(0);
221     copyflash();
222     JumpToApp(APP_ENTRY_ADDRESS);
223 
224     /**************if run to here,it is abnormal!!****************/
225     //SYSTEM_DEBUG("Bootloader jump failure!  \r\n");
226 
227     while(1)
228     {
229     }
230 
231 }
232 
233 
234 
235 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

 

标签:u8,system,len,crc,u32,bootloader,config
From: https://www.cnblogs.com/ycpkbql/p/16883734.html

相关文章