Teensy 4.1 core updated for C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

atomic.h 2.4KB

6 vuotta sitten
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * This is port of Dean Camera's ATOMIC_BLOCK macros for AVR to ARM Cortex M3
  3. * v1.0
  4. * Mark Pendrith, Nov 27, 2012.
  5. *
  6. * From Mark:
  7. * >When I ported the macros I emailed Dean to ask what attribution would be
  8. * >appropriate, and here is his response:
  9. * >
  10. * >>Mark,
  11. * >>I think it's great that you've ported the macros; consider them
  12. * >>public domain, to do with whatever you wish. I hope you find them >useful .
  13. * >>
  14. * >>Cheers!
  15. * >>- Dean
  16. */
  17. #ifdef __arm__
  18. #ifndef _CORTEX_M3_ATOMIC_H_
  19. #define _CORTEX_M3_ATOMIC_H_
  20. static __inline__ uint32_t __get_primask(void) \
  21. { uint32_t primask = 0; \
  22. __asm__ volatile ("MRS %[result], PRIMASK\n\t":[result]"=r"(primask)::); \
  23. return primask; } // returns 0 if interrupts enabled, 1 if disabled
  24. static __inline__ void __set_primask(uint32_t setval) \
  25. { __asm__ volatile ("MSR PRIMASK, %[value]\n\t""dmb\n\t""dsb\n\t""isb\n\t"::[value]"r"(setval):);
  26. __asm__ volatile ("" ::: "memory");}
  27. static __inline__ uint32_t __iSeiRetVal(void) \
  28. { __asm__ volatile ("CPSIE i\n\t""dmb\n\t""dsb\n\t""isb\n\t"); \
  29. __asm__ volatile ("" ::: "memory"); return 1; }
  30. static __inline__ uint32_t __iCliRetVal(void) \
  31. { __asm__ volatile ("CPSID i\n\t""dmb\n\t""dsb\n\t""isb\n\t"); \
  32. __asm__ volatile ("" ::: "memory"); return 1; }
  33. static __inline__ void __iSeiParam(const uint32_t *__s) \
  34. { __asm__ volatile ("CPSIE i\n\t""dmb\n\t""dsb\n\t""isb\n\t"); \
  35. __asm__ volatile ("" ::: "memory"); (void)__s; }
  36. static __inline__ void __iCliParam(const uint32_t *__s) \
  37. { __asm__ volatile ("CPSID i\n\t""dmb\n\t""dsb\n\t""isb\n\t"); \
  38. __asm__ volatile ("" ::: "memory"); (void)__s; }
  39. static __inline__ void __iRestore(const uint32_t *__s) \
  40. { __set_primask(*__s); __asm__ volatile ("dmb\n\t""dsb\n\t""isb\n\t"); \
  41. __asm__ volatile ("" ::: "memory"); }
  42. #define ATOMIC_BLOCK(type) \
  43. for ( type, __ToDo = __iCliRetVal(); __ToDo ; __ToDo = 0 )
  44. #define ATOMIC_RESTORESTATE \
  45. uint32_t primask_save __attribute__((__cleanup__(__iRestore))) = __get_primask()
  46. #define ATOMIC_FORCEON \
  47. uint32_t primask_save __attribute__((__cleanup__(__iSeiParam))) = 0
  48. #define NONATOMIC_BLOCK(type) \
  49. for ( type, __ToDo = __iSeiRetVal(); __ToDo ; __ToDo = 0 )
  50. #define NONATOMIC_RESTORESTATE \
  51. uint32_t primask_save __attribute__((__cleanup__(__iRestore))) = __get_primask()
  52. #define NONATOMIC_FORCEOFF \
  53. uint32_t primask_save __attribute__((__cleanup__(__iCliParam))) = 0
  54. #endif
  55. #endif