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.
Asked
Active
Viewed 1,304 times
0

Arun Joe Cheriyan
- 707
- 2
- 8
- 22
-
Do you mean store them there in run-time or at link-time? – Lundin Dec 12 '16 at 07:16
-
During Link-time – Arun Joe Cheriyan Dec 12 '16 at 07:24
2 Answers
2
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 ...

BufferOverflow
- 677
- 1
- 8
- 15
-
1I 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. – Arun Joe Cheriyan Dec 11 '16 at 20:16
-
1The 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. – Lundin Dec 12 '16 at 07:42
0
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.
-
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 ?] – Arun Joe Cheriyan Dec 12 '16 at 08:00
-
@ArunCheriyan Then you have to write/use a flash programming driver for the specific MCU. – Lundin Dec 12 '16 at 08:22
-
Why the down vote? I posted this after explicitly prompting the OP about what they wanted. – Lundin Dec 12 '16 at 08:39
-
-
-
@m.Alin Ah now I see what was missing. Yes all compilers will actually do this (all I have ever used anyhow) but _only_ if the variable also has static storage duration. I thought that went without saying, but now realized it might not be obvious at all. Answer updated with clarification. – Lundin Dec 12 '16 at 10:40
-
(BTW, I haven't down-voted you) I don't agree with you when you say: "Yes all compilers will actually do this (all I have ever used anyhow) but only if the variable also has static storage duration". If I remember correctly, some compilers won't put a static cost in flash without an additional special keyword. C18 required the 'rom' keyword. Avr-gcc requires the 'PROGMEM' macro to put variables in Flash. These are the ones I'm aware of, but I'm sure there are others out there that require a special keyword to put a variable in Flash (regardless of the use of 'static'). – m.Alin Dec 12 '16 at 10:49
-
The IAR compiler also seems to be using the '__flash' keyword (at least for the AVR MCU's).. – m.Alin Dec 12 '16 at 10:52
-
@m.Alin Well, AVR is weird I guess. Funny how gcc is inconsistent across different platform ports. – Lundin Dec 12 '16 at 10:56