Skip to content

Commit 6389584

Browse files
committed
Bluetooth: Controller: Reduce assertion check code size
Reduce Controller assertion check code size for ARM Cortex-M CPUs by using the undefined instruction exception. `arm-none-eabi-addr2line` commandline can be used to get the source file and line number. Signed-off-by: Vinayak Kariappa Chettimada <[email protected]>
1 parent 9db3045 commit 6389584

File tree

6 files changed

+65
-12
lines changed

6 files changed

+65
-12
lines changed

doc/connectivity/bluetooth/api/hci.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,14 +1085,13 @@ represents.
10851085
| Event | Event Code | Event Parameters |
10861086
+-------------------------------+------------+-------------------------------+
10871087
| Fatal_Error | 0xFF | Subevent_Code, |
1088-
| | | Error_Data_Type, |
10891088
| | | Error_Data |
10901089
+-------------------------------+------------+-------------------------------+
10911090

1092-
The Error_Data_Type provides an information about what is the Error_Data size
1093-
and content.
1091+
The Subevent_Code provides an information about what is the Error_Data size and
1092+
content.
10941093

1095-
Error_Data_Type: Size: 1 Octet
1094+
Subevent_Code: Size: 1 Octet
10961095
+--------------------+--------------------------------------+
10971096
| Value | Parameter Description |
10981097
+--------------------+--------------------------------------+
@@ -1157,6 +1156,7 @@ Zephyr Fatal Error event may be generated by k_sys_fatal_error_handler.
11571156
| a4 | 4 octets | General purpose register |
11581157
| ip | 4 octets | Instruction pointer register |
11591158
| lr | 4 octets | Link register |
1159+
| pc | 4 octets | Program counter register |
11601160
| xpsr | 4 octets | Program status register |
11611161
+--------------------+--------------------------------------------+
11621162

include/zephyr/bluetooth/hci_vs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,6 @@ struct bt_hci_evt_vs {
222222
uint8_t subevent;
223223
} __packed;
224224

225-
#define BT_HCI_EVT_VS_FATAL_ERROR 0x02
226-
227225
#define BT_HCI_EVT_VS_ERROR_DATA_TYPE_STACK_FRAME 0x01
228226
#define BT_HCI_EVT_VS_ERROR_DATA_TYPE_CTRL_ASSERT 0x02
229227
#define BT_HCI_EVT_VS_ERROR_DATA_TYPE_TRACE 0x03
@@ -234,8 +232,10 @@ struct bt_hci_vs_fata_error_cpu_data_cortex_m {
234232
uint32_t a4;
235233
uint32_t ip;
236234
uint32_t lr;
235+
uint32_t pc;
237236
uint32_t xpsr;
238237
} __packed;
238+
239239
#define BT_HCI_EVT_VS_ERROR_CPU_TYPE_CORTEX_M 0x01
240240
struct bt_hci_vs_fatal_error_stack_frame {
241241
uint32_t reason;

subsys/bluetooth/controller/Kconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,9 @@ rsource "Kconfig.df"
11881188
rsource "Kconfig.ll_sw_split"
11891189
rsource "Kconfig.dtm"
11901190

1191+
config BT_CTLR_ASSERT_OPTIMIZE_FOR_SIZE_SUPPORT
1192+
bool
1193+
11911194
config BT_CTLR_ASSERT_DEBUG
11921195
bool "Development asserts"
11931196
default y
@@ -1209,6 +1212,18 @@ config BT_CTLR_ASSERT_HANDLER
12091212
and will be invoked whenever the controller code encounters
12101213
an unrecoverable error.
12111214

1215+
config BT_CTLR_ASSERT_OPTIMIZE_FOR_SIZE
1216+
bool "Assertions optimized for code size"
1217+
depends on BT_CTLR_ASSERT_OPTIMIZE_FOR_SIZE_SUPPORT
1218+
default y
1219+
help
1220+
Optimize Controller assertion check for code size.
1221+
1222+
Example, reduces assertion check code size for ARM Cortex-M CPUs by using the undefined
1223+
instruction exception.
1224+
`arm-none-eabi-addr2line` commandline can be used to get the source file and line number
1225+
from the program counter value.
1226+
12121227
config BT_CTLR_VS_SCAN_REQ_RX
12131228
bool "Use scan request reporting"
12141229
depends on BT_HCI_VS && !BT_CTLR_ADV_EXT

subsys/bluetooth/controller/Kconfig.ll_sw_split

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ config BT_LLL_VENDOR_NORDIC
7070

7171
select BT_TICKER_PREFER_START_BEFORE_STOP if BT_TICKER_SLOT_AGNOSTIC
7272

73+
select BT_CTLR_ASSERT_OPTIMIZE_FOR_SIZE_SUPPORT if CPU_CORTEX_M
74+
7375
default y
7476
help
7577
Use Nordic Lower Link Layer implementation.

subsys/bluetooth/controller/hal/debug.h

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,66 @@
11
/*
2-
* Copyright (c) 2016 Nordic Semiconductor ASA
2+
* Copyright (c) 2016-2025 Nordic Semiconductor ASA
33
* Copyright (c) 2016 Vinayak Kariappa Chettimada
44
*
55
* SPDX-License-Identifier: Apache-2.0
66
*/
77

88
#include "common/assert.h"
99

10-
#ifdef CONFIG_BT_CTLR_ASSERT_HANDLER
10+
#if defined(CONFIG_BT_CTLR_ASSERT_HANDLER)
1111
void bt_ctlr_assert_handle(char *file, uint32_t line);
12+
13+
#if defined(CONFIG_BT_CTLR_ASSERT_OPTIMIZE_FOR_SIZE)
14+
BUILD_ASSERT(IS_ENABLED(CONFIG_CPU_CORTEX_M));
15+
/* Generate assertion as undefined instruction exception.
16+
*/
17+
#define LL_ASSERT(x) \
18+
do { \
19+
if (unlikely(!(x))) { \
20+
__asm__ inline volatile (".inst 0xde00\n"); \
21+
} \
22+
} while (0)
23+
24+
#else /* !CONFIG_BT_CTLR_ASSERT_OPTIMIZE_FOR_SIZE */
25+
/* Generate assertion with file name and line number.
26+
* NOTE: Variable code size increase per assertion check, depends on full file name path string
27+
* length.
28+
*/
1229
#define LL_ASSERT(cond) \
13-
if (!(cond)) { \
30+
if (unlikely(!(cond))) { \
1431
BT_ASSERT_PRINT(cond); \
1532
bt_ctlr_assert_handle(__FILE__, __LINE__); \
1633
}
34+
#endif /* !CONFIG_BT_CTLR_ASSERT_OPTIMIZE_FOR_SIZE */
35+
1736
#define LL_ASSERT_MSG(cond, fmt, ...) \
18-
if (!(cond)) { \
37+
if (unlikely(!(cond))) { \
1938
BT_ASSERT_PRINT(cond); \
2039
BT_ASSERT_PRINT_MSG(fmt, ##__VA_ARGS__); \
2140
bt_ctlr_assert_handle(__FILE__, __LINE__); \
2241
}
23-
#else
42+
43+
#else /* !CONFIG_BT_CTLR_ASSERT_HANDLER */
44+
45+
#if defined(CONFIG_BT_CTLR_ASSERT_OPTIMIZE_FOR_SIZE)
46+
BUILD_ASSERT(IS_ENABLED(CONFIG_CPU_CORTEX_M));
47+
/* Generate assertion as undefined instruction exception.
48+
*/
49+
#define LL_ASSERT(x) \
50+
do { \
51+
if (unlikely(!(x))) { \
52+
__asm__ inline volatile (".inst 0xde00\n"); \
53+
} \
54+
} while (0)
55+
56+
#else /* !CONFIG_BT_CTLR_ASSERT_OPTIMIZE_FOR_SIZE */
2457
#define LL_ASSERT(cond) \
2558
BT_ASSERT(cond)
59+
#endif /* !CONFIG_BT_CTLR_ASSERT_OPTIMIZE_FOR_SIZE */
60+
2661
#define LL_ASSERT_MSG(cond, fmt, ...) \
2762
BT_ASSERT_MSG(cond, fmt, ##__VA_ARGS__)
28-
#endif
63+
#endif /* !CONFIG_BT_CTLR_ASSERT_HANDLER */
2964

3065
/* Fatal asserts.
3166
* The Controller will otherwise misbehave causing memory leak or system-wide memory corruptions due

subsys/bluetooth/controller/hci/hci.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5284,6 +5284,7 @@ static void vs_err_fatal_cpu_data_fill(bt_hci_vs_fatal_error_cpu_data *cpu_data,
52845284
cpu_data->a4 = sys_cpu_to_le32(esf->basic.a4);
52855285
cpu_data->ip = sys_cpu_to_le32(esf->basic.ip);
52865286
cpu_data->lr = sys_cpu_to_le32(esf->basic.lr);
5287+
cpu_data->pc = sys_cpu_to_le32(esf->basic.pc);
52875288
cpu_data->xpsr = sys_cpu_to_le32(esf->basic.xpsr);
52885289
}
52895290
#endif /* CONFIG_CPU_CORTEX_M */

0 commit comments

Comments
 (0)