View previous topic :: View next topic |
Author |
Message |
cypher
Joined: 05 Oct 2007 Posts: 31
|
Storing a large structure somewhere in memory |
Posted: Thu Sep 04, 2008 9:46 pm |
|
|
Hi Everyone,
I'm trying to store a 32 x 60 structure with 3 int elements somewhere on a PIC18F8722. It is not too important to me where this gets stored as long as I can access/(re)write to the contents of the structure from time to time.
For example, I want some values to be stored for each structure element like below:
Code: |
struct swvar{
int x;
int y;
int io_test_pin;
};
struct swvar swtest[32][60];
void main(void)
{
swtest[0][0].x = 35;
swtest[0][0].y = 67;
swtest[0][0].io_test_pin = 215;
}
|
This seems to work when I store a similar 2D structure but much smaller in RAM, but for a large one like this, I need to store it in program memory. I have tried the addressmod statement, and I had some issues with it which I posted here, but did not get many replies:
http://www.ccsinfo.com/forum/viewtopic.php?t=35884
Can someone please suggest another method of doing this, I simply just need to do something similar as the above code...
Thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Sep 04, 2008 10:18 pm |
|
|
Put it in an external eeprom. Create two access routines, to read or
write a structure to/from external eeprom. The parameters for the
access routines will be the two array indexes, and a pointer to a structure
in RAM. The access routines will handle converting the indexes into
an eeprom address. The data for one structure will be stored in 3 bytes
of eeprom. Or for quicker address calculation, you could store the
structures on 4-byte boundaries in eeprom. Note that it will take up
to 5 ms per byte to write a structure to eeprom, unless you use FRAM
from Ramtron. |
|
|
cypher
Joined: 05 Oct 2007 Posts: 31
|
|
Posted: Thu Sep 04, 2008 11:17 pm |
|
|
Thanks for the reply. I would have taken that approach as well however I do not have the flexibility to add an external eeprom right now....
Are there any code examples of storing 2D structures in program eeprom using the addressmod function? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Sep 05, 2008 5:07 pm |
|
|
I just spent a half hour trying to make addressmod work. I think it's
buggy or it has undocumented features that I don't understand. I don't
want to spend any more time on it. I suggest that you use access
routines to read/write your data to flash rom. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri Sep 05, 2008 5:25 pm |
|
|
Compiler manual page 41 has an example on using the addressmod modifier for writing to the internal EEPROM. Changing this example to use the internal Program Flash memory should be easy.
A few notes however:
- The Addressmod function is new and likely to contain bugs. It is the successor of the typemod modifier in the v3 compiler which had some problems and undocumented behaviour.
- Flash memory can only be erased a limited number of times. For the PIC18F8722 Flash has a minimum of 10,000 erase cycles.
- 10k erase cycles sounds like a lot but, for example, when writing once per second you have destroyed the chip in 3 hours.
- On the PIC18F8722 the smallest unit for erasing Flash memory is a row of 64 bytes. This means that for modifying a single byte you have to read the 64 bytes, erase the row in Flash, change the byte in RAM, write back the 4 bytes. This makes modifying a single byte slow and bad for your Flash; try to combine modifying multiple bytes in one call.
- Use of Addressmod feature could be dangerous as you loose control over how the data is written to Flash memory. For example when writing a structure to Flash this might be implemented as a series of byte oriented writes, so instead of writing once 60 bytes it becomes a series of 60 read/erase/write cycles and your Flash memory wears out much quicker than you anticipated.
For the above reasons I like dedicated write and read functions much better than an addressmod function with hidden functionality. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Sep 05, 2008 5:33 pm |
|
|
Quote: | Changing this example to use the internal Program Flash memory should be easy. |
That's what I thought. Why don't you do it with his structure example ?
just kidding.
Significantly, CCS has no examples of using addressmod in all their
current \Examples or \Drivers files. |
|
|
|