void system_set_exec_alarm(uint8_t code) {
uint8_t sreg = SREG;
cli();
sys_rt_exec_alarm = code;
SREG = sreg;
}
上面的这个函数的意思是不是这样
把状态寄存器的数据保存到sreg这里。
然后关全局中断的函数cli();
变量赋值
还原状态寄存器的值
这个过程就是个 保存现场——执行原子操作——恢复现场 的过程,无论之前是否 I 置位,都不会发生错误或影响。
上面的函数是在AVR中实现的原子操作。
请问STM32F4中要实现这个操作怎么写
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
https://github.com/ARMmbed/mbed- ... orm/mbed_critical.c
- bool core_util_atomic_cas_u8(uint8_t *ptr, uint8_t *expectedCurrentValue, uint8_t desiredValue)
- {
- uint8_t currentValue = __LDREXB((volatile uint8_t*)ptr);
- if (currentValue != *expectedCurrentValue) {
- *expectedCurrentValue = currentValue;
- __CLREX();
- return false;
- }
- return !__STREXB(desiredValue, (volatile uint8_t*)ptr);
- }
- bool core_util_atomic_cas_u16(uint16_t *ptr, uint16_t *expectedCurrentValue, uint16_t desiredValue)
- {
- uint16_t currentValue = __LDREXH((volatile uint16_t*)ptr);
- if (currentValue != *expectedCurrentValue) {
- *expectedCurrentValue = currentValue;
- __CLREX();
- return false;
- }
- return !__STREXH(desiredValue, (volatile uint16_t*)ptr);
- }
- bool core_util_atomic_cas_u32(uint32_t *ptr, uint32_t *expectedCurrentValue, uint32_t desiredValue)
- {
- uint32_t currentValue = __LDREXW((volatile uint32_t*)ptr);
- if (currentValue != *expectedCurrentValue) {
- *expectedCurrentValue = currentValue;
- __CLREX();
- return false;
- }
- return !__STREXW(desiredValue, (volatile uint32_t*)ptr);
- }
- uint8_t core_util_atomic_incr_u8(uint8_t *valuePtr, uint8_t delta)
- {
- uint8_t newValue;
- do {
- newValue = __LDREXB((volatile uint8_t*)valuePtr) + delta;
- } while (__STREXB(newValue, (volatile uint8_t*)valuePtr));
- return newValue;
- }
- uint16_t core_util_atomic_incr_u16(uint16_t *valuePtr, uint16_t delta)
- {
- uint16_t newValue;
- do {
- newValue = __LDREXH((volatile uint16_t*)valuePtr) + delta;
- } while (__STREXH(newValue, (volatile uint16_t*)valuePtr));
- return newValue;
- }
- uint32_t core_util_atomic_incr_u32(uint32_t *valuePtr, uint32_t delta)
- {
- uint32_t newValue;
- do {
- newValue = __LDREXW((volatile uint32_t*)valuePtr) + delta;
- } while (__STREXW(newValue, (volatile uint32_t*)valuePtr));
- return newValue;
- }
- uint8_t core_util_atomic_decr_u8(uint8_t *valuePtr, uint8_t delta)
- {
- uint8_t newValue;
- do {
- newValue = __LDREXB((volatile uint8_t*)valuePtr) - delta;
- } while (__STREXB(newValue, (volatile uint8_t*)valuePtr));
- return newValue;
- }
- uint16_t core_util_atomic_decr_u16(uint16_t *valuePtr, uint16_t delta)
- {
- uint16_t newValue;
- do {
- newValue = __LDREXH((volatile uint16_t*)valuePtr) - delta;
- } while (__STREXH(newValue, (volatile uint16_t*)valuePtr));
- return newValue;
- }
- uint32_t core_util_atomic_decr_u32(uint32_t *valuePtr, uint32_t delta)
- {
- uint32_t newValue;
- do {
- newValue = __LDREXW((volatile uint32_t*)valuePtr) - delta;
- } while (__STREXW(newValue, (volatile uint32_t*)valuePtr));
- return newValue;
- }
复制代码一周热门 更多>