I am writting the driver about flash of mc9s12xep100,I wrote some datas to the P-flash by the follow way,but I am not sure the datas had been wrote into the P-flash,NOW,I want to read them,I don't knom how to do.
void PFlash_Init()
{
while(FSTAT_CCIF==0);
FCLKDIV=0x03;
FCNFG=0x00;
while(FCLKDIV_FDIVLD==0);
}
void Erase_Pflash_block_2()
{
while(FSTAT_CCIF==0);
if(FSTAT_ACCERR)
FSTAT_ACCERR=1;
if(FSTAT_FPVIOL)
FSTAT_FPVIOL=1;
FCCOBIX=0x00;
FCCOB=0x0974;
FCCOBIX=0x01;
FCCOB=0x0000;
FSTAT_CCIF=1;
while(FSTAT_CCIF==0);
}
void Write_PFlash(u32 ADDR,u8 *data,u8 len)
{
u16 addr_H = 0;
u16 addr_L = 0;
u8 test_data[8] = {0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88};
u16 LS_data = 0;
addr_H = (ADDR >> 16) & 0xffff;
addr_L = ADDR & 0xffff;
if(len < 8)
return;
while(FSTAT_CCIF == 0);
if(FSTAT_ACCERR)
FSTAT_ACCERR = 1;
if(FSTAT_FPVIOL)
FSTAT_FPVIOL = 1;
FCCOBIX = 0x00;
FCCOB = 0x0600 | addr_H;
FCCOBIX = 0x01;
FCCOB = addr_L;
LS_data = test_data[1];
LS_data = (LS_data << 8) | test_data[0];
FCCOBIX = 0x02;
FCCOB = 0x1221;
LS_data = test_data[3];
LS_data = (LS_data << 8) | test_data[2];
FCCOBIX = 0x03; //写入第二个数据
FCCOB = 0x1331;
LS_data = test_data[5];
LS_data = (LS_data << 8) | test_data[4];
FCCOBIX = 0x04; //写入第三个数据
FCCOB = 0x1441;
LS_data = test_data[7];
LS_data = (LS_data << 8) | test_data[6];
FCCOBIX = 0x05; //写入第四个数据
FCCOB = 0x1551;
FSTAT_CCIF = 1; //写入执行命令
while(FSTAT_CCIF == 0); //等待执行完毕
}
Solved! Go to Solution.
Hi Youdong,
The global address 0x740000 refers to logical address 0xC08000 (PPAGE=0xC0, address window 0x8000). See attached S12XE memory map.
You may use followed routine for conversion between paged address and global address:
/
/==============================================================================
//Convert_Ppage2Global
//==============================================================================
unsigned long int Convert_Ppage2Global(unsigned long int Addr)
{
Addr = ((Addr>>2) & 0x003FC000UL) | (Addr & 0x00003FFFUL) | 0x00400000;
return(Addr);
}
You should use far pointer for reading data from paged flash. For example:
//==============================================================================
//PFLASH_Read_Word_Global
//==============================================================================
unsigned int PFLASH_Read_Word_Global(unsigned long int address)
{
unsigned int data16;
data16 = *(unsigned int *far)address;
return data16;
}
//==============================================================================
//PFLASH_Read_Word_Ppage
//==============================================================================
unsigned int PFLASH_Read_Word_Ppage(unsigned long int address)
{
return(PFLASH_Read_Word_Global(Convert_Ppage2Global(address)));
}
Please look at TN238 and TN240 documents for more detail information about S12X addressing.
The default path to these files is:
"c:\Program Files (x86)\Freescale\CWS12v5.2\Help\pdf"
Please, be aware, that your current code may work only due to the fact, that you probably program different Flash block than where your code is located. We cannot simultaneously execute code and erase/write the same flash block. The recommended solution is keeping “critical section” (set CCIF flag and wait for command finish) inside RAM.
Please look at attached example code.
I hope it helps you.
Have a great day,
Radek
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Hello Radek:
Thank you~~ I solve my problem with your help
Hi Youdong,
The global address 0x740000 refers to logical address 0xC08000 (PPAGE=0xC0, address window 0x8000). See attached S12XE memory map.
You may use followed routine for conversion between paged address and global address:
/
/==============================================================================
//Convert_Ppage2Global
//==============================================================================
unsigned long int Convert_Ppage2Global(unsigned long int Addr)
{
Addr = ((Addr>>2) & 0x003FC000UL) | (Addr & 0x00003FFFUL) | 0x00400000;
return(Addr);
}
You should use far pointer for reading data from paged flash. For example:
//==============================================================================
//PFLASH_Read_Word_Global
//==============================================================================
unsigned int PFLASH_Read_Word_Global(unsigned long int address)
{
unsigned int data16;
data16 = *(unsigned int *far)address;
return data16;
}
//==============================================================================
//PFLASH_Read_Word_Ppage
//==============================================================================
unsigned int PFLASH_Read_Word_Ppage(unsigned long int address)
{
return(PFLASH_Read_Word_Global(Convert_Ppage2Global(address)));
}
Please look at TN238 and TN240 documents for more detail information about S12X addressing.
The default path to these files is:
"c:\Program Files (x86)\Freescale\CWS12v5.2\Help\pdf"
Please, be aware, that your current code may work only due to the fact, that you probably program different Flash block than where your code is located. We cannot simultaneously execute code and erase/write the same flash block. The recommended solution is keeping “critical section” (set CCIF flag and wait for command finish) inside RAM.
Please look at attached example code.
I hope it helps you.
Have a great day,
Radek
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------