fix(hal): Wrap LL macros with atomic env in do-while for control safety

- Standardize all LL macros using atomic env variables with `do { ... } while (0)`
- Prevent potential macro misuse in control flow constructs (e.g., if/else)
- Affected targets: esp32, esp32c2, esp32c5, esp32s3, esp32p4, etc.
- Affected modules include: LCD, I2S, TIMER, and others
This commit is contained in:
Yuan Yu
2025-05-22 14:41:19 +08:00
parent 5133b89183
commit 18091976ec
162 changed files with 1742 additions and 500 deletions

View File

@@ -852,7 +852,10 @@ static inline void lp_i2c_ll_set_source_clk(i2c_dev_t *hw, soc_periph_lp_i2c_clk
}
/// LP_CLKRST.lpperi is a shared register, so this function must be used in an atomic way
#define lp_i2c_ll_set_source_clk(...) (void)__DECLARE_RCC_ATOMIC_ENV; lp_i2c_ll_set_source_clk(__VA_ARGS__)
#define lp_i2c_ll_set_source_clk(...) do { \
(void)__DECLARE_RCC_ATOMIC_ENV; \
lp_i2c_ll_set_source_clk(__VA_ARGS__); \
} while(0)
/**
* @brief Enable bus clock for the LP I2C module
@@ -867,7 +870,10 @@ static inline void _lp_i2c_ll_enable_bus_clock(int hw_id, bool enable)
}
/// LPPERI.clk_en is a shared register, so this function must be used in an atomic way
#define lp_i2c_ll_enable_bus_clock(...) (void)__DECLARE_RCC_ATOMIC_ENV; _lp_i2c_ll_enable_bus_clock(__VA_ARGS__)
#define lp_i2c_ll_enable_bus_clock(...) do { \
(void)__DECLARE_RCC_ATOMIC_ENV; \
_lp_i2c_ll_enable_bus_clock(__VA_ARGS__); \
} while(0)
/**
* @brief Reset LP I2C module
@@ -882,7 +888,10 @@ static inline void lp_i2c_ll_reset_register(int hw_id)
}
/// LPPERI.reset_en is a shared register, so this function must be used in an atomic way
#define lp_i2c_ll_reset_register(...) (void)__DECLARE_RCC_ATOMIC_ENV; lp_i2c_ll_reset_register(__VA_ARGS__)
#define lp_i2c_ll_reset_register(...) do { \
(void)__DECLARE_RCC_ATOMIC_ENV; \
lp_i2c_ll_reset_register(__VA_ARGS__); \
} while(0)
/**
* @brief Enable I2C peripheral controller clock

View File

@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -25,7 +25,10 @@ static inline void _lp_clkrst_ll_enable_rng_clock(bool en)
}
/// LPPERI.clk_en is a shared register, so this function must be used in an atomic way
#define lp_clkrst_ll_enable_rng_clock(...) (void)__DECLARE_RCC_ATOMIC_ENV; _lp_clkrst_ll_enable_rng_clock(__VA_ARGS__)
#define lp_clkrst_ll_enable_rng_clock(...) do { \
(void)__DECLARE_RCC_ATOMIC_ENV; \
_lp_clkrst_ll_enable_rng_clock(__VA_ARGS__); \
} while(0)
#ifdef __cplusplus
}

View File

@@ -29,7 +29,10 @@ static inline __attribute__((always_inline)) void _regi2c_ctrl_ll_master_enable_
/// use a macro to wrap the function, force the caller to use it in a critical section
/// the critical section needs to declare the __DECLARE_RCC_RC_ATOMIC_ENV variable in advance
#define regi2c_ctrl_ll_master_enable_clock(...) (void)__DECLARE_RCC_RC_ATOMIC_ENV; _regi2c_ctrl_ll_master_enable_clock(__VA_ARGS__)
#define regi2c_ctrl_ll_master_enable_clock(...) do { \
(void)__DECLARE_RCC_RC_ATOMIC_ENV; \
_regi2c_ctrl_ll_master_enable_clock(__VA_ARGS__); \
} while(0)
/**
* @brief Check whether analog I2C master clock is enabled
@@ -50,7 +53,10 @@ static inline __attribute__((always_inline)) void _regi2c_ctrl_ll_master_reset(v
/// use a macro to wrap the function, force the caller to use it in a critical section
/// the critical section needs to declare the __DECLARE_RCC_RC_ATOMIC_ENV variable in advance
#define regi2c_ctrl_ll_master_reset(...) (void)__DECLARE_RCC_RC_ATOMIC_ENV; _regi2c_ctrl_ll_master_reset(__VA_ARGS__)
#define regi2c_ctrl_ll_master_reset(...) do { \
(void)__DECLARE_RCC_RC_ATOMIC_ENV; \
_regi2c_ctrl_ll_master_reset(__VA_ARGS__); \
} while(0)
/**
* @brief Force enable analog I2C master clock

View File

@@ -64,7 +64,10 @@ static inline void _rtcio_ll_enable_io_clock(bool enable)
}
}
#define rtcio_ll_enable_io_clock(...) (void)__DECLARE_RCC_ATOMIC_ENV; _rtcio_ll_enable_io_clock(__VA_ARGS__)
#define rtcio_ll_enable_io_clock(...) do { \
(void)__DECLARE_RCC_ATOMIC_ENV; \
_rtcio_ll_enable_io_clock(__VA_ARGS__); \
} while(0)
/**
* @brief Select the rtcio function.

View File

@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -49,7 +49,10 @@ static inline void systimer_ll_enable_bus_clock(bool enable)
/// use a macro to wrap the function, force the caller to use it in a critical section
/// the critical section needs to declare the __DECLARE_RCC_RC_ATOMIC_ENV variable in advance
#define systimer_ll_enable_bus_clock(...) (void)__DECLARE_RCC_RC_ATOMIC_ENV; systimer_ll_enable_bus_clock(__VA_ARGS__)
#define systimer_ll_enable_bus_clock(...) do { \
(void)__DECLARE_RCC_RC_ATOMIC_ENV; \
systimer_ll_enable_bus_clock(__VA_ARGS__); \
} while(0)
/**
* @brief Reset the systimer module
@@ -64,7 +67,10 @@ static inline void systimer_ll_reset_register(void)
/// use a macro to wrap the function, force the caller to use it in a critical section
/// the critical section needs to declare the __DECLARE_RCC_RC_ATOMIC_ENV variable in advance
#define systimer_ll_reset_register(...) (void)__DECLARE_RCC_RC_ATOMIC_ENV; systimer_ll_reset_register(__VA_ARGS__)
#define systimer_ll_reset_register(...) do { \
(void)__DECLARE_RCC_RC_ATOMIC_ENV; \
systimer_ll_reset_register(__VA_ARGS__); \
} while(0)
/********************** ETM *****************************/

View File

@@ -68,7 +68,10 @@ static inline void _timer_ll_enable_bus_clock(int group_id, bool enable)
/// use a macro to wrap the function, force the caller to use it in a critical section
/// the critical section needs to declare the __DECLARE_RCC_RC_ATOMIC_ENV variable in advance
#define timer_ll_enable_bus_clock(...) (void)__DECLARE_RCC_RC_ATOMIC_ENV; _timer_ll_enable_bus_clock(__VA_ARGS__)
#define timer_ll_enable_bus_clock(...) do { \
(void)__DECLARE_RCC_RC_ATOMIC_ENV; \
_timer_ll_enable_bus_clock(__VA_ARGS__); \
} while(0)
/**
* @brief Reset the timer group module
@@ -94,7 +97,10 @@ static inline void _timer_ll_reset_register(int group_id)
/// use a macro to wrap the function, force the caller to use it in a critical section
/// the critical section needs to declare the __DECLARE_RCC_RC_ATOMIC_ENV variable in advance
#define timer_ll_reset_register(...) (void)__DECLARE_RCC_RC_ATOMIC_ENV; _timer_ll_reset_register(__VA_ARGS__)
#define timer_ll_reset_register(...) do { \
(void)__DECLARE_RCC_RC_ATOMIC_ENV; \
_timer_ll_reset_register(__VA_ARGS__); \
} while(0)
/**
* @brief Set clock source for timer

View File

@@ -149,7 +149,10 @@ static inline void lp_uart_ll_set_source_clk(uart_dev_t *hw, soc_periph_lp_uart_
}
/// LP_CLKRST.lpperi is a shared register, so this function must be used in an atomic way
#define lp_uart_ll_set_source_clk(...) (void)__DECLARE_RCC_ATOMIC_ENV; lp_uart_ll_set_source_clk(__VA_ARGS__)
#define lp_uart_ll_set_source_clk(...) do { \
(void)__DECLARE_RCC_ATOMIC_ENV; \
lp_uart_ll_set_source_clk(__VA_ARGS__); \
} while(0)
/**
* @brief Configure the lp uart baud-rate.
@@ -192,7 +195,10 @@ static inline void _lp_uart_ll_enable_bus_clock(int hw_id, bool enable)
}
/// LPPERI.clk_en is a shared register, so this function must be used in an atomic way
#define lp_uart_ll_enable_bus_clock(...) (void)__DECLARE_RCC_ATOMIC_ENV; _lp_uart_ll_enable_bus_clock(__VA_ARGS__)
#define lp_uart_ll_enable_bus_clock(...) do { \
(void)__DECLARE_RCC_ATOMIC_ENV; \
_lp_uart_ll_enable_bus_clock(__VA_ARGS__); \
} while(0)
/**
* @brief Enable the UART clock.
@@ -231,7 +237,10 @@ static inline void lp_uart_ll_reset_register(int hw_id)
}
/// LPPERI.reset_en is a shared register, so this function must be used in an atomic way
#define lp_uart_ll_reset_register(...) (void)__DECLARE_RCC_ATOMIC_ENV; lp_uart_ll_reset_register(__VA_ARGS__)
#define lp_uart_ll_reset_register(...) do { \
(void)__DECLARE_RCC_ATOMIC_ENV; \
lp_uart_ll_reset_register(__VA_ARGS__); \
} while(0)
/*************************************** General LL functions ******************************************/