Hello guys! The thing is: SPI still works even during an ISR?
I need to know this for my memory dumper function:
// Dumps the whole SRAM into a file on a SD card
void dumpMemory() { // This is the ISR
if (sd.exists("MemDump.bin")) {
sd.remove("MemDump.bin");
}
File dump = sd.open("MemDump.bin", FILE_WRITE);
for (unsigned int memPointer = 0; memPointer < 2048; memPointer++) { // 2048 == SRAM size of the ATmega 328P
dump.write(*memPointer);
}
dump.close();
}
// "sd" is lower-cased bacause I'm using the "SdFat" library instead of the "SD" one.
I've read the article about the SPI library's function usingInterrupt, but I have not clear how to use it. I mean, what is an "interrupt number"?
Is that function mandatory to make SPI work during an ISR?
Thank you beforehand!
PD: am I using the memPointer variable correctly? The idea is to read every single byte of the RAM.
The SD SPI is not going to work in the interrupt routine because interrupts are off and SD needs them to be on.
What are you trying to do? What generates the interrupt which calls the dumpMemory function?
Pete
el_supremo:
The SD SPI is not going to work in the interrupt routine because interrupts are off and SD needs them to be on.
If not, I hope that doesn't mess up the dumping process.
What are you trying to do?
Trying to "pause" the execution of the main program. I guess this avoids an altered "snapshot" of the RAM... right?
What generates the interrupt which calls the dumpMemory function?
An external interrupt attached to the pin 2:
attachInterrupt(digitalPinToInterrupt(dumpTrigger), dumpMemory, FALLING);
// dumpTrigger == 2
// FALLING because pin 2 is configured is an input with internal pull-up resistor enabled.
Your interrupt routine should only set a global flag (declared as volatile char) which is continuously tested in the loop() function. When the flag is set, you can call the dumpMemory function and then reset the flag.
Trying to "pause" the execution of the main program
What is the "main program" doing?
Pete
el_supremo:
Your interrupt routine should only set a global flag (declared as volatile char)
Or, a volatile boolean?
What is the "main program" doing?
The complete sketch might be too large to check out (+320 lines); but, in a nutshell, it plays WAV files from the SD card (in a MP3-player-fashion).
I'm using the TMRpcm library to accomplish this.
A boolean will work too. All you have to do is arrange that the interrupt flag is tested frequently enough.
I don't see why you'd want to dump ram in that sort of application but be aware that the SD library uses 512 bytes as a buffer. This reduces the amount of ram available to the "main" program and will be included in the memory dump which, if nothing else, won't contain useful info. But then, you don't really have control over where that buffer will be in ram.
Pete
el_supremo:
I don't see why you'd want to dump ram in that sort of application
Simple: curiosity
. I want to see how data is arranged (specially to see where, the strings and the audio buffer, are located), and how "random" this arrangement gets over time and after every restart (reset or re-power up).
My only concern about this, is that the RAM's content may change during the dumping process (altered "snapshot"). This is why I tried to use the function as the ISR.
By the way, what exactly does usingInterrupt in the SPI library then?
I still have this quiestion 
It is something to with SPI transactions, which I haven't used (directly). It tells SPI transactions that you will be using SPI from within an interrupt and specifies which interrupt number it will be. But this does not mean that you can use SD/SdFat within an ISR.
Pete