allenhuffman
Joined: 17 Jun 2019 Posts: 661 Location: Des Moines, Iowa, USA
|
| Data table in rom (const) to save RAM |
Posted: Mon May 11, 2026 3:22 pm |
|
|
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. |
|