Skip to content

stm32l152re: hard-fault unless power-cycled after flash, or depending on optimization #11820

@fjmolinas

Description

@fjmolinas

Description

#8518 introduced a _NOP() after _WFI to avoid a hard-fault because of the loss of a register content after exiting sleep (or at least that was the best possible cause at the time).

When #11159 was introduced the way pm_set_lowest() was called changed because pm_set() was now implemented for STM32L1, this made the problem re-appear. At the time the PR was merged I did not see the bug because I was using a different version of gcc and for some reason the bug didn't appear under those conditions. I did see it when @aabadie tested #11489 and I switched compiler versions.

As for what I can tell the hardfault still occurs at the same point.

There are many puzzling things about this:

  • I did not have the issue when using different gcc versions (see bottom of post).
  • The issue appears after flashing but if a power-cycle is performed (soft reset is not enough), the issue is gone.
  • I also checked out cpu/cortexm: add __NOP(); after __WFI(); for stm32l152 to avoid hardfault #8518 and removed the change it introduced and I get the exact same behavior, if a power-cycle is performed, no hardfault!
  • Removing the NOP solves the issue now...

Summing the possible fixes I have found:

  1. remove the _NOP()
  2. add more _NOP() after and even after irqs are enabled again
  3. change optimization from -Os to -O2
  4. declare state as volatile (when calling irq_disable())
  5. lower optimization to O2 only for cortexm_sleep

I've tried to find a proper formal explanation for this but I haven't succeeded. I'm specially intrigued by the fact that after a power-cycle it works again I feel that could be an important insight into the problem #8518 tried to fix.

I can post the disassembly for any of these solutions, I haven't been able to find something that could explain it (but I'm not very knowledged in assembler).

Steps to reproduce the issue

Checkout upstream master and flash an application (different than hello-world), I usually do:

make -C tests/xtimer_usleep BOARD=nucleo-l152re clean -j3 flash term

2019-07-09 11:47:35,335 - INFO # �main(): This is RIOT! (Version: 2019.10-devel-11-gecdcc-HEAD)
2019-07-09 11:47:35,337 - INFO # Running test 5 times with 7 distinct sleep times
2019-07-09 11:47:35,341 - INFO # Please hit any key and then ENTER to continue
a
2019-07-09 11:47:36,301 - INFO # *** RIOT kernel panic:
2019-07-09 11:47:36,307 - INFO # Context before hardfault:

Plug and un-plug the board (or remove IDD jumper on nucleo boards), in the same terminal and without flashing again run the application again:


2019-07-09 11:48:03,429 - INFO # Please hit any key and then ENTER to continue

2019-07-09 11:48:04,868 - INFO # �main(): This is RIOT! (Version: 2019.10-devel-11-gecdcc-HEAD)
2019-07-09 11:48:04,876 - INFO # Running test 5 times with 7 distinct sleep times
2019-07-09 11:48:04,877 - INFO # Please hit any key and then ENTER to continue
a
2019-07-09 11:48:06,340 - INFO # Slept for 10023 us (expected: 10000 us) Offset: 23 us
2019-07-09 11:48:06,393 - INFO # Slept for 50023 us (expected: 50000 us) Offset: 23 us
2019-07-09 11:48:06,411 - INFO # Slept for 10257 us (expected: 10234 us) Offset: 23 us
2019-07-09 11:48:06,472 - INFO # Slept for 56803 us (expected: 56780 us) Offset: 23 us

It works as expected.

Expected results

Should work fine after flashing with or without power-cycle.

Actual results

Crashes unless complete power-cycle is performed.

Versions

No issues toolchain:

Operating System Environment
-----------------------------
       Operating System: "Ubuntu" "16.04.2 LTS (Xenial Xerus)"
                 Kernel: Linux 4.13.0-32-generic x86_64 x86_64

Installed compiler toolchains
-----------------------------
             native gcc: gcc (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609
      arm-none-eabi-gcc: arm-none-eabi-gcc (15:4.9.3+svn231177-1) 4.9.3 20150529 (prerelease)
                avr-gcc: missing
       mips-mti-elf-gcc: missing
             msp430-gcc: msp430-gcc (GCC) 4.6.3 20120301 (mspgcc LTS 20120406 unpatched)
   riscv-none-embed-gcc: missing
   xtensa-esp32-elf-gcc: missing
   xtensa-lx106-elf-gcc: missing
                  clang: missing

Installed compiler libs
-----------------------
   arm-none-eabi-newlib: "2.2.0"
    mips-mti-elf-newlib: missing
riscv-none-embed-newlib: missing
xtensa-esp32-elf-newlib: missing
xtensa-lx106-elf-newlib: missing
               avr-libc: missing (missing)

Installed development tools
---------------------------
                  cmake: cmake version 3.5.1
               cppcheck: missing
                doxygen: 1.8.11
                 flake8: missing
                    git: git version 2.7.4
                   make: GNU Make 4.1
                openocd: Open On-Chip Debugger 0.10.0+dev-00166-g0e4fbfb (2017-07-05-19:14)
                 python: Python 3.6.8
                python2: Python 2.7.12
                python3: Python 3.6.8
             coccinelle: missing

Issues toolchain:

Operating System Environment
-----------------------------
       Operating System: "Ubuntu" "18.04.2 LTS (Bionic Beaver)"
                 Kernel: Linux 4.18.0-25-generic x86_64 x86_64

Installed compiler toolchains
-----------------------------
             native gcc: gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
      arm-none-eabi-gcc: arm-none-eabi-gcc (GNU Tools for Arm Embedded Processors 8-2018-q4-major) 8.2.1 20181213 (release) [gcc-8-branch revision 267074]
                avr-gcc: missing
       mips-mti-elf-gcc: missing
             msp430-gcc: missing
   riscv-none-embed-gcc: missing
   xtensa-esp32-elf-gcc: missing
   xtensa-lx106-elf-gcc: missing
                  clang: missing

Installed compiler libs
-----------------------
   arm-none-eabi-newlib: "3.0.0"
    mips-mti-elf-newlib: missing
riscv-none-embed-newlib: missing
xtensa-esp32-elf-newlib: missing
xtensa-lx106-elf-newlib: missing
               avr-libc: missing (missing)

Installed development tools
---------------------------
                  cmake: cmake version 3.14.0-rc3
               cppcheck: Cppcheck 1.82
                doxygen: 1.8.16
                 flake8: missing
                    git: git version 2.21.0
                   make: GNU Make 4.1
                openocd: Open On-Chip Debugger 0.10.0+dev-00920-g6ea43726-dirty (2019-07-05-08:43)
                 python: Python 3.6.7
                python2: Python 2.7.15rc1
                python3: Python 3.6.7
             coccinelle: missing

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area: pmArea: (Low) power managementType: bugThe issue reports a bug / The PR fixes a bug (including spelling errors)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions