Hi, the problem is that the compiler uses MOVX instead of MOVC when I'm reading the rom, of course it doesn't work. There's no problem to write the flash. I don't want to add an assembly routine. Is there any other solution? I'm using Silabs C8051F020.
Thanks.
The compiler (and the CPU) knows nothing about RAM or ROM - all it knows are the CODE and XDATA address spaces.
http://www.keil.com/support/man/docs/c51/c51_le_memareas.htm
So you just have to specify the appropriate memory space qualifier for your variables...
Thank you, I did not know/remember the code type. After a little fight I made it work. The routine, if somebody needs it.
unsigned char read_flash(unsigned char code *source) { bit Ea_ant;
unsigned char var_read = 0x01;
Ea_ant = EA;
EA = 0;
FLSCL |= 0x01; //enable write/read flash
PSCTL |= 0x01; //movx target to flash
var_read = *source;
PSCTL &= ~0x01;
EA = Ea_ant;
return var_read; }
you do not need to go through all that to read the flash.
any variable with the code keyword (Please read the manual) will be read from flash
Erik
You're right, but first I need to write a string in rom, and then read one character at a time. If I declare "char code string[5]", as instance, I can't write in it, even if I use a xdata pointer. So I did this:
unsigned char xdata vector[10]; unsigned char code *pflashread;
unsigned char xdata *pflashwrite;
pflashread = 0xbbbb;
pflashwrite = pflashread;
write_flash(pflashwrite,vector);
var_2 = pflashread[0]; //here I read directly
Don't think I haven't try a few things before this.
Is that necessarily true?
Don't these SiLabs chips have the facility to map Flash into XDATA space...?
Write in flash only clear bits, so you have to initialize the code variable with 0xff, or erase the entire block before write.
unsigned char xdata variable[2]={0x46,0x11}; unsigned char xdata variable_2[2]={0}; unsigned char code string_1[2]={0xff,0xff};
write_flash(string_1,variable); //writing to flash
variable_2[0] = string_1[0]; //reading from flash variable_2[1] = string_1[1];
Thank you.
" ... so you have to initialize the code variable with 0xff"
Eh?!
If writing can only clear bits (as, indeed, is usually the case with flash), then how can you initialise the variable to 0xFF other than by erasing?!
You do it by originally storing the value 0xff in the variables while flashing the program. Then the program may replace these 0xff with other values.