2021-05-18 10:51:00 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-03-29 17:37:23 +00:00
|
|
|
#include <stdbool.h>
|
2022-07-20 10:56:33 +00:00
|
|
|
#include <FreeRTOS.h>
|
|
|
|
#include <task.h>
|
2022-03-29 17:37:23 +00:00
|
|
|
|
2022-03-30 15:23:40 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <cmsis_compiler.h>
|
|
|
|
|
2021-05-18 18:54:56 +00: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-09 21:37:32 +00: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 18:54:56 +00:00
|
|
|
#ifndef CLAMP
|
|
|
|
#define CLAMP(x, upper, lower) (MIN(upper, MAX(x, lower)))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef COUNT_OF
|
2021-05-18 10:51:00 +00:00
|
|
|
#define COUNT_OF(x) (sizeof(x) / sizeof(x[0]))
|
2021-05-18 18:54:56 +00:00
|
|
|
#endif
|
2021-09-01 21:05:00 +00:00
|
|
|
|
|
|
|
#ifndef FURI_SWAP
|
|
|
|
#define FURI_SWAP(x, y) \
|
|
|
|
do { \
|
|
|
|
typeof(x) SWAP = x; \
|
|
|
|
x = y; \
|
|
|
|
y = SWAP; \
|
|
|
|
} while(0)
|
|
|
|
#endif
|
2021-09-09 21:11:32 +00: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 11:25:32 +00:00
|
|
|
|
2022-03-30 15:23:40 +00:00
|
|
|
#ifndef __weak
|
|
|
|
#define __weak __attribute__((weak))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef UNUSED
|
|
|
|
#define UNUSED(X) (void)(X)
|
|
|
|
#endif
|
|
|
|
|
2021-10-16 11:25:32 +00:00
|
|
|
#ifndef STRINGIFY
|
|
|
|
#define STRINGIFY(x) #x
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef TOSTRING
|
|
|
|
#define TOSTRING(x) STRINGIFY(x)
|
2021-11-08 19:41:40 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef REVERSE_BYTES_U32
|
|
|
|
#define REVERSE_BYTES_U32(x) \
|
|
|
|
((((x)&0x000000FF) << 24) | (((x)&0x0000FF00) << 8) | (((x)&0x00FF0000) >> 8) | \
|
|
|
|
(((x)&0xFF000000) >> 24))
|
2021-11-30 22:07:17 +00:00
|
|
|
#endif
|
|
|
|
|
2022-03-23 22:14:34 +00:00
|
|
|
#ifndef FURI_BIT
|
2022-05-24 14:00:15 +00:00
|
|
|
#define FURI_BIT(x, n) (((x) >> (n)) & 1)
|
2022-03-23 22:14:34 +00:00
|
|
|
#endif
|
|
|
|
|
2022-07-26 15:30:49 +00:00
|
|
|
#ifndef FURI_BIT_SET
|
|
|
|
#define FURI_BIT_SET(x, n) ((x) |= (1 << (n)))
|
|
|
|
#endif
|
|
|
|
|
2022-03-29 17:37:23 +00: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 20:50:25 +00:00
|
|
|
#define FURI_IS_ISR() (FURI_IS_IRQ_MODE() || FURI_IS_IRQ_MASKED())
|
2022-03-29 17:37:23 +00:00
|
|
|
#endif
|
|
|
|
|
2021-11-30 22:07:17 +00:00
|
|
|
#ifndef FURI_CRITICAL_ENTER
|
2022-07-20 10:56:33 +00:00
|
|
|
#define FURI_CRITICAL_ENTER() \
|
|
|
|
uint32_t __isrm = 0; \
|
|
|
|
bool __from_isr = FURI_IS_ISR(); \
|
|
|
|
bool __kernel_running = (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING); \
|
|
|
|
if(__from_isr) { \
|
|
|
|
__isrm = taskENTER_CRITICAL_FROM_ISR(); \
|
|
|
|
} else if(__kernel_running) { \
|
|
|
|
taskENTER_CRITICAL(); \
|
|
|
|
} else { \
|
|
|
|
__disable_irq(); \
|
2022-03-29 17:37:23 +00:00
|
|
|
}
|
2021-11-30 22:07:17 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FURI_CRITICAL_EXIT
|
2022-03-29 17:37:23 +00:00
|
|
|
#define FURI_CRITICAL_EXIT() \
|
|
|
|
if(__from_isr) { \
|
|
|
|
taskEXIT_CRITICAL_FROM_ISR(__isrm); \
|
2022-04-13 20:50:25 +00:00
|
|
|
} else if(__kernel_running) { \
|
2022-03-29 17:37:23 +00:00
|
|
|
taskEXIT_CRITICAL(); \
|
2022-04-13 20:50:25 +00:00
|
|
|
} else { \
|
|
|
|
__enable_irq(); \
|
2022-03-29 17:37:23 +00:00
|
|
|
}
|
2022-01-02 21:39:56 +00:00
|
|
|
#endif
|
2022-03-30 15:23:40 +00:00
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
static inline bool furi_is_irq_context() {
|
|
|
|
bool irq = false;
|
|
|
|
BaseType_t state;
|
|
|
|
|
|
|
|
if(FURI_IS_IRQ_MODE()) {
|
|
|
|
/* Called from interrupt context */
|
|
|
|
irq = true;
|
|
|
|
} else {
|
|
|
|
/* Get FreeRTOS scheduler state */
|
|
|
|
state = xTaskGetSchedulerState();
|
|
|
|
|
|
|
|
if(state != taskSCHEDULER_NOT_STARTED) {
|
|
|
|
/* Scheduler was started */
|
|
|
|
if(FURI_IS_IRQ_MASKED()) {
|
|
|
|
/* Interrupts are masked */
|
|
|
|
irq = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return context, 0: thread context, 1: IRQ context */
|
|
|
|
return (irq);
|
|
|
|
}
|
|
|
|
|
2022-03-30 15:23:40 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|