October 19, 2007 - 5:57pm
Guest |
In a C source file, changing the memory allocation for a local variable in a function from xdata to idata causes RC51 to compile allocate local varables in the following function differently. This isn't fatal, but it's odd. I've sent the listings to Francis Lamotte.
PUBLIC BIT Scratchpad_WrByte( T_FlashAddr addr, U8 theByte )
{
BIT cacheEA;
_DECL_SFRPageCache // to cache the SFR page, if used.
U8 IDATA c; << ------------------ Setting this to idata
if(addr > _ScratchFlashBytes-1 ) // Out of scratchpad address range?
{
return 0; // then fail
}
else // else write the byte
{
// Write the flash
cacheEA = EA; // cache global interrupt status
EA = 0; // disable interrupts (precautionary)
_Push_SFR_Page(0); // FLSCL, PSCTL are page 0
FLSCL |= 0x01; // enable FLASH write/erase
PSCTL = 0x05; // MOVX writes scratchpad FLASH
*((U8 xdata*)addr) = theByte; // Flash the byte (takes about 20usec)
c = *((U8 code*)addr); // read back the byte written (while in the Flash bank)
PSCTL = 0x00; // MOVX writes XRAM
FLSCL &= ~0x01; // disable FLASH write/erase
_Pop_SFR_Page; // return with SFR page preserved.
EA = cacheEA; // restore global interrupt status
// and check it was written... Note that the written location is read as 'code' (MOVC)
return c == theByte;
}
}
changes the way the compiler compiles this.....................
PUBLIC BIT Scratchpad_WrBlock( U8 const *buf, T_FlashAddr flashAddr, U8 cnt )
{
U8 c;
if( flashAddr + cnt > _ScratchFlashBytes ) // Block extends outside scratchpad area?
{
return 0; // then fail
}
else // else try writing each byte
{
for( c = 0; c < cnt; c++) // For each byte;
{
if( !Scratchpad_WrByte(flashAddr+c, buf[c]) ) // Write failed?
{ return 0; } // then quit
} // else keep going
return 1; // All written -> success.
}
}
|
When a function has been previously compiled in the same source file (i.e. Scratchpad_WrByte is above Scratchpad_WrBlock), the compiler knows the usage of the registers. Therefore, some optimization could be done (that are not possible with an external function).
That makes sense; thank you very much
Steven Pruzina