View previous topic :: View next topic |
Author |
Message |
mgiuliani
Joined: 30 Mar 2023 Posts: 23
|
read_program_memory only reading 0xFF with PIC24FJ1024GA606 |
Posted: Tue Jun 10, 2025 7:10 am |
|
|
The PIC I am using is a PIC24FJ1024GA606. I have a file saved in program memory using #import. In the lst file I can see that the file is where it should be based on the LOCATION value given by #import. Below is an boiled down version of what I am doing to get a byte from this file in program memory.
Code: |
#import(RAW, FILE="file.txt", LOCATION=FILE_START, SIZE=FILE_SIZE)
__ADDRESS__ pointer;
static char ScriptGetc(void)
{
char c;
read_program_memory(pointer++, &c, 1);
return (c);
}
void main (void)
{
char c;
// ... other code
pointer = FILE_START;
c = ScriptGetc();
// ... other code
}
|
Printing "pointer" shows the correct memory address. Printing "c" always shows 0xFF. I've read the manual and can't see what I'm doing wrong. |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19854
|
|
Posted: Tue Jun 10, 2025 7:47 am |
|
|
You don;t show us your declarations of the size, position etc. However tehre
are issues. Look at how I tell you to do it in the other thread You can't just
read the bytes in, remember every third one will be missing. Hence the BPI
in my code.
To read, I use:
Code: |
union access {
unsigned int32 whole;
unsigned int8 bytes[4];
};
//routine to give access to ROM stored data - used for bitmap
BYTE getval(unsigned int32 locn, unsigned int32 index)
{
unsigned int32 calc;
union access rdg;
unsigned int8 bval;
//Now I need to calculate the actual cell I need to read, fetch this
//then extract the byte
calc=(index/3)*2;
//This gives the actual cell offset from locn.
read_program_memory(locn+calc, &rdg, 4);
//Gives the 32bit value containing the byte required.
bval=index % 3;
//Now gives the byte number required
return rdg.bytes[bval];
//return the physical byte
}
|
Here 'locn' is the variable 'where_us' created by the import as I show it, and
index is the byte number I wish to access in the stored data.
Have to ask though why you are going so complex?. Just use cons, but get
rid of the ANSI declaration or override it. this handles all the work for you.
Understand, you don't specify where the data is to go. The compiler
creates the variable for you saying where it has been put.
Also understand that you can't increment the pointer as you show. The
address used must always be the start of an instruction, so a multiple
of four. |
|
 |
mgiuliani
Joined: 30 Mar 2023 Posts: 23
|
|
Posted: Tue Jun 10, 2025 9:25 am |
|
|
Honestly I have no idea why I'm making this so complex. What I need to do works for what I need by making the text a big const char array and indexing through it as needed. I also forgot about the four byte instructions and accounting for those. I'm not very experienced and I'm a one man firmware/embedded show so this is how I learn the super low level topics like this one. The CCS manual doesn't make things like this clear.
I implemented what you gave and it worked, so that is much appreciated. I see what I was doing wrong. I can definitely see it being useful when importing a file is easier than using a const, so I will keep this method in my pocket. |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19854
|
|
Posted: Tue Jun 10, 2025 11:15 am |
|
|
Good.
Glad you have progress.
However worth trying again using the const array.
Try without ANSI (guessing you are using this), and then if you need other
parts of the ANSI behaviour try just turning these on separately. |
|
 |
|