Jump to content
Sign in to follow this  
tempest690

Storing IR codes in flash memory

Recommended Posts

Hi, i'm trying to make IR controlled relay board with programmable button to change IR remote codes, The IRremote library returning decoded infrared values in unsigned long type, when i try to write raw code in flash memory (energia mspflash.h library ) i get result similar to :

Share this post


Link to post
Share on other sites

First off, make sure you execute an erase() before the first time writing; flash memory can only alter bits to one direction (most can only flip bits from 1 to 0) and use an erase command to reset all those bits to the default value.

As you can read in the MSP430x2xx User's Guide (slau144 pdf document) the MSP splits memory in segments, segment A is reserved mostly for calibration values, segment B, C and D are free for the application's use. The smaller controllers only have segment 0, larger ones have also segments 1, 2, 3, etc.

The segments with a letter are the data segments, while the numbered segments are application segments. This is only by convention and can be broken at will. Don't do so unless you're very sure what you're doing, or unexpected stuff may happen.

After this you use the write() instruction from the mspflash.h to write your data. The prototype of this function is

void MspFlashClass::write(unsigned char *flash, unsigned char *src, int len);
so it expects a pointer to the location to write data first, then a pointer to a region to copy into the flash location and finally the number of bytes to copy.
Let's assume you want to write 20 long integers (32 bits) to the flash, you call it like this
const int NUM_IR_CODES = 20;
unsigned long IR_codes[NUM_IR_CODES] = {/* your longs here */};
MSPFlashClass store_IR;

store_IR.write(IR_FLASH_LOCATION, static_cast<unsigned char*>(IR_codes), sizeof(*IR_codes) * NUM_IR_CODES);

Instead of static_cast<unsigned char*>(IR_codes) one could write (unsigned char*)IR_codes, but this is the C way and less verbose. Try to use C++ casting wherever possible.

sizeof(*IR_codes) will result in the number of bytes in an element of IR_codes. Since IR_codes is an array of unsigned long, the size of an element is the size of an unsigned long. In almost all cases this is 4, but you should not rely upon the ratio between integer type sizes. Since sizeof() is not a function, but a language construct, the overhead is zero.

By multiplying the latter value by NUM_IR_CODES (the same constant used for the array definition) the integer passed as third argument to write() is equal to the size of structure passed as second element in bytes.

Since I don't know your application (and don't know where you store your data) I used a symbol named IR_FLASH_LOCATION, which is for you to fill in.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×