2023-03-29 11:52 PM
I would like to share a bug I encountered and how to fix it:
I am using an STM32L151, the goal is to set it into SLEEP mode and wake it up using a UART receive interrupt while debugging. All configurations to pins, registers and interrupts are made correctly.
The problem:
MCU goes to sleep just fine. When sending data over UART, the MCU wakes up, but doesnt enter the UART RX ISR. Instead it enters the HardFaultHandler. Readout of the Bus Fault Status Register (BFSR) of the Configurable Fault Status Registers (CFSR) shows that bit 0 is set, indicating an Instruction bus error.
The fix:
Full credit goes to https://web.archive.org/web/20210625090753/https://lists.riot-os.org/pipermail/devel/2016-September/004534.html.
The fix states, before entering wait-for-interrupt instruction, one must disable interrupts (they will still trigger wakeup and be registered), then entering wait-for-interrupt, then issuing a nop-instruction, then re-enabling interrupts.
The fix requires to modify the HAL/SPL, so be careful when using CubeMX code generation. In my case I am using the old Standard Preipheral Library, so in the file "stm32l1xx_pwr.c" in function "PWR_EnterSleepMode" i changed
if(PWR_SLEEPEntry == PWR_SLEEPEntry_WFI)
{
/* Request Wait For Interrupt */
__DSB();
__WFI();
__NOP();
__ISB();
}
to
if(PWR_SLEEPEntry == PWR_SLEEPEntry_WFI)
{
/* Request Wait For Interrupt */
__DSB();
__disable_irq();
asm ("DMB");
__WFI();
asm ("nop");
__enable_irq();
__NOP();
__ISB();
}
This solves the problem. It might be necessary in some cases to insert more than one nop-Instruction after the wfi instruction.
2023-03-30 03:09 AM
Hello @makas005 ,
Thanks for sharing the issue and fix 👍
I will check this reported issue and take the necessary action.
An internal ticket (ID 148705) is submitted related to this issue.
(PS: ID 148705 is an internal tracking number and is not accessible or usable by customers).
Imen
2023-04-07 05:18 AM
Hi @@makas005 ,
Can you please precise which package version are you using?
This soft code is not existed in the STSW-STM32077 - STM32L1xx standard peripherals library:
if(PWR_SLEEPEntry == PWR_SLEEPEntry_WFI)
{
/* Request Wait For Interrupt */
__DSB();
__WFI();
__NOP();
__ISB();
}
Imen
2023-04-10 11:28 PM
Hi @Imen DAHMEN, thank you for you quick response.
I am using SPL version 1.3.1. However, i see that the __DSB, __NOP and __ISB functions that i posted in the first code snippet are not present in the SPL that ST provides. They might have been added company internally.
However, i think that the problem can still occur when only the __WFI is used (as it is in the SPL download version), given that the archived forum post of riot-os states the exact same error.