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 support@ccsinfo.com

Passing address of rom array into a function.

 
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: 449
Location: Des Moines, Iowa, USA

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

Passing address of rom array into a function.
PostPosted: Tue Feb 04, 2020 9:41 am     Reply with quote

Based on other threads in this forum dealing with strings, I think this is the way to do it... Am I close? I wasn't able to printf() the ptr[x] value as a number directly, so maybe there's a trick with that.

Code:
#include <main.h>
#include <stddef.h> // NULL

// Store this in ROM.
rom int8 data1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

rom int8 data2[] = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };

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");
   }
}

void main()
{
   // Call function.
   function (data1, 10);

   function (data2, 10);

   while(TRUE)
   {
      //TODO: User Code
   }
}

// End of main.c

_________________
Allen C. Huffman, Sub-Etha Software (est. 1990) www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC programmer.
www.whywouldyouwanttodothat.com ?
allenhuffman



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

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

PostPosted: Tue Feb 04, 2020 10:45 am     Reply with quote

We already ran into a snag. How do I move the ROM array to its own file, and "extern" it? I tried variations of "extern", "rom extern", "extern rom", etc.

If we have something like this...

romarrays.c:
Code:
#include <main.h>

// Store this in ROM.
rom int8 data1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

rom int8 data2[] = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };

// End of romarrays.c


...what lets us access that in other files?

I tried this, and while it compiles, it doesn't work.
Code:
extern rom int8 data1[];
extern rom int8 data2[];

_________________
Allen C. Huffman, Sub-Etha Software (est. 1990) www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC programmer.
www.whywouldyouwanttodothat.com ?
ralph79



Joined: 29 Aug 2007
Posts: 85

View user's profile Send private message

PostPosted: Tue Feb 04, 2020 11:15 am     Reply with quote

My suggestion IMHO (based in some work already made and situations I had to change according to CCS versions) is:
Declare the const variables directly in the main.h file (putting these arrays as globals) and acessing it from other files.
allenhuffman



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

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

PostPosted: Tue Feb 04, 2020 11:24 am     Reply with quote

Ah, CCS reports I should try "const" right now as there are some issues with "rom" they are working on.

I still had no luck with an extern of a const:

extern const int8 data1[];
extern const int8 data2[];
_________________
Allen C. Huffman, Sub-Etha Software (est. 1990) www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC programmer.
www.whywouldyouwanttodothat.com ?
allenhuffman



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

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

PostPosted: Tue Feb 04, 2020 11:25 am     Reply with quote

ralph79 wrote:
my suggestion IMHO (based in some work already made and situations I had to change according to CCS versions) is:
declare the const variables directly in the main.h file (putting these arrays as globals) and acessing it from other files...


Yes, doing a #include of the .c or .h file appears to work. All roads always seem to lead back to "put everything in one file using include" to make it go Smile
_________________
Allen C. Huffman, Sub-Etha Software (est. 1990) www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC programmer.
www.whywouldyouwanttodothat.com ?
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