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

Data table in rom (const) to save RAM

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



Joined: 17 Jun 2019
Posts: 661
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

Data table in rom (const) to save RAM
PostPosted: Mon May 11, 2026 3:22 pm     Reply with quote

I am porting some C routines to PIC24 using the CCS PCD compiler.

I have many tables and limited RAM. I wanted to make them const/rom since I have plenty of program space.

The macros below are unimportant -- they are just helping generate the uint8/16_t values for this structure:

Code:

typedef struct
{
   uint8_t  type;
   uint8_t  length;
   uint16_t offset; // or uint8_t if not struct is > 255 bytes.
} tlv_offset_entry_t;


/*const*/ tlv_offset_entry_t dataTLV[] =
{
    TLVSTRUCTENTRY (1, data_t, byte),
    TLVSTRUCTENTRY (2, data_t, word),
    TLVSTRUCTENTRY (3, data_t, dword),
    TLVSTRUCTENTRY (4, data_t, string),
    TLVSTRUCTENTRYEND
};


Normally I just comment out the "const" in similar code and "it just works." This code works fine that way, too.

I can modify the function to expect a rom *:

Code:

size_t tlv_write_struct (void * p_dest,
                         unsigned int dest_size,
                         rom /*const*/ tlv_offset_entry_t * p_tlv_table,
                         /*const*/ void * p_struct);


...but PCD does not (seemingly) allow indexing into a rom structure like this:

Code:

while ((0 != p_tlv_table[table_entry].type) &&
       (0 != p_tlv_table[table_entry].length))


The compiler does support accessing numeric types such as int8, int16, etc. out of ROM.

Code:

void function(rom int8 *ptr, int size)
{
   printf ("function( 0x%x, %d)\r\n", ptr, size);
   
   if (ptr != NULL)
   {
      for (int idx=0; idx < size; idx++)
      {
         // Load value from ROM.
         int8 value = ptr[idx];
         printf ("%d ", value);
      }
      printf ("\r\n");
   }
}


The solution I learned in this forum years ago was to use read_program_memory() to get access to that. For example:

Code:

tlv_offset_entry_t entry;

read_program_memory (&p_tlv_table[table_entry], &entry, sizeof(entry));


I did not test that, but I have done similar in the past. The idea is to pull that entry out of ROM and into a local varaible.

This is all fine. But it makes having C code that works between a PC and the PIC24 firmware "not easy."

Am I missing some easier way to make code that uses const on a PC work on PIC24?

I got my code running quickly just by commenting out "const" but I expect once all the new tables are built, I will be out of rom on one of our boards. I can't even use printf on that board because the rom is full ;-)
_________________
Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ?

Using: 24FJ256GA106, 24EP256GP202, 24FJ64GA002 and 24FJ1024GA606.
Ttelmah



Joined: 11 Mar 2010
Posts: 20075

View user's profile Send private message

PostPosted: Tue May 12, 2026 12:11 am     Reply with quote

Try adding:

#DEVICE PSV=16
in your chip setup near the start.

This then allows pointers to constants as if they are in RAM!....
You use RAM pointers to constants with this.

Program space visibility.

problem is that the PIC24/30/33 chips have the ROM organised as
data
data
data
blank
data
data
data
blank

You can imagine that handling data laid out like this using a pointer
becomes very difficult. Program space visibility is a feature that allows
a 'window' into the ROM to be accessed using conventional accesses
as if the data was continuous.

Do a search in the manual for this - under getenv.
Quote:

[PCD] PSV
Returns TRUE if program space visibility (PSV) is enabled. If PSV is enabled, data in program memory ('const char *' or 'rom char *') can be assigned to a regular RAM pointer ('char *') and a regular RAM pointer can dereference data from program memory or RAM.


Unfortunately very poorly documented.... Sad
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