This might appear to be too much data way too scattered to understand, but I'll make a genuine effort to explain.
Controller: R5F111MJ (ROM 256KB, RAM 16KB), RL-78/L1C family. Datasheet can be found here.
IDE: CS+ V7.00.00
Compiler: CC-RL V1.07.00
We have designed a custom hardware with references from an eval kit. The team that designed the hardware is well-versed with the design approach as they have been doing this for decades.
I had been developing the firmware for a couple of weeks and the board was powered up using the debugger all the while. When I tested the same functionality with debugger disconnected and board used to powered up using the power supply, it would reset randomly. Note that the reset issue got resolved with something that I tried (explained below). My question is not how to resolve the issue but why it solved the issue?
What have I tried:
- Ensured that the internal watchdog is not causing it, it has been disabled temporarily
- We have verified the hardware and power rails, they seems to be okay and voltage level never drop below 3V3 through the reset
- System clock is 24 MHz I have tried reducing it to 1 MHz, the issue remains
- I have added a debug data (UART) for knowing the cause. At bootup I read the registers that hold the values that indicate the cause of reset (RESF and PORSR registers). They always remain 0 as if the reset wasn't due to low power detection or invalid memory access.
- There is a 'Memory Mapping Profiler' which lets developers know the amount of memory consumed so far. The code as of now consumers 10 % of the total flash and 11% of RAM. There is no memory allocated dynamically (i.e. Heap usage is 0).
- I've tried debugging with some fragments in the code disabled. If there is nothing after the initialization and just a simple
while(1), the issue disappears. I enabled some part of the the code (based of functionality/module), the system resets when about 80% of functionality is included, but then it resets at random places. That made me take a guess that its related to stack rather than a stupid logical error in code.
Now, I scorched through the user manual of IDE and understood that the toolchain (probably a utility with linker) does the stack allocation. And the total stack allocated is equal to 'total RAM available on controller' minus 'whatever is used by user through code'. Somehow I didn't comprehend this. So I went on to manually override the stack allocation in the .asm file and set the stack size to 8192 Bytes. And, the issue disappeared. Like I said, my question is not how to resolve the issue but is more about why did it resolve the issue.