0
\$\begingroup\$

Is it possible to store 8 bit character to 32 bit Internal Data Flash in the Microcontroller by just using only 8 bit of space for each bytes?,by using a combination of 4 bytes which can be also recovered as individual bytes later. I'm using Nuc240 (ARM-M0) Microcontoller with 32 bit Data Flash and 512 bytes erase limit,read write limit is 32 bit.
Please explain a code template if possible.

\$\endgroup\$
  • \$\begingroup\$ Do you mean store them there in run-time or at link-time? \$\endgroup\$ – Lundin Dec 12 '16 at 7:16
  • \$\begingroup\$ During Link-time \$\endgroup\$ – Arun Joe Cheriyan Dec 12 '16 at 7:24
2
\$\begingroup\$

Yes, by either array or structure.

char memory [4] = { 'a', 'b', 'c', 'd' };  // Uses 4-bytes (32-bits)

// memory [0] = 'a';
// memory [1] = 'b';
// memory [2] = 'c';
// memory [3] = 'd';

// ... store 'memory' in internal flash memory ...

Or

typedef struct
{
    char byte_0;
    char byte_1;
    char byte_2;
    char byte_3;
} memory_t;

memory_t memory; // Uses 4-bytes (32-bits );

memory.byte_0 = 'a';
memory.byte_1 = 'b';
memory.byte_2 = 'c';
memory.byte_3 = 'd';

// ... store 'memory' in internal flash memory ...
\$\endgroup\$
  • 1
    \$\begingroup\$ I think I can store the array you defined at first directly into the memory by using the array name , and also read back with an array of 4 character size; am I right ? Thank You. \$\endgroup\$ – Arun Joe Cheriyan Dec 11 '16 at 20:16
  • 1
    \$\begingroup\$ The OP apparently wondered how to store it in flash at link-time. This seems to be an example of how to store it there in run-time, minus the whole flash driver. \$\endgroup\$ – Lundin Dec 12 '16 at 7:42
0
\$\begingroup\$

The standard way of storing data in flash at link-time when C programming, is to declare the variable as const.

const uint8_t array[4] = {0, 1, 2, 3};

Most compilers will then know to place this array in flash instead of RAM, but only if the variable also has static storage duration. Meaning that the above array must either be declared at file scope, or as static. See this for details.

Compilers also have non-standard syntax to place an array in a specific part of flash, which is what you have to use if the above won't work on your specific compiler. It can also be useful when for example dealing with eeprom/data flash.

\$\endgroup\$
  • \$\begingroup\$ what if I have an array of 200 characters. 'char Room[200]' . which I need to store at data flash during run time. How can I do this? [ by using multiple of 4 -1 array index to each memory location ?] \$\endgroup\$ – Arun Joe Cheriyan Dec 12 '16 at 8:00
  • \$\begingroup\$ @ArunCheriyan Then you have to write/use a flash programming driver for the specific MCU. \$\endgroup\$ – Lundin Dec 12 '16 at 8:22
  • \$\begingroup\$ Why the down vote? I posted this after explicitly prompting the OP about what they wanted. \$\endgroup\$ – Lundin Dec 12 '16 at 8:39
  • \$\begingroup\$ I did not do it, anyways I 've given an upvote \$\endgroup\$ – Arun Joe Cheriyan Dec 12 '16 at 8:49
  • \$\begingroup\$ Note: Not all compilers will put a const variable in flash. \$\endgroup\$ – m.Alin Dec 12 '16 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.