2022-06-22 10:54 PM
Hi,
I'm looking for a way to store data directly in SRAM.
You may ask "Why?":
So, my system is collecting data from sensors and writing them to SD card. One full block of data is 128 bytes long. Collecting data and write operation happens when certain trigger happens. I don't want to f_write them every single time, because it simply takes too much time.
How I want to fix my problem:
Performing a f_write operation for bigger chunk of data, like 512 bytes or 1K bytes or even more. I tested my SD card and I could get better performance If I would write more bytes at one time. So basically, my problem comes to storing "a few" blocks of my data (one block = 128 bytes) and then flushing all of them to SD card directly.
How I want to do that?
I would like to store this data in SRAM, since I have 192 Kbytes of space.
I was thinking about using DMA Memory-To-Memory. I even found an example on how to do that: https://electronics.stackexchange.com/questions/543555/memory-to-memory-dma-on-stm32
But, I'm not sure if this is what I'm looking for. I dont want to copy one array to another. I would like to create (for example) an array which could contain 4,8,12,... blocks of my data and then refer to it when writing to SD card, but the array would be placed in SRAM so it wouldn't take space from my stack.
Please help me If my direction of thinking is right, or show me the way, or what tools to look for. I know what I want to use, but I'm not sure how to do that.
I'm using STM32F407.
Thanks!
Edit:
After testing my SD card I decided that I would like to write block of data of size 16KB at one f_write operation.
2022-06-22 11:19 PM
If you use SM32CubeIDE, check the Build Analyzer to get an idea of memory use and the Static Stack Analyzer to get an idea of how the stack will be used.
Global variables in C share the SRAM peacefully with stack and heap, as long as they all fit in.
Why not simply using an array of data blocks as a global variable, or 2 arrays for a double buffering scheme?
hth
KnarfB
2022-06-22 11:26 PM
Are you sure that if I declare and use an array like that:
uint8_t array[4096]; which has size of 4Kbytes i wont overflow the stack? ;)
2022-06-22 11:38 PM
variables decalred at external level as well as vars declared with static keyword inside routines have nothing to do with the stack, and after compilation/linking you get memory usage status info - just read it.
2022-06-23 01:44 AM
So, where is the memory for this variables allocated?
If I decide to create an array of size 16KB will I be okay? That seems bizzare
2022-06-23 12:36 PM
That's pretty obvious - .bss section if not-/zero-initialized, .data if non-zero.
2022-06-23 09:13 PM
Well, if I'm asking this question it's not that pretty obvious to me. I'm not very experienced, I'm still learning a lot of things and memory managment is one of the newest things to me.
2022-06-23 09:44 PM
But, you pointed me a direction. My way of thinking was wrong, i dont know why i was looking for some magical way to store this data by myself.
So i initialized a variable:
uint8_t Data[16384];
It went to .bss section. Now for my whole program RAM usage is 38,46%. Is this too much? Is there limited space for .bss section? Is this a safe way to store 16KB data like that?
2022-06-23 09:45 PM
Only auto/local variable get stored on the stack.
If you have this as a global, it will get a section of RAM, see the .MAP file for where the linker puts it.
uint8_t array[16384];
Or allocate from the heap, need to ensure it's large enough, otherwise it will return NULL (zero)
uint8_t *buffer;
main()
{
buffer = malloc(16384);
...
}
2022-06-23 09:51 PM
Thanks! If you could see my answer to @gbm, i still have a few questions.
I was fixiated in this weird way of thinking that you should initialize too big arrays and variables, dont really know where it came from :).