本帖最后由 JY-DX-JY 于 2016-4-23 10:06 编辑
我正在看《例说STM32》
第31章是FLASH 模拟 EEPROM实验。
我参考书上的例子和网上找到的例子,写了个程序。
烧到板子里去,可以成功写FLASH,但是一直卡在读那个地方。
我贴出部分代码,请大神帮我看看哪里有问题。谢谢!我用的是STM32F103RBT6的板子
const u8 TEXT_Buffer[]={"STM32 FLASH TEST"};
#define SIZE sizeof(TEXT_Buffer)
#define FLASH_SAVE_ADDR 0x08020000
。。。。。。。。。。。。。。。。。。。。
void STMFLASH_Unlock(void)
{
FLASH->KEYR=FLASH_KEY1;
FLASH->KEYR=FLASH_KEY2;
}
void STMFLASH_Lock(void)
{
FLASH->CR|=1<<7;
}
//µÃµ½FLASH״̬
u8 STMFLASH_GetStatus(void)
{
u32 res;
res=FLASH->SR;
if(res&(1<<0))return 1; //æ
else if(res&(1<<2))return 2; //±à³Ì´íÎó
else if(res&(1<<4))return 3; //д±£»¤´íÎó
return 0; //²Ù×÷Íê³É
}
//µÈ´ý²Ù×÷Íê³É
//time:ÒªÑÓʱµÄ³¤¶Ì
//·µ»ØÖµ:״̬.
u8 STMFLASH_WaitDone(u16 time)
{
u8 res;
do
{
res=STMFLASH_GetStatus();
if(res!=1)break;//·Çæ,ÎÞÐèµÈ´ýÁË,Ö±½ÓÍ˳ö.
delay_us(1);
time--;
}while(time);
if(time==0)res=0xff;//TIMEOUT
return res;
}
//²Á³ýÒ³
//paddr:Ò³µØÖ·
//·µ»ØÖµ:Ö´ÐÐÇé¿ö
u8 STMFLASH_ErasePage(u32 paddr)
{
u8 res=0;
res=STMFLASH_WaitDone(0X5FFF);//µÈ´ýÉϴβÙ×÷½áÊø,>20ms
if(res==0)
{
FLASH->CR|=1<<1;//Ò³²Á³ý
FLASH->AR=paddr;//ÉèÖÃÒ³µØÖ·
FLASH->CR|=1<<6;//¿ªÊ¼²Á³ý
res=STMFLASH_WaitDone(0X5FFF);//µÈ´ý²Ù×÷½áÊø,>20ms
if(res!=1)//·Çæ
{
FLASH->CR&=~(1<<1);//Çå³ýÒ³²Á³ý±êÖ¾.
}
}
return res;
}
//ÔÚFLASHÖ¸¶¨µØַдÈë°ë×Ö
//faddr:Ö¸¶¨µØÖ·(´ËµØÖ·±ØÐëΪ2µÄ±¶Êý!!)
//dat:ҪдÈëµÄÊý¾Ý
//·µ»ØÖµ:дÈëµÄÇé¿ö
u8 STMFLASH_WriteHalfWord(u32 faddr, u16 dat)
{
u8 res;
res=STMFLASH_WaitDone(0XFF);
if(res==0)//OK
{
FLASH->CR|=1<<0;//±à³ÌʹÄÜ
*(vu16*)faddr=dat;//дÈëÊý¾Ý
res=STMFLASH_WaitDone(0XFF);//µÈ´ý²Ù×÷Íê³É
if(res!=1)//²Ù×÷³É¹¦
{
FLASH->CR&=~(1<<0);//Çå³ýPGλ.
}
}
return res;
}
//¶ÁÈ¡Ö¸¶¨µØÖ·µÄ°ë×Ö(16λÊý¾Ý)
//faddr:¶ÁµØÖ·
//·µ»ØÖµ:¶ÔÓ¦Êý¾Ý.
u16 STMFLASH_ReadHalfWord(u32 faddr)
{
return *(vu16*)faddr;
}
//´ÓÖ¸¶¨µØÖ·¿ªÊ¼¶Á³öÖ¸¶¨³¤¶ÈµÄÊý¾Ý
//ReadAddr:ÆðʼµØÖ·
//pBuffer:Êý¾ÝÖ¸Õë
//NumToWrite:°ë×Ö(16λ)Êý
//void STMFLASH_Read(u32 ReadAddr,u16 *pBuffer,u16 NumToRead)
//{
// u16 i;
// for(i=0;i<NumToRead;i++)
// {
// pBuffer=STMFLASH_ReadHalfWord(ReadAddr);//¶ÁÈ¡2¸ö×Ö½Ú.
// ReadAddr+=2;//Æ«ÒÆ2¸ö×Ö½Ú.
// }
//}
void STMFLASH_Read(u32 ReadAddr,u16 *pBuffer,u16 NumToRead)
{
u16 i;
for(i=0;i<NumToRead;i++)
{
pBuffer=STMFLASH_ReadHalfWord(ReadAddr);
ReadAddr+=2;
}
}
//////////////////////////////////////////²âÊÔÓÃ///////////////////////////////////////////
#if STM32_FLASH_WREN
void STMFLASH_Write_NoCheck(u32 WriteAddr,u16 *pBuffer,u16 NumToWrite)
{
u16 i;
for(i=0;i<NumToWrite;i++)
{
STMFLASH_WriteHalfWord(WriteAddr,pBuffer);
WriteAddr+=2;
}
}
#if STM32_FLASH_SIZE<256
#define STM_SECTOR_SIZE 1024
#else
#define STM_SECTOR_SIZE 2048
#endif
u16 STMFLASH_BUF[STM_SECTOR_SIZE/2];
void STMFLASH_Write(u32 WriteAddr,u16 *pBuffer,u16 NumToWrite)
{
u32 secpos;
u16 secoff;
u16 secremain;
u16 i;
u32 offaddr;
if(WriteAddr<STM32_FLASH_BASE||(WriteAddr>=(STM32_FLASH_BASE+1024*STM32_FLASH_SIZE)))return;
STMFLASH_Unlock();
offaddr=WriteAddr-STM32_FLASH_BASE;
secpos=offaddr/STM_SECTOR_SIZE;
secoff=(offaddr%STM_SECTOR_SIZE)/2;
secremain=STM_SECTOR_SIZE/2-secoff;
if(NumToWrite<=secremain)secremain=NumToWrite;
while(1)
{
STMFLASH_Read(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE,STMFLASH_BUF,STM_SECTOR_SIZE/2);
for(i=0;i<secremain;i++)
{
if(STMFLASH_BUF[secoff+i]!=0xffff)break;
}
if(i<secremain)
{
STMFLASH_ErasePage(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE);
for(i=0;i<secremain;i++)STMFLASH_BUF[i+secoff]=pBuffer;
STMFLASH_Write_NoCheck(secpos*STM_SECTOR_SIZE+STM32_FLASH_BASE,STMFLASH_BUF,STM_SECTOR_SIZE/2);
}
else
STMFLASH_Write_NoCheck(WriteAddr,pBuffer,secremain);
if(NumToWrite==secremain)break;
else
{
secpos++;
secoff=0;
pBuffer+=secremain;
WriteAddr+=secremain;
NumToWrite-=secremain;
if(NumToWrite>(STM_SECTOR_SIZE/2))secremain=STM_SECTOR_SIZE/2;
else
secremain=NumToWrite;
}
};
STMFLASH_Lock();
}
#endif
//WriteAddr:ÆðʼµØÖ·
//WriteData:ҪдÈëµÄÊý¾Ý
void Test_Write(u32 WriteAddr,u16 WriteData)
{
STMFLASH_Write(WriteAddr,&WriteData,1);//дÈëÒ»¸ö×Ö
}
int main(void)
{
u8 key;u16 i=0;
u8 datatemp[SIZE];
u8 t=0;
float pro=0;
u8 lcd_id[12];
Stm32_Clock_Init(9); //72MHz
uart_init(72,9600);
delay(1000);
LED_Init();
KEY_Init();
LCD_Init();
POINT_COLOR=RED;
sprintf((char *)lcd_id,"LCD ID:%04X",lcddev.id);
LCD_ShowString(60,50,200,16,16,"Mini STM32");
LCD_ShowString(60,70,200,16,16,"FLASH EEPROM TEST");
LCD_ShowString(60,90,200,16,16,"ATOM@ALIENTEK");
LCD_ShowString(60,110,200,16,16,"2014/3/12");
LCD_ShowString(60,130,200,16,16,"WK_UP:Write KEY0:Read");
POINT_COLOR=BLUE;
while(1)
{
key=KEY_Scan(0);
if(key==WKUP_PRES)
{
LCD_Fill(0,150,239,319,WHITE);
LCD_ShowString(60,150,200,16,16,"Start Write FLASH....");
STMFLASH_Write(FLASH_SAVE_ADDR,(u16 *)TEXT_Buffer,SIZE);
LCD_ShowString(60,150,200,16,16,"FLASH Write Finished!");
}
if(key==KEY0_PRES)//KEY0°´ÏÂ,¶ÁÈ¡×Ö·û´®²¢ÏÔʾ
{
LCD_ShowString(60,150,200,16,16,"Start Read FLASH.... ");
STMFLASH_Read(FLASH_SAVE_ADDR,(u16*)datatemp,SIZE);
LCD_ShowString(60,150,200,16,16,"The Data Readed Is: ");//Ìáʾ´«ËÍÍê³É
LCD_ShowString(60,170,200,16,16,datatemp);//ÏÔʾ¶Áµ½µÄ×Ö·û´®
}
i++;delay_ms(10);
if(i==20){i=0;}
//
}
}
|
|