main.c 3.8 KB

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