开发环境:IAR FOR STM8
MCU:STM8L151C8T6
RAM: 4KBytes
FLASH: 64KBytes (Addr:0x8000~0x17fff)
最近在做STM8的IAP程序,STM8 程序区起始地址为0x8000,预留10KBytesFLASH空间给bootloader,即APP(应用程序)起始地址为0xA800,写完IAP后调试发现,当APP(应用程序)小于22KBtes时程序运行正常,大于22KBytes则无法运行,程序跑飞,检查后发现对FLASH读写的函数有问题,如下:
unsigned char *p;
unsigned char flash_Write(unsigned int offset,unsigned char *str,unsigned int len)
{
unsigned int i=0;
unsigned int timeOut=0;
p=(unsigned char *)(0x8000+offset);
if(flash_UnLock()) return 1;
for(i=0;i<len;i++)
{
*p=str[i];
//*((unsigned char *)(uint32_t)0x8000+offset+i)=str[i];
timeOut=50000;
while(--timeOut)
{
if((FLASH->IAPSR & 0x04)) break;
}
if(timeOut==0) return 2;
p++;
}
flash_Lock();
return 0;
}
指针p最多能指到地址0xffff,即指针p本身是2Bytes,按照网上查找的资料,在指针变量定义时增加关键字 __far 就可以指定指针p本身为24bits(3Bytes),但实际测试却仍是2Bytes,测试程序如下:
unsigned char __far *p;
main()
{
while(1)
{
p++;
}
}
用ST_LINK调试监控,p仍是增到0xffff后再自增就归0了,那么该如何让p能指到0xffff之后的地址呢?
|