unleashed-firmware/furi/core/event_flag.c
Sergey Gavrilov e3d473bf42
[FL-2435] SD over SPI improvements (#2204)
* get rid of BSP layer
* sector_cache: init in any case
* new sd-spi driver: init
* Delete stm32_adafruit_sd.c.old
* Delete spi_sd_hal.c.old
* Storage: faster api lock primitive
* Threads: priority control
* Flags: correct error code
* Spi: dma mode
* SD-card: use DMA for big blocks of SPI data
* Fix wrong SD_TOKEN_START_DATA_MULTIPLE_BLOCK_WRITE value
* do not memset cache if it is NULL
* remove top-level timeouts
* sd hal: disable debug
* Furi HAL: DMA
* Furi HAL RFID: use furi_hal_dma
* Furi HAL DMA: tests
* Furi HAL DMA: docs
* rollback "Furi HAL RFID: use furi_hal_dma"
* 4 channels taken from DMA manager for core HAL.
* Furi HAL DMA: live fast, die young
* RPC tests: increase message buffer
* SPI HAL: use second DMA instance
* sd hal: new CID getter
* SPI hal: use non-DMA version if kernel is not running
* IR hal: generalize DMA definition
* storage: add CID data to sd info
* RFID hal: generalize DMA definition
* SUBGHZ hal: generalize DMA definition. Core hal moved to DMA2.
* Storage: small optimizations, removal of extra mutex
* Storage: redundant macro
* SD hal: more timeouts
* SPI hal: DMA init
* Target: make furi_hal_spi_dma_init symbol private
* Update F18 api symbols

Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2023-02-08 13:41:22 +09:00

140 lines
4.0 KiB
C

#include "event_flag.h"
#include "common_defines.h"
#include "check.h"
#include <event_groups.h>
#define FURI_EVENT_FLAG_MAX_BITS_EVENT_GROUPS 24U
#define FURI_EVENT_FLAG_INVALID_BITS (~((1UL << FURI_EVENT_FLAG_MAX_BITS_EVENT_GROUPS) - 1U))
FuriEventFlag* furi_event_flag_alloc() {
furi_assert(!FURI_IS_IRQ_MODE());
EventGroupHandle_t handle = xEventGroupCreate();
furi_check(handle);
return ((FuriEventFlag*)handle);
}
void furi_event_flag_free(FuriEventFlag* instance) {
furi_assert(!FURI_IS_IRQ_MODE());
vEventGroupDelete((EventGroupHandle_t)instance);
}
uint32_t furi_event_flag_set(FuriEventFlag* instance, uint32_t flags) {
furi_assert(instance);
furi_assert((flags & FURI_EVENT_FLAG_INVALID_BITS) == 0U);
EventGroupHandle_t hEventGroup = (EventGroupHandle_t)instance;
uint32_t rflags;
BaseType_t yield;
if(FURI_IS_IRQ_MODE()) {
yield = pdFALSE;
if(xEventGroupSetBitsFromISR(hEventGroup, (EventBits_t)flags, &yield) == pdFAIL) {
rflags = (uint32_t)FuriFlagErrorResource;
} else {
rflags = flags;
portYIELD_FROM_ISR(yield);
}
} else {
rflags = xEventGroupSetBits(hEventGroup, (EventBits_t)flags);
}
/* Return event flags after setting */
return (rflags);
}
uint32_t furi_event_flag_clear(FuriEventFlag* instance, uint32_t flags) {
furi_assert(instance);
furi_assert((flags & FURI_EVENT_FLAG_INVALID_BITS) == 0U);
EventGroupHandle_t hEventGroup = (EventGroupHandle_t)instance;
uint32_t rflags;
if(FURI_IS_IRQ_MODE()) {
rflags = xEventGroupGetBitsFromISR(hEventGroup);
if(xEventGroupClearBitsFromISR(hEventGroup, (EventBits_t)flags) == pdFAIL) {
rflags = (uint32_t)FuriStatusErrorResource;
} else {
/* xEventGroupClearBitsFromISR only registers clear operation in the timer command queue. */
/* Yield is required here otherwise clear operation might not execute in the right order. */
/* See https://github.com/FreeRTOS/FreeRTOS-Kernel/issues/93 for more info. */
portYIELD_FROM_ISR(pdTRUE);
}
} else {
rflags = xEventGroupClearBits(hEventGroup, (EventBits_t)flags);
}
/* Return event flags before clearing */
return (rflags);
}
uint32_t furi_event_flag_get(FuriEventFlag* instance) {
furi_assert(instance);
EventGroupHandle_t hEventGroup = (EventGroupHandle_t)instance;
uint32_t rflags;
if(FURI_IS_IRQ_MODE()) {
rflags = xEventGroupGetBitsFromISR(hEventGroup);
} else {
rflags = xEventGroupGetBits(hEventGroup);
}
/* Return current event flags */
return (rflags);
}
uint32_t furi_event_flag_wait(
FuriEventFlag* instance,
uint32_t flags,
uint32_t options,
uint32_t timeout) {
furi_assert(!FURI_IS_IRQ_MODE());
furi_assert(instance);
furi_assert((flags & FURI_EVENT_FLAG_INVALID_BITS) == 0U);
EventGroupHandle_t hEventGroup = (EventGroupHandle_t)instance;
BaseType_t wait_all;
BaseType_t exit_clr;
uint32_t rflags;
if(options & FuriFlagWaitAll) {
wait_all = pdTRUE;
} else {
wait_all = pdFAIL;
}
if(options & FuriFlagNoClear) {
exit_clr = pdFAIL;
} else {
exit_clr = pdTRUE;
}
rflags = xEventGroupWaitBits(
hEventGroup, (EventBits_t)flags, exit_clr, wait_all, (TickType_t)timeout);
if(options & FuriFlagWaitAll) {
if((flags & rflags) != flags) {
if(timeout > 0U) {
rflags = (uint32_t)FuriStatusErrorTimeout;
} else {
rflags = (uint32_t)FuriStatusErrorResource;
}
}
} else {
if((flags & rflags) == 0U) {
if(timeout > 0U) {
rflags = (uint32_t)FuriStatusErrorTimeout;
} else {
rflags = (uint32_t)FuriStatusErrorResource;
}
}
}
/* Return event flags before clearing */
return (rflags);
}