2021-05-18 13:51:00 +03:00
|
|
|
#pragma once
|
|
|
|
|
2022-03-29 20:37:23 +03:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <cmsis_os2.h>
|
|
|
|
|
2022-03-30 18:23:40 +03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <cmsis_compiler.h>
|
|
|
|
|
2021-05-18 21:54:56 +03:00
|
|
|
#ifndef MAX
|
|
|
|
#define MAX(a, b) \
|
|
|
|
({ \
|
|
|
|
__typeof__(a) _a = (a); \
|
|
|
|
__typeof__(b) _b = (b); \
|
|
|
|
_a > _b ? _a : _b; \
|
|
|
|
})
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MIN
|
|
|
|
#define MIN(a, b) \
|
|
|
|
({ \
|
|
|
|
__typeof__(a) _a = (a); \
|
|
|
|
__typeof__(b) _b = (b); \
|
|
|
|
_a < _b ? _a : _b; \
|
|
|
|
})
|
|
|
|
#endif
|
|
|
|
|
2021-09-10 00:37:32 +03:00
|
|
|
#ifndef ROUND_UP_TO
|
|
|
|
#define ROUND_UP_TO(a, b) \
|
|
|
|
({ \
|
|
|
|
__typeof__(a) _a = (a); \
|
|
|
|
__typeof__(b) _b = (b); \
|
|
|
|
_a / _b + !!(_a % _b); \
|
|
|
|
})
|
|
|
|
#endif
|
|
|
|
|
2021-05-18 21:54:56 +03:00
|
|
|
#ifndef CLAMP
|
|
|
|
#define CLAMP(x, upper, lower) (MIN(upper, MAX(x, lower)))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef COUNT_OF
|
2021-05-18 13:51:00 +03:00
|
|
|
#define COUNT_OF(x) (sizeof(x) / sizeof(x[0]))
|
2021-05-18 21:54:56 +03:00
|
|
|
#endif
|
2021-09-02 00:05:00 +03:00
|
|
|
|
|
|
|
#ifndef FURI_SWAP
|
|
|
|
#define FURI_SWAP(x, y) \
|
|
|
|
do { \
|
|
|
|
typeof(x) SWAP = x; \
|
|
|
|
x = y; \
|
|
|
|
y = SWAP; \
|
|
|
|
} while(0)
|
|
|
|
#endif
|
2021-09-10 00:11:32 +03:00
|
|
|
|
|
|
|
#ifndef PLACE_IN_SECTION
|
|
|
|
#define PLACE_IN_SECTION(x) __attribute__((section(x)))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef ALIGN
|
|
|
|
#define ALIGN(n) __attribute__((aligned(n)))
|
|
|
|
#endif
|
2021-10-16 14:25:32 +03:00
|
|
|
|
2022-03-30 18:23:40 +03:00
|
|
|
#ifndef __weak
|
|
|
|
#define __weak __attribute__((weak))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef UNUSED
|
|
|
|
#define UNUSED(X) (void)(X)
|
|
|
|
#endif
|
|
|
|
|
2021-10-16 14:25:32 +03:00
|
|
|
#ifndef STRINGIFY
|
|
|
|
#define STRINGIFY(x) #x
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef TOSTRING
|
|
|
|
#define TOSTRING(x) STRINGIFY(x)
|
2021-11-08 22:41:40 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef REVERSE_BYTES_U32
|
|
|
|
#define REVERSE_BYTES_U32(x) \
|
|
|
|
((((x)&0x000000FF) << 24) | (((x)&0x0000FF00) << 8) | (((x)&0x00FF0000) >> 8) | \
|
|
|
|
(((x)&0xFF000000) >> 24))
|
2021-12-01 01:07:17 +03:00
|
|
|
#endif
|
|
|
|
|
2022-03-24 01:14:34 +03:00
|
|
|
#ifndef FURI_BIT
|
2022-05-24 17:00:15 +03:00
|
|
|
#define FURI_BIT(x, n) (((x) >> (n)) & 1)
|
2022-03-24 01:14:34 +03:00
|
|
|
#endif
|
|
|
|
|
2022-03-29 20:37:23 +03:00
|
|
|
#ifndef FURI_IS_IRQ_MASKED
|
|
|
|
#define FURI_IS_IRQ_MASKED() (__get_PRIMASK() != 0U)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FURI_IS_IRQ_MODE
|
|
|
|
#define FURI_IS_IRQ_MODE() (__get_IPSR() != 0U)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FURI_IS_ISR
|
2022-04-13 23:50:25 +03:00
|
|
|
#define FURI_IS_ISR() (FURI_IS_IRQ_MODE() || FURI_IS_IRQ_MASKED())
|
2022-03-29 20:37:23 +03:00
|
|
|
#endif
|
|
|
|
|
2021-12-01 01:07:17 +03:00
|
|
|
#ifndef FURI_CRITICAL_ENTER
|
2022-04-13 23:50:25 +03:00
|
|
|
#define FURI_CRITICAL_ENTER() \
|
|
|
|
uint32_t __isrm = 0; \
|
|
|
|
bool __from_isr = FURI_IS_ISR(); \
|
|
|
|
bool __kernel_running = (osKernelGetState() == osKernelRunning); \
|
|
|
|
if(__from_isr) { \
|
|
|
|
__isrm = taskENTER_CRITICAL_FROM_ISR(); \
|
|
|
|
} else if(__kernel_running) { \
|
|
|
|
taskENTER_CRITICAL(); \
|
|
|
|
} else { \
|
|
|
|
__disable_irq(); \
|
2022-03-29 20:37:23 +03:00
|
|
|
}
|
2021-12-01 01:07:17 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FURI_CRITICAL_EXIT
|
2022-03-29 20:37:23 +03:00
|
|
|
#define FURI_CRITICAL_EXIT() \
|
|
|
|
if(__from_isr) { \
|
|
|
|
taskEXIT_CRITICAL_FROM_ISR(__isrm); \
|
2022-04-13 23:50:25 +03:00
|
|
|
} else if(__kernel_running) { \
|
2022-03-29 20:37:23 +03:00
|
|
|
taskEXIT_CRITICAL(); \
|
2022-04-13 23:50:25 +03:00
|
|
|
} else { \
|
|
|
|
__enable_irq(); \
|
2022-03-29 20:37:23 +03:00
|
|
|
}
|
2022-01-03 00:39:56 +03:00
|
|
|
#endif
|
2022-03-30 18:23:40 +03:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|