Furi: core refactoring and CMSIS removal part 2 (#1410)

* Furi: rename and move core
* Furi: drop CMSIS_OS header and unused api, partially refactor and cleanup the rest
* Furi: CMSIS_OS drop and refactoring.
* Furi: refactoring, remove cmsis legacy
* Furi: fix incorrect assert on queue deallocation, cleanup timer
* Furi: improve delay api, get rid of floats
* hal: dropped furi_hal_crc
* Furi: move DWT based delay to cortex HAL
* Furi: update core documentation

Co-authored-by: hedger <hedger@nanode.su>
This commit is contained in:
あく
2022-07-20 13:56:33 +03:00
committed by GitHub
parent f9c2287ea7
commit e3c7201a20
264 changed files with 2569 additions and 3883 deletions

View File

@@ -32,7 +32,7 @@ extern "C" {
#include <stdlib.h>
#include <stdarg.h>
#include <furi/common_defines.h>
#include <core/common_defines.h>
#include "app_conf.h"

View File

@@ -22,8 +22,8 @@ _Static_assert(
"Ble stack config structure size mismatch");
typedef struct {
osMutexId_t hci_mtx;
osSemaphoreId_t hci_sem;
FuriMutex* hci_mtx;
FuriSemaphore* hci_sem;
FuriThread* thread;
} BleApp;
@@ -37,8 +37,8 @@ bool ble_app_init() {
SHCI_CmdStatus_t status;
ble_app = malloc(sizeof(BleApp));
// Allocate semafore and mutex for ble command buffer access
ble_app->hci_mtx = osMutexNew(NULL);
ble_app->hci_sem = osSemaphoreNew(1, 0, NULL);
ble_app->hci_mtx = furi_mutex_alloc(FuriMutexTypeNormal);
ble_app->hci_sem = furi_semaphore_alloc(1, 0);
// HCI transport layer thread to handle user asynch events
ble_app->thread = furi_thread_alloc();
furi_thread_set_name(ble_app->thread, "BleHciDriver");
@@ -113,8 +113,8 @@ void ble_app_thread_stop() {
furi_thread_join(ble_app->thread);
furi_thread_free(ble_app->thread);
// Free resources
osMutexDelete(ble_app->hci_mtx);
osSemaphoreDelete(ble_app->hci_sem);
furi_mutex_free(ble_app->hci_mtx);
furi_semaphore_free(ble_app->hci_sem);
free(ble_app);
ble_app = NULL;
memset(&ble_app_cmd_buffer, 0, sizeof(ble_app_cmd_buffer));
@@ -126,7 +126,7 @@ static int32_t ble_app_hci_thread(void* arg) {
uint32_t flags = 0;
while(1) {
flags = furi_thread_flags_wait(BLE_APP_FLAG_ALL, osFlagsWaitAny, osWaitForever);
flags = furi_thread_flags_wait(BLE_APP_FLAG_ALL, FuriFlagWaitAny, FuriWaitForever);
if(flags & BLE_APP_FLAG_KILL_THREAD) {
break;
}
@@ -151,14 +151,14 @@ void hci_notify_asynch_evt(void* pdata) {
void hci_cmd_resp_release(uint32_t flag) {
UNUSED(flag);
if(ble_app) {
osSemaphoreRelease(ble_app->hci_sem);
furi_semaphore_release(ble_app->hci_sem);
}
}
void hci_cmd_resp_wait(uint32_t timeout) {
UNUSED(timeout);
if(ble_app) {
osSemaphoreAcquire(ble_app->hci_sem, osWaitForever);
furi_semaphore_acquire(ble_app->hci_sem, FuriWaitForever);
}
}
@@ -178,9 +178,9 @@ static void ble_app_hci_event_handler(void* pPayload) {
static void ble_app_hci_status_not_handler(HCI_TL_CmdStatus_t status) {
if(status == HCI_TL_CmdBusy) {
osMutexAcquire(ble_app->hci_mtx, osWaitForever);
furi_mutex_acquire(ble_app->hci_mtx, FuriWaitForever);
} else if(status == HCI_TL_CmdAvailable) {
osMutexRelease(ble_app->hci_mtx);
furi_mutex_release(ble_app->hci_mtx);
}
}

View File

@@ -30,8 +30,8 @@ ALIGN(4)
static uint8_t ble_glue_ble_spare_event_buff[sizeof(TL_PacketHeader_t) + TL_EVT_HDR_SIZE + 255];
typedef struct {
osMutexId_t shci_mtx;
osSemaphoreId_t shci_sem;
FuriMutex* shci_mtx;
FuriSemaphore* shci_sem;
FuriThread* thread;
BleGlueStatus status;
BleGlueKeyStorageChangedCallback callback;
@@ -74,8 +74,8 @@ void ble_glue_init() {
// Reference table initialization
TL_Init();
ble_glue->shci_mtx = osMutexNew(NULL);
ble_glue->shci_sem = osSemaphoreNew(1, 0, NULL);
ble_glue->shci_mtx = furi_mutex_alloc(FuriMutexTypeNormal);
ble_glue->shci_sem = furi_semaphore_alloc(1, 0);
// FreeRTOS system task creation
ble_glue->thread = furi_thread_alloc();
@@ -201,7 +201,7 @@ bool ble_glue_wait_for_c2_start(int32_t timeout) {
started = ble_glue->status == BleGlueStatusC2Started;
if(!started) {
timeout--;
osDelay(1);
furi_delay_tick(1);
}
} while(!started && (timeout > 0));
@@ -300,10 +300,10 @@ BleGlueCommandResult ble_glue_force_c2_mode(BleGlueC2Mode desired_mode) {
static void ble_glue_sys_status_not_callback(SHCI_TL_CmdStatus_t status) {
switch(status) {
case SHCI_TL_CmdBusy:
osMutexAcquire(ble_glue->shci_mtx, osWaitForever);
furi_mutex_acquire(ble_glue->shci_mtx, FuriWaitForever);
break;
case SHCI_TL_CmdAvailable:
osMutexRelease(ble_glue->shci_mtx);
furi_mutex_release(ble_glue->shci_mtx);
break;
default:
break;
@@ -368,8 +368,8 @@ void ble_glue_thread_stop() {
furi_thread_join(ble_glue->thread);
furi_thread_free(ble_glue->thread);
// Free resources
osMutexDelete(ble_glue->shci_mtx);
osSemaphoreDelete(ble_glue->shci_sem);
furi_mutex_free(ble_glue->shci_mtx);
furi_semaphore_free(ble_glue->shci_sem);
ble_glue_clear_shared_memory();
free(ble_glue);
ble_glue = NULL;
@@ -382,7 +382,7 @@ static int32_t ble_glue_shci_thread(void* context) {
uint32_t flags = 0;
while(true) {
flags = furi_thread_flags_wait(BLE_GLUE_FLAG_ALL, osFlagsWaitAny, osWaitForever);
flags = furi_thread_flags_wait(BLE_GLUE_FLAG_ALL, FuriFlagWaitAny, FuriWaitForever);
if(flags & BLE_GLUE_FLAG_SHCI_EVENT) {
shci_user_evt_proc();
}
@@ -406,14 +406,14 @@ void shci_notify_asynch_evt(void* pdata) {
void shci_cmd_resp_release(uint32_t flag) {
UNUSED(flag);
if(ble_glue) {
osSemaphoreRelease(ble_glue->shci_sem);
furi_semaphore_release(ble_glue->shci_sem);
}
}
void shci_cmd_resp_wait(uint32_t timeout) {
UNUSED(timeout);
if(ble_glue) {
osSemaphoreAcquire(ble_glue->shci_sem, osWaitForever);
furi_semaphore_acquire(ble_glue->shci_sem, FuriWaitForever);
}
}
@@ -468,7 +468,7 @@ BleGlueCommandResult ble_glue_fus_wait_operation() {
}
wip = fus_status == BleGlueCommandResultOperationOngoing;
if(wip) {
osDelay(20);
furi_delay_ms(20);
}
} while(wip);

View File

@@ -27,12 +27,12 @@ typedef struct {
GapConfig* config;
GapConnectionParams connection_params;
GapState state;
osMutexId_t state_mutex;
FuriMutex* state_mutex;
GapEventCallback on_event_cb;
void* context;
osTimerId_t advertise_timer;
FuriTimer* advertise_timer;
FuriThread* thread;
osMessageQueueId_t command_queue;
FuriMessageQueue* command_queue;
bool enable_adv;
} Gap;
@@ -94,7 +94,7 @@ SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification(void* pckt) {
event_pckt = (hci_event_pckt*)((hci_uart_pckt*)pckt)->data;
if(gap) {
osMutexAcquire(gap->state_mutex, osWaitForever);
furi_mutex_acquire(gap->state_mutex, FuriWaitForever);
}
switch(event_pckt->evt) {
case EVT_DISCONN_COMPLETE: {
@@ -152,7 +152,7 @@ SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification(void* pckt) {
gap->connection_params.supervisor_timeout = event->Supervision_Timeout;
// Stop advertising as connection completed
osTimerStop(gap->advertise_timer);
furi_timer_stop(gap->advertise_timer);
// Update connection status and handle
gap->state = GapStateConnected;
@@ -268,7 +268,7 @@ SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification(void* pckt) {
break;
}
if(gap) {
osMutexRelease(gap->state_mutex);
furi_mutex_release(gap->state_mutex);
}
return SVCCTL_UserEvtFlowEnable;
}
@@ -382,7 +382,7 @@ static void gap_advertise_start(GapState new_state) {
max_interval = 0x0fa0; // 2.5 s
}
// Stop advertising timer
osTimerStop(gap->advertise_timer);
furi_timer_stop(gap->advertise_timer);
if((new_state == GapStateAdvLowPower) &&
((gap->state == GapStateAdvFast) || (gap->state == GapStateAdvLowPower))) {
@@ -413,7 +413,7 @@ static void gap_advertise_start(GapState new_state) {
gap->state = new_state;
GapEvent event = {.type = GapEventTypeStartAdvertising};
gap->on_event_cb(event, gap->context);
osTimerStart(gap->advertise_timer, INITIAL_ADV_TIMEOUT);
furi_timer_start(gap->advertise_timer, INITIAL_ADV_TIMEOUT);
}
static void gap_advertise_stop() {
@@ -429,7 +429,7 @@ static void gap_advertise_stop() {
}
}
// Stop advertising
osTimerStop(gap->advertise_timer);
furi_timer_stop(gap->advertise_timer);
ret = aci_gap_set_non_discoverable();
if(ret != BLE_STATUS_SUCCESS) {
FURI_LOG_E(TAG, "set_non_discoverable failed %d", ret);
@@ -443,32 +443,32 @@ static void gap_advertise_stop() {
}
void gap_start_advertising() {
osMutexAcquire(gap->state_mutex, osWaitForever);
furi_mutex_acquire(gap->state_mutex, FuriWaitForever);
if(gap->state == GapStateIdle) {
gap->state = GapStateStartingAdv;
FURI_LOG_I(TAG, "Start advertising");
gap->enable_adv = true;
GapCommand command = GapCommandAdvFast;
furi_check(osMessageQueuePut(gap->command_queue, &command, 0, 0) == osOK);
furi_check(furi_message_queue_put(gap->command_queue, &command, 0) == FuriStatusOk);
}
osMutexRelease(gap->state_mutex);
furi_mutex_release(gap->state_mutex);
}
void gap_stop_advertising() {
osMutexAcquire(gap->state_mutex, osWaitForever);
furi_mutex_acquire(gap->state_mutex, FuriWaitForever);
if(gap->state > GapStateIdle) {
FURI_LOG_I(TAG, "Stop advertising");
gap->enable_adv = false;
GapCommand command = GapCommandAdvStop;
furi_check(osMessageQueuePut(gap->command_queue, &command, 0, 0) == osOK);
furi_check(furi_message_queue_put(gap->command_queue, &command, 0) == FuriStatusOk);
}
osMutexRelease(gap->state_mutex);
furi_mutex_release(gap->state_mutex);
}
static void gap_advetise_timer_callback(void* context) {
UNUSED(context);
GapCommand command = GapCommandAdvLowPower;
furi_check(osMessageQueuePut(gap->command_queue, &command, 0, 0) == osOK);
furi_check(furi_message_queue_put(gap->command_queue, &command, 0) == FuriStatusOk);
}
bool gap_init(GapConfig* config, GapEventCallback on_event_cb, void* context) {
@@ -480,14 +480,14 @@ bool gap_init(GapConfig* config, GapEventCallback on_event_cb, void* context) {
gap->config = config;
srand(DWT->CYCCNT);
// Create advertising timer
gap->advertise_timer = osTimerNew(gap_advetise_timer_callback, osTimerOnce, NULL, NULL);
gap->advertise_timer = furi_timer_alloc(gap_advetise_timer_callback, FuriTimerTypeOnce, NULL);
// Initialization of GATT & GAP layer
gap->service.adv_name = config->adv_name;
gap_init_svc(gap);
// Initialization of the BLE Services
SVCCTL_Init();
// Initialization of the GAP state
gap->state_mutex = osMutexNew(NULL);
gap->state_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
gap->state = GapStateIdle;
gap->service.connection_handle = 0xFFFF;
gap->enable_adv = true;
@@ -501,7 +501,7 @@ bool gap_init(GapConfig* config, GapEventCallback on_event_cb, void* context) {
furi_thread_start(gap->thread);
// Command queue allocation
gap->command_queue = osMessageQueueNew(8, sizeof(GapCommand), NULL);
gap->command_queue = furi_message_queue_alloc(8, sizeof(GapCommand));
uint8_t adv_service_uid[2];
gap->service.adv_svc_uuid_len = 1;
@@ -518,9 +518,9 @@ bool gap_init(GapConfig* config, GapEventCallback on_event_cb, void* context) {
GapState gap_get_state() {
GapState state;
if(gap) {
osMutexAcquire(gap->state_mutex, osWaitForever);
furi_mutex_acquire(gap->state_mutex, FuriWaitForever);
state = gap->state;
osMutexRelease(gap->state_mutex);
furi_mutex_release(gap->state_mutex);
} else {
state = GapStateUninitialized;
}
@@ -529,19 +529,19 @@ GapState gap_get_state() {
void gap_thread_stop() {
if(gap) {
osMutexAcquire(gap->state_mutex, osWaitForever);
furi_mutex_acquire(gap->state_mutex, FuriWaitForever);
gap->enable_adv = false;
GapCommand command = GapCommandKillThread;
osMessageQueuePut(gap->command_queue, &command, 0, osWaitForever);
osMutexRelease(gap->state_mutex);
furi_message_queue_put(gap->command_queue, &command, FuriWaitForever);
furi_mutex_release(gap->state_mutex);
furi_thread_join(gap->thread);
furi_thread_free(gap->thread);
// Free resources
osMutexDelete(gap->state_mutex);
osMessageQueueDelete(gap->command_queue);
osTimerStop(gap->advertise_timer);
while(xTimerIsTimerActive(gap->advertise_timer) == pdTRUE) osDelay(1);
furi_check(osTimerDelete(gap->advertise_timer) == osOK);
furi_mutex_free(gap->state_mutex);
furi_message_queue_free(gap->command_queue);
furi_timer_stop(gap->advertise_timer);
while(xTimerIsTimerActive(gap->advertise_timer) == pdTRUE) furi_delay_tick(1);
furi_timer_free(gap->advertise_timer);
free(gap);
gap = NULL;
}
@@ -551,12 +551,12 @@ static int32_t gap_app(void* context) {
UNUSED(context);
GapCommand command;
while(1) {
osStatus_t status = osMessageQueueGet(gap->command_queue, &command, NULL, osWaitForever);
if(status != osOK) {
FuriStatus status = furi_message_queue_get(gap->command_queue, &command, FuriWaitForever);
if(status != FuriStatusOk) {
FURI_LOG_E(TAG, "Message queue get error: %d", status);
continue;
}
osMutexAcquire(gap->state_mutex, osWaitForever);
furi_mutex_acquire(gap->state_mutex, FuriWaitForever);
if(command == GapCommandKillThread) {
break;
}
@@ -567,7 +567,7 @@ static int32_t gap_app(void* context) {
} else if(command == GapCommandAdvStop) {
gap_advertise_stop();
}
osMutexRelease(gap->state_mutex);
furi_mutex_release(gap->state_mutex);
}
return 0;

View File

@@ -11,7 +11,7 @@ typedef struct {
uint16_t rx_char_handle;
uint16_t tx_char_handle;
uint16_t flow_ctrl_char_handle;
osMutexId_t buff_size_mtx;
FuriMutex* buff_size_mtx;
uint32_t buff_size;
uint16_t bytes_ready_to_receive;
SerialServiceEventCallback callback;
@@ -44,7 +44,9 @@ static SVCCTL_EvtAckStatus_t serial_svc_event_handler(void* event) {
} else if(attribute_modified->Attr_Handle == serial_svc->rx_char_handle + 1) {
FURI_LOG_D(TAG, "Received %d bytes", attribute_modified->Attr_Data_Length);
if(serial_svc->callback) {
furi_check(osMutexAcquire(serial_svc->buff_size_mtx, osWaitForever) == osOK);
furi_check(
furi_mutex_acquire(serial_svc->buff_size_mtx, FuriWaitForever) ==
FuriStatusOk);
if(attribute_modified->Attr_Data_Length > serial_svc->bytes_ready_to_receive) {
FURI_LOG_W(
TAG,
@@ -62,7 +64,7 @@ static SVCCTL_EvtAckStatus_t serial_svc_event_handler(void* event) {
}};
uint32_t buff_free_size = serial_svc->callback(event, serial_svc->context);
FURI_LOG_D(TAG, "Available buff size: %d", buff_free_size);
furi_check(osMutexRelease(serial_svc->buff_size_mtx) == osOK);
furi_check(furi_mutex_release(serial_svc->buff_size_mtx) == FuriStatusOk);
}
ret = SVCCTL_EvtAckFlowEnable;
}
@@ -140,7 +142,7 @@ void serial_svc_start() {
FURI_LOG_E(TAG, "Failed to add Flow Control characteristic: %d", status);
}
// Allocate buffer size mutex
serial_svc->buff_size_mtx = osMutexNew(NULL);
serial_svc->buff_size_mtx = furi_mutex_alloc(FuriMutexTypeNormal);
}
void serial_svc_set_callbacks(
@@ -165,7 +167,7 @@ void serial_svc_notify_buffer_is_empty() {
furi_assert(serial_svc);
furi_assert(serial_svc->buff_size_mtx);
furi_check(osMutexAcquire(serial_svc->buff_size_mtx, osWaitForever) == osOK);
furi_check(furi_mutex_acquire(serial_svc->buff_size_mtx, FuriWaitForever) == FuriStatusOk);
if(serial_svc->bytes_ready_to_receive == 0) {
FURI_LOG_D(TAG, "Buffer is empty. Notifying client");
serial_svc->bytes_ready_to_receive = serial_svc->buff_size;
@@ -177,7 +179,7 @@ void serial_svc_notify_buffer_is_empty() {
sizeof(uint32_t),
(uint8_t*)&buff_size_reversed);
}
furi_check(osMutexRelease(serial_svc->buff_size_mtx) == osOK);
furi_check(furi_mutex_release(serial_svc->buff_size_mtx) == FuriStatusOk);
}
void serial_svc_stop() {
@@ -202,7 +204,7 @@ void serial_svc_stop() {
FURI_LOG_E(TAG, "Failed to delete Serial service: %d", status);
}
// Delete buffer size mutex
osMutexDelete(serial_svc->buff_size_mtx);
furi_mutex_free(serial_svc->buff_size_mtx);
free(serial_svc);
serial_svc = NULL;
}