eric2013 发表于 2021-10-7 09:14:31

STM32F4的新版HAL库增加的几个原子操作API

代码:

/* Use of CMSIS compiler intrinsics for register exclusive access */
/* Atomic 32-bit register access macro to set one or several bits */
#define ATOMIC_SET_BIT(REG, BIT)                           \
do {                                                       \
    uint32_t val;                                          \
    do {                                                   \
      val = __LDREXW((__IO uint32_t *)&(REG)) | (BIT);       \
    } while ((__STREXW(val,(__IO uint32_t *)&(REG))) != 0U); \
} while(0)

/* Atomic 32-bit register access macro to clear one or several bits */
#define ATOMIC_CLEAR_BIT(REG, BIT)                           \
do {                                                       \
    uint32_t val;                                          \
    do {                                                   \
      val = __LDREXW((__IO uint32_t *)&(REG)) & ~(BIT);      \
    } while ((__STREXW(val,(__IO uint32_t *)&(REG))) != 0U); \
} while(0)

/* Atomic 32-bit register access macro to clear and set one or several bits */
#define ATOMIC_MODIFY_REG(REG, CLEARMSK, SETMASK)                        \
do {                                                                     \
    uint32_t val;                                                          \
    do {                                                                   \
      val = (__LDREXW((__IO uint32_t *)&(REG)) & ~(CLEARMSK)) | (SETMASK); \
    } while ((__STREXW(val,(__IO uint32_t *)&(REG))) != 0U);               \
} while(0)

/* Atomic 16-bit register access macro to set one or several bits */
#define ATOMIC_SETH_BIT(REG, BIT)                            \
do {                                                       \
    uint16_t val;                                          \
    do {                                                   \
      val = __LDREXH((__IO uint16_t *)&(REG)) | (BIT);       \
    } while ((__STREXH(val,(__IO uint16_t *)&(REG))) != 0U); \
} while(0)

/* Atomic 16-bit register access macro to clear one or several bits */
#define ATOMIC_CLEARH_BIT(REG, BIT)                        \
do {                                                       \
    uint16_t val;                                          \
    do {                                                   \
      val = __LDREXH((__IO uint16_t *)&(REG)) & ~(BIT);      \
    } while ((__STREXH(val,(__IO uint16_t *)&(REG))) != 0U); \
} while(0)

/* Atomic 16-bit register access macro to clear and set one or several bits */
#define ATOMIC_MODIFYH_REG(REG, CLEARMSK, SETMASK)                         \
do {                                                                     \
    uint16_t val;                                                          \
    do {                                                                   \
      val = (__LDREXH((__IO uint16_t *)&(REG)) & ~(CLEARMSK)) | (SETMASK); \
    } while ((__STREXH(val,(__IO uint16_t *)&(REG))) != 0U);               \
} while(0)


eric2013 发表于 2021-10-7 09:18:37

RTX5里面也有一大批原子操作函数。

云琴箫龙 发表于 2021-11-12 14:47:58

请教硬汉哥为何直接调用会有个黄色警告?

eric2013 发表于 2021-11-13 08:53:48

云琴箫龙 发表于 2021-11-12 14:47
请教硬汉哥为何直接调用会有个黄色警告?

回头我试试,ST的这个还没有测试过。

PYL4869 发表于 2022-5-11 09:48:20

请问在哪里可以查询到这种原子操作呢,在F4的HAL库的哪个文件里?

skylar 发表于 2023-6-20 10:16:52

云琴箫龙 发表于 2021-11-12 14:47
请教硬汉哥为何直接调用会有个黄色警告?

请问您这个问题解决了没,加了头文件也还有警告,但编译不报错。

eric2013 发表于 2023-6-20 11:08:22

skylar 发表于 2023-6-20 10:16
请问您这个问题解决了没,加了头文件也还有警告,但编译不报错。

编译不报错没关系
页: [1]
查看完整版本: STM32F4的新版HAL库增加的几个原子操作API