Hi, bigmac.
I have been doing some test and here are the results.
Right now, I have been programming in flash strings of bytes, but I have been doing it byte by byte always. After your last message, I have modified my code to read a byte via IIC and move it to the flash. Here is the function to program a byte on the flash:
#pragma CODE_SEG FLASH_ROUTINES
unsigned char Flash_Cmd(unsigned int FlashAddress, unsigned int FlashDataCounter, unsigned char* pFlashDataPtr, unsigned char FlashCommand)
{
/* Check to see if FACCERR or PVIOL is set */
if (FSTAT&0x30)
{
/* Clear Flags if set*/
FSTAT = FSTAT | 0x30;
}
if (FlashDataCounter)
{
do
{
/* Wait for the Last Busrt Command to complete */
while(!(FSTAT&0x80));
/* Write Data into Flash*/
(*((volatile unsigned char *)(FlashAddress++))) = *pFlashDataPtr;
pFlashDataPtr++;
/* Write Command */
FCMD = FlashCommand;
_asm NOP;
_asm NOP;
_asm NOP;
_asm NOP;
/* Put FCBEF at 1 */
FSTAT = 0x80;
/* Check if Flash Access Error or Protection Violation Error are Set */
if (FSTAT&0x30)
{
/* If so, finish the function returning 1 to indicate error */
return (1);
}
}while (--FlashDataCounter);
}
/* wait for the last command to complete */
while ((FSTAT&0x40)==0);
/* Return zero to indicate that the function executed Ok */
return (0);
}
#pragma CODE_SEG DEFAULT
#define Flash_Program(Address, Data) \
Flash_Cmd((unsigned int)Address, (unsigned int)1, (unsigned char*)&Data, FLASH_PROGRAM_CMD)
My MCU is the slave, and the master does not support holding the scl line to make a pause.
I tried next code:
DisableInterrupts;
Flash_Erase(0xC000);
for (tmp4=0;tmp4<0xff;tmp4++)
Flash_Program(0xC000 + tmp4, tmp4);
tmp4=0;
EnableInterrupts;
And it works succesfully, but when I tried:
while (!I2C1_GetCharsInRxBuf()){}
if(I2C1_RecvChar(&tmp4)==ERR_OK) { //Numero de bytes que se van a recibir
I2C1_ClearRxBuf();
DisableInterrupts;
Flash_Erase(0xC000);
EnableInterrupts;
for (tmp5=0;tmp5<tmp4;tmp5++){ //Bucle a esos bytes
while (!I2C1_GetCharsInRxBuf()){}
if(I2C1_RecvChar(&tmp3)==ERR_OK) {
if(I2C1_RecvChar(&tmp3));
I2C1_ClearRxBuf();
DisableInterrupts;
Flash_Program(0xC000 + tmp5, tmp3);
EnableInterrupts;
}
}
It is basically the same code reading data via IIC and waiting for every byte. This code fails, and disconnects the debugger after erasing the rom. It´s needed to reprogram again. Each IIC byte has a delay of 1 second. Right now, I have no idea about what´s wrong...
IIC functions are located on a specific "partition" of the ROM, which is never flashed.
Any suggestion?