Message Edited by Henry L on 2008-12-08 06:53 PM
Added p/n to subject.
char FchrEepromWrite (unsigned char *PAuchrCanData){ unsigned char LuchrBankNum = 0; unsigned char LuchrReturn = 0; unsigned char LuchrIndex = 0; unsigned char LuchrDone = 0; int * LptrWriteAddr; //initialize commands //clear errors if (ESTAT & (PVIOL | ACCERR)) ESTAT |= (PVIOL | ACCERR); //wait for cmd registries to be empty and ready to take new cmds while(!(ESTAT & CBEIF)); while(!LuchrDone) { switch(LuchrIndex) { case 0: LptrWriteAddr = (int *)((PAuchrCanData[1] << 8) | PAuchrCanData[2]); ECMD = defEeCmdSectErase; break; case 1: LptrWriteAddr = (int *)((PAuchrCanData[1] << 8) | PAuchrCanData[2]); *LptrWriteAddr = (PAuchrCanData[3] << 8) | PAuchrCanData[4]; ECMD = defEeCmdProgram; break; case 2: LptrWriteAddr = (int *)(((PAuchrCanData[1] << 8) | PAuchrCanData[2]) + 2); *LptrWriteAddr = (PAuchrCanData[5] << 8) | PAuchrCanData[6]; ECMD = defEeCmdProgram; break; } ESTAT |= CBEIF; if (ESTAT & PVIOL) { ESTAT |= PVIOL; LuchrDone = 1; } else if (ESTAT & ACCERR) { ESTAT |= ACCERR; LuchrDone = 1; } else { while (!(ESTAT & CBEIF)); while (!(ESTAT & CCIF)); LuchrIndex++; if (LuchrIndex > 2) { LuchrDone = 1; LuchrReturn = 1; } } } return LuchrReturn;}
char FchrEepromWrite (unsigned char *PAuchrCanData){ unsigned char LuchrBankNum = 0; unsigned char LuchrReturn = 0; unsigned char LuchrIndex = 0; unsigned char LuchrDone = 0; int * LptrWriteAddr; //initialize commands //clear errors if (ESTAT & (PVIOL | ACCERR)) ESTAT |= (PVIOL | ACCERR); //wait for cmd registries to be empty and ready to take new cmds while(!(ESTAT & CBEIF)); while(!LuchrDone) { switch(LuchrIndex) { case 0: LptrWriteAddr = (int *)((PAuchrCanData[1] << 8) | PAuchrCanData[2]); ECMD = defEeCmdSectErase; break; case 1: LptrWriteAddr = (int *)((PAuchrCanData[1] << 8) | PAuchrCanData[2]); *LptrWriteAddr = (PAuchrCanData[3] << 8) | PAuchrCanData[4]; ECMD = defEeCmdProgram; break; case 2: LptrWriteAddr = (int *)(((PAuchrCanData[1] << 8) | PAuchrCanData[2]) + 2); *LptrWriteAddr = (PAuchrCanData[5] << 8) | PAuchrCanData[6]; ECMD = defEeCmdProgram; break; } ESTAT |= CBEIF; if (ESTAT & PVIOL) { ESTAT |= PVIOL; LuchrDone = 1; } else if (ESTAT & ACCERR) { ESTAT |= ACCERR; LuchrDone = 1; } else { while (!(ESTAT & CBEIF)); while (!(ESTAT & CCIF)); LuchrIndex++; if (LuchrIndex > 2) { LuchrDone = 1; LuchrReturn = 1; } } } return LuchrReturn;}
case 0: LptrWriteAddr = (int *)((PAuchrCanData[1] << 8) | PAuchrCanData[2]); *LptrWriteAddr = 0; ECMD = defEeCmdSectErase; break;
In normal modes, the FADDR (FADDRHI, FADDRLO) register reads zeros and is not writable.
Instead of using FADDR and FDATA, as docs say, you should
...Write the aligned data word to be programmed to the valid Flash address space. The address and data will be stored in internal buffers...
Henry L wrote:so what is FADDR and FDATA used for, if anything at all?
"Write the aligned data word to be programmed to the valid Flash address space. The address and data will be stored in internal buffers..."
Meaning I should do something like this?
unsigned int * Pointer = 0x8000;
*Pointer = WordOfFlashData;
FCMD = 0x20;
FSTAT |= CBEIF;
with FADDR & FDATA, you had to write 2 bytes (a word) of data at a time, no more no less. Does this still hold true using this method?
And while we're on the subject, is writing to eeprom similar?
I will running code from RAM. I'm implementing something similar to the app notes for LRAE & LFAE.
kef wrote:Yes. But also keep in mind that flash and EEPROM are not readable while CCIF bit is 0. Not readable memory means also that CPU can't execute code from memory you are programming. Also CPU can't fetch interrupt vectors from flash array being progammed / erased. Your flash programming routines should run from RAM, EEPROM, another flash array (on D256 you have 4 flash arrays), or external memory.
If you are referring to the FTS256K block guide, then yes, it seems that I do have to write one word at a time, allowing the CBEIF to trigger before continuing.You should find flash program and erase algorithms in the docs.
While programming many words, you don't have to wait for CCIF==1. But flash is not readable while FSTAT CCIF==0. So for example you can't verify what you programmed until CCIF==1.
Henry L wrote:Also, according to Fig4-2 of that document, after I launch the command, I should wait for CBEIF to be set before writing the next word. Should not also be waiting for CCIF for the command to complete, before writing the next word?