main.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include "board.h"
  2. #include "bootloader.h"
  3. #include "core_cmFunc.h"
  4. #include "dev_iap.h"
  5. #include "global.h"
  6. #include "hal_flash.h"
  7. #include "iap.h"
  8. #include "queue.h"
  9. #include "stm32f4xx_gpio.h"
  10. #include "system_stm32f4xx.h"
  11. #include <stdint.h>
  12. #include <stm32f4xx.h>
  13. extern SqQueue CanQueueImportant; // CAN��Ҫ���ݶ���
  14. uint16_t cache_buf[1024] = {0};
  15. int main(void)
  16. {
  17. rt_hw_board_init();
  18. uint32_t w_size = 0;
  19. u16 cnt = 0;
  20. CanData_TypeDef element;
  21. rt_pin_mode(RED_LED_PIN, PIN_MODE_OUTPUT);
  22. rt_pin_mode(GREEN_LED_PIN, PIN_MODE_OUTPUT);
  23. InitQueue(&CanQueueImportant);
  24. can_init();
  25. iap_param_init();
  26. fmc_read_n_half_word(UPGRADE_FLAG_START_ADDR, upgrade.infor_buf, 4);
  27. if (upgrade.param.sign == UPGRADE_SIGN)
  28. {
  29. if (check_addr_sp(DOWNLOAD_START_ADDR))
  30. {
  31. fmc_clear_flag_star();
  32. erase_app_flash(APP_START_ADDR, APP_END_ADDR);
  33. while (w_size < upgrade.param.moving_size)
  34. {
  35. if ((upgrade.param.moving_size - w_size) > FMC_PAGE_SIZE)
  36. {
  37. fmc_read_n_half_word(DOWNLOAD_START_ADDR + w_size, cache_buf, FMC_PAGE_SIZE_U16);
  38. fmc_write_n_half_word(APP_START_ADDR + w_size, cache_buf, FMC_PAGE_SIZE_U16);
  39. w_size += FMC_PAGE_SIZE;
  40. }
  41. else
  42. {
  43. if (((upgrade.param.moving_size - w_size) % 2) != 0)
  44. w_size--;
  45. fmc_read_n_half_word(DOWNLOAD_START_ADDR + w_size, cache_buf, (upgrade.param.moving_size - w_size) / 2);
  46. fmc_write_n_half_word(APP_START_ADDR + w_size, cache_buf, (upgrade.param.moving_size - w_size) / 2);
  47. w_size = upgrade.param.moving_size;
  48. }
  49. }
  50. fmc_clear_flag_end();
  51. if (check_addr_sp(APP_START_ADDR) && check_addr_pc(APP_START_ADDR + 4))
  52. {
  53. CAN_DeInit(CAN1);
  54. __set_PRIMASK(1);
  55. boot_goto_app();
  56. }
  57. else
  58. {
  59. iap_flag_release();
  60. }
  61. }
  62. else
  63. {
  64. iap_flag_release();
  65. CAN_DeInit(CAN1);
  66. __set_PRIMASK(1);
  67. boot_goto_app();
  68. }
  69. }
  70. if (INVALID == check_addr_sp(APP_START_ADDR))
  71. {
  72. if (check_addr_sp(DOWNLOAD_START_ADDR))
  73. {
  74. fmc_clear_flag_star();
  75. erase_app_flash(APP_START_ADDR, APP_END_ADDR);
  76. while (w_size < upgrade.param.moving_size)
  77. {
  78. if ((upgrade.param.moving_size - w_size) > FMC_PAGE_SIZE)
  79. {
  80. fmc_read_n_half_word(DOWNLOAD_START_ADDR + w_size, cache_buf, FMC_PAGE_SIZE_U16);
  81. fmc_write_n_half_word(APP_START_ADDR + w_size, cache_buf, FMC_PAGE_SIZE_U16);
  82. w_size += FMC_PAGE_SIZE;
  83. }
  84. else
  85. {
  86. if (((upgrade.param.moving_size - w_size) % 2) != 0)
  87. w_size--;
  88. fmc_read_n_half_word(DOWNLOAD_START_ADDR + w_size, cache_buf, (upgrade.param.moving_size - w_size) / 2);
  89. fmc_write_n_half_word(APP_START_ADDR + w_size, cache_buf, (upgrade.param.moving_size - w_size) / 2);
  90. w_size = upgrade.param.moving_size;
  91. }
  92. }
  93. fmc_clear_flag_end();
  94. if (check_addr_sp(APP_START_ADDR) && check_addr_pc(APP_START_ADDR))
  95. {
  96. CAN_DeInit(CAN1);
  97. __set_PRIMASK(1);
  98. boot_goto_app();
  99. }
  100. }
  101. }
  102. else
  103. {
  104. CAN_DeInit(CAN1);
  105. __set_PRIMASK(1);
  106. boot_goto_app();
  107. }
  108. while (1)
  109. {
  110. // if (GetHead(&CanQueueImportant, &element) != ERROR)
  111. // {
  112. // cnt = 0;
  113. // // delay_ms(1000);
  114. // GPIO_ToggleBits(GPIOF, GPIO_Pin_9);
  115. // iap_process(&element);
  116. // }
  117. // else if (flash_up_is_ok() == TRUE)
  118. // {
  119. // iap_from_flash();
  120. // boot_goto_app();
  121. // }
  122. // else
  123. // {
  124. // cnt++;
  125. // delay_ms(10);
  126. // if (cnt > 100)
  127. // {
  128. // boot_goto_app();
  129. // }
  130. // }
  131. }
  132. }