STM32F4的原子操作怎么写?

2019-12-22 13:43发布

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中要实现这个操作怎么写
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
2条回答
fengyunyu
1楼-- · 2019-12-22 14:20
shangdawei
2楼-- · 2019-12-22 19:11
https://github.com/ARMmbed/mbed- ... orm/mbed_critical.h
https://github.com/ARMmbed/mbed- ... orm/mbed_critical.c


  1. bool core_util_atomic_cas_u8(uint8_t *ptr, uint8_t *expectedCurrentValue, uint8_t desiredValue)
  2. {
  3.     uint8_t currentValue = __LDREXB((volatile uint8_t*)ptr);
  4.     if (currentValue != *expectedCurrentValue) {
  5.         *expectedCurrentValue = currentValue;
  6.         __CLREX();
  7.         return false;
  8.     }

  9.     return !__STREXB(desiredValue, (volatile uint8_t*)ptr);
  10. }

  11. bool core_util_atomic_cas_u16(uint16_t *ptr, uint16_t *expectedCurrentValue, uint16_t desiredValue)
  12. {
  13.     uint16_t currentValue = __LDREXH((volatile uint16_t*)ptr);
  14.     if (currentValue != *expectedCurrentValue) {
  15.         *expectedCurrentValue = currentValue;
  16.         __CLREX();
  17.         return false;
  18.     }

  19.     return !__STREXH(desiredValue, (volatile uint16_t*)ptr);
  20. }


  21. bool core_util_atomic_cas_u32(uint32_t *ptr, uint32_t *expectedCurrentValue, uint32_t desiredValue)
  22. {
  23.     uint32_t currentValue = __LDREXW((volatile uint32_t*)ptr);
  24.     if (currentValue != *expectedCurrentValue) {
  25.         *expectedCurrentValue = currentValue;
  26.         __CLREX();
  27.         return false;
  28.     }

  29.     return !__STREXW(desiredValue, (volatile uint32_t*)ptr);
  30. }

  31. uint8_t core_util_atomic_incr_u8(uint8_t *valuePtr, uint8_t delta)
  32. {
  33.     uint8_t newValue;
  34.     do {
  35.         newValue = __LDREXB((volatile uint8_t*)valuePtr) + delta;
  36.     } while (__STREXB(newValue, (volatile uint8_t*)valuePtr));
  37.     return newValue;
  38. }

  39. uint16_t core_util_atomic_incr_u16(uint16_t *valuePtr, uint16_t delta)
  40. {
  41.     uint16_t newValue;
  42.     do {
  43.         newValue = __LDREXH((volatile uint16_t*)valuePtr) + delta;
  44.     } while (__STREXH(newValue, (volatile uint16_t*)valuePtr));
  45.     return newValue;
  46. }

  47. uint32_t core_util_atomic_incr_u32(uint32_t *valuePtr, uint32_t delta)
  48. {
  49.     uint32_t newValue;
  50.     do {
  51.         newValue = __LDREXW((volatile uint32_t*)valuePtr) + delta;
  52.     } while (__STREXW(newValue, (volatile uint32_t*)valuePtr));
  53.     return newValue;
  54. }


  55. uint8_t core_util_atomic_decr_u8(uint8_t *valuePtr, uint8_t delta)
  56. {
  57.     uint8_t newValue;
  58.     do {
  59.         newValue = __LDREXB((volatile uint8_t*)valuePtr) - delta;
  60.     } while (__STREXB(newValue, (volatile uint8_t*)valuePtr));
  61.     return newValue;
  62. }

  63. uint16_t core_util_atomic_decr_u16(uint16_t *valuePtr, uint16_t delta)
  64. {
  65.     uint16_t newValue;
  66.     do {
  67.         newValue = __LDREXH((volatile uint16_t*)valuePtr) - delta;
  68.     } while (__STREXH(newValue, (volatile uint16_t*)valuePtr));
  69.     return newValue;
  70. }

  71. uint32_t core_util_atomic_decr_u32(uint32_t *valuePtr, uint32_t delta)
  72. {
  73.     uint32_t newValue;
  74.     do {
  75.         newValue = __LDREXW((volatile uint32_t*)valuePtr) - delta;
  76.     } while (__STREXW(newValue, (volatile uint32_t*)valuePtr));
  77.     return newValue;
  78. }
复制代码

一周热门 更多>