0

My intention is to read a file (of max. 128 KB) from an SD card and store it in (external SRAM). I will use an Arduino Mega which has 8 KB of internal SRAM but even 8 KB is not really much for some processing and keeping some administrative instances of classes always available. I don't need SD access after the copy process anymore.

Would it be a crazy idea to change the SD library buffer instead of using a global or internal buffer in the SD library, instead to pass a local variable which is removed automatically after the copy function ends?

So in other words, I create the SD buffer as local variable, I call the SD library using instead the buffer I created, and afterwards the local variable (buffer) will be removed, saving me 512 bytes.

Of course I will only do this when getting into SRAM problems, but I think there is a chance to get into this.

Update:

The reason I want this. In my project (at least initially), I want to process (fast) MIDI messages, that means receiving MIDI messages, process them and send MIDI messages. Since this needs to be done in about 10-20 ms (between receiving/sending), performance is important.

The configuration of the processing can be completely configured (via an external program). The configuration file will be written to a (micro) SD card. The typical size of such a file will be between 1 and 128 KB. Since it will be to slow for every incoming MIDI message to read the SD card, the content of the SD card (max 128 KB) will be copied to SRAM.

So after this 'copy' I do not need the SD card (reader) anymore, so the 512 byte buffer is never used anymore.

(maybe in the future I will make some way to alter the configuration on the Arduino, with an LCD/keypad, but even than the saving of a configuration file will be a very occasional task where I can make the 512 byte buffer temporarily).

During processing I might need more (internal) SRAM. SInce I expect my application will contain quite a bit of classes/functions so the stack can grow; also I need to store intermediate result. Of course I also can store this in the external SRAM, but it will be much less comfortable.

  • 3
    Realistic == possible, yes why not. Difficult, no not really if you understand how the buffer is used. But in general this question has an xy-problem aura over it. – Mikael Patel Mar 30 '17 at 16:04
  • You mean with xy-problem that it causes other problems to be fixed? – Michel Keijzers Mar 30 '17 at 22:01
  • 1
    xyproblem.info – Mikael Patel Mar 31 '17 at 5:30
  • 1
    The SD library should have a SD.end function to release the memory. The 512 kbyte as local buffer on the stack is possible. Filling 8kbyte with classes is very hard. Do you have a lot of large buffers inside them ? Perhaps you have to rewrite that code. Do you use the F() macro ? and PROGMEM ? and functions like strcpy_P and so on ? – Jot Mar 31 '17 at 8:08
  • 1
    A few KB on various structures seems a lot. I suggest to go ahead, and if you can't optimize the structures and have put as much data as possible in flash memory, then you can scale up to Arduino Due or Arduino Zero or Arduino M0. Changing standard libraries can get you into trouble later. The PROGMEM, F(), PSTR(), strcpy_P, sprintf_P, and so on do all the same thing, they can use data in flash memory. – Jot Mar 31 '17 at 10:10

Your Answer

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

Browse other questions tagged or ask your own question.