Skip to content

Commit 6d502b7

Browse files
committed
Merge branch 'bugfix/fix_lcd_ll_macro_safety' into 'master'
fix(hal): Wrap LL macros with atomic env in do-while for control safety Closes IDF-11981 See merge request espressif/esp-idf!39202
2 parents 4d59537 + 1809197 commit 6d502b7

File tree

162 files changed

+1741
-499
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

162 files changed

+1741
-499
lines changed

components/hal/README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,17 @@ One compromise is to **highlight** the LL function which needs the caller to use
1919
```c
2020
/// use a macro to wrap the function, force the caller to use it in a critical section
2121
/// the critical section needs to declare the __DECLARE_RCC_RC_ATOMIC_ENV variable in advance
22-
#define timer_ll_reset_register(...) (void)__DECLARE_RCC_RC_ATOMIC_ENV; timer_ll_reset_register(__VA_ARGS__)
22+
#define timer_ll_reset_register(...) do { \
23+
(void)__DECLARE_RCC_RC_ATOMIC_ENV; \
24+
timer_ll_reset_register(__VA_ARGS__); \
25+
} while(0)
2326

2427
/// use a macro to wrap the function, force the caller to use it in a critical section
2528
/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance
26-
#define timer_ll_set_clock_source(...) (void)__DECLARE_RCC_ATOMIC_ENV; timer_ll_set_clock_source(__VA_ARGS__)
29+
#define timer_ll_set_clock_source(...) do { \
30+
(void)__DECLARE_RCC_ATOMIC_ENV; \
31+
timer_ll_set_clock_source(__VA_ARGS__); \
32+
} while(0)
2733
```
2834
2935
By referencing a variable which is only declared in the critical section, the compiler will report an error if the caller forgets to use the critical section. The following macros are provided by `esp_private/periph_ctrl.h`, which contain the above *magic* variables.

components/hal/esp32/include/hal/aes_ll.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2020-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -43,7 +43,10 @@ static inline void aes_ll_enable_bus_clock(bool enable)
4343

4444
/// use a macro to wrap the function, force the caller to use it in a critical section
4545
/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance
46-
#define aes_ll_enable_bus_clock(...) (void)__DECLARE_RCC_ATOMIC_ENV; aes_ll_enable_bus_clock(__VA_ARGS__)
46+
#define aes_ll_enable_bus_clock(...) do { \
47+
(void)__DECLARE_RCC_ATOMIC_ENV; \
48+
aes_ll_enable_bus_clock(__VA_ARGS__); \
49+
} while(0)
4750

4851
/**
4952
* @brief Reset the AES peripheral module
@@ -60,7 +63,10 @@ static inline void aes_ll_reset_register(void)
6063

6164
/// use a macro to wrap the function, force the caller to use it in a critical section
6265
/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance
63-
#define aes_ll_reset_register(...) (void)__DECLARE_RCC_ATOMIC_ENV; aes_ll_reset_register(__VA_ARGS__)
66+
#define aes_ll_reset_register(...) do { \
67+
(void)__DECLARE_RCC_ATOMIC_ENV; \
68+
aes_ll_reset_register(__VA_ARGS__); \
69+
} while(0)
6470

6571
/**
6672
* @brief Write the encryption/decryption key to hardware

components/hal/esp32/include/hal/emac_ll.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,10 @@ static inline void emac_ll_enable_bus_clock(int group_id, bool enable)
150150

151151
/// use a macro to wrap the function, force the caller to use it in a critical section
152152
/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance
153-
#define emac_ll_enable_bus_clock(...) (void)__DECLARE_RCC_ATOMIC_ENV; emac_ll_enable_bus_clock(__VA_ARGS__)
153+
#define emac_ll_enable_bus_clock(...) do { \
154+
(void)__DECLARE_RCC_ATOMIC_ENV; \
155+
emac_ll_enable_bus_clock(__VA_ARGS__); \
156+
} while(0)
154157

155158
/**
156159
* @brief Reset the EMAC module
@@ -166,7 +169,10 @@ static inline void emac_ll_reset_register(int group_id)
166169

167170
/// use a macro to wrap the function, force the caller to use it in a critical section
168171
/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance
169-
#define emac_ll_reset_register(...) (void)__DECLARE_RCC_ATOMIC_ENV; emac_ll_reset_register(__VA_ARGS__)
172+
#define emac_ll_reset_register(...) do { \
173+
(void)__DECLARE_RCC_ATOMIC_ENV; \
174+
emac_ll_reset_register(__VA_ARGS__); \
175+
} while(0)
170176

171177
/************** Start of mac regs operation ********************/
172178
/* emacgmiiaddr */

components/hal/esp32/include/hal/i2s_ll.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2020-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -109,7 +109,10 @@ static inline void i2s_ll_enable_bus_clock(int i2s_id, bool enable)
109109

110110
/// use a macro to wrap the function, force the caller to use it in a critical section
111111
/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance
112-
#define i2s_ll_enable_bus_clock(...) (void)__DECLARE_RCC_ATOMIC_ENV; i2s_ll_enable_bus_clock(__VA_ARGS__)
112+
#define i2s_ll_enable_bus_clock(...) do { \
113+
(void)__DECLARE_RCC_ATOMIC_ENV; \
114+
i2s_ll_enable_bus_clock(__VA_ARGS__); \
115+
} while(0)
113116

114117

115118
/**
@@ -130,7 +133,10 @@ static inline void i2s_ll_reset_register(int i2s_id)
130133

131134
/// use a macro to wrap the function, force the caller to use it in a critical section
132135
/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance
133-
#define i2s_ll_reset_register(...) (void)__DECLARE_RCC_ATOMIC_ENV; i2s_ll_reset_register(__VA_ARGS__)
136+
#define i2s_ll_reset_register(...) do { \
137+
(void)__DECLARE_RCC_ATOMIC_ENV; \
138+
i2s_ll_reset_register(__VA_ARGS__); \
139+
} while(0)
134140

135141
/**
136142
* @brief I2S module general init, enable I2S clock.
@@ -150,7 +156,10 @@ static inline void i2s_ll_enable_core_clock(i2s_dev_t *hw, bool enable)
150156

151157
/// use a macro to wrap the function, force the caller to use it in a critical section
152158
/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance
153-
#define i2s_ll_enable_core_clock(...) (void)__DECLARE_RCC_ATOMIC_ENV; i2s_ll_enable_core_clock(__VA_ARGS__)
159+
#define i2s_ll_enable_core_clock(...) do { \
160+
(void)__DECLARE_RCC_ATOMIC_ENV; \
161+
i2s_ll_enable_core_clock(__VA_ARGS__); \
162+
} while(0)
154163

155164
/**
156165
* @brief I2S tx msb right enable

components/hal/esp32/include/hal/ledc_ll.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2019-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -57,7 +57,10 @@ static inline void ledc_ll_enable_bus_clock(bool enable)
5757

5858
/// use a macro to wrap the function, force the caller to use it in a critical section
5959
/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance
60-
#define ledc_ll_enable_bus_clock(...) (void)__DECLARE_RCC_ATOMIC_ENV; ledc_ll_enable_bus_clock(__VA_ARGS__)
60+
#define ledc_ll_enable_bus_clock(...) do { \
61+
(void)__DECLARE_RCC_ATOMIC_ENV; \
62+
ledc_ll_enable_bus_clock(__VA_ARGS__); \
63+
} while(0)
6164

6265
/**
6366
* @brief Reset whole peripheral register to init value defined by HW design
@@ -73,7 +76,10 @@ static inline void ledc_ll_enable_reset_reg(bool enable)
7376

7477
/// use a macro to wrap the function, force the caller to use it in a critical section
7578
/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance
76-
#define ledc_ll_enable_reset_reg(...) (void)__DECLARE_RCC_ATOMIC_ENV; ledc_ll_enable_reset_reg(__VA_ARGS__)
79+
#define ledc_ll_enable_reset_reg(...) do { \
80+
(void)__DECLARE_RCC_ATOMIC_ENV; \
81+
ledc_ll_enable_reset_reg(__VA_ARGS__); \
82+
} while(0)
7783

7884
/**
7985
* @brief Enable the power for LEDC memory block

components/hal/esp32/include/hal/mcpwm_ll.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -89,7 +89,10 @@ static inline void mcpwm_ll_enable_bus_clock(int group_id, bool enable)
8989

9090
/// use a macro to wrap the function, force the caller to use it in a critical section
9191
/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance
92-
#define mcpwm_ll_enable_bus_clock(...) (void)__DECLARE_RCC_ATOMIC_ENV; mcpwm_ll_enable_bus_clock(__VA_ARGS__)
92+
#define mcpwm_ll_enable_bus_clock(...) do { \
93+
(void)__DECLARE_RCC_ATOMIC_ENV; \
94+
mcpwm_ll_enable_bus_clock(__VA_ARGS__); \
95+
} while(0)
9396

9497
/**
9598
* @brief Reset the MCPWM module
@@ -109,7 +112,10 @@ static inline void mcpwm_ll_reset_register(int group_id)
109112

110113
/// use a macro to wrap the function, force the caller to use it in a critical section
111114
/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance
112-
#define mcpwm_ll_reset_register(...) (void)__DECLARE_RCC_ATOMIC_ENV; mcpwm_ll_reset_register(__VA_ARGS__)
115+
#define mcpwm_ll_reset_register(...) do { \
116+
(void)__DECLARE_RCC_ATOMIC_ENV; \
117+
mcpwm_ll_reset_register(__VA_ARGS__); \
118+
} while(0)
113119

114120
/**
115121
* @brief Enable MCPWM function clock

components/hal/esp32/include/hal/mpi_ll.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -35,7 +35,10 @@ static inline void mpi_ll_enable_bus_clock(bool enable)
3535

3636
/// use a macro to wrap the function, force the caller to use it in a critical section
3737
/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance
38-
#define mpi_ll_enable_bus_clock(...) (void)__DECLARE_RCC_ATOMIC_ENV; mpi_ll_enable_bus_clock(__VA_ARGS__)
38+
#define mpi_ll_enable_bus_clock(...) do { \
39+
(void)__DECLARE_RCC_ATOMIC_ENV; \
40+
mpi_ll_enable_bus_clock(__VA_ARGS__); \
41+
} while(0)
3942

4043
/**
4144
* @brief Reset the MPI peripheral module
@@ -51,7 +54,10 @@ static inline void mpi_ll_reset_register(void)
5154

5255
/// use a macro to wrap the function, force the caller to use it in a critical section
5356
/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance
54-
#define mpi_ll_reset_register(...) (void)__DECLARE_RCC_ATOMIC_ENV; mpi_ll_reset_register(__VA_ARGS__)
57+
#define mpi_ll_reset_register(...) do { \
58+
(void)__DECLARE_RCC_ATOMIC_ENV; \
59+
mpi_ll_reset_register(__VA_ARGS__); \
60+
} while(0)
5561

5662
/* Round up number of words to nearest
5763
512 bit (16 word) block count.

components/hal/esp32/include/hal/pcnt_ll.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -457,7 +457,10 @@ static inline void pcnt_ll_enable_bus_clock(int group_id, bool enable)
457457

458458
/// use a macro to wrap the function, force the caller to use it in a critical section
459459
/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance
460-
#define pcnt_ll_enable_bus_clock(...) (void)__DECLARE_RCC_ATOMIC_ENV; pcnt_ll_enable_bus_clock(__VA_ARGS__)
460+
#define pcnt_ll_enable_bus_clock(...) do { \
461+
(void)__DECLARE_RCC_ATOMIC_ENV; \
462+
pcnt_ll_enable_bus_clock(__VA_ARGS__); \
463+
} while(0)
461464

462465
/**
463466
* @brief Reset the PCNT module
@@ -471,7 +474,10 @@ static inline void pcnt_ll_reset_register(int group_id)
471474

472475
/// use a macro to wrap the function, force the caller to use it in a critical section
473476
/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance
474-
#define pcnt_ll_reset_register(...) (void)__DECLARE_RCC_ATOMIC_ENV; pcnt_ll_reset_register(__VA_ARGS__)
477+
#define pcnt_ll_reset_register(...) do { \
478+
(void)__DECLARE_RCC_ATOMIC_ENV; \
479+
pcnt_ll_reset_register(__VA_ARGS__); \
480+
} while(0)
475481

476482
#ifdef __cplusplus
477483
}

components/hal/esp32/include/hal/rmt_ll.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2019-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -58,7 +58,10 @@ static inline void rmt_ll_enable_bus_clock(int group_id, bool enable)
5858

5959
/// use a macro to wrap the function, force the caller to use it in a critical section
6060
/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance
61-
#define rmt_ll_enable_bus_clock(...) (void)__DECLARE_RCC_ATOMIC_ENV; rmt_ll_enable_bus_clock(__VA_ARGS__)
61+
#define rmt_ll_enable_bus_clock(...) do { \
62+
(void)__DECLARE_RCC_ATOMIC_ENV; \
63+
rmt_ll_enable_bus_clock(__VA_ARGS__); \
64+
} while(0)
6265

6366
/**
6467
* @brief Reset the RMT module
@@ -74,7 +77,10 @@ static inline void rmt_ll_reset_register(int group_id)
7477

7578
/// use a macro to wrap the function, force the caller to use it in a critical section
7679
/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance
77-
#define rmt_ll_reset_register(...) (void)__DECLARE_RCC_ATOMIC_ENV; rmt_ll_reset_register(__VA_ARGS__)
80+
#define rmt_ll_reset_register(...) do { \
81+
(void)__DECLARE_RCC_ATOMIC_ENV; \
82+
rmt_ll_reset_register(__VA_ARGS__); \
83+
} while(0)
7884

7985
/**
8086
* @brief Enable clock gate for register and memory

components/hal/esp32/include/hal/sdio_slave_ll.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ static inline void _sdio_slave_ll_enable_bus_clock(bool enable)
9292

9393
/// use a macro to wrap the function, force the caller to use it in a critical section
9494
/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance
95-
#define sdio_slave_ll_enable_bus_clock(...) (void)__DECLARE_RCC_ATOMIC_ENV; _sdio_slave_ll_enable_bus_clock(__VA_ARGS__)
95+
#define sdio_slave_ll_enable_bus_clock(...) do { \
96+
(void)__DECLARE_RCC_ATOMIC_ENV; \
97+
_sdio_slave_ll_enable_bus_clock(__VA_ARGS__); \
98+
} while(0)
9699

97100
/**
98101
* @brief Reset the SDIO slave module
@@ -105,7 +108,10 @@ static inline void _sdio_slave_ll_reset_register(void)
105108

106109
/// use a macro to wrap the function, force the caller to use it in a critical section
107110
/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance
108-
#define sdio_slave_ll_reset_register(...) (void)__DECLARE_RCC_ATOMIC_ENV; _sdio_slave_ll_reset_register(__VA_ARGS__)
111+
#define sdio_slave_ll_reset_register(...) do { \
112+
(void)__DECLARE_RCC_ATOMIC_ENV; \
113+
_sdio_slave_ll_reset_register(__VA_ARGS__); \
114+
} while(0)
109115

110116
/**
111117
* Initialize the hardware.

0 commit comments

Comments
 (0)