A: Write_N_4Byte_flash(FLASH_APP1_ADDR,(uint32_t*)(valid_data+8),len);
B:Write_N_4Byte_flash(FLASH_APP1_ADDR,(uint32_t*)(valid_data+6),len);
A语句存在写入flash时读取valid_data数据失败
B语句则可以正常的读写valid_data中数据的数据内容
不知道照成上述问题的原因,本人估计是内存对齐的问题
代码展示:
uint8_t __NeetErease(uint32_t start_addr ,uint32_t len)
{
//该函数len必须是4的倍数 即 len%4==0
volatile uint32_t i=0;
for(;i<len;i++)
{
if(Read_4Byte_flash(start_addr)!=0xffffffff)
{
break;
}
start_addr+=4;
}
return i==len ? 0:1;
}
uint8_t _Erease_Flash(uint32_t base_addr)
{
uint8_t flag=0;
FLASH_EraseInitTypeDef hflash;
//擦除页
hflash.TypeErase=FLASH_TYPEERASE_PAGES; //擦除类型
hflash.Banks = FLASH_BANK_1;
hflash.PageAddress = base_addr;//页基地址
hflash.NbPages = 1;//需要擦除的页数
uint32_t error=0;
if(HAL_FLASHEx_Erase(&hflash,&error)==HAL_OK)
{
flag=1;
}
return flag;
}
uint8_t Write_One_Page_Data(volatile uint32_t base_addr,volatile uint32_t page_offst,volatile uint32_t *ppdata,volatile uint32_t erease_size)
{
volatile uint8_t flag=0;
volatile uint16_t i=0;
volatile uint16_t cnt;
volatile uint32_t cur_addr=0;
//static uint8_t add=0;
//add++;
//HAL_FLASH_Unlock();
if(__NeetErease(base_addr,erease_size))
{
memset((void*)flash_data,0,STM32_FLASH_PAGE_SIZE);
for(i=0;i<page_offst ;i++)
{
cur_addr = base_addr + i*4;
flash_data[i]=Read_4Byte_flash(cur_addr);
}
_Erease_Flash(base_addr);
//写入数据
i=page_offst;
cnt = erease_size+page_offst;
for(;i<cnt;i++)
{
flash_data[i]=*ppdata;
//flash_data[i]=i+add;
ppdata++;
}
for(i=0;i<erease_size+page_offst;i++)
{
cur_addr = base_addr+i*4;
if(cur_addr%4==0&& cur_addr >STM32_FLASH_BASE)
{
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,cur_addr,flash_data[i]);
}
}
}
else //不需要擦除就从偏移开始
{
for(i=page_offst;i<erease_size+page_offst;i++)
{
cur_addr = base_addr+i*4;
if(cur_addr%4==0&& cur_addr >STM32_FLASH_BASE)
{
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,cur_addr,*ppdata);
ppdata++;
}
}
}
//HAL_FLASH_Lock();
return 1;
}
/*
本函数操作的整型数据 4个字节;flash是以一个字节为一个地址的
addr 起始地址
data 数据开始地址
surplus 整型数据长度1len=4Byte ;最大为 STM32_FLASH_PAGE_SIZE/4;
*/
uint8_t Write_N_4Byte_flash(uint32_t addr,uint32_t *data,uint32_t len)
{
volatile uint32_t start_page=0; //起始页
volatile uint32_t page_size=0; //需要操作页的大小
volatile uint32_t _offset=0; //页操作页码的起始地址
volatile uint32_t page_offset=0; //页偏移
volatile uint32_t erease_size=0; //本次操作页可以使用的剩余大小
volatile uint32_t base_addr=0; //本次操作页的基地址
volatile uint32_t surplus=0;
start_page=((uint32_t)addr-STM32_FLASH_BASE)/STM32_FLASH_PAGE_SIZE;
_offset = (((uint32_t)addr-STM32_FLASH_BASE)%STM32_FLASH_PAGE_SIZE)/4;
if(PAGE_INT_SIZE-_offset>=len)
{
page_size=1;
surplus=0;
}
else
{
page_size=1;
surplus = len-(PAGE_INT_SIZE-_offset);
page_size +=surplus/PAGE_INT_SIZE + surplus%PAGE_INT_SIZE>0?1:0;
}
surplus=len;
for(page_offset=0;page_offset<page_size;page_offset++)
{
if(page_offset==0)
{
erease_size =(PAGE_INT_SIZE-_offset)<surplus ? (PAGE_INT_SIZE-_offset):surplus;
base_addr = STM32_FLASH_BASE + start_page*STM32_FLASH_PAGE_SIZE;
surplus = len-erease_size;
}
else
{
_offset=0;
erease_size =surplus>PAGE_INT_SIZE ? PAGE_INT_SIZE:surplus;
base_addr = STM32_FLASH_BASE + (start_page+page_offset)*STM32_FLASH_PAGE_SIZE;
surplus = surplus-erease_size;
}
Write_One_Page_Data(base_addr,_offset,data,erease_size);
data+=erease_size;
//_offset=0;
}
return 1;
}
补充说明,问题产生原因:
上图中给指针赋值就会出问题,程序就会跑到HardFault_Handler()里
0x20000a1和0x20000a0 都不是4的倍数
根据map文件的内容可以看见每个变量都是4的倍数的地址,STM32内存4字节对齐