#include "gd32f10x.h" #include "boot.h" #include "main.h" #include "delay.h" #include "fmc.h" #include "w25q32.h" #include #include "ota_message.h" #include "ec800m.h" #include "usart.h" #define APP_PROGRAMMER_BLOCK 2 //程序在W25Q32中存放的位置 load_a load_A; //返回str在数组中的索引 static char* find_string(char *strs, char *str,int first, int len) { int i = 0; char *start = NULL; strs = strs + first; while(i < len){ start = strstr(strs, str); if(start != NULL){ break; } i++; strs++; } if(i == len + 1){ return NULL; } return start; } int WaitForReadData(uint8_t* data,int timeout,int datalen) { uint32_t len = 0; bool timeoutFlag = false; if (timeout >= 0) { timeoutFlag = true; } while (1) { Delay_Ms(50); if ((UART0_RX_STAT > 0) && (find_string((char *)&UART0_RX_BUF, "\r\nOK\r\n",datalen, sizeof(UART0_RX_BUF)))) { UART0_RX_STAT = 0; // char *p = strstr((char *)&UART0_RX_BUF, "CONNECT "); // if (p) // { // p += 8; // sscanf(p, "%u\r", &len); // 读取长度 // if(len == datalen) // { // p = strchr(p, '\n') + 1; // 跳过换行 memcpy(data,(char *)&UART0_RX_BUF,datalen); Clear_DMA_Buffer(); return datalen; // } // else // { // Clear_DMA_Buffer(); // return len; // } } timeout -= 50; if (timeoutFlag == true && timeout <= 0) { Clear_DMA_Buffer(); return -1; } } } //进入bootloader更新 void BootLoader_Brance(uint32_t app_byte) { GD32_EraseFlash(GD32_A_START_PAGE,GD32_A_PAGE_NUM);//擦除A区原有程序 /* 打开UFS:otaDATA.txt */ EC800MSendCmd(CLOSE_otaDATA_FILE,strlen(CLOSE_otaDATA_FILE)); WaitResponse("QFCLOSE",1000); EC800MSendCmd(OPEN_otaDATA_FILE,strlen(OPEN_otaDATA_FILE)); if(WaitResponse("QFOPEN:", 2000) == 0) return; for(uint8_t i=0;i=0x20000000)&&(*(uint32_t *)addr<=0x20017FFF)){ MSR_SP(*(uint32_t *)addr); load_A = (load_a)*(uint32_t *)(addr+4); BootLoader_Clear(); load_A(); }else ; } /*-------------------------------------------------*/ /*函数名:清除B区使用的外设 */ /*参 数:无 */ /*返回值:无 */ /*-------------------------------------------------*/ void BootLoader_Clear(void) { gpio_deinit(GPIOA); gpio_deinit(GPIOB); }