Skip to content

Commit 6be3199

Browse files
committed
[example] Fiber overflow detection on ARMv8-M
1 parent cab8ee9 commit 6be3199

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (c) 2024, Niklas Hauser
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#include <modm/board.hpp>
13+
#include <modm/processing.hpp>
14+
15+
using namespace Board;
16+
using namespace std::chrono_literals;
17+
18+
modm::Fiber<> blinky([]
19+
{
20+
while(1)
21+
{
22+
Board::LedBlue::toggle();
23+
modm::this_fiber::sleep_for(0.5s);
24+
}
25+
});
26+
27+
modm::Fiber<> bad_fiber([]
28+
{
29+
30+
MODM_LOG_INFO << "\nReboot!\nPush the button to overflow the stack!" << modm::endl;
31+
32+
while(1)
33+
{
34+
// cause stack overflow on button push
35+
if (Button::read()) asm volatile ("push {r0-r7}");
36+
modm::this_fiber::yield();
37+
}
38+
});
39+
40+
// On fiber stack overflow this handler will be called
41+
extern "C" void
42+
UsageFault_Handler()
43+
{
44+
// check if the fault is a stack overflow *from the fiber stack*
45+
if (SCB->CFSR & SCB_CFSR_STKOF_Msk and __get_PSP() == __get_PSPLIM())
46+
{
47+
// lower the priority of the usage fault to allow the UART interrupts to work
48+
NVIC_SetPriority(UsageFault_IRQn, (1ul << __NVIC_PRIO_BITS) - 1ul);
49+
// raise an assertion to report this overflow
50+
modm_assert(false, "fbr.stkof", "Fiber stack overflow", modm::this_fiber::get_id());
51+
}
52+
else HardFault_Handler();
53+
}
54+
55+
int
56+
main()
57+
{
58+
Board::initialize();
59+
// Enable the UsageFault handler
60+
SCB->SHCSR |= SCB_SHCSR_USGFAULTENA_Msk;
61+
62+
modm::fiber::Scheduler::run();
63+
64+
return 0;
65+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<library>
2+
<extends>modm:nucleo-u575zi-q</extends>
3+
<options>
4+
<option name="modm:build:build.path">../../../build/nucleo_u575zi-q/fiber_overflow</option>
5+
</options>
6+
<modules>
7+
<module>modm:build:scons</module>
8+
<module>modm:processing:fiber</module>
9+
</modules>
10+
</library>

0 commit comments

Comments
 (0)