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

Storing large array in program memory

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







Storing large array in program memory
PostPosted: Wed Mar 18, 2009 7:06 pm     Reply with quote

Hi,

I'm having some trouble storing a very large 1-D array in program memory and not in SRAM. I want to do this so even if the PIC resets, I have access not my array contents. I want the data to be non-volatile and therefore wish to store it in the flash for program memory.

I have done something like this in the past for storing look-up tables and it worked with the const int array[]; method. However, now I wish to overwrite the contents of the array from time to time and still remain non-volatile.

Can I do this in anyway possible? I realized I cannot use the const declaration of the array. This method does not let me over-write the contents, it keep them fixed, which is why it works quite well if it's only a look-up table.

Any suggestions are highly appreciated...

Regards
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Mar 19, 2009 12:39 am     Reply with quote

Generally, it's possible, but there are some points to consider:

1. Limited flash endurance. Microchip specifies minimum cell endurance of 10K write cycles. Depends on your understanding of overwrite the contents of the array from time to time if it's feasible. As my personal criterion, everything that's performed on explicite user interaction should be O.K., automatic actions only if they are timed appropriately (> once a week).

2. Functional limitations. Flash memory blocks have a FLASH_ERASE_SIZE of e.g. 64 bytes with common PIC18 devices. Thus individual variable writes require an additional translation layer, complete array writes are straightforward.

3. Hardware access to flash write and software support. Read the Microchip device's datasheet paragraph Writing to Flash Program Memory and the CCS C description of write_program_memory() and erase_program_memory() for details.

4. Application level aspects. Obviously, a const array in flash must be read only. Write accesses have to go through a different interface.

As an example, with PIC24F devices, EEPROM has to be emulated in program memory. Although a translation layer is required to allow word writes and to minimize the number of erase cycles, it's a smart solution and apparently working in a reliable way.
nverma
Guest







PostPosted: Thu Mar 19, 2009 12:30 pm     Reply with quote

Thanks for the reply, very helpful information...

Yes i'm keeping points 1 and 2 in mind.

Yes, I did look at the CCS documentation and have come across the write_program_eeprom() and erase_program_eeprom() functions.

However, I do have a concern by just (re)writing specific bytes to a explicit address in memory. I want to store 40,000 bytes, therefore what I am thinking is I start from 0x1FFFF and go back 40,000 addresses and I use this space in program eeprom for my log buffer. But isn't that a bit risky, how do I explicitly tell the compiler to reserve this 40,000 address buffer for me? How do I know it will not over-write any of the buffer with opcode for program code that I develop in the future?

After reading back the program memory, I see that the CCS compiler start at 0x00000 and incrementally starts writing opcode, that's why I have chosen to implement my log buffer at the end of memory. Do you think this is a safe way to 'emulate' an EEPROM in program memory?
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Mar 19, 2009 1:12 pm     Reply with quote

The manual clarifies how an #org preprocessor statement can be used to place constant data to a
defined memory location. The compiler doesn't overwrite flash regions reserved by #org.
nverma
Guest







PostPosted: Thu Mar 19, 2009 3:50 pm     Reply with quote

Thank you, ok that makes a lot of sense....

Now i'm trying to write a byte to an address using the write_program_memory() function using the following code:

Code:

int test = 0xAA;
write_program_memory(0x1FFF0, test, 1);


Here, i'm trying to store 0xAA at the 0x1FFF0 address, however when I read the program memory after running the program, I see 0x00 at the 0x1FFF0 address. So it seems as if i'm clearing the value at that address. Why does it not store 0xAA at the address? What am I doing wrong?
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu Mar 19, 2009 6:25 pm     Reply with quote

The second parameter must be an address pointer. Change 'test' to '&test'.

Similar, on reading the data from memory you have to provide a pointer to a buffer for storing the data:
Code:
int Buffer[10];
read_program_memory(0x1FFF0, Buffer, 1);
printf("Value = %d\n", Buffer[0]);
nverma
Guest







PostPosted: Fri Mar 20, 2009 11:25 am     Reply with quote

Thank you for your help....I am now able to write & read from the program memory.
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