common_defines.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #ifndef COUNT_OF
  36. #define COUNT_OF(x) (sizeof(x) / sizeof(x[0]))
  37. #endif
  38. #ifndef FURI_SWAP
  39. #define FURI_SWAP(x, y) \
  40. do { \
  41. typeof(x) SWAP = x; \
  42. x = y; \
  43. y = SWAP; \
  44. } while(0)
  45. #endif
  46. #ifndef PLACE_IN_SECTION
  47. #define PLACE_IN_SECTION(x) __attribute__((section(x)))
  48. #endif
  49. #ifndef ALIGN
  50. #define ALIGN(n) __attribute__((aligned(n)))
  51. #endif
  52. #ifndef __weak
  53. #define __weak __attribute__((weak))
  54. #endif
  55. #ifndef UNUSED
  56. #define UNUSED(X) (void)(X)
  57. #endif
  58. #ifndef STRINGIFY
  59. #define STRINGIFY(x) #x
  60. #endif
  61. #ifndef TOSTRING
  62. #define TOSTRING(x) STRINGIFY(x)
  63. #endif
  64. #ifndef REVERSE_BYTES_U32
  65. #define REVERSE_BYTES_U32(x) \
  66. ((((x)&0x000000FF) << 24) | (((x)&0x0000FF00) << 8) | (((x)&0x00FF0000) >> 8) | \
  67. (((x)&0xFF000000) >> 24))
  68. #endif
  69. #ifndef FURI_BIT
  70. #define FURI_BIT(x, n) (((x) >> (n)) & 1)
  71. #endif
  72. #ifndef FURI_IS_IRQ_MASKED
  73. #define FURI_IS_IRQ_MASKED() (__get_PRIMASK() != 0U)
  74. #endif
  75. #ifndef FURI_IS_IRQ_MODE
  76. #define FURI_IS_IRQ_MODE() (__get_IPSR() != 0U)
  77. #endif
  78. #ifndef FURI_IS_ISR
  79. #define FURI_IS_ISR() (FURI_IS_IRQ_MODE() || FURI_IS_IRQ_MASKED())
  80. #endif
  81. #ifndef FURI_CRITICAL_ENTER
  82. #define FURI_CRITICAL_ENTER() \
  83. uint32_t __isrm = 0; \
  84. bool __from_isr = FURI_IS_ISR(); \
  85. bool __kernel_running = (osKernelGetState() == osKernelRunning); \
  86. if(__from_isr) { \
  87. __isrm = taskENTER_CRITICAL_FROM_ISR(); \
  88. } else if(__kernel_running) { \
  89. taskENTER_CRITICAL(); \
  90. } else { \
  91. __disable_irq(); \
  92. }
  93. #endif
  94. #ifndef FURI_CRITICAL_EXIT
  95. #define FURI_CRITICAL_EXIT() \
  96. if(__from_isr) { \
  97. taskEXIT_CRITICAL_FROM_ISR(__isrm); \
  98. } else if(__kernel_running) { \
  99. taskEXIT_CRITICAL(); \
  100. } else { \
  101. __enable_irq(); \
  102. }
  103. #endif
  104. #ifdef __cplusplus
  105. }
  106. #endif