Skip to content

Commit f8a91e6

Browse files
authored
Merge pull request #7 from shadow578/refactor/panic-logic
refactor panic logic + systick fix
2 parents 6f19b41 + ce09dcf commit f8a91e6

File tree

10 files changed

+245
-258
lines changed

10 files changed

+245
-258
lines changed

README.md

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ the core offers the following multiple options, which can be set in `platformio.
1010

1111
## Debug Options
1212

13-
| Option | Description |
14-
| ---------------------------------- | --------------------------------------------------------------------------------------------- |
15-
| `__CORE_DEBUG` | enable debug mode. this enables additional debug output at the cost of flash space. |
16-
| `__CORE_DEBUG_SHORT_FILENAMES` | use only the filename, not the full path, in debug output. saves some flash space. |
17-
| `__CORE_DEBUG_OMIT_PANIC_MESSAGES` | omit error messages in `panic()` output. saves some flash space. |
18-
| `CORE_DISABLE_FAULT_HANDLER` | disable the core-internal fault handler. only recommended if you have your own fault handler. |
19-
| `HARDFAULT_EXCLUDE_CFSR_INFO` | exclude CFSR flag parsing from fault output. saves some flash space. |
20-
| `HANG_ON_PANIC` | enter a infinite loop on `panic()`. default behaviour is to reset the mcu. |
21-
| `PANIC_USART<n>_TX_PIN` | set the gpio pin that is used to transmit panic messages. `[n]` can be any value in [1,2,3,4] |
13+
| Option | Description |
14+
| ----------------------------- | --------------------------------------------------------------------------------------------- |
15+
| `__CORE_DEBUG` | enable debug mode. this enables additional debug output at the cost of flash space. |
16+
| `__PANIC_SHORT_FILENAMES` | use only the filename, not the full path, in debug output. saves some flash space. |
17+
| `__OMIT_PANIC_MESSAGE` | omit error messages in `panic()` output. saves some flash space. |
18+
| `CORE_DISABLE_FAULT_HANDLER` | disable the core-internal fault handler. only recommended if you have your own fault handler. |
19+
| `HARDFAULT_EXCLUDE_CFSR_INFO` | exclude CFSR flag parsing from fault output. saves some flash space. |
20+
| `HANG_ON_PANIC` | enter a infinite loop on `panic()`. default behaviour is to reset the mcu. |
21+
| `PANIC_USART<n>_TX_PIN` | set the gpio pin that is used to transmit panic messages. `[n]` can be any value in [1,2,3,4] |
2222

2323
## Hardware Serial Options
2424

@@ -34,7 +34,6 @@ the core offers the following multiple options, which can be set in `platformio.
3434
| Option | Description |
3535
| -------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
3636
| `CORE_ADC_RESOLUTION` | set the default resolution of ADC driver. can be `8`, `10`, or `12`. default is `10`. can be overwritten using `analogReadResolution()` |
37-
| `ENABLE_MICROS` | enable `micros()` function. this increases the speed of the SysTick interrupt, which may cause reduced performance. default is disabled |
3837
| `F_CPU=SYSTEM_CLOCK_FREQUENCIES.pclk1` | overwrites the `F_CPU` value. by default, `hclk` is used. refer to the HC32F460 user manual, Section 4.3, Table 4-1 for more details on the different clocks. |
3938

4039
## Example
@@ -43,22 +42,21 @@ the core offers the following multiple options, which can be set in `platformio.
4342
# ...
4443
build_flags =
4544
-D __CORE_DEBUG
46-
-D ENABLE_MICROS
4745
```
4846

4947
# Arduino Core Panic
5048

5149
the core includes a panic mechanism that can print panic messages to one or more usart outputs. this is useful for debugging, as it allows you to see what went wrong.
5250
the following options are available for the core panic mechanism. they can be set in `platformio.ini` using the `build_flags` option.
5351

54-
| Name | Description |
55-
| --------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
56-
| `__CORE_DEBUG` | enables arduino core debug mode. if disabled, panic (and thus faults) will be silent |
57-
| `__CORE_DEBUG_SHORT_FILENAMES` | uses only the filename, not the full path, in panic messages |
58-
| `__CORE_DEBUG_OMIT_PANIC_MESSAGE` | omits the panic message string from panic output. only file and line number will be printed. this reduces the output size, but you'll have to look up the message yourself |
59-
| `PANIC_USART[n]_TX_PIN` | set the gpio pin that is used to transmit panic messages. `[n]` can be any value in [1,2,3,4], and up to four outputs may be defined |
60-
| `HARDFAULT_EXCLUDE_CFSR_INFO` | exclude CFSR flag messages from fault output. this will reduce the output size, but you'll have to look up the flag values yourself |
61-
| `HANG_ON_PANIC` | enter a infinite loop on panic condition. default behaviour is to reset the mcu |
52+
| Name | Description |
53+
| ----------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
54+
| `__CORE_DEBUG` | enables arduino core debug mode. if disabled, panic (and thus faults) will be silent |
55+
| `__PANIC_SHORT_FILENAMES` | uses only the filename, not the full path, in panic messages |
56+
| `__OMIT_PANIC_MESSAGE` | omits the panic message string from panic output. only file and line number will be printed. this reduces the output size, but you'll have to look up the message yourself |
57+
| `PANIC_USART[n]_TX_PIN` | set the gpio pin that is used to transmit panic messages. `[n]` can be any value in [1,2,3,4], and up to four outputs may be defined |
58+
| `HARDFAULT_EXCLUDE_CFSR_INFO` | exclude CFSR flag messages from fault output. this will reduce the output size, but you'll have to look up the flag values yourself |
59+
| `HANG_ON_PANIC` | enter a infinite loop on panic condition. default behaviour is to reset the mcu |
6260

6361
```ini
6462
build_flags =

cores/arduino/core_checks.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,31 @@
33
#ifdef __CORE_DEBUG
44
#warning "'__CORE_DEBUG' is defined, HC32 Arduino Core Debug is Enabled"
55

6-
#ifdef __CORE_DEBUG_SHORT_FILENAMES
7-
#warning "'__CORE_DEBUG_SHORT_FILENAMES' is defined, short filenames are used in debug output"
6+
#if !ENABLE_PANIC_HANDLER
7+
#warning "panic handler is disabled, but __CORE_DEBUG mode is enabled! panic output will not be available"
88
#endif
9+
#endif // __CORE_DEBUG
910

10-
#ifdef __CORE_DEBUG_OMIT_PANIC_MESSAGE
11-
#warning "'__CORE_DEBUG_OMIT_PANIC_MESSAGE' is defined, panic message is omitted"
11+
#if PANIC_ENABLE
12+
#if defined(PANIC_USART1_TX_PIN) || defined(PANIC_USART2_TX_PIN) || defined(PANIC_USART3_TX_PIN) || defined(PANIC_USART3_TX_PIN)
13+
#warning "PANIC_ENABLE defined alongside PANIC_USARTn_TX_PIN. please disable one of them"
1214
#endif
15+
#endif // PANIC_ENABLE
16+
17+
#if ENABLE_PANIC_HANDLER
18+
#ifdef __PANIC_SHORT_FILENAMES
19+
#warning "'__PANIC_SHORT_FILENAMES' is defined, short filenames are used in debug output"
1320
#endif
1421

15-
#if !defined(__CORE_DEBUG) && PANIC_PRINT_ENABLED
16-
#warning "panic output is enabled, but __CORE_DEBUG mode is disabled! panic output will not be available"
22+
#ifdef __OMIT_PANIC_MESSAGE
23+
#warning "'__OMIT_PANIC_MESSAGE' is defined, panic message is omitted"
1724
#endif
25+
#endif // ENABLE_PANIC_HANDLER
1826

1927
#ifdef CORE_DISABLE_FAULT_HANDLER
2028
#warning "'CORE_DISABLE_FAULT_HANDLER' is defined, core-internal fault handler is disabled"
2129
#endif
30+
31+
#ifdef ENABLE_MICROS
32+
#warning "ENABLE_MICROS is deprecated. micros() is always available"
33+
#endif

cores/arduino/core_hooks.h

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -71,38 +71,6 @@ extern "C"
7171
* 7. call power_mode_update_post()
7272
*/
7373
DEF_HOOK(sysclock_init);
74-
75-
/**
76-
* hook called before panic_begin() is executed
77-
*
78-
* @note panic_printf() is not available at this point
79-
* @note code in this hook should avoid creating another panic
80-
* @note code in this hook should assume the system to be in a unstable state
81-
* @note this hook may be called from a IRQ context, so keep it short and sweet
82-
*/
83-
DEF_HOOK(panic_begin_pre);
84-
85-
/**
86-
* hook called after panic_begin() was executed
87-
*
88-
* @note panic_printf() is available at this point
89-
* @note code in this hook should avoid creating another panic
90-
* @note code in this hook should assume the system to be in a unstable state
91-
* @note this hook may be called from a IRQ context, so keep it short and sweet
92-
*/
93-
DEF_HOOK(panic_begin_post);
94-
95-
/**
96-
* hook called before panic_end() is executed
97-
*
98-
* @note panic_printf() is available at this point
99-
* @note code in this hook should avoid creating another panic
100-
* @note code in this hook should assume the system to be in a unstable state
101-
* @note this hook may be called from a IRQ context, so keep it short and sweet
102-
* @note the system will stop execution after this hook returns, either by reset or hang
103-
*/
104-
DEF_HOOK(panic_end);
105-
10674
#ifdef __cplusplus
10775
}
10876
#endif

cores/arduino/delay.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ extern "C"
4040
return systick_millis();
4141
}
4242

43-
#ifdef ENABLE_MICROS
4443
/**
4544
* \brief Returns the number of microseconds since the Arduino board began running the current program.
4645
*
@@ -55,7 +54,6 @@ extern "C"
5554
{
5655
return systick_micros();
5756
}
58-
#endif
5957

6058
/**
6159
* \brief Pauses the program for the amount of time (in miliseconds) specified as parameter.
Lines changed: 83 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,104 @@
11
#include "panic.h"
22

3-
// only compile in core debug mode
4-
#ifdef __CORE_DEBUG
5-
#include "../gpio/gpio.h"
6-
#include "../../core_hooks.h"
7-
#include "../usart/usart_sync.h"
8-
#include <stdarg.h>
9-
#include <stdio.h>
10-
#include <hc32_ddl.h>
3+
#if ENABLE_PANIC_HANDLER
114

12-
// only compile usart output code if a panic print is enabled
13-
#ifdef PANIC_PRINT_ENABLED
14-
15-
// panic usart baud rate
16-
#ifndef PANIC_USART_BAUDRATE
17-
#define PANIC_USART_BAUDRATE 115200
18-
#endif
19-
20-
// panic usart configuration
21-
const stc_usart_uart_init_t panic_usart_config = {
22-
.enClkMode = UsartIntClkCkNoOutput,
23-
.enClkDiv = UsartClkDiv_1,
24-
.enDataLength = UsartDataBits8,
25-
.enDirection = UsartDataLsbFirst,
26-
.enStopBit = UsartOneStopBit,
27-
.enParity = UsartParityNone,
28-
.enSampleMode = UsartSampleBit8,
29-
.enDetectMode = UsartStartBitFallEdge,
30-
.enHwFlow = UsartRtsEnable,
31-
};
5+
#include "../usart/usart_sync.h"
6+
#include <hc32_ddl.h>
7+
#include <stdarg.h>
8+
#include <stdio.h>
329

3310
//
34-
// panic helper functions
11+
// default panic output
3512
//
36-
#define PANIC_PRINTF_BUFFER_SIZE 256
37-
char panic_printf_buffer[PANIC_PRINTF_BUFFER_SIZE];
38-
39-
void panic_begin(void)
13+
__attribute__((weak)) void panic_begin(void)
4014
{
41-
core_hook_panic_begin_pre();
15+
#if PANIC_OUTPUT_AVAILABLE
16+
const stc_usart_uart_init_t panic_usart_config = {
17+
.enClkMode = UsartIntClkCkNoOutput,
18+
.enClkDiv = UsartClkDiv_1,
19+
.enDataLength = UsartDataBits8,
20+
.enDirection = UsartDataLsbFirst,
21+
.enStopBit = UsartOneStopBit,
22+
.enParity = UsartParityNone,
23+
.enSampleMode = UsartSampleBit8,
24+
.enDetectMode = UsartStartBitFallEdge,
25+
.enHwFlow = UsartRtsEnable,
26+
};
4227

43-
// initialize USART
44-
#ifdef PANIC_USART1_TX_PIN
45-
usart_sync_init(M4_USART1, PANIC_USART1_TX_PIN, PANIC_USART_BAUDRATE, &panic_usart_config);
46-
#endif
47-
#ifdef PANIC_USART2_TX_PIN
48-
usart_sync_init(M4_USART2, PANIC_USART2_TX_PIN, PANIC_USART_BAUDRATE, &panic_usart_config);
49-
#endif
50-
#ifdef PANIC_USART3_TX_PIN
51-
usart_sync_init(M4_USART3, PANIC_USART3_TX_PIN, PANIC_USART_BAUDRATE, &panic_usart_config);
52-
#endif
53-
#ifdef PANIC_USART4_TX_PIN
54-
usart_sync_init(M4_USART4, PANIC_USART4_TX_PIN, PANIC_USART_BAUDRATE, &panic_usart_config);
55-
#endif
28+
// initialize USART
29+
#ifdef PANIC_USART1_TX_PIN
30+
usart_sync_init(M4_USART1, PANIC_USART1_TX_PIN, PANIC_USART_BAUDRATE, &panic_usart_config);
31+
#endif
32+
#ifdef PANIC_USART2_TX_PIN
33+
usart_sync_init(M4_USART2, PANIC_USART2_TX_PIN, PANIC_USART_BAUDRATE, &panic_usart_config);
34+
#endif
35+
#ifdef PANIC_USART3_TX_PIN
36+
usart_sync_init(M4_USART3, PANIC_USART3_TX_PIN, PANIC_USART_BAUDRATE, &panic_usart_config);
37+
#endif
38+
#ifdef PANIC_USART4_TX_PIN
39+
usart_sync_init(M4_USART4, PANIC_USART4_TX_PIN, PANIC_USART_BAUDRATE, &panic_usart_config);
40+
#endif
41+
#endif // PANIC_OUTPUT_AVAILABLE
42+
}
43+
44+
__attribute__((weak)) void panic_puts(const char *str)
45+
{
46+
// print to USART
47+
#ifdef PANIC_USART1_TX_PIN
48+
usart_sync_write(M4_USART1, str);
49+
#endif
50+
#ifdef PANIC_USART2_TX_PIN
51+
usart_sync_write(M4_USART2, str);
52+
#endif
53+
#ifdef PANIC_USART3_TX_PIN
54+
usart_sync_write(M4_USART3, str);
55+
#endif
56+
#ifdef PANIC_USART4_TX_PIN
57+
usart_sync_write(M4_USART4, str);
58+
#endif
59+
}
5660

57-
core_hook_panic_begin_post();
61+
__attribute__((noreturn, weak)) void panic_end(void)
62+
{
63+
#ifdef HANG_ON_PANIC
64+
// enter infinite loop
65+
while (1)
66+
;
67+
#else
68+
// reset MCU
69+
NVIC_SystemReset();
70+
#endif
5871
}
5972

73+
//
74+
// helper functions
75+
//
76+
static char panic_printf_buffer[PANIC_PRINTF_BUFFER_SIZE];
77+
6078
size_t panic_printf(const char *fmt, ...)
6179
{
62-
// format the string into buffer
63-
va_list args;
64-
va_start(args, fmt);
65-
size_t len = vsnprintf(panic_printf_buffer, PANIC_PRINTF_BUFFER_SIZE, fmt, args);
66-
va_end(args);
80+
// format the string into buffer
81+
va_list args;
82+
va_start(args, fmt);
83+
size_t len = vsnprintf(panic_printf_buffer, PANIC_PRINTF_BUFFER_SIZE, fmt, args);
84+
va_end(args);
6785

68-
// print to USART
69-
#ifdef PANIC_USART1_TX_PIN
70-
usart_sync_write(M4_USART1, panic_printf_buffer);
71-
#endif
72-
#ifdef PANIC_USART2_TX_PIN
73-
usart_sync_write(M4_USART2, panic_printf_buffer);
74-
#endif
75-
#ifdef PANIC_USART3_TX_PIN
76-
usart_sync_write(M4_USART3, panic_printf_buffer);
77-
#endif
78-
#ifdef PANIC_USART4_TX_PIN
79-
usart_sync_write(M4_USART4, panic_printf_buffer);
80-
#endif
86+
// then use puts to print it
87+
panic_puts(panic_printf_buffer);
8188

82-
// return length of formatted string
83-
return len;
89+
// return length of formatted string
90+
return len;
8491
}
8592

86-
#endif // PANIC_PRINT_ENABLED
87-
88-
void panic_end(void)
93+
void _panic(const char *message)
8994
{
90-
core_hook_panic_end();
95+
if (message != NULL)
96+
{
97+
panic_begin();
98+
panic_printf(message);
99+
}
91100

92-
#ifdef HANG_ON_PANIC
93-
// enter infinite loop
94-
while (1)
95-
;
96-
#else
97-
// reset MCU
98-
NVIC_SystemReset();
99-
#endif
101+
panic_end();
100102
}
101103

102-
#endif // __CORE_DEBUG
104+
#endif // ENABLE_PANIC_HANDLER

0 commit comments

Comments
 (0)