2022-07-20 10:56:33 +00:00
|
|
|
#include "kernel.h"
|
|
|
|
#include "base.h"
|
|
|
|
#include "check.h"
|
|
|
|
#include "common_defines.h"
|
|
|
|
|
|
|
|
#include <furi_hal.h>
|
|
|
|
|
2023-11-01 07:24:11 +00:00
|
|
|
#include <FreeRTOS.h>
|
|
|
|
#include <task.h>
|
|
|
|
|
2024-03-19 14:43:52 +00:00
|
|
|
bool furi_kernel_is_irq_or_masked(void) {
|
2023-01-29 10:12:24 +00:00
|
|
|
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 */
|
2024-07-15 04:38:49 +00:00
|
|
|
return irq;
|
2023-01-29 10:12:24 +00:00
|
|
|
}
|
|
|
|
|
2024-03-19 14:43:52 +00:00
|
|
|
bool furi_kernel_is_running(void) {
|
2024-10-14 13:39:09 +00:00
|
|
|
return xTaskGetSchedulerState() == taskSCHEDULER_RUNNING;
|
2023-11-01 07:24:11 +00:00
|
|
|
}
|
|
|
|
|
2024-03-19 14:43:52 +00:00
|
|
|
int32_t furi_kernel_lock(void) {
|
|
|
|
furi_check(!furi_kernel_is_irq_or_masked());
|
2022-07-20 10:56:33 +00:00
|
|
|
|
|
|
|
int32_t lock;
|
|
|
|
|
|
|
|
switch(xTaskGetSchedulerState()) {
|
|
|
|
case taskSCHEDULER_SUSPENDED:
|
|
|
|
lock = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case taskSCHEDULER_RUNNING:
|
|
|
|
vTaskSuspendAll();
|
|
|
|
lock = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case taskSCHEDULER_NOT_STARTED:
|
|
|
|
default:
|
|
|
|
lock = (int32_t)FuriStatusError;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return previous lock state */
|
2024-07-15 04:38:49 +00:00
|
|
|
return lock;
|
2022-07-20 10:56:33 +00:00
|
|
|
}
|
|
|
|
|
2024-03-19 14:43:52 +00:00
|
|
|
int32_t furi_kernel_unlock(void) {
|
|
|
|
furi_check(!furi_kernel_is_irq_or_masked());
|
2022-07-20 10:56:33 +00:00
|
|
|
|
|
|
|
int32_t lock;
|
|
|
|
|
|
|
|
switch(xTaskGetSchedulerState()) {
|
|
|
|
case taskSCHEDULER_SUSPENDED:
|
|
|
|
lock = 1;
|
|
|
|
|
|
|
|
if(xTaskResumeAll() != pdTRUE) {
|
|
|
|
if(xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED) {
|
|
|
|
lock = (int32_t)FuriStatusError;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case taskSCHEDULER_RUNNING:
|
|
|
|
lock = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case taskSCHEDULER_NOT_STARTED:
|
|
|
|
default:
|
|
|
|
lock = (int32_t)FuriStatusError;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return previous lock state */
|
2024-07-15 04:38:49 +00:00
|
|
|
return lock;
|
2022-07-20 10:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t furi_kernel_restore_lock(int32_t lock) {
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_check(!furi_kernel_is_irq_or_masked());
|
2022-07-20 10:56:33 +00:00
|
|
|
|
|
|
|
switch(xTaskGetSchedulerState()) {
|
|
|
|
case taskSCHEDULER_SUSPENDED:
|
|
|
|
case taskSCHEDULER_RUNNING:
|
|
|
|
if(lock == 1) {
|
|
|
|
vTaskSuspendAll();
|
|
|
|
} else {
|
|
|
|
if(lock != 0) {
|
|
|
|
lock = (int32_t)FuriStatusError;
|
|
|
|
} else {
|
|
|
|
if(xTaskResumeAll() != pdTRUE) {
|
|
|
|
if(xTaskGetSchedulerState() != taskSCHEDULER_RUNNING) {
|
|
|
|
lock = (int32_t)FuriStatusError;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case taskSCHEDULER_NOT_STARTED:
|
|
|
|
default:
|
|
|
|
lock = (int32_t)FuriStatusError;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return new lock state */
|
2024-07-15 04:38:49 +00:00
|
|
|
return lock;
|
2022-07-20 10:56:33 +00:00
|
|
|
}
|
|
|
|
|
2024-03-19 14:43:52 +00:00
|
|
|
uint32_t furi_kernel_get_tick_frequency(void) {
|
2022-07-20 10:56:33 +00:00
|
|
|
/* Return frequency in hertz */
|
2024-07-15 04:38:49 +00:00
|
|
|
return configTICK_RATE_HZ_RAW;
|
2022-07-20 10:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void furi_delay_tick(uint32_t ticks) {
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_check(!furi_kernel_is_irq_or_masked());
|
2024-10-14 13:39:09 +00:00
|
|
|
furi_check(furi_thread_get_current_id() != xTaskGetIdleTaskHandle());
|
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
if(ticks == 0U) {
|
|
|
|
taskYIELD();
|
|
|
|
} else {
|
|
|
|
vTaskDelay(ticks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FuriStatus furi_delay_until_tick(uint32_t tick) {
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_check(!furi_kernel_is_irq_or_masked());
|
2024-10-14 13:39:09 +00:00
|
|
|
furi_check(furi_thread_get_current_id() != xTaskGetIdleTaskHandle());
|
2022-07-20 10:56:33 +00:00
|
|
|
|
|
|
|
TickType_t tcnt, delay;
|
|
|
|
FuriStatus stat;
|
|
|
|
|
|
|
|
stat = FuriStatusOk;
|
|
|
|
tcnt = xTaskGetTickCount();
|
|
|
|
|
|
|
|
/* Determine remaining number of tick to delay */
|
|
|
|
delay = (TickType_t)tick - tcnt;
|
|
|
|
|
|
|
|
/* Check if target tick has not expired */
|
|
|
|
if((delay != 0U) && (0 == (delay >> (8 * sizeof(TickType_t) - 1)))) {
|
|
|
|
if(xTaskDelayUntil(&tcnt, delay) == pdFALSE) {
|
|
|
|
/* Did not delay */
|
|
|
|
stat = FuriStatusError;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* No delay or already expired */
|
|
|
|
stat = FuriStatusErrorParameter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return execution status */
|
2024-07-15 04:38:49 +00:00
|
|
|
return stat;
|
2022-07-20 10:56:33 +00:00
|
|
|
}
|
|
|
|
|
2024-03-19 14:43:52 +00:00
|
|
|
uint32_t furi_get_tick(void) {
|
2022-07-20 10:56:33 +00:00
|
|
|
TickType_t ticks;
|
|
|
|
|
2023-01-29 10:12:24 +00:00
|
|
|
if(furi_kernel_is_irq_or_masked() != 0U) {
|
2022-07-20 10:56:33 +00:00
|
|
|
ticks = xTaskGetTickCountFromISR();
|
|
|
|
} else {
|
|
|
|
ticks = xTaskGetTickCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ticks;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t furi_ms_to_ticks(uint32_t milliseconds) {
|
|
|
|
#if configTICK_RATE_HZ_RAW == 1000
|
|
|
|
return milliseconds;
|
|
|
|
#else
|
|
|
|
return (uint32_t)((float)configTICK_RATE_HZ_RAW) / 1000.0f * (float)milliseconds;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void furi_delay_ms(uint32_t milliseconds) {
|
|
|
|
if(!FURI_IS_ISR() && xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) {
|
|
|
|
if(milliseconds > 0 && milliseconds < portMAX_DELAY - 1) {
|
|
|
|
milliseconds += 1;
|
|
|
|
}
|
|
|
|
#if configTICK_RATE_HZ_RAW == 1000
|
|
|
|
furi_delay_tick(milliseconds);
|
|
|
|
#else
|
|
|
|
furi_delay_tick(furi_ms_to_ticks(milliseconds));
|
|
|
|
#endif
|
|
|
|
} else if(milliseconds > 0) {
|
|
|
|
furi_delay_us(milliseconds * 1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void furi_delay_us(uint32_t microseconds) {
|
|
|
|
furi_hal_cortex_delay_us(microseconds);
|
|
|
|
}
|