Skip to content

Commit 9af6a27

Browse files
committed
stm32/boardctrl: Allow boards to override fatal-error handler.
To override it a board must define MICROPY_BOARD_FATAL_ERROR to a function that takes a string message and does not return. Signed-off-by: Damien George <[email protected]>
1 parent 9f8087b commit 9af6a27

File tree

9 files changed

+51
-51
lines changed

9 files changed

+51
-51
lines changed

ports/stm32/boardctrl.c

+22
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include <string.h>
28+
2729
#include "py/runtime.h"
2830
#include "py/objstr.h"
2931
#include "py/mphal.h"
@@ -33,6 +35,26 @@
3335
#include "led.h"
3436
#include "usrsw.h"
3537

38+
NORETURN void boardctrl_fatal_error(const char *msg) {
39+
for (volatile uint delay = 0; delay < 10000000; delay++) {
40+
}
41+
led_state(1, 1);
42+
led_state(2, 1);
43+
led_state(3, 1);
44+
led_state(4, 1);
45+
mp_hal_stdout_tx_strn("\nFATAL ERROR:\n", 14);
46+
mp_hal_stdout_tx_strn(msg, strlen(msg));
47+
for (uint i = 0;;) {
48+
led_toggle(((i++) & 3) + 1);
49+
for (volatile uint delay = 0; delay < 10000000; delay++) {
50+
}
51+
if (i >= 16) {
52+
// to conserve power
53+
__WFI();
54+
}
55+
}
56+
}
57+
3658
STATIC void flash_error(int n) {
3759
for (int i = 0; i < n; i++) {
3860
led_state(PYB_LED_RED, 1);

ports/stm32/boardctrl.h

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
#define MICROPY_BOARD_PENDSV_ENTRIES
3434
#endif
3535

36+
#ifndef MICROPY_BOARD_FATAL_ERROR
37+
#define MICROPY_BOARD_FATAL_ERROR boardctrl_fatal_error
38+
#endif
39+
3640
#ifndef MICROPY_BOARD_STARTUP
3741
#define MICROPY_BOARD_STARTUP powerctrl_check_enter_bootloader
3842
#endif
@@ -110,6 +114,7 @@ typedef struct _boardctrl_state_t {
110114
bool log_soft_reset;
111115
} boardctrl_state_t;
112116

117+
NORETURN void boardctrl_fatal_error(const char *msg);
113118
void boardctrl_maybe_enter_mboot(size_t n_args, const void *args);
114119
void boardctrl_before_soft_reset_loop(boardctrl_state_t *state);
115120
void boardctrl_top_soft_reset_loop(boardctrl_state_t *state);

ports/stm32/main.c

+3-23
Original file line numberDiff line numberDiff line change
@@ -97,41 +97,21 @@ STATIC pyb_uart_obj_t pyb_uart_repl_obj;
9797
STATIC uint8_t pyb_uart_repl_rxbuf[MICROPY_HW_UART_REPL_RXBUF];
9898
#endif
9999

100-
void NORETURN __fatal_error(const char *msg) {
101-
for (volatile uint delay = 0; delay < 10000000; delay++) {
102-
}
103-
led_state(1, 1);
104-
led_state(2, 1);
105-
led_state(3, 1);
106-
led_state(4, 1);
107-
mp_hal_stdout_tx_strn("\nFATAL ERROR:\n", 14);
108-
mp_hal_stdout_tx_strn(msg, strlen(msg));
109-
for (uint i = 0;;) {
110-
led_toggle(((i++) & 3) + 1);
111-
for (volatile uint delay = 0; delay < 10000000; delay++) {
112-
}
113-
if (i >= 16) {
114-
// to conserve power
115-
__WFI();
116-
}
117-
}
118-
}
119-
120100
void nlr_jump_fail(void *val) {
121101
printf("FATAL: uncaught exception %p\n", val);
122102
mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(val));
123-
__fatal_error("");
103+
MICROPY_BOARD_FATAL_ERROR("");
124104
}
125105

126106
void abort(void) {
127-
__fatal_error("abort");
107+
MICROPY_BOARD_FATAL_ERROR("abort");
128108
}
129109

130110
#ifndef NDEBUG
131111
void MP_WEAK __assert_func(const char *file, int line, const char *func, const char *expr) {
132112
(void)func;
133113
printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line);
134-
__fatal_error("");
114+
MICROPY_BOARD_FATAL_ERROR("");
135115
}
136116
#endif
137117

ports/stm32/modmachine.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,7 @@ STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) {
339339
if (ret == -MP_EINVAL) {
340340
mp_raise_ValueError(MP_ERROR_TEXT("invalid freq"));
341341
} else if (ret < 0) {
342-
void NORETURN __fatal_error(const char *msg);
343-
__fatal_error("can't change freq");
342+
MICROPY_BOARD_FATAL_ERROR("can't change freq");
344343
}
345344
return mp_const_none;
346345
#endif

ports/stm32/pybthread.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <stdio.h>
2929

3030
#include "py/obj.h"
31+
#include "boardctrl.h"
3132
#include "gccollect.h"
3233
#include "irq.h"
3334
#include "pybthread.h"
@@ -42,8 +43,6 @@
4243
#define RAISE_IRQ_PRI() raise_irq_pri(IRQ_PRI_PENDSV)
4344
#define RESTORE_IRQ_PRI(state) restore_irq_pri(state)
4445

45-
extern void __fatal_error(const char *);
46-
4746
volatile int pyb_thread_enabled;
4847
pyb_thread_t *volatile pyb_thread_all;
4948
pyb_thread_t *volatile pyb_thread_cur;
@@ -57,7 +56,7 @@ static inline void pyb_thread_add_to_runable(pyb_thread_t *thread) {
5756

5857
static inline void pyb_thread_remove_from_runable(pyb_thread_t *thread) {
5958
if (thread->run_next == thread) {
60-
__fatal_error("deadlock");
59+
MICROPY_BOARD_FATAL_ERROR("deadlock");
6160
}
6261
thread->run_prev->run_next = thread->run_next;
6362
thread->run_next->run_prev = thread->run_prev;
@@ -112,7 +111,7 @@ STATIC void pyb_thread_terminate(void) {
112111
SCB->ICSR = SCB_ICSR_PENDSVSET_Msk;
113112
enable_irq(irq_state);
114113
// should not return
115-
__fatal_error("could not terminate");
114+
MICROPY_BOARD_FATAL_ERROR("could not terminate");
116115
}
117116

118117
uint32_t pyb_thread_new(pyb_thread_t *thread, void *stack, size_t stack_len, void *entry, void *arg) {

ports/stm32/sdram.c

+4-5
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
#ifdef FMC_SDRAM_BANK
5151

5252
static void sdram_init_seq(SDRAM_HandleTypeDef *hsdram, FMC_SDRAM_CommandTypeDef *command);
53-
extern void __fatal_error(const char *msg);
5453

5554
bool sdram_init(void) {
5655
SDRAM_HandleTypeDef hsdram;
@@ -325,7 +324,7 @@ bool __attribute__((optimize("Os"))) sdram_test(bool exhaustive) {
325324
snprintf(error_buffer, sizeof(error_buffer),
326325
"Data bus test failed at 0x%p expected 0x%x found 0x%lx",
327326
&mem_base[0], (1 << i), ((volatile uint32_t *)mem_base)[0]);
328-
__fatal_error(error_buffer);
327+
MICROPY_BOARD_FATAL_ERROR(error_buffer);
329328
#endif
330329
return false;
331330
}
@@ -340,7 +339,7 @@ bool __attribute__((optimize("Os"))) sdram_test(bool exhaustive) {
340339
snprintf(error_buffer, sizeof(error_buffer),
341340
"Address bus test failed at 0x%p expected 0x%x found 0x%x",
342341
&mem_base[i], pattern, mem_base[i]);
343-
__fatal_error(error_buffer);
342+
MICROPY_BOARD_FATAL_ERROR(error_buffer);
344343
#endif
345344
return false;
346345
}
@@ -355,7 +354,7 @@ bool __attribute__((optimize("Os"))) sdram_test(bool exhaustive) {
355354
snprintf(error_buffer, sizeof(error_buffer),
356355
"Address bus overlap at 0x%p expected 0x%x found 0x%x",
357356
&mem_base[i], pattern, mem_base[i]);
358-
__fatal_error(error_buffer);
357+
MICROPY_BOARD_FATAL_ERROR(error_buffer);
359358
#endif
360359
return false;
361360
}
@@ -376,7 +375,7 @@ bool __attribute__((optimize("Os"))) sdram_test(bool exhaustive) {
376375
snprintf(error_buffer, sizeof(error_buffer),
377376
"Address bus slow test failed at 0x%p expected 0x%x found 0x%x",
378377
&mem_base[i], ((i % 2) ? pattern : antipattern), mem_base[i]);
379-
__fatal_error(error_buffer);
378+
MICROPY_BOARD_FATAL_ERROR(error_buffer);
380379
#endif
381380
return false;
382381
}

ports/stm32/stm32_it.c

+4-5
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@
8383
#include "i2c.h"
8484
#include "usb.h"
8585

86-
extern void __fatal_error(const char *);
8786
#if defined(MICROPY_HW_USB_FS)
8887
extern PCD_HandleTypeDef pcd_fs_handle;
8988
#endif
@@ -192,7 +191,7 @@ void HardFault_C_Handler(ExceptionRegisters_t *regs) {
192191

193192
/* Go to infinite loop when Hard Fault exception occurs */
194193
while (1) {
195-
__fatal_error("HardFault");
194+
MICROPY_BOARD_FATAL_ERROR("HardFault");
196195
}
197196
}
198197

@@ -246,7 +245,7 @@ void NMI_Handler(void) {
246245
void MemManage_Handler(void) {
247246
/* Go to infinite loop when Memory Manage exception occurs */
248247
while (1) {
249-
__fatal_error("MemManage");
248+
MICROPY_BOARD_FATAL_ERROR("MemManage");
250249
}
251250
}
252251

@@ -258,7 +257,7 @@ void MemManage_Handler(void) {
258257
void BusFault_Handler(void) {
259258
/* Go to infinite loop when Bus Fault exception occurs */
260259
while (1) {
261-
__fatal_error("BusFault");
260+
MICROPY_BOARD_FATAL_ERROR("BusFault");
262261
}
263262
}
264263

@@ -270,7 +269,7 @@ void BusFault_Handler(void) {
270269
void UsageFault_Handler(void) {
271270
/* Go to infinite loop when Usage Fault exception occurs */
272271
while (1) {
273-
__fatal_error("UsageFault");
272+
MICROPY_BOARD_FATAL_ERROR("UsageFault");
274273
}
275274
}
276275

ports/stm32/system_stm32.c

+8-9
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,11 @@
7676
*/
7777

7878
#include "py/mphal.h"
79+
#include "boardctrl.h"
7980
#include "powerctrl.h"
8081

8182
#if defined(STM32F4) || defined(STM32F7) || defined(STM32G4) || defined(STM32H7) || defined(STM32L4)
8283

83-
void __fatal_error(const char *msg);
84-
8584
/**
8685
* @brief System Clock Configuration
8786
*
@@ -390,7 +389,7 @@ MP_WEAK void SystemClock_Config(void) {
390389
#endif
391390

392391
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
393-
__fatal_error("HAL_RCC_OscConfig");
392+
MICROPY_BOARD_FATAL_ERROR("HAL_RCC_OscConfig");
394393
}
395394

396395
#if defined(MICROPY_HW_CLK_PLL2M)
@@ -478,20 +477,20 @@ MP_WEAK void SystemClock_Config(void) {
478477
#endif
479478

480479
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
481-
__fatal_error("HAL_RCCEx_PeriphCLKConfig");
480+
MICROPY_BOARD_FATAL_ERROR("HAL_RCCEx_PeriphCLKConfig");
482481
}
483482
#endif // defined(STM32H7)
484483

485484
#if defined(STM32F7)
486485
/* Activate the OverDrive to reach the 200 MHz Frequency */
487486
if (HAL_PWREx_EnableOverDrive() != HAL_OK) {
488-
__fatal_error("HAL_PWREx_EnableOverDrive");
487+
MICROPY_BOARD_FATAL_ERROR("HAL_PWREx_EnableOverDrive");
489488
}
490489
#endif
491490

492491
#if defined(STM32G4)
493492
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_8) != HAL_OK) {
494-
__fatal_error("HAL_RCC_ClockConfig");
493+
MICROPY_BOARD_FATAL_ERROR("HAL_RCC_ClockConfig");
495494
}
496495
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC | RCC_PERIPHCLK_LPUART1
497496
| RCC_PERIPHCLK_RNG | RCC_PERIPHCLK_ADC12
@@ -503,14 +502,14 @@ MP_WEAK void SystemClock_Config(void) {
503502
PeriphClkInitStruct.Adc12ClockSelection = RCC_ADC12CLKSOURCE_SYSCLK;
504503
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
505504
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
506-
__fatal_error("HAL_RCCEx_PeriphCLKConfig");
505+
MICROPY_BOARD_FATAL_ERROR("HAL_RCCEx_PeriphCLKConfig");
507506
}
508507
#else
509508
uint32_t vco_out = RCC_OscInitStruct.PLL.PLLN * (MICROPY_HW_CLK_VALUE / 1000000) / RCC_OscInitStruct.PLL.PLLM;
510509
uint32_t sysclk_mhz = vco_out / RCC_OscInitStruct.PLL.PLLP;
511510
bool need_pll48 = vco_out % 48 != 0;
512511
if (powerctrl_rcc_clock_config_pll(&RCC_ClkInitStruct, sysclk_mhz, need_pll48) != 0) {
513-
__fatal_error("HAL_RCC_ClockConfig");
512+
MICROPY_BOARD_FATAL_ERROR("HAL_RCC_ClockConfig");
514513
}
515514
#endif
516515

@@ -572,7 +571,7 @@ MP_WEAK void SystemClock_Config(void) {
572571
| RCC_PLLSAI1_ADC1CLK;
573572

574573
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
575-
__fatal_error("HAL_RCCEx_PeriphCLKConfig");
574+
MICROPY_BOARD_FATAL_ERROR("HAL_RCCEx_PeriphCLKConfig");
576575
}
577576

578577
__PWR_CLK_ENABLE();

ports/stm32/uart.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@
112112

113113
#endif
114114

115-
extern void NORETURN __fatal_error(const char *msg);
116-
117115
typedef struct _pyb_uart_irq_map_t {
118116
uint16_t irq_en;
119117
uint16_t flag;
@@ -152,7 +150,7 @@ void uart_init0(void) {
152150
RCC_PeriphClkInit.Usart16ClockSelection = RCC_USART16CLKSOURCE_D2PCLK2;
153151
RCC_PeriphClkInit.Usart234578ClockSelection = RCC_USART234578CLKSOURCE_D2PCLK1;
154152
if (HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphClkInit) != HAL_OK) {
155-
__fatal_error("HAL_RCCEx_PeriphCLKConfig");
153+
MICROPY_BOARD_FATAL_ERROR("HAL_RCCEx_PeriphCLKConfig");
156154
}
157155
#endif
158156
}

0 commit comments

Comments
 (0)