1

I am working on one Embedded C project. The main problem with Embedded System is limited memory. I am running in to one issue where I need to take 32 Byte of data from a serial communication link using interrupt and then save this data to EEPROM.

The time to write one byte in EEPROM requires 4ms time. In order to save my memory, I can write the data as I receive it from the serial interface (inside ISR). But is is not a viable solution.

Another method I can use is to use one structure containing 32 byte buffer :

struct example_struct
{
unsigned char buffer[32];
};

allocate it 32 byte of memory, use it to save my data in ISR and after writing the EEPROM free the memory. Unfortunately, I do not have this liberty in my C compiler. The development environment does not support malloc and free.

To be specific about the problem , I want to optimize the memory use. Instead of using global buffer to hold data what could be the possible way of transfer data in between ISR and EEPROM write function?

The RAM available for the processing is limited to 368 Bytes.

malloc in embedded systems; Dynamic memory allocation in embedded C

Please advice

Thank you

Community
  • 1
  • 1
Kunal Sonone
  • 43
  • 3
  • 14

4 Answers4

3

Since malloc and free are missing, the concept of "freeing" memory becomes simply "reusing the same statically allocated block of memory for different purpose".

To that end, you could reuse a block of memory by allocating it inside a global union, as long as you are careful not to use it for multiple purposes at once.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

I had a similar issue once.

My solution was to split my available memory in two buffers, while the serial ISR writes to buffer A, you write buffer B to the EEPROM (in a lower priority software interruption, if available, or in the main loop).

After filling A you swap the buffers and start writing A to the EEPROM.


You should also tell us which is your compiler. Ie, for baremetal gcc you can provide your implementation of malloc() and free().

xvan
  • 4,554
  • 1
  • 22
  • 37
1

If you have no malloc and free, you should just reuse memory

I think using an union could help you do that

It will allow you to reuse the same variable as other types

for example you could use char[8] variable and use it as int[2] (if the size of int is 4 bytes on your system)

while using the same memory

Arnaud Aliès
  • 1,079
  • 13
  • 26
  • I don't see the need for unions as this is an 8 bit architecture, so alignment shouldn't matter. He could just define a char[N] then assign segments of that block to his structures. – xvan Mar 11 '16 at 15:33
1

You could implement a ring buffer. Allocate an array of two or more blocks of 32 bytes. Have a read and write index; the write index points to the block that the ISR will write next, and the read index points to the block that will next be written to EEPROM by some non-interrupt-level task. The write index will only be incremented by the ISR and the read index will only be incremented by the task. How do I implement a circular list (ring buffer) in C?

Community
  • 1
  • 1
Vercingatorix
  • 1,838
  • 1
  • 13
  • 22
  • I can do that it if I have 64 byte free SRAM available to use. I am running out of memory because of already built functions. The only way I can incorporate my new function if I can reduce this 32 Byte usage in my program or use and release these blocks after ISR and EEPROM writing is done. – Kunal Sonone Mar 11 '16 at 15:44
  • 1
    Good one. The buffer should be of 2's power size as modulo arithmetic is expensive. – xvan Mar 11 '16 at 15:44