/** ****************************************************************************** * 版 本 :V1.0.0 * 作 者 :liuyanbin * 版权所有,盗版必究。 * Copyright(C) All rights reserved ****************************************************************************** * 历史记录 * 序号 版本 变更内容 作者 日期 * 1 V0.0.1 实现flash存储 刘艳斌 2024/1/24 * ****************************************************************************** */ #include "stm32f10x.h" #include "INflash.h" #include "AT.h" #include "string.h" #include "Delay.h" Coefficient_Data flashdata; uint8_t register_success; uint16 STMFLASH_BUF[STM32_SECTOR_SIZE/2];//最多是2K字节 void write_to_flash(uint32 WriteAddr,uint16 *pBuffer,uint16 NumToWrite) { uint16 i; for(i=0;i=(STM32_FLASH_BASE+1024*STM32_FLASH_SIZE)))return;//非法地址 FLASH_Unlock(); offaddr=WriteAddr-STM32_FLASH_BASE; //实际偏移地址. secpos=offaddr/STM32_SECTOR_SIZE; //扇区地址 0~127 for STM32F103RBT6 secoff=(offaddr%STM32_SECTOR_SIZE)/2; //在扇区内的偏移(2个字节为基本单位.) secremain=STM32_SECTOR_SIZE/2-secoff; //扇区剩余空间大小 if(NumToWrite<=secremain)secremain=NumToWrite;//不大于该扇区范围 while(1) { STMFLASH_Read(secpos*STM32_SECTOR_SIZE+STM32_FLASH_BASE,STMFLASH_BUF,STM32_SECTOR_SIZE/2);//读出整个扇区的内容 for(i=0;i(STM32_SECTOR_SIZE/2))secremain=STM32_SECTOR_SIZE/2;//下一个扇区还是写不完 else secremain=NumToWrite;//下一个扇区可以写完了 } }; FLASH_Lock();//上锁 } //清空flash void clearflash(void){ flashdata.read_flag=0; memcpy(flashdata.deviceSecret,"0",40); memcpy(flashdata.productKey,"0",20); memcpy(flashdata.deviceName,"0",20); delay_ms(10); STMFLASH_Write(FLASH_SAVE_ADDR,(uint16*)&flashdata,sizeof(flashdata)); } void storedata(void){ flashdata.read_flag=1; Log_Printf_Debug("###########\r\n"); Log_Printf_Debug("deviceSecret:%s,productKey:%s,deviceName:%s,read_flag:%d\r\n",flashdata.deviceSecret,flashdata.productKey,flashdata.deviceName,flashdata.read_flag); Log_Printf_Debug("结构体大小: %d\r\n", sizeof(Coefficient_Data)); delay_ms(10); STMFLASH_Write(FLASH_SAVE_ADDR,(uint16*)&flashdata,sizeof(flashdata)); flashdata.read_flag=0; delay_ms(10); Log_Printf_Debug("写入数据完成\r\n"); STMFLASH_Read(FLASH_SAVE_ADDR,(uint16*)&flashdata,sizeof(flashdata)); Log_Printf_Debug("读取完成read_flag:%d,deviceSecret:%s,productKey:%s,deviceName:%s\r\n",flashdata.read_flag,flashdata.deviceSecret,flashdata.productKey,flashdata.deviceName); } void store(void){ if(register_success==1){ storedata(); } else { Log_Printf_Debug("数据解析失败,没有存储flash\r\n"); flashdata.read_flag=0; } }