boot.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "gd32f10x.h"
  2. #include "boot.h"
  3. #include "main.h"
  4. #include "delay.h"
  5. #include "fmc.h"
  6. #include "w25q32.h"
  7. #include <stdio.h>
  8. #include "ota_message.h"
  9. #define APP_PROGRAMMER_BLOCK 2 //程序在W25Q32中存放的位置
  10. load_a load_A;
  11. //进入bootloader更新
  12. void BootLoader_Brance(uint32_t app_byte)
  13. {
  14. GD32_EraseFlash(GD32_A_START_PAGE,GD32_A_PAGE_NUM);//擦除A区原有程序
  15. for(uint8_t i=0;i<app_byte/1024;i++)
  16. {
  17. uint8_t app_programmer[1024];
  18. memset(app_programmer,0,1024);
  19. uint32_t addr=APP_PROGRAMMER_BLOCK*64*1024+i*1024; //其在w25q32中的地址
  20. W25Q32_Read(app_programmer,addr,1024);
  21. GD32_WriteFlash(GD32_A_SADDR+i*1024,(uint32_t *)&app_programmer,1024); //将读出的数据按页写入数据
  22. }
  23. //将不满1024数据单独写入
  24. if(app_byte%1024!=0)
  25. {
  26. uint32_t startAddress=(app_byte/1024)*1024;
  27. uint8_t app_programmer[1024];
  28. uint32_t addr=APP_PROGRAMMER_BLOCK*64*1024+(app_byte/1024)*1024; //其在w25q32中的地址
  29. memset(app_programmer,0,1024);
  30. W25Q32_Read(app_programmer,addr,app_byte%1024);
  31. GD32_WriteFlash(GD32_A_SADDR+(app_byte/1024)*1024,(uint32_t *)&app_programmer,app_byte%1024);
  32. }
  33. clear_ota_message_config_block();
  34. LOAD_A(GD32_A_SADDR);
  35. }
  36. /*-------------------------------------------------*/
  37. /*函数名:设置SP */
  38. /*参 数:addr:栈顶指针初始值 */
  39. /*返回值:无 */
  40. /*-------------------------------------------------*/
  41. __asm void MSR_SP(uint32_t addr)
  42. {
  43. MSR MSP, r0
  44. BX r14
  45. }
  46. /*-------------------------------------------------*/
  47. /*函数名:跳转到A区 */
  48. /*参 数:addr:A区的起始地址 */
  49. /*返回值:无 */
  50. /*-------------------------------------------------*/
  51. void LOAD_A(uint32_t addr)
  52. {
  53. if((*(uint32_t *)addr>=0x20000000)&&(*(uint32_t *)addr<=0x20017FFF)){
  54. MSR_SP(*(uint32_t *)addr);
  55. load_A = (load_a)*(uint32_t *)(addr+4);
  56. BootLoader_Clear();
  57. load_A();
  58. }else ;
  59. }
  60. /*-------------------------------------------------*/
  61. /*函数名:清除B区使用的外设 */
  62. /*参 数:无 */
  63. /*返回值:无 */
  64. /*-------------------------------------------------*/
  65. void BootLoader_Clear(void)
  66. {
  67. gpio_deinit(GPIOA);
  68. gpio_deinit(GPIOB);
  69. }