Page 1 of 2
Looking for hints how to debug
Posted: Fri Jul 13, 2012 8:30 am
by rubenswerk
Hello,
I'd like to ask you about your opinion how to continue the debugging of my project.
I have three identical boards, equipped with STM32F4 and LAN, all of them running the same software based on an older trunk (4090) of the lwip demo. Two of the boards are working as expected since >60 hours. One board fails after a random period of time (5...30min). It is always the same board that fails. No problem with the other two boards. Exchanging the power supply, network cable or MAC adress with the other boards makes no difference.
Sometimes the software ends up in a Hardfault exception, sometimes the blinker thread is still alive, but Ethernet input routine isn't called any more.
With the debugger I can see that stack consumption is quite low, so this shouldn't be a problem. Nevertheless I increased stacks, but it makes no difference.
With a breakpoint in the hardfault handler I can see that the backtrace on the stack is invalid. It shows the exception handler, and the adress before is 0xfffffffc. How can that be - I would expect that there's a valid adress pointing to some piece of code.
Once I ran into the exception handler, the stackpointer was 0x20000400, which is the absolute top of the main stack. Is this plausible? Shouldn't there be at least the adress of the main function itself, so that SP is always <=0x200003FC?
Another time I got the excpetion, SP pointet to 0x200007F0, which is close to the top of the process stack.
I see several options:
1.
Hardware - check power supply, reset and boot pins, what else...? I did a quick check, looked good, same as on the other boards.
2.
Update operating system to the latest trunk. I saw that there was a configuration bug with the MAC clock, but I merged the fix into my trunk when Giovanni reported about it. I hope I didn't miss anything.
3.
SW debugging. What could help me to find the reason for the exception? Without a valid caller tree, it's not so easy. I could instrument the software with a manual 'call tree', writing to a ring buffer whenever calling a function. I don't know any other way how to find out the root cause.
What's your recommendation how to proceed the debugging?
Regards, ruwe
Re: Looking for hints how to debug
Posted: Fri Jul 13, 2012 8:45 am
by noether
It seems a hardware problem. Try to run your code but without the Ethernet routines. However, Ethernet drains a lot of power, but at least you can make your searching area shorter.
sometimes the blinker thread is still alive
If you are driving your led with a timer (PWM), then it will be "working" although the core is in the hardfault. Only a full reset (not only for the core) brings the peripherals to the initial state.
Re: Looking for hints how to debug
Posted: Fri Jul 13, 2012 8:56 am
by rubenswerk
Thanks for response, yes Ethernet power consumption is high, but also Ethernet software code consumption is high

A lot of code for potential risks... If I unplug the network cable there is no problem. In that case, the software is only in idle loops.
The LED blinker is driven by a ChibiOS thread, not by PWM.
Re: Looking for hints how to debug
Posted: Fri Jul 13, 2012 8:59 am
by Giovanni
Yes, the MAC clock was out of spec in older revisions, give the latest trunk a try.
Giovanni
Re: Looking for hints how to debug
Posted: Fri Jul 13, 2012 9:22 am
by Abhishek
Hi ruben,
I suspect a problem with the micro chip itself, since 2 of 3 boards are running the firmware properly. My doubts arise because of your call stack problems. If that's the case then more info about the board might be required.
Which SW do you use to program the flash? Also, did you verify the flash contents after programming? Would you mind giving it a try?
Best Regards.
Re: Looking for hints how to debug
Posted: Fri Jul 13, 2012 12:17 pm
by rubenswerk
I flashed several test softwares in the last days using different methods:
- STlink (onboard interface of F4 Discovery) using the STlink Windows GUI
- my own bootloader, software update via LAN
In any case, a verify is done.
The easiest explanation would be a wrong MAC clock, which might work on some controllers, but not on others. But I was aware of this problem and included the fix. I'll check on the weekend if I fixed correctly. Maybe I can even update to latest trunk.
I still wonder about the call stack and how this situation can arise, that there's no valid return adress.
Thanks to all, ruwe
Re: Looking for hints how to debug
Posted: Mon Jul 16, 2012 9:11 am
by rubenswerk
I updated to latest trunk, but it did not change the situation.
Two boards running for >100hours without exception. One board crashes after some time when Ethernet is used.
This will need deeper analysis.
The latest trunk offers USB OTG support, I tried just for fun, USB enumeration worked immediately. Nice! Haven't tried data transfer yet.
Re: Looking for hints how to debug
Posted: Mon Jul 23, 2012 11:21 pm
by rubenswerk
Two boards passed a several hundred hours test without problems, but one board tends to fail.
The problem is linked to interrupt calls.
I have timer, USART and DMA interrupts running. If I disable ALL of them (it's not linked to one single ISR), the suspicious board works stable.
I'm not familiar with ARM7 interrupt programming. I just put a pointer to my C function (regular function without any ISR attributes) into vectors.c. The code inside the ISR's is very small. Low CPU and stack consumption.
I didn't change anything in NVIC configuration. The reset value of the interrupt priority registers NVIC_IPRx is 0, so I assume that all of my interrupts have highest priority, being in the same preemption group and subgroup.
ChibiOS sets the priority group configuration to 0b100, resulting in 8 priority groups und 2 subpriorities.
Is it correct that OS interrupts (SysTick) should have lowest priority? I think the SysTick should not preempt my ISRs, this could mix up the stack if the OS tries to schedule a thread. CORTEX_PRIORITY_SYSTICK is 0b1000, so it's in a lower preemption group than all of my interrupts.
Is there anything to take care of when using interrupts? The article 'How to write interrupt handlers' mentions an important application note for ARM7, but I couldn't find it when I browsed the port doc.
Any hint is welcome. Regards, Rudi
Edit:
I use the wrapper CH_FAST_IRQ_HANDLER for my ISRs, which does effectively nothing than void()
I don't use any OS functions inside my ISRs.
I use NVIC_EnableIRQ() to activate the interrupts.
Re: Looking for hints how to debug
Posted: Tue Jul 24, 2012 7:35 am
by Giovanni
rubenswerk wrote:I use NVIC_EnableIRQ() to activate the interrupts.
You should use the functions provided by the OS in nvic.c/nvic.h.
rubenswerk wrote:ChibiOS sets the priority group configuration to 0b100, resulting in 8 priority groups und 2 subpriorities.
It is supposed to contain 3 (0x011), that is the ChibiOS default, no sub-priorities. If you enable sub-priorities there are a series of other settings to adapt and verify. Is that changed by the alien libraries you are using?
rubenswerk wrote:Is it correct that OS interrupts (SysTick) should have lowest priority? I think the SysTick should not preempt my ISRs, this could mix up the stack if the OS tries to schedule a thread. CORTEX_PRIORITY_SYSTICK is 0b1000, so it's in a lower preemption group than all of my interrupts.
Not necessarily, the reschedule is always performed by the last ISR in the chain regardless their relative priorities. But you are doing something wrong: CH_FAST_IRQ_HANDLER() can only be used for priorities above the kernel level (CORTEX_MAX_KERNEL_PRIORITY) and should never be preempted by an ISR declared using CH_IRQ_HANDLER().
If your ISR has a priority lower or equal CORTEX_MAX_KERNEL_PRIORITY then it must use CH_IRQ_HANDLER() regardless it uses OS APIs or not.
rubenswerk wrote:Is there anything to take care of when using interrupts? The article 'How to write interrupt handlers' mentions an important application note for ARM7, but I couldn't find it when I browsed the port doc.
It is not an ARM7 but a Cortex-M, are you looking in the wrong documents? Anyway, no special care is required, just write the ISR using the OS macros.
Giovanni
Re: Looking for hints how to debug
Posted: Sat Jul 28, 2012 12:30 pm
by rubenswerk
Thanks for your recommendation Giovanni, I replaced NVIC_EnableIRQ() with the ChibiOS interface nvicEnableVector().
Result:
The board with the unstable behaviour works since 36 hours. The problem seems to have disappeared.
NVIC_EnableIRQ is part of the ARM CMSIS lib, so I wouldn't call it an Alien lib

What's the difference between the two functions? Let's have a look at the code behind:
Code: Select all
CMSIS Lib:
__STATIC_INLINE void NVIC_EnableIRQ(IRQn_Type IRQn)
{
NVIC->ISER[(uint32_t)((int32_t)IRQn) >> 5] = (uint32_t)(1 << ((uint32_t)((int32_t)IRQn) & (uint32_t)0x1F));
}
ChibiOS:
void nvicEnableVector(uint32_t n, uint32_t prio) {
unsigned sh = (n & 3) << 3;
NVIC_IPR(n >> 2) = (NVIC_IPR(n >> 2) & ~(0xFF << sh)) | (prio << sh);
NVIC_ICPR(n >> 5) = 1 << (n & 0x1F);
NVIC_ISER(n >> 5) = 1 << (n & 0x1F);
}
So what ChibiOS additionally does is setting the priority and clearing pending IRQs.
I don't think that clearing pending IRQs makes a difference - my IRQs are only enabled once during initialization, and that's it.
About the priority: according to the datasheet, the initial value of the ICPR registers is 0, and that's the value which I pass to nvicEnableVector.
So I cannot explain why it makes a difference, but there IS a difference

Also I still wonder why two boards work absolutely stable with NVIC_EnableIRQ...
About the grouping: you're right, SCB_AIRCR is initialized with CORTEX_PRIGROUP_INIT, which is (7 - CORTEX_PRIORITY_BITS) = 3.
So there're 16 priority groups.
CORTEX_PRIORITY_SYSTICK is 8, so it has medium priority.
I only use fast IRQs without OS function calls, so they need to have a priority higher than CORTEX_MAX_KERNEL_PRIORITY.
Looking at the code:
Code: Select all
#define CORTEX_MAX_KERNEL_PRIORITY (CORTEX_PRIORITY_SVCALL + 1)
#define CORTEX_PRIORITY_SVCALL (CORTEX_MAXIMUM_PRIORITY + 1)
#define CORTEX_MAXIMUM_PRIORITY 0
So CORTEX_MAX_KERNEL_PRIORITY is 2. This means that only priority levels 0 and 1 are left for my fast interrupts.
I would like to have a higher granularity for my ISRs, using at least 3 different priority groups.
Is it allowed to set the kernel prio to a higher value (=lower prio)? For example:
#define CORTEX_PRIORITY_SVCALL (CORTEX_MAXIMUM_PRIORITY + 5)
Regards, RuWe