0
\$\begingroup\$

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.

Problem statement:
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.

\$\endgroup\$
3
  • \$\begingroup\$ Possibly due to memory writes that the compiler / linker did not recognise during compilation. This sounds like a classic 'smash the stack' issue where values on the stack are being over written. \$\endgroup\$ Commented May 30, 2019 at 7:55
  • \$\begingroup\$ Do you assign any function pointers non-atomically, where the assignment potentially gets interrupted by an ISR which uses that pointer? \$\endgroup\$
    – Rev
    Commented May 30, 2019 at 9:22
  • \$\begingroup\$ @Rev1.0: Thanks for the thought, but no, I haven't used a function pointers in this code yet. \$\endgroup\$
    – WedaPashi
    Commented May 30, 2019 at 9:32

1 Answer 1

1
\$\begingroup\$

(too long for a comment)

I think you ahev a memory problem, but it can either be using too much, or re-using something that should not be re-used in that way.

You say the RAM usage is 11%, but that can refer only to what is statically allocated, maybe including stack allocation, but it doesn't include dymanic memory (do you use malloc?) nor the actual stack use.

As a first step, determine the memory layout (DSEG, stack, heap) in both situations (with and without debugger). You might experiment with different layouts, to see how they influenece the problem.

If you use dynamic memory (heap, malloc() calls)you must get a grip on your heap use. How much memory is allocated to the heap, waht is your (peak) use?

Idem for stack use. What is your peak stack use?

Another approach is to remove parts of your application, intill you have identified the smallest code that shows the problem, and the line that, when removed, removes the problem. Very tedious, but sometimes the only option. Note that that one line might still not be the actual problem, but it sure gives you more info.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks, I appreciate such a response. Sorry, I should have posted that the application does not allocate any memory dynamically. (I'll append the next part in the OP) 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. \$\endgroup\$
    – WedaPashi
    Commented May 30, 2019 at 9:15
  • 2
    \$\begingroup\$ Determine what the memory maps are (how the memory is used) in debug and non-debug situation. \$\endgroup\$ Commented May 30, 2019 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.