<h4>Description</h4>

<p><a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="294455711" data-permission-text="Issue title is private" data-url="https://github.com/RIOT-OS/RIOT/issues/8518" data-hovercard-type="pull_request" data-hovercard-url="/RIOT-OS/RIOT/pull/8518/hovercard" href="https://github.com/RIOT-OS/RIOT/pull/8518">#8518</a> introduced a <code>_NOP()</code> after <code>_WFI</code> 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).</p>
<p>When <a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="419873715" data-permission-text="Issue title is private" data-url="https://github.com/RIOT-OS/RIOT/issues/11159" data-hovercard-type="pull_request" data-hovercard-url="/RIOT-OS/RIOT/pull/11159/hovercard" href="https://github.com/RIOT-OS/RIOT/pull/11159">#11159</a> was introduced the way <code>pm_set_lowest()</code> was called changed because <code>pm_set()</code> was now implemented for <code>STM32L1</code>, 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 <a class="user-mention" data-hovercard-type="user" data-hovercard-url="/hovercards?user_id=1375137" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/aabadie">@aabadie</a> tested <a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="440569264" data-permission-text="Issue title is private" data-url="https://github.com/RIOT-OS/RIOT/issues/11489" data-hovercard-type="pull_request" data-hovercard-url="/RIOT-OS/RIOT/pull/11489/hovercard" href="https://github.com/RIOT-OS/RIOT/pull/11489">#11489</a> and I switched compiler versions.</p>
<p>As for what I can tell the hardfault still occurs at the same point.</p>
<p>There are many puzzling things about this:</p>
<ul>
<li>I did not have the issue when using different gcc versions (see bottom of post).</li>
<li>The issue appears after flashing but if a power-cycle is performed (soft reset is not enough), the issue is gone.</li>
<li>I also checked out <a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="294455711" data-permission-text="Issue title is private" data-url="https://github.com/RIOT-OS/RIOT/issues/8518" data-hovercard-type="pull_request" data-hovercard-url="/RIOT-OS/RIOT/pull/8518/hovercard" href="https://github.com/RIOT-OS/RIOT/pull/8518">#8518</a> and removed the change it introduced and I get the exact same behavior, if a power-cycle is performed, no hardfault!</li>
<li>Removing the <code>NOP</code> solves the issue now...</li>
</ul>
<p>Summing the possible fixes I have found:</p>
<ul>
<li>remove the <code>_NOP()</code></li>
<li>add more <code>_NOP()</code> after and even after <code>irqs</code> are enabled again</li>
<li>change optimization from <code>-Os</code> to <code>-O2</code></li>
<li>declare <code>state</code> (when calling <code>irq_disable()</code>) as volatile</li>
</ul>
<p>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 <a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="294455711" data-permission-text="Issue title is private" data-url="https://github.com/RIOT-OS/RIOT/issues/8518" data-hovercard-type="pull_request" data-hovercard-url="/RIOT-OS/RIOT/pull/8518/hovercard" href="https://github.com/RIOT-OS/RIOT/pull/8518">#8518</a> tried to fix.</p>
<p>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).</p>
<h4>Steps to reproduce the issue</h4>

<p>Checkout upstream master and flash an application (different than hello-world), I usually do:</p>
<p><code>make -C tests/xtimer_usleep BOARD=nucleo-l152re clean -j3 flash term</code></p>
<pre><code>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:
</code></pre>
<p>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:</p>
<pre><code>
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
</code></pre>
<p>It works as expected.</p>
<h4>Expected results</h4>

<p>Should work fine after flashing with or without power-cycle.</p>
<h4>Actual results</h4>

<p>Crashes unless complete power-cycle is performed.</p>
<h4>Versions</h4>
<p>No issues toolchain:</p>
<pre><code>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

</code></pre>
<p>Issues toolchain:</p>
<pre><code>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
</code></pre>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br />You are receiving this because you are subscribed to this thread.<br />Reply to this email directly, <a href="https://github.com/RIOT-OS/RIOT/issues/11820?email_source=notifications&email_token=ABE7WYDKV6BCWJWS5VHUZPTP6RVYNA5CNFSM4H7EJQMKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4G6CIELA">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/ABE7WYDDPWIRIMH7UU2GH3LP6RVYNANCNFSM4H7EJQMA">mute the thread</a>.<img src="https://github.com/notifications/beacon/ABE7WYANX7MO7YWOBOT7WJTP6RVYNA5CNFSM4H7EJQMKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4G6CIELA.gif" height="1" width="1" alt="" /></p>
<script type="application/ld+json">[
{
"@context": "http://schema.org",
"@type": "EmailMessage",
"potentialAction": {
"@type": "ViewAction",
"target": "https://github.com/RIOT-OS/RIOT/issues/11820?email_source=notifications\u0026email_token=ABE7WYDKV6BCWJWS5VHUZPTP6RVYNA5CNFSM4H7EJQMKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4G6CIELA",
"url": "https://github.com/RIOT-OS/RIOT/issues/11820?email_source=notifications\u0026email_token=ABE7WYDKV6BCWJWS5VHUZPTP6RVYNA5CNFSM4H7EJQMKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4G6CIELA",
"name": "View Issue"
},
"description": "View this Issue on GitHub",
"publisher": {
"@type": "Organization",
"name": "GitHub",
"url": "https://github.com"
}
}
]</script>