CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

read_program_memory only reading 0xFF with PIC24FJ1024GA606

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
mgiuliani



Joined: 30 Mar 2023
Posts: 23

View user's profile Send private message

read_program_memory only reading 0xFF with PIC24FJ1024GA606
PostPosted: Tue Jun 10, 2025 7:10 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jun 10, 2025 7:47 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jun 10, 2025 9:25 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jun 10, 2025 11:15 am     Reply with quote

Good.
Glad you have progress. Very Happy
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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group