I know this question is somewhat vague, but I'm stuck with this problem for all the day and I don't know what is happening.
I created a full program that works well on ATmega328. It uses the UART for a half-duplex master-slave RS485 bus (it's the master on the bus), some LEDS, some digital inputs, some analog inputs, some digital outputs...
Now I noticed I have some problems when the bus configuration changes. When more slaves are added to the bus, it seems the MCU doesn't communicate with one slave anymore.
I started investigate and I noticed I can make very small modifications on the source code, but the result is very different. For example, I can comment out a function call in the main task and the problem on the bus doesn't appear anymore. But the function isn't directly related to the bus stuff. Even if I disable compiler optimization, the problem disappears.
I know this kind of problem can be related to stack overflow, but the RAM usage is about 70%, so I don't think.
Interrupts? I disabled every interrupt, leaving only timer interrupt that I need for timing operations. Moreover, interrupt related problems should appear at random times, but in my case the behaviour is always the same.
I'm trying to reduce the code to a small test program that shows the problem, but it's very difficult: if I comment something, the problem disappears, but it doesn't depends on the code just eliminated.
I tried step-by-step debug, but in this case I can't reproduce the problem. Could the problem be related to timings on the bus?