Skip to content

Commit

Permalink
Enable AVM_ABORT() and use it for hard_fault_handler on stm32
Browse files Browse the repository at this point in the history
- Impliment newlib stubs to support `getpid()` and `kill()` needed for
`AVM_ABORT()` to work properly.
- Redefine weak linked `hard_fault_handler` defined in libopencm3/cm3/nvic.h to
use `AVM_ABORT()` rather than get trapped in the default do nothing `while(1)`
loop.

Signed-off-by: Winford <[email protected]>
  • Loading branch information
UncleGrumpy committed Oct 22, 2023
1 parent 8308b43 commit 448eab9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Fixed a bug where guards would raise exceptions instead of just being false
- Fixed STM32 not aborting when `AVM_ABORT()` is used
- Fixed a bug that would leave the STM32 trapped in a loop on hard faults, rather than aborting

## [0.6.0-alpha.1] - 2023-10-09

Expand Down
27 changes: 27 additions & 0 deletions src/platforms/stm32/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>

Expand Down Expand Up @@ -61,6 +62,8 @@
"\n"

int _write(int file, char *ptr, int len);
pid_t _getpid(void);
int _kill(pid_t pid, int sig);

static void clock_setup()
{
Expand Down Expand Up @@ -124,6 +127,30 @@ int _write(int file, char *ptr, int len)
return -1;
}

// newlib stubs to support AVM_ABORT
pid_t _getpid()
{
return 1;
}

int _kill(pid_t pid, int sig)
{
UNUSED(pid);
if (sig == SIGABRT) {
fprintf(stderr, "Aborted\n");
} else {
fprintf(stderr, "Unknown signal %d\n", sig);
}
errno = EINVAL;
return -1;
}

// Redefine weak linked while(1) loop from libopencm3/cm3/nvic.h.
void hard_fault_handler() {
fprintf(stderr, "\nHard Fault detected!\n");
AVM_ABORT();
}

int main()
{
clock_setup();
Expand Down

0 comments on commit 448eab9

Please sign in to comment.