common_defines.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #pragma once
  2. #include <stdbool.h>
  3. #include <cmsis_os2.h>
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. #include <cmsis_compiler.h>
  8. #ifndef MAX
  9. #define MAX(a, b) \
  10. ({ \
  11. __typeof__(a) _a = (a); \
  12. __typeof__(b) _b = (b); \
  13. _a > _b ? _a : _b; \
  14. })
  15. #endif
  16. #ifndef MIN
  17. #define MIN(a, b) \
  18. ({ \
  19. __typeof__(a) _a = (a); \
  20. __typeof__(b) _b = (b); \
  21. _a < _b ? _a : _b; \
  22. })
  23. #endif
  24. #ifndef ROUND_UP_TO
  25. #define ROUND_UP_TO(a, b) \
  26. ({ \
  27. __typeof__(a) _a = (a); \
  28. __typeof__(b) _b = (b); \
  29. _a / _b + !!(_a % _b); \
  30. })
  31. #endif
  32. #ifndef CLAMP
  33. #define CLAMP(x, upper, lower) (MIN(upper, MAX(x, lower)))
  34. #endif
  35. // need some common semantics for those two
  36. #ifndef SIZEOF_ARRAY
  37. #define SIZEOF_ARRAY(arr) (sizeof(arr) / sizeof(arr[0]))
  38. #endif
  39. #ifndef COUNT_OF
  40. #define COUNT_OF(x) (sizeof(x) / sizeof(x[0]))
  41. #endif
  42. #ifndef FURI_SWAP
  43. #define FURI_SWAP(x, y) \
  44. do { \
  45. typeof(x) SWAP = x; \
  46. x = y; \
  47. y = SWAP; \
  48. } while(0)
  49. #endif
  50. #ifndef PLACE_IN_SECTION
  51. #define PLACE_IN_SECTION(x) __attribute__((section(x)))
  52. #endif
  53. #ifndef ALIGN
  54. #define ALIGN(n) __attribute__((aligned(n)))
  55. #endif
  56. #ifndef __weak
  57. #define __weak __attribute__((weak))
  58. #endif
  59. #ifndef UNUSED
  60. #define UNUSED(X) (void)(X)
  61. #endif
  62. #ifndef STRINGIFY
  63. #define STRINGIFY(x) #x
  64. #endif
  65. #ifndef TOSTRING
  66. #define TOSTRING(x) STRINGIFY(x)
  67. #endif
  68. #ifndef REVERSE_BYTES_U32
  69. #define REVERSE_BYTES_U32(x) \
  70. ((((x)&0x000000FF) << 24) | (((x)&0x0000FF00) << 8) | (((x)&0x00FF0000) >> 8) | \
  71. (((x)&0xFF000000) >> 24))
  72. #endif
  73. #ifndef FURI_BIT
  74. #define FURI_BIT(x, n) ((x) >> (n)&1)
  75. #endif
  76. #ifndef FURI_IS_IRQ_MASKED
  77. #define FURI_IS_IRQ_MASKED() (__get_PRIMASK() != 0U)
  78. #endif
  79. #ifndef FURI_IS_IRQ_MODE
  80. #define FURI_IS_IRQ_MODE() (__get_IPSR() != 0U)
  81. #endif
  82. #ifndef FURI_IS_ISR
  83. #define FURI_IS_ISR() (FURI_IS_IRQ_MODE() || FURI_IS_IRQ_MASKED())
  84. #endif
  85. #ifndef FURI_CRITICAL_ENTER
  86. #define FURI_CRITICAL_ENTER() \
  87. uint32_t __isrm = 0; \
  88. bool __from_isr = FURI_IS_ISR(); \
  89. bool __kernel_running = (osKernelGetState() == osKernelRunning); \
  90. if(__from_isr) { \
  91. __isrm = taskENTER_CRITICAL_FROM_ISR(); \
  92. } else if(__kernel_running) { \
  93. taskENTER_CRITICAL(); \
  94. } else { \
  95. __disable_irq(); \
  96. }
  97. #endif
  98. #ifndef FURI_CRITICAL_EXIT
  99. #define FURI_CRITICAL_EXIT() \
  100. if(__from_isr) { \
  101. taskEXIT_CRITICAL_FROM_ISR(__isrm); \
  102. } else if(__kernel_running) { \
  103. taskEXIT_CRITICAL(); \
  104. } else { \
  105. __enable_irq(); \
  106. }
  107. #endif
  108. #ifdef __cplusplus
  109. }
  110. #endif