Jump to content

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 :

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.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...