| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- /**
- ******************************************************************************
- * @file main.c
- * @author LI
- * @version V3.5.0
- * @date 08-April-2015
- * @brief Main program body
- ******************************************************************************
- * @attention
- *
- * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
- * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
- * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
- * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
- * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
- * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
- *
- * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "includes.h"
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- /* Private functions ---------------------------------------------------------*/
- uint8_t run_led_state = LED_OFF;
- uint32_t count = 0;
- uint16_t read_data_dr2 = 0, write_data_dr2 = 0;
- uint8_t updata_flag = 0;
- uint32_t read_crc1,read_crc2,read_crc3;
- uint32_t get_crc1,get_crc2,get_crc3;
- uint32_t datalen1,datalen2,datalen3;
- uint8_t app1_check_result,app2_check_result,app3_check_result;
- uint8_t app1_check_num=0,app2_check_num=0,app3_check_num=0;
- uint8_t jump_flag = 0;
- uint32_t update_addr=0,update_len=0;
- uint16_t read_data_dr=0;
- uint8_t copy_addr=0,update_type;
- int main(void)
- {
- // SysTick_init();
- RCC_Configuration();
- LED_Init();
- uart_init(UART1_ID, 115200, USART_WL_8BIT, USART_PM_NONE);
- I2C_init();
-
- IWDG_Config();
- printf("bootloader begin\n");
- eeprom_init_handle();
- read_data_dr2 = bkp_data_read(BKP_DATA_1);
- printf("BKP_DR2=%04x\n",read_data_dr2);
- if(flag_not_cmp(read_data_dr2))
- {
- read_data_dr=bkp_data_read(BKP_DATA_6);//升级的固件类型
- if(read_data_dr == 0x02)//升级产测
- {
- update_addr = OTA_UPDATE_PT_FLASH_START_ADDR;
- update_len = OTA_UPDATE_PT_FLASH_SIZE;
- copy_addr = UPDATE_ENUM_PT;
- update_type = 2;
- printf("升级产测\n");
- }
- else if(read_data_dr == 0x03)//升级app
- {
- update_addr = OTA_UPDATE_APP1_FLASH_START_ADDR;
- update_len = OTA_UPDATE_APP1_FLASH_SIZE;
- copy_addr = UPDATE_ENUM_APP1;
- update_type = 3;
- printf("升级app\n");
- }
-
- if((read_data_dr2&0x00ff) == UPDATA_FLAG)//升级
- {
- printf("updata begin\n");
- //校验app1
- app1_check_result = app_check(update_addr,update_len,&get_crc1);
- app3_check_result = app_check(OTA_UPDATE_APP3_FLASH_START_ADDR,OTA_UPDATE_APP3_FLASH_SIZE,&get_crc3);
- move1:
- erase_app(UPDATE_ENUM_APP2);
- fwdgt_counter_reload();
- // printf("erase app2 end\n");
- copy_app(copy_addr, UPDATE_ENUM_APP2);
- fwdgt_counter_reload();
- // printf("copy app1 end\n");
- //校验app2
- app2_check_result = app_check(OTA_UPDATE_APP2_FLASH_START_ADDR,OTA_UPDATE_APP2_FLASH_SIZE,&get_crc2);
- if(get_crc1 != get_crc2)//校验错误,拷贝过程出错
- {
- if(app2_check_num == 0)//如果是第一次,则重新拷贝一次
- {
- app2_check_num++;
- goto move1;
- }
- else
- {
- printf("copy app1 to app2 error\n");
- }
- }
-
- move2:
- erase_app(copy_addr);
- fwdgt_counter_reload();
- // printf("erase app1 end\n");
- copy_app(UPDATE_ENUM_APP3, copy_addr);
- fwdgt_counter_reload();
- // printf("copy app3 end\n");
- app1_check_result = app_check(update_addr,update_len,&get_crc1);
- if(get_crc1 != get_crc3)//校验错误,拷贝过程出错
- {
- if(app1_check_num == 0)//如果是第一次,则重新拷贝一次
- {
- app1_check_num++;
- goto move2;
- }
- else
- {
- printf("copy app3 to app1 error\n");
- jump_flag = 1;
- }
- }
-
- write_data_dr2 = set_flag_not(BACKOFF_FLAG);
- bkp_data_write(BKP_DATA_1,write_data_dr2);
-
- if(update_type == 2)//产测
- {
- device_mcu_info.app2_ver = device_mcu_info.pt_ver;
- device_mcu_info.pt_ver = device_mcu_info.app3_ver;
- updata_flag = 1;
- }
- else if(update_type == 3)//app
- {
- device_mcu_info.app2_ver = device_mcu_info.app1_ver;
- device_mcu_info.app1_ver = device_mcu_info.app3_ver;
- updata_flag = 1;
- }
-
- }
- else if((read_data_dr2&0x00ff) == BACKOFF_FLAG)//回退
- {
- printf("back off begin\n");
- app2_check_result = app_check(OTA_UPDATE_APP2_FLASH_START_ADDR,OTA_UPDATE_APP2_FLASH_SIZE,&get_crc2);
- move3:
- erase_app(copy_addr);
- fwdgt_counter_reload();
- copy_app(UPDATE_ENUM_APP2, copy_addr);
- fwdgt_counter_reload();
- app1_check_result = app_check(update_addr,update_len,&get_crc1);
- if(get_crc1 != get_crc2)//校验错误,拷贝过程出错
- {
- if(app1_check_num == 0)//如果是第一次,则重新拷贝一次
- {
- app1_check_num++;
- goto move3;
- }
- else
- {
- printf("copy app2 to app1 error\n");
- jump_flag = 1;
- }
- }
-
- write_data_dr2 = set_flag_not(BACKOFF_OVER);
- bkp_data_write(BKP_DATA_1,write_data_dr2);
-
- if(update_type == 2)//产测
- {
- device_mcu_info.pt_ver = device_mcu_info.app2_ver;
- updata_flag = 1;
- }
- else if(update_type == 3)//app
- {
- device_mcu_info.app1_ver = device_mcu_info.app2_ver;
- updata_flag = 1;
- }
- }
- else if((read_data_dr2&0x00ff) == BACKOFF_OVER)//回退失败
- {
- bkp_data_write(BKP_DATA_1,0x0000);
- }
- }
- fwdgt_counter_reload();
- eeprom_info_updata();
- //打印信息
- printf("bootloader信息:\n");
- printf(" first init = %04x\n",device_public_info.eeprom_first_flag);
- printf(" eeprom ver = %02x\n",device_public_info.eeprom_ver);
- printf(" reset num = %u\n",Reset_cnt);
- printf(" boot ver = %08x\n",device_mcu_info.bootloader_ver);
- printf(" app1 ver = %08x\n",device_mcu_info.app1_ver);
- printf(" app2 ver = %08x\n",device_mcu_info.app2_ver);
- printf(" app3 ver = %08x\n",device_mcu_info.app3_ver);
-
- fwdgt_counter_reload();
- run_app1();
- fwdgt_counter_reload();
-
- while(1)
- {
- if(run_led_state)
- LED_STATE_OFF();
- else
- LED_STATE_ON();
- run_led_state = !run_led_state;
-
- delay_ms(500);
- fwdgt_counter_reload(); //喂狗
-
- count++;
- if(count > 10)
- {
- count = 0;
- NVIC_SystemReset();
- }
- }
-
- }
|