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 parameters in flash on PIC24FJ64GB106

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



Joined: 10 Oct 2011
Posts: 24

View user's profile Send private message

Storing parameters in flash on PIC24FJ64GB106
PostPosted: Thu Oct 13, 2011 10:49 am     Reply with quote

I have a project running on a PIC24FJGB106 and need to store some parameters in the flash program memory. I just can't see where I am going wrong on writing to and reading from the flash. It's either not getting properly stored or read back properly.

Here are the relevant pieces of the code:

Code:

#define MAXZONE         6
#define PARAM_ADDR      0x9800
#define WRITE_SIZE      getenv("FLASH_WRITE_SIZE")


// * ZONE CONTROL STRUCTURE ****************************************************
struct z_type {
   unsigned int32 dose_c;        // Dose counter for zone
   unsigned int32 on_t;          // On time (minutes, def=5 min)
   unsigned int32 off_t;         // Off time (minutes, def=115 min)
   unsigned int32 pk_off_t;      // Peak off time (minutes, def=115 min)
   unsigned int32 drain_t;       // Drain timer to keep zone / field valve open
   unsigned int32 flt_flsh_t;    // Filter flush time (seconds, def=20 sec)
   unsigned int32 flt_flsh_c;    // Filter flush counter (def=1 cycle)
   unsigned int32 flt_flsh_cyc;  // Cycle counter for flush valve
   unsigned int32 fld_flsh_t;    // Field flush time (minutes, def=1 min)
   unsigned int32 fld_flsh_c;    // Field flush counter (def=20 cycles)
   unsigned int32 fld_flsh_cyc;  // Cycle counter for field valve
};
// *****************************************************************************

static struct z_type zone[MAXZONE];

// * PARAMETER SAVE ROUTINE ****************************************************
void save_param(unsigned int32 addr) {
   disable_interrupts(INTR_GLOBAL);
   write_program_memory(addr, zone, WRITE_SIZE);
   enable_interrupts(INTR_GLOBAL);
}
// *****************************************************************************

// * PARAMETER LOAD ROUTINE ****************************************************
void load_param(unsigned int32 addr) {
   disable_interrupts(INTR_GLOBAL);
   read_program_memory(addr, zone, WRITE_SIZE);
   enable_interrupts(INTR_GLOBAL);   
}
// *****************************************************************************
Ttelmah



Joined: 11 Mar 2010
Posts: 19335

View user's profile Send private message

PostPosted: Thu Oct 13, 2011 2:52 pm     Reply with quote

The program memory, is written in pages. An erase only takes place, with the write function, if you write to the first address of the page. Now this gets fractionally difficult, since the write instruction works in bytes, while the page size is in instruction words (512words). The page size is 1536 bytes (0x600), and your 0x9800 address, does not divide evenly by this. You need to use 0x9600, or 0x9C00, which are the bank addresses either side.

Best Wishes
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Fri Oct 14, 2011 12:13 am     Reply with quote

Quote:
The page size is 1536 bytes (0x600), and your 0x9800 address, does not divide evenly by this. You need to use 0x9600, or 0x9C00, which are the bank addresses either side.

The assumption is wrong. I agree, that the PIC24 flash size calculation is confusing, if you're not familiar to it.

PIC24 flash memory addresses are word addresses (except for the *.hex file representation that uses byte addresses). Page base addresses are a multiple of 0x400. They span a memory size of 1024 words respectively 2048 bytes each. But only 3 of 4 bytes are populated, so you get a 1536 bytes of actual storage. The 512 dummy bytes also imply the fact, that you can't store continuous data structures to the flash without remapping it.

If you are interested in an effective storage of data in PIC24 flash memory, you should review AN1095 Emulating Data EEPROM for PIC18 and PIC24.
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