main.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "board.h"
  2. #include "core_cmFunc.h"
  3. #include "dev_boot.h"
  4. #include "dev_iap.h"
  5. #include "drv_flash.h"
  6. #include "schedule.h"
  7. #include <stdint.h>
  8. #include <stm32f4xx.h>
  9. uint16_t cache_buf[1024] = {0};
  10. int main(void)
  11. {
  12. drv_board_init();
  13. dev_can_network_init();
  14. uint32_t w_size = 0;
  15. u16 cnt = 0;
  16. iap_param_init();
  17. fmc_read_n_half_word(UPGRADE_FLAG_START_ADDR, upgrade.infor_buf, 4);
  18. if (upgrade.param.sign == UPGRADE_SIGN)
  19. {
  20. if (check_addr_sp(DOWNLOAD_START_ADDR))
  21. {
  22. fmc_clear_flag_star();
  23. erase_app_flash(APP_START_ADDR, APP_END_ADDR);
  24. while (w_size < upgrade.param.moving_size)
  25. {
  26. if ((upgrade.param.moving_size - w_size) > FMC_PAGE_SIZE)
  27. {
  28. fmc_read_n_half_word(DOWNLOAD_START_ADDR + w_size, cache_buf, FMC_PAGE_SIZE_U16);
  29. fmc_write_n_half_word(APP_START_ADDR + w_size, cache_buf, FMC_PAGE_SIZE_U16);
  30. w_size += FMC_PAGE_SIZE;
  31. }
  32. else
  33. {
  34. if (((upgrade.param.moving_size - w_size) % 2) != 0)
  35. w_size--;
  36. fmc_read_n_half_word(DOWNLOAD_START_ADDR + w_size, cache_buf, (upgrade.param.moving_size - w_size) / 2);
  37. fmc_write_n_half_word(APP_START_ADDR + w_size, cache_buf, (upgrade.param.moving_size - w_size) / 2);
  38. w_size = upgrade.param.moving_size;
  39. }
  40. }
  41. fmc_clear_flag_end();
  42. if (check_addr_sp(APP_START_ADDR) && check_addr_pc(APP_START_ADDR + 4))
  43. {
  44. CAN_DeInit(CAN1);
  45. __set_PRIMASK(1);
  46. run_app();
  47. }
  48. else
  49. {
  50. iap_flag_release();
  51. }
  52. }
  53. else
  54. {
  55. iap_flag_release();
  56. CAN_DeInit(CAN1);
  57. __set_PRIMASK(1);
  58. run_app();
  59. }
  60. }
  61. if (INVALID == check_addr_sp(APP_START_ADDR))
  62. {
  63. if (check_addr_sp(DOWNLOAD_START_ADDR))
  64. {
  65. fmc_clear_flag_star();
  66. erase_app_flash(APP_START_ADDR, APP_END_ADDR);
  67. while (w_size < upgrade.param.moving_size)
  68. {
  69. if ((upgrade.param.moving_size - w_size) > FMC_PAGE_SIZE)
  70. {
  71. fmc_read_n_half_word(DOWNLOAD_START_ADDR + w_size, cache_buf, FMC_PAGE_SIZE_U16);
  72. fmc_write_n_half_word(APP_START_ADDR + w_size, cache_buf, FMC_PAGE_SIZE_U16);
  73. w_size += FMC_PAGE_SIZE;
  74. }
  75. else
  76. {
  77. if (((upgrade.param.moving_size - w_size) % 2) != 0)
  78. w_size--;
  79. fmc_read_n_half_word(DOWNLOAD_START_ADDR + w_size, cache_buf, (upgrade.param.moving_size - w_size) / 2);
  80. fmc_write_n_half_word(APP_START_ADDR + w_size, cache_buf, (upgrade.param.moving_size - w_size) / 2);
  81. w_size = upgrade.param.moving_size;
  82. }
  83. }
  84. fmc_clear_flag_end();
  85. if (check_addr_sp(APP_START_ADDR) && check_addr_pc(APP_START_ADDR))
  86. {
  87. CAN_DeInit(CAN1);
  88. __set_PRIMASK(1);
  89. run_app();
  90. }
  91. }
  92. }
  93. else
  94. {
  95. CAN_DeInit(CAN1);
  96. __set_PRIMASK(1);
  97. run_app();
  98. }
  99. schedule_init(TASK_PERIOD);
  100. while (1)
  101. {
  102. schedule();
  103. }
  104. }