#include "gd_ota_flash.h"
#include "gd32f10x_fmc.h"
#include "delay.h"
#include "string.h"
#include <stdlib.h>

void GD32_WriteFlash(uint32_t saddr, uint32_t *wdata, uint32_t wnum);
void GD32_EraseFlash(uint16_t start, uint16_t num);

//读取soft版本信息
int read_soft_version()
{
	
}


void write_soft_version(BOOT_MESSAGE *save_message)
{
	
	GD32_EraseFlash(FMC_Page,1);
	uint32_t data=FMC_READ(FMC_WRITE_START_ADDR);
	GD32_WriteFlash(FMC_WRITE_START_ADDR,(uint32_t *) save_message,sizeof(BOOT_MESSAGE));
	
}


//读取GD32flash
void GD32_READ_OTA(BOOT_MESSAGE *READ)
{
	uint32_t data=FMC_READ(FMC_WRITE_START_ADDR);
	Delay_Ms(10);
	memcpy(READ,&data,4);
}
//对系统存储版本信息进行初始化
void soft_init()
{
	BOOT_MESSAGE *currentBoot=malloc(sizeof(BOOT_MESSAGE));
	GD32_READ_OTA(currentBoot);
	if(currentBoot->VERSION_H==0xFF && currentBoot->VERSION_M==0xFF &&currentBoot->VERSION_L==0xFF )
	{
		currentBoot->VERSION_H=1;currentBoot->VERSION_L=0;currentBoot->VERSION_M=0;currentBoot->UP_FLAG=0;
		write_soft_version(currentBoot);
	}
	
	free(currentBoot);
	
}


