|
我使用stm32对flash进行写入,但是每次校验的时候都出错,相当于没写进去,我也看了一下写入的格式和要求,配置了RCC和Unlock了flash,大家看一下我的代码:
u8 flash_write(u32 StartAddr,u16 *p_data,u32 size)
{
volatile FLASH_Status FLASHStatus;
u32 EndAddr=StartAddr+size*2;
vu32 NbrOfPage = 0;
u32 EraseCounter = 0x0, Address = 0x0;
int i;
int MemoryProgramStatus=1;
FLASH_Unlock(); //flash解锁
NbrOfPage=((EndAddr-StartAddr)>>10)+1; //有多少页被擦除
FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
//清除所有标志
FLASHStatus=FLASH_COMPLETE;
for(EraseCounter=0;(EraseCounter<NbrOfPage)&&(FLASHStatus==FLASH_COMPLETE);EraseCounter++)
{
FLASHStatus=FLASH_ErasePage(StartAddr+(1024*EraseCounter));
}
Address = StartAddr; //写数据
i=0;
while((Address<EndAddr)&&(FLASHStatus==FLASH_COMPLETE))
{ FLASHStatus=FLASH_ProgramHalfWord(Address,p_data[i++]);
Address=Address+2;
}
//检验数据是否出错
Address = StartAddr;
i=0;
while((Address < EndAddr) && (MemoryProgramStatus != 0)) // while((Address < EndAddr) )
{
if((*(u32*) Address) != p_data[i++])
{
MemoryProgramStatus = 0;
FLASH_Lock();
return 1;
}
Address += 2;
LED3_ON;
}
FLASH_Lock();
return 0;
}
u8 numstor[13]= "";
u16 numstorch[13];
u8 n[13] = "15902975570";//短信号码
主函数调用:
for(i=0;i<13;i++)
{numstorch=(u16)n;}
while(flash_write(0x8002000 ,numstorch,13))
{}
LED3_ON;
那个LED3就没亮过,说明Flash_write校验出错,这是为什么,求大家指导一下
还有这是我的RCC配置:
void SystemInit (void)
{
/* Reset the RCC clock configuration to the default reset state(for debug purpose) */
/* Set HSION bit */
RCC->CR |= (uint32_t)0x00000001;
/* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
#ifndef STM32F10X_CL
RCC->CFGR &= (uint32_t)0xF8FF0000;
#else
RCC->CFGR &= (uint32_t)0xF0FF0000;
#endif /* STM32F10X_CL */
/* Reset HSEON, CSSON and PLLON bits */
RCC->CR &= (uint32_t)0xFEF6FFFF;
/* Reset HSEBYP bit */
RCC->CR &= (uint32_t)0xFFFBFFFF;
/* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
RCC->CFGR &= (uint32_t)0xFF80FFFF;
#ifdef STM32F10X_CL
/* Reset PLL2ON and PLL3ON bits */
RCC->CR &= (uint32_t)0xEBFFFFFF;
/* Disable all interrupts and clear pending bits */
RCC->CIR = 0x00FF0000;
/* Reset CFGR2 register */
RCC->CFGR2 = 0x00000000;
#elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
/* Disable all interrupts and clear pending bits */
RCC->CIR = 0x009F0000;
/* Reset CFGR2 register */
RCC->CFGR2 = 0x00000000;
#else
/* Disable all interrupts and clear pending bits */
RCC->CIR = 0x009F0000;
#endif /* STM32F10X_CL */
#if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)
#ifdef DATA_IN_ExtSRAM
SystemInit_ExtMemCtl();
#endif /* DATA_IN_ExtSRAM */
#endif
FLASH_SetLatency(FLASH_Latency_2);
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);/* Configure the Flash Latency cycles and enable prefetch buffer */
SetSysClock();
#ifdef VECT_TAB_SRAM
SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
#else
SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
#endif
} |
|