main.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /**
  2. ******************************************************************************
  3. * @file main.c
  4. * @author LI
  5. * @version V3.5.0
  6. * @date 08-April-2015
  7. * @brief Main program body
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  14. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17. *
  18. * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  19. ******************************************************************************
  20. */
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "includes.h"
  23. /* Private typedef -----------------------------------------------------------*/
  24. /* Private define ------------------------------------------------------------*/
  25. /* Private macro -------------------------------------------------------------*/
  26. /* Private variables ---------------------------------------------------------*/
  27. /* Private function prototypes -----------------------------------------------*/
  28. /* Private functions ---------------------------------------------------------*/
  29. uint8_t run_led_state = LED_OFF;
  30. uint32_t count = 0;
  31. uint16_t read_data_dr2 = 0, write_data_dr2 = 0;
  32. uint8_t updata_flag = 0;
  33. uint32_t read_crc1,read_crc2,read_crc3;
  34. uint32_t get_crc1,get_crc2,get_crc3;
  35. uint32_t datalen1,datalen2,datalen3;
  36. uint8_t app1_check_result,app2_check_result,app3_check_result;
  37. uint8_t app1_check_num=0,app2_check_num=0,app3_check_num=0;
  38. uint8_t jump_flag = 0;
  39. uint32_t update_addr=0,update_len=0;
  40. uint16_t read_data_dr=0;
  41. uint8_t copy_addr=0,update_type;
  42. int main(void)
  43. {
  44. // SysTick_init();
  45. RCC_Configuration();
  46. LED_Init();
  47. uart_init(UART1_ID, 115200, USART_WL_8BIT, USART_PM_NONE);
  48. I2C_init();
  49. IWDG_Config();
  50. printf("bootloader begin\n");
  51. eeprom_init_handle();
  52. read_data_dr2 = bkp_data_read(BKP_DATA_1);
  53. printf("BKP_DR2=%04x\n",read_data_dr2);
  54. if(flag_not_cmp(read_data_dr2))
  55. {
  56. read_data_dr=bkp_data_read(BKP_DATA_6);//升级的固件类型
  57. if(read_data_dr == 0x02)//升级产测
  58. {
  59. update_addr = OTA_UPDATE_PT_FLASH_START_ADDR;
  60. update_len = OTA_UPDATE_PT_FLASH_SIZE;
  61. copy_addr = UPDATE_ENUM_PT;
  62. update_type = 2;
  63. printf("升级产测\n");
  64. }
  65. else if(read_data_dr == 0x03)//升级app
  66. {
  67. update_addr = OTA_UPDATE_APP1_FLASH_START_ADDR;
  68. update_len = OTA_UPDATE_APP1_FLASH_SIZE;
  69. copy_addr = UPDATE_ENUM_APP1;
  70. update_type = 3;
  71. printf("升级app\n");
  72. }
  73. if((read_data_dr2&0x00ff) == UPDATA_FLAG)//升级
  74. {
  75. printf("updata begin\n");
  76. //校验app1
  77. app1_check_result = app_check(update_addr,update_len,&get_crc1);
  78. app3_check_result = app_check(OTA_UPDATE_APP3_FLASH_START_ADDR,OTA_UPDATE_APP3_FLASH_SIZE,&get_crc3);
  79. move1:
  80. erase_app(UPDATE_ENUM_APP2);
  81. fwdgt_counter_reload();
  82. // printf("erase app2 end\n");
  83. copy_app(copy_addr, UPDATE_ENUM_APP2);
  84. fwdgt_counter_reload();
  85. // printf("copy app1 end\n");
  86. //校验app2
  87. app2_check_result = app_check(OTA_UPDATE_APP2_FLASH_START_ADDR,OTA_UPDATE_APP2_FLASH_SIZE,&get_crc2);
  88. if(get_crc1 != get_crc2)//校验错误,拷贝过程出错
  89. {
  90. if(app2_check_num == 0)//如果是第一次,则重新拷贝一次
  91. {
  92. app2_check_num++;
  93. goto move1;
  94. }
  95. else
  96. {
  97. printf("copy app1 to app2 error\n");
  98. }
  99. }
  100. move2:
  101. erase_app(copy_addr);
  102. fwdgt_counter_reload();
  103. // printf("erase app1 end\n");
  104. copy_app(UPDATE_ENUM_APP3, copy_addr);
  105. fwdgt_counter_reload();
  106. // printf("copy app3 end\n");
  107. app1_check_result = app_check(update_addr,update_len,&get_crc1);
  108. if(get_crc1 != get_crc3)//校验错误,拷贝过程出错
  109. {
  110. if(app1_check_num == 0)//如果是第一次,则重新拷贝一次
  111. {
  112. app1_check_num++;
  113. goto move2;
  114. }
  115. else
  116. {
  117. printf("copy app3 to app1 error\n");
  118. jump_flag = 1;
  119. }
  120. }
  121. write_data_dr2 = set_flag_not(BACKOFF_FLAG);
  122. bkp_data_write(BKP_DATA_1,write_data_dr2);
  123. if(update_type == 2)//产测
  124. {
  125. device_mcu_info.app2_ver = device_mcu_info.pt_ver;
  126. device_mcu_info.pt_ver = device_mcu_info.app3_ver;
  127. updata_flag = 1;
  128. }
  129. else if(update_type == 3)//app
  130. {
  131. device_mcu_info.app2_ver = device_mcu_info.app1_ver;
  132. device_mcu_info.app1_ver = device_mcu_info.app3_ver;
  133. updata_flag = 1;
  134. }
  135. }
  136. else if((read_data_dr2&0x00ff) == BACKOFF_FLAG)//回退
  137. {
  138. printf("back off begin\n");
  139. app2_check_result = app_check(OTA_UPDATE_APP2_FLASH_START_ADDR,OTA_UPDATE_APP2_FLASH_SIZE,&get_crc2);
  140. move3:
  141. erase_app(copy_addr);
  142. fwdgt_counter_reload();
  143. copy_app(UPDATE_ENUM_APP2, copy_addr);
  144. fwdgt_counter_reload();
  145. app1_check_result = app_check(update_addr,update_len,&get_crc1);
  146. if(get_crc1 != get_crc2)//校验错误,拷贝过程出错
  147. {
  148. if(app1_check_num == 0)//如果是第一次,则重新拷贝一次
  149. {
  150. app1_check_num++;
  151. goto move3;
  152. }
  153. else
  154. {
  155. printf("copy app2 to app1 error\n");
  156. jump_flag = 1;
  157. }
  158. }
  159. write_data_dr2 = set_flag_not(BACKOFF_OVER);
  160. bkp_data_write(BKP_DATA_1,write_data_dr2);
  161. if(update_type == 2)//产测
  162. {
  163. device_mcu_info.pt_ver = device_mcu_info.app2_ver;
  164. updata_flag = 1;
  165. }
  166. else if(update_type == 3)//app
  167. {
  168. device_mcu_info.app1_ver = device_mcu_info.app2_ver;
  169. updata_flag = 1;
  170. }
  171. }
  172. else if((read_data_dr2&0x00ff) == BACKOFF_OVER)//回退失败
  173. {
  174. bkp_data_write(BKP_DATA_1,0x0000);
  175. }
  176. }
  177. fwdgt_counter_reload();
  178. eeprom_info_updata();
  179. //打印信息
  180. printf("bootloader信息:\n");
  181. printf(" first init = %04x\n",device_public_info.eeprom_first_flag);
  182. printf(" eeprom ver = %02x\n",device_public_info.eeprom_ver);
  183. printf(" reset num = %u\n",Reset_cnt);
  184. printf(" boot ver = %08x\n",device_mcu_info.bootloader_ver);
  185. printf(" app1 ver = %08x\n",device_mcu_info.app1_ver);
  186. printf(" app2 ver = %08x\n",device_mcu_info.app2_ver);
  187. printf(" app3 ver = %08x\n",device_mcu_info.app3_ver);
  188. fwdgt_counter_reload();
  189. run_app1();
  190. fwdgt_counter_reload();
  191. while(1)
  192. {
  193. if(run_led_state)
  194. LED_STATE_OFF();
  195. else
  196. LED_STATE_ON();
  197. run_led_state = !run_led_state;
  198. delay_ms(500);
  199. fwdgt_counter_reload(); //喂狗
  200. count++;
  201. if(count > 10)
  202. {
  203. count = 0;
  204. NVIC_SystemReset();
  205. }
  206. }
  207. }