I am using PIC24fj256ga702 controller in my project. I wanted to store some bytes data in a non-volatile memory in case if there is a power loss.
so i wrote some functions following http://microchipdeveloper.com/16bit:flash-write
#define pagelength 1024 //page 42, one erase block 1024 instruction words
#define rowlength 128 //one write block = 128 instruction words.
/*
* program memory upper boundary (instruction words) 0x02AFFE,
* +2, i.e. 0x02B000, start of address
* page length 1024 instruction words(erase block)
* write block 128 instruction block; 255 bytes.
*/
#define address 0x2B002 // might have less than 255 blocks for writing data
uint8_t Rambuffer[pagelength * 2];
uint8_t Rowbuffer[rowlength * 2]; //one row of data, that can be written once. 255 bytes.
void readFlashPage(void)
{
int offset, i;
TBLPAG = __builtin_tblpage (address); //returns the page number of the memory address received as a parameter. For table instructions the returned value is placed in TBLPAG
offset = __builtin_tbloffset (address); //returns the offset from the base address for a memory location whose address is passed as a parameter. The return value of this function is passed as a parameter to table read and table write instructions
offset = offset & 0xF800; //set to the base of page
for(i = 0; i<(pagelength * 2); i++){
Rambuffer[i++] = __builtin_tblrdl(offset); //returns the lower 16 bits of the memory address specified by TBLPAG and the offset parameter(calls TBLRDL instruction)
Rambuffer[i] = __builtin_tblrdh(offset); //returns the upper 8-bits of the memory adddress specified by TBLPAG and the offset parameter(calls TBLRDL instruction)
offset = offset + 2;
}
}
void eraseFlashPage(void){
int offset;
NVMADRU = __builtin_tblpage(address);
offset = __builtin_tbloffset(address);
NVMADR = (offset & 0xF800); // for page size of 1024 PM words
//set WREN and page Erase in NVMCON
NVMCON = 0x4003;
__builtin_disi(6); //disable interrupts for next six instructions
__builtin_write_NVM(); //intiate write process
}
void rowFlashWrite(void){
int offset, i;
TBLPAG = 0xFA; // base address of write latches 0xFA0000h till 0xFA00FEh
//load row of data into write latches
offset = 0;
for (i = 0; i < rowlength * 2 ; i++){
__builtin_tblwtl(offset, Rowbuffer[i++]);
__builtin_tblwth(offset, Rowbuffer[i]);
offset+=2;
}
//set the destination address into the NVM address registers
NVMADRU = __builtin_tblpage(address);
offset = __builtin_tbloffset(address);
NVMADR = (offset & 0xF800); // for page size of 1024 PM words
//set WREN and enable row write in NVMCON
NVMCON = 0x4002;
__builtin_disi(6); // disable interrupts for 6 instruction cycles
__builtin_write_NVM(); // initate write process
}
void rowFlashRead(void){
int offset, i;
TBLPAG = __builtin_tblpage (address); //returns the page number of the memory address received as a parameter. For table instructions the returned value is placed in TBLPAG
offset = __builtin_tbloffset (address); //returns the offset from the base address for a memory location whose address is passed as a parameter. The return value of this function is passed as a parameter to table read and table write instructions
for(i = 0; i<(rowlength * 2); i++){
Rowbuffer[i++] = __builtin_tblrdl(offset); //returns the lower 16 bits of the memory address specified by TBLPAG and the offset parameter(calls TBLRDL instruction)
Rowbuffer[i] = __builtin_tblrdh(offset); //returns the upper 8-bits of the memory adddress specified by TBLPAG and the offset parameter(calls TBLRDL instruction)
offset = offset + 2;
}
}
but I am getting this error when I try to build it
make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
make[1]: Entering directory 'C:/Users/HP/MPLABXProjects/Rollman/emulate.X'
make -f nbproject/Makefile-default.mk dist/default/production/emulate.X.production.hex
make[2]: Entering directory 'C:/Users/HP/MPLABXProjects/Rollman/emulate.X'
"C:\Program Files (x86)\Microchip\xc16\v1.35\bin\xc16-gcc.exe" main.c -o build/default/production/main.o -c -mcpu=24FJ256GA702 -MMD -MF "build/default/production/main.o.d" -mno-eds-warn -g -omf=elf -DXPRJ_default=default -legacy-libc -O0 -msmart-io=1 -Wall -msfr-warn=off
nbproject/Makefile-default.mk:155: recipe for target 'build/default/production/main.o' failed
make[2]: Leaving directory 'C:/Users/HP/MPLABXProjects/Rollman/emulate.X'
nbproject/Makefile-default.mk:90: recipe for target '.build-conf' failed
make[1]: Leaving directory 'C:/Users/HP/MPLABXProjects/Rollman/emulate.X'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
main.c: In function 'readFlashPage':
main.c:83:32: error: Argument to __builtin_tblpage() is not the address
of an object in a code, psv, or eedata section;
the object must not be qualified with any form of index
main.c:84:34: error: Argument to __builtin_tbloffset() is not the address
of an object in a code, psv, or eedata section;
the object must not be qualified with any form of index
main.c: In function 'eraseFlashPage':
main.c:95:32: error: Argument to __builtin_tblpage() is not the address
of an object in a code, psv, or eedata section;
the object must not be qualified with any form of index
main.c:96:33: error: Argument to __builtin_tbloffset() is not the address
of an object in a code, psv, or eedata section;
the object must not be qualified with any form of index
main.c: In function 'rowFlashWrite':
main.c:120:32: error: Argument to __builtin_tblpage() is not the address
of an object in a code, psv, or eedata section;
the object must not be qualified with any form of index
main.c:121:33: error: Argument to __builtin_tbloffset() is not the address
of an object in a code, psv, or eedata section;
the object must not be qualified with any form of index
main.c: In function 'rowFlashRead':
main.c:134:32: error: Argument to __builtin_tblpage() is not the address
of an object in a code, psv, or eedata section;
the object must not be qualified with any form of index
main.c:135:34: error: Argument to __builtin_tbloffset() is not the address
of an object in a code, psv, or eedata section;
the object must not be qualified with any form of index
make[2]: *** [build/default/production/main.o] Error 255
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 573ms)
Will this fix it?
uint16_t address __attribute__ ((space(prog))) = 0x2B002;
and passing &address to __builtin_tblpage ?
but the space attribute is used to direct the compiler to allocate a variable in specific memory spaces, so this can't be correct?
I really need some help here.
Thanks
p.s. also, can you please see the code, and tell me what I might be doing wrong