main.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "main.h"
  2. void soft_wdt(void *params);
  3. void init_task(void *params);
  4. bool jump_to_app(uint32_t address);
  5. void (*pftarget)(void);
  6. bool boot_failed = false;
  7. int main()
  8. {
  9. uint8_t load_mode;
  10. uint8_t boot_try;
  11. uint8_t fw_invalid = 0;
  12. nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
  13. crm_periph_clock_enable(CRM_PWC_PERIPH_CLOCK, TRUE);
  14. crm_periph_clock_enable(CRM_BPR_PERIPH_CLOCK, TRUE);
  15. pwc_battery_powered_domain_access(TRUE);
  16. system_clock_config();
  17. delay_init();
  18. uart_print_init(115200);
  19. // Флаг - нужно ли обновляться
  20. load_mode = bpr_data_read(BPR_DATA1);
  21. // Число попыток загрузки основного ПО
  22. boot_try = bpr_data_read(BPR_DATA2);
  23. DBG printf("[IAP] load_mode: %u, boot_try: %u\r\n", load_mode, boot_try);
  24. // Если есть попытки, то пытаемся загрузить FW.
  25. // Используем ограниченное число попыток загрузить основное FW.
  26. if (boot_try > 1)
  27. {
  28. boot_try--;
  29. bpr_data_write(BPR_DATA2, boot_try);
  30. if (!jump_to_app(USER_FLASH_FIRST_PAGE_ADDRESS))
  31. {
  32. // Флеш пустая. Нечего загружать
  33. fw_invalid = 1;
  34. boot_failed = false;
  35. DBG printf("FW empty! Bootloader starting...\n\r");
  36. }
  37. }
  38. else if (boot_try == 1)
  39. {
  40. boot_failed = true;
  41. DBG printf("FW boot failed. Bootloader starting...\r\n");
  42. load_mode = 1;
  43. boot_try = 0;
  44. bpr_data_write(BPR_DATA1, load_mode);
  45. bpr_data_write(BPR_DATA2, boot_try);
  46. }
  47. if ((load_mode == 0) && (!fw_invalid))
  48. {
  49. DBG printf("Run main FW\r\n");
  50. bpr_data_write(BPR_DATA2, BOOT_TRY);
  51. DBG printf("Go to main FW...\r\n");
  52. jump_to_app(USER_FLASH_FIRST_PAGE_ADDRESS);
  53. }
  54. DBG printf("IAP starting...\r\n");
  55. #if 0
  56. // -------------------------------------------------------------------------
  57. // Для теста. Сброс.
  58. DBG printf("For test reset load_mode flag and jump to FW\r\n");
  59. bpr_data_write(BPR_DATA1, 0);
  60. jump_to_app(0x08021000);
  61. #endif
  62. //
  63. cm_gpio_init();
  64. taskENTER_CRITICAL();
  65. xTaskCreate(soft_wdt, "soft_wdt", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL);
  66. xTaskCreate(init_task, "init_task", 10*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  67. taskEXIT_CRITICAL();
  68. vTaskStartScheduler();
  69. while (1) {}
  70. }
  71. //
  72. bool jump_to_app(uint32_t address)
  73. {
  74. uint32_t stkptr, jumpaddr;
  75. stkptr = *(uint32_t *)address;
  76. jumpaddr = *(uint32_t *)(address + sizeof(uint32_t));
  77. //if (((*(__IO uint32_t*)USER_FLASH_FIRST_PAGE_ADDRESS) & 0x20000000) == 0x20000000)
  78. if (*(__IO uint32_t*)USER_FLASH_FIRST_PAGE_ADDRESS != 0xFFFFFFFF)
  79. {
  80. __set_MSP(stkptr);
  81. pftarget = (void (*) (void))jumpaddr;
  82. pftarget();
  83. }
  84. return false;
  85. }
  86. //
  87. void soft_wdt(void *params)
  88. {
  89. (void)params;
  90. for (;;)
  91. {
  92. extern_wdt_togle(); // extern WDT
  93. vTaskDelay(500);
  94. }
  95. }
  96. //
  97. void init_task(void *params)
  98. {
  99. (void)params;
  100. crm_periph_clock_enable(CRM_CRC_PERIPH_CLOCK, TRUE);
  101. init_settings();
  102. settings_load(&settings);
  103. mb_init();
  104. for (;;)
  105. {
  106. vTaskDelay(500);
  107. }
  108. }