#ifndef INCLUDED_ATOMIC_H #define INCLUDED_ATOMIC_H #ifndef INCLUDED_OS_TYPES_H #include #endif /*INCLUDED_OS_TYPES_H*/ #ifndef INCLUDED_OS_COMPILER_H #include #endif /*INCLUDED_OS_COMPILER_H*/ #ifndef INCLUDED_CMSIS_H #include #endif /*INCLUDED_CMSIS_H*/ /* ------------------------------------------------------------------------------------------------------------------ */ /* */ #ifdef __cplusplus extern "C" { #endif /* ======================================================================== */ /* 1. 内存序枚举 (简化版,适配 Cortex-M3 硬件特性) */ /* ======================================================================== */ typedef enum { memory_order_relaxed = 0, /* Maps to simple atomic access (no barriers) */ memory_order_consume = 1, memory_order_acquire = 2, /* Usually implemented with a DMB instruction to ensure memory operations do not cross a boundary */ memory_order_release = 3, /* Usually implemented with a DMB instruction to ensure memory operations do not cross a boundary */ memory_order_acq_rel = 4, memory_order_seq_cst = 5 /* The default and safest; guarantees absolute ordering (uses DMB/DSB) */ } atomic_memory_order; /* ======================================================================== */ /* 2. 原子类型定义 */ /* ======================================================================== */ typedef volatile os_uint_t atomic_uint_t; typedef volatile os_int_t atomic_int_t; typedef volatile uint32_t atomic_uint32_t; typedef volatile int32_t atomic_int32_t; typedef volatile uint16_t atomic_uint16_t; typedef volatile int16_t atomic_int16_t; typedef volatile uint8_t atomic_uint8_t; typedef volatile int8_t atomic_int8_t; typedef volatile bool atomic_bool; /* 指针原子类型 (32位系统) */ typedef volatile void* atomic_ptr_t; /* ======================================================================== */ /* 3. 位带操作宏 (仅适用于 SRAM 0x20000000-0x200FFFFF 和 PERIPH 0x40000000-0x400FFFFF) */ /* ======================================================================== */ #define BITBAND_SRAM_REF 0x20000000 #define BITBAND_SRAM_BASE 0x22000000 #define BITBAND_PERI_REF 0x40000000 #define BITBAND_PERI_BASE 0x42000000 // 计算位带别名地址的宏 // addr: 原始寄存器地址 (如 &GPIOA->ODR) // bit: 位序号 (0-31) #define BITBAND_ADDR(addr, bit) \ (((uint32_t)(addr) & 0xF0000000) == 0x20000000 ? \ (BITBAND_SRAM_BASE + ((uint32_t)(addr) - BITBAND_SRAM_REF) * 32 + (bit) * 4) : \ (BITBAND_PERI_BASE + ((uint32_t)(addr) - BITBAND_PERI_REF) * 32 + (bit) * 4)) // 访问位带地址的指针宏 #define BITBAND_PTR(addr, bit) ((volatile uint32_t *)BITBAND_ADDR(addr, bit)) // 示例:原子操作 GPIOA Pin 5 的输出 // 定义一个指向位带别名地址的指针 //#define PA5_OUT_BIT BITBAND_ADDR(&GPIOA->ODR, 5) /* ======================================================================== */ /* 4. 核心原子操作函数声明 */ /* ======================================================================== */ /* --- 基础加载与存储 --- */ uint32_t atomic_load_32(const atomic_uint32_t *obj); void atomic_store_32(atomic_uint32_t *obj, uint32_t desired); /* --- 交换操作, 返回交换前的值 --- */ uint32_t atomic_exchange_32(atomic_uint32_t *obj, uint32_t desired); /* --- 比较并交换 (CAS) --- */ /* 返回 true 如果交换成功,false 如果失败 (expected 会被更新为当前值) */ bool atomic_compare_exchange_strong_32(atomic_uint32_t *obj, uint32_t *expected, uint32_t desired); bool atomic_compare_exchange_weak_32(atomic_uint32_t *obj, uint32_t *expected, uint32_t desired); /* --- 算术与位运算 (Fetch-and-Op) --- */ /* 返回操作前的旧值 */ uint32_t atomic_fetch_add_32(atomic_uint32_t *obj, uint32_t operand); uint32_t atomic_fetch_sub_32(atomic_uint32_t *obj, uint32_t operand); uint32_t atomic_fetch_and_32(atomic_uint32_t *obj, uint32_t operand); uint32_t atomic_fetch_or_32(atomic_uint32_t *obj, uint32_t operand); uint32_t atomic_fetch_xor_32(atomic_uint32_t *obj, uint32_t operand); /* --- 8位/16位支持 (通过掩码和LDREX/STREX实现) --- */ uint8_t atomic_load_8(const atomic_uint8_t *obj); void atomic_store_8(atomic_uint8_t *obj, uint8_t desired); uint8_t atomic_fetch_add_8(atomic_uint8_t *obj, uint8_t operand); bool atomic_compare_exchange_strong_8(atomic_uint8_t *obj, uint8_t *expected, uint8_t desired); /* --- 位带专用原子操作 (仅用于 GPIO 或支持位带的寄存器/SRAM) --- */ /* 设置指定位为 1 */ void atomic_bit_set(volatile void *addr, os_uint_t bit); /* 清除指定位为 0 */ void atomic_bit_clear(volatile void *addr, os_uint_t bit); /* 读取指定位的值 (0 或 1) */ os_uint_t atomic_bit_read(volatile void *addr, os_uint_t bit); /* 翻转指定位 */ void atomic_bit_toggle(volatile void *addr, os_uint_t bit); /* ------------------------------------------------------------------------------------------------------------------ */ /* */ #define atomic_load atomic_load_32 #define atomic_store atomic_store_32 #define atomic_exchange atomic_exchange_32 #define atomic_compare_exchange_strong atomic_compare_exchange_strong_32 #define atomic_compare_exchange_weak atomic_compare_exchange_weak_32 #define atomic_fetch_add atomic_fetch_add_32 #define atomic_fetch_sub atomic_fetch_sub_32 #define atomic_fetch_and atomic_fetch_and_32 #define atomic_fetch_or atomic_fetch_or_32 #define atomic_fetch_xor atomic_fetch_xor_32 #ifdef __cplusplus } #endif #endif /*INCLUDED_ATOMIC_H*/