This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TMS320F28069M: Printf issues and stack overflow

Other Parts Discussed in Thread: TMS320F28069M, MOTORWARE, CONTROLSUITE

art Number: TMS320F28069M

Hello,

Sometimes when I use too many printf instructions, trying to run my code (from RAM) results in illegal ISR or the debugger halting in a memory location that doesn't belong to any source file (before even reaching the START symbol). I thought it may be caused by exceeding stack range - and indeed, increasing stack size usually helps. But methods of detecting stack overflow don't reveal anything wrong here!

1) First, I find the STACK_END size in memory browser when the problem occurs, but there is still plenty of empty space available (filled with 0xDEAD in linker file). Very often the whole .stack section seems to be untouched.

Does it mean I can be sure that the stack didn't get corrupted?

2) Next, I decided to implement a method described in Online Stack Overflow Detection (spra820) document. I implemented the exemplary source code so that no error is returned and then added the interrupt routine as simple as it can be:

extern interrupt void Stackmonitor_ovfISR(void)
{
    asm(" ESTOP0");
}

...which I linked to PIE in HAL_initIntVectorTable() function:

pie->RTOSINT = &Stackmonitor_ovfISR;

The funny thing is, after implementing this feature the problem changed: debugger properly stops at the START symbol and the vulnerable code executes. However, it halts at some other part of the code (after a few seconds of normal execution) with a note that ' No source available for "0x3ff4fa" '. I commented my changes trying to evoke the initial situation once again, but this time the code run properly - until it started to restart over and over again.

What may cause such behavior? Increasing heap size doesn't change anything, but increasing stack makes the program run alright.

3) To make sure that the interrupt triggers properly, I redu the stack size trying to enforce its overflow. But using any value below default 0x1000 results in the following problem: the debugger doesn't even start properly with the "suspend" button grayed out and most of the views staying blank (registers, memory browser, etc).

How can I test this functionality to make sure that the interrupt will trigger to save the stack?

4) And I have one more bonus question. In the memory browser I can see two symbols existing: "_stack" and "_STACK_END". But I cannot find them anywhere in my project (definitely not in a linker file)! They only exist in a map file under "global data symbols" (double underscore).

Where are they defined?

5) Some additional information:

- I am developing the code basing on one of the motorWare labs examples, currently in RAM version only.

- The stack size that I'm using is 0x1000 and heap is 0x400. Trying to solve the problems by changing my code (using less printf) instead of blindly increasing the stack each time they occur.

- The command linker file that I use is attached here (I have STACK_END_X defined to differentiate it from already existing STACK_END): https://ufile.io/kl6g5 

  • 1) It sounds like it's probably not stack corruption/overflow, but I wouldn't rule it out entirely.

    2) I believe 0x3ff4fa is somewhere in boot ROM. You could try loading the ROM symbols to see where specifically. The .lib and the source are found under libs\utilities\boot_rom\2806x in controlSUITE. It could give you a hint of why it ended up there.

    3) I can't check for certain at the moment because I don't have my hardware with me, but I believe in order to see anything in the Memory Browser, Registers window, etc. you need to select a core in the Debug window. You screenshot just shows the device session selected. Confirm that you can see them when you select the C28x.

    4) The symbol __stack is defined in the RTS library module boot28.obj. The source code can be found in a location similar to

    C:\ti\ccsv7\tools\compiler\ti-cgt-c2000_16.9.2.LTS\lib\src

    The key lines are:

    ****************************************************************************
    * Declare the stack. Size is determined by the linker option -stack. The *
    * default value is 1K words. *
    ****************************************************************************
    __stack: .usect ".stack",0

    The symbol __STACK_END is automatically defined by the linker when you use the usual options for linking C/C++ code, either --ram_model or --rom_model. Search for __STACK_END in the C28x assembly tools manual.

    5) Could you possibly share your .cmd file using the forum's attachment capabilities? If it complains about the file format, you can rename it to .txt. I'm just having trouble with the ufile.io link and our firewall. Seeing your .map file might be helpful too if you don't mind sharing it.

    Thanks,
    Whitney
  • Thank you for rapid answer,

    1) Why does the stack_size have such a big influence then? Increasing it (preferably twice) solves the problem and reducing it makes it fail every single time. It seems that the value of 0x1000 is on the edge. One more thing I noticed: when the program runs alright part of the stack is used and part is 0xDEAD. But when I compile the faulty version, stack is all empty (0xDEAD) even before the crash happens!

    2) I cannot evoke this behavior right now - today the program keeps restarting at the same situation (can see it by initial printf). Will update the post when I get this weird stop next time. But I guess it also stems from unplanned restart, how can I find the reason for it? Unfortunatelly all those effects are quite random, other times it would stop at PIE_illegalIsr()... 

    3) You're right, it's just that the debugger would select the core automatically during normal operation. After I select C28xx manually, I can see the windows and Disassembly content is set at the very first address:

    000000: 0040E284    LB           $XXXsw/drivers/cpu/src/32b/f28x/f2806x/CodeStartBranch.asm:95:106

    And here's what I see after suspending the program at a random moment:

    dafs

    Another try gave me:

    And yet another... guess I'll stop here:

    5) That was the issue, here are my files with *.txt extension:

    Linker command file: /cfs-file/__key/communityserver-discussions-components-files/171/8666.F28069F_5F00_ram_5F00_lnk.txt

    Map file (1) - Compilation resulting in this auto-restart issue: /cfs-file/__key/communityserver-discussions-components-files/171/1-Program-restarts_5F00_map.txt

    Map file (2) - Compilation used when the program doesn't even start: /cfs-file/__key/communityserver-discussions-components-files/171/2-Program-doesnt-start_5F00_map.txt

  • Your map files are showing the stack being placed above 0x10000, but the stack pointer is only a 16-bit pointer. Adjust your linker file to put the stack in one of the RAM sections with an address that's within its range. Hopefully that will take care of the weird behavior you're seeing.

    Whitney

  • Thank you, indeed that was the problem!

    I had .stack section allocated to RAML0-L8 memory range what was OK until my program grew big enough to push the stack above 0x10000 address. What I did now is: I changed RAML0-L8 memory range to RAML1-L8 and separated RAML0 specifically for stack (size set to 0x800 what should be plenty of space). Do you think it is a good solution, or is there anything special in RAML0 and I shouldn't change what was allocated there?

  • Glad that took care of it! Using RAML0 is fine.

    Whitney