Atlantis wrote:
Non-working code was identical with the one I use in my "TalkingClock" project. This is why I also posted link to that repository. The same library used the same way to call the same functions that are executed now by the workaround code.
But for some reason this code works perfectly fine in one project, but breaks stability of the other.
The issue is that it works in your TalkingClock project, so looking at your TalkingClock project isn't really instructive.
In C, the compiler generally arranges things in memory in the exact order they are declared in the C code (with some distinctions made between initialized variables and uninitialized variables, which are often stored in separate sections). If you have some piece of code that is accidentally writing beyond it's reserved memory, it will overwrite whatever comes next in memory, which might be your "key_t" structures. If you rearrange the order the variables are declared, then it might overwrite other variables that are not used at that time and go unnoticed (therefore it "appears" to work fine).
This means that, for troubleshooting, the exact order and types of variables declared matters. I would be looking at variables above your key_t structures that might accidentally write too much, like the buffer and the other variables declared just above your structures.
Ideally, I'd compile your non-working code and then investigate where the compiler actually put things by looking in the listing or map file (which you can ask the compiler to make for you). I also might try to run it in a simulator with a breakpoint on write to the memory locations of the key_t structures to try to capture the rogue writes and see where they come from (but your project has enough hardware dependencies that it might not be feasible to do that). I would need the misbehaving code, in a compilable format, to do any of that. It's very important when asking for help on a bug to give a method to reproduce the bug (ideally a "simplest working example", which might have a lot of your other libraries/code removed if the bug still shows up without them). If you can work the problem down to a simplest-working-example of the bug, that also helps narrow down the problem significantly, and you may even discover the root cause of the issue while trying to narrow things down.
Because you are using github, you can make a branch without disturbing your working code and try to build a minimum working example of your problem. Removing code for hardware that is not related to the problem also makes the code easier to simulate. Once you have a good example of the bug in action, you can push your branch to github and then post the link to your branch for people to look at.