Hello Everyone,
I have a project I'm rushing to finish, but I've run into a weird issue I can't seem to crack. I have an 8-Bit R2R DAC connected to an ATMEGA32u4, and a 16MB Adesto SPI Flash Memory IC. Reading and writing just fine, and I have most of my code done. Originally, I had split it into two pieces of code, one which uploads stuff to the memory IC, through the serial port, and another which plays back the data in my application. From testing, I found that SPIMemory took around 52-55microseconds to do a single read. Here is the code loop in question to hit the DAC:
for (uint32_t i=0;i<leng;i++) {
dacdat=flash.readByte((saddr*4096)+i); //start sector addr
playdat();
delayMicroseconds(1); //seems roughly correct for 16KHz
} //16KHz = 62.5microsec period. readByte takes 52-55microsecs in testing
That's it, I just read a byte and play it. I do have the OneButton library running, but I suspect it does nothing unless you .tick() on it, so no other timers or anything to interfere. Been playing back audio for days with this loop.
As I was wrapping it up, I decide I have more than enough room to combine the two pieces of code, they already shared a lot of stuff anyway. I have a serial menu, which I just keep adding menu options to, then moving on. At one point however, I tested, and the DAC started playing slower. Not sure by how much, maybe 15-20 microseconds slower, but consistently slower. I dug for an hour, and I couldn't find anything wrong, or that should be stomping on this super simple loop. However, if I just simply start commenting stuff out from the serial menu, it will work again when the code size takes less than 50% of available program space. It has nothing to do with RAM as I actually have more free now than ever before, as I added F() to all of my serial stuff, had forgotten to do so, and was down to 398 bytes, and it was working fine. Now, I have 1573 bytes free?
All of the functions work, as I've been testing them as I go, and I can totally just comment them out of the code. (Mind you, it's not even trying to run these guys, unless I connect serial.) So I figure, some voodoo in the stack? I started getting rid of nested function calls, and all sorts of things, but I know all of the code is working either here, or in the other app, so it really didn't add up that any changes would be needed.
My backup plan is just to split the apps back into two, and try to forget abour it. However, I really hate not knowing why this is happening and I really do want the combined functions now. I'd be happy to share the code, but it's over 600 lines, and would take a lot of explaining.
Just really wondering if there is anything I can do to isolate this issue? I can imagine for some reason(s) the compilier decided it needed to preserve more data because of the mere existance of a certain function or functions? Each of these functions are very simple and single-purpose, so I really am not sure what to do.
Needless to say, it's not worth a crap if the DAC is going too slow.
Thanks!
-Kevin