Skip to content

Commit

Permalink
Furi: wrap message queue in container, prepare it for epoll. Accessor…
Browse files Browse the repository at this point in the history
…: disable expansion service on start. (#3672)

* Furi: wrap message queue in container, prepare it for epoll. Accessor: disable expansion service on start.
* Furi: one allocation, smaller message queue struct, type compatibility with FreeRTOS.
  • Loading branch information
skotopes authored May 30, 2024
1 parent f309844 commit 3f04d6d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 15 deletions.
4 changes: 4 additions & 0 deletions applications/debug/accessor/accessor_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@ void AccessorApp::run(void) {
AccessorApp::AccessorApp()
: text_store{0} {
notification = static_cast<NotificationApp*>(furi_record_open(RECORD_NOTIFICATION));
expansion = static_cast<Expansion*>(furi_record_open(RECORD_EXPANSION));
onewire_host = onewire_host_alloc(&gpio_ibutton);
expansion_disable(expansion);
furi_hal_power_enable_otg();
}

AccessorApp::~AccessorApp() {
furi_hal_power_disable_otg();
expansion_enable(expansion);
furi_record_close(RECORD_EXPANSION);
furi_record_close(RECORD_NOTIFICATION);
onewire_host_free(onewire_host);
}
Expand Down
2 changes: 2 additions & 0 deletions applications/debug/accessor/accessor_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "helpers/wiegand.h"
#include <one_wire/one_wire_host.h>
#include <notification/notification_messages.h>
#include <expansion/expansion.h>

class AccessorApp {
public:
Expand Down Expand Up @@ -51,4 +52,5 @@ class AccessorApp {
OneWireHost* onewire_host;

NotificationApp* notification;
Expansion* expansion;
};
10 changes: 6 additions & 4 deletions applications/system/snake_game/snake_game.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,17 @@ static void snake_game_render_callback(Canvas* const canvas, void* ctx) {
furi_mutex_release(snake_state->mutex);
}

static void snake_game_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
furi_assert(event_queue);
static void snake_game_input_callback(InputEvent* input_event, void* context) {
furi_assert(context);
FuriMessageQueue* event_queue = context;

SnakeEvent event = {.type = EventTypeKey, .input = *input_event};
furi_message_queue_put(event_queue, &event, FuriWaitForever);
}

static void snake_game_update_timer_callback(FuriMessageQueue* event_queue) {
furi_assert(event_queue);
static void snake_game_update_timer_callback(void* context) {
furi_assert(context);
FuriMessageQueue* event_queue = context;

SnakeEvent event = {.type = EventTypeTick};
furi_message_queue_put(event_queue, &event, 0);
Expand Down
35 changes: 25 additions & 10 deletions furi/core/message_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,38 @@
#include <FreeRTOS.h>
#include <queue.h>

struct FuriMessageQueue {
// !!! Semi-Opaque type inheritance, Very Fragile, DO NOT MOVE !!!
StaticQueue_t container;

// !!! Data buffer, must be last in the structure, DO NOT MOVE !!!
uint8_t buffer[];
};

FuriMessageQueue* furi_message_queue_alloc(uint32_t msg_count, uint32_t msg_size) {
furi_check((furi_kernel_is_irq_or_masked() == 0U) && (msg_count > 0U) && (msg_size > 0U));

QueueHandle_t handle = xQueueCreate(msg_count, msg_size);
furi_check(handle);
FuriMessageQueue* instance = malloc(sizeof(FuriMessageQueue) + msg_count * msg_size);

// 3 things happens here:
// - create queue
// - check results
// - ensure that queue container is first in the FuriMessageQueue structure
//
// As a bonus it guarantees that FuriMessageQueue* can be casted into StaticQueue_t* or QueueHandle_t.
furi_check(
xQueueCreateStatic(msg_count, msg_size, instance->buffer, &instance->container) ==
(void*)instance);

return ((FuriMessageQueue*)handle);
return instance;
}

void furi_message_queue_free(FuriMessageQueue* instance) {
furi_check(furi_kernel_is_irq_or_masked() == 0U);
furi_check(instance);

vQueueDelete((QueueHandle_t)instance);
free(instance);
}

FuriStatus
Expand Down Expand Up @@ -102,11 +120,10 @@ FuriStatus furi_message_queue_get(FuriMessageQueue* instance, void* msg_ptr, uin
uint32_t furi_message_queue_get_capacity(FuriMessageQueue* instance) {
furi_check(instance);

StaticQueue_t* mq = (StaticQueue_t*)instance;
uint32_t capacity;

/* capacity = pxQueue->uxLength */
capacity = mq->uxDummy4[1];
capacity = instance->container.uxDummy4[1];

/* Return maximum number of messages */
return (capacity);
Expand All @@ -115,11 +132,10 @@ uint32_t furi_message_queue_get_capacity(FuriMessageQueue* instance) {
uint32_t furi_message_queue_get_message_size(FuriMessageQueue* instance) {
furi_check(instance);

StaticQueue_t* mq = (StaticQueue_t*)instance;
uint32_t size;

/* size = pxQueue->uxItemSize */
size = mq->uxDummy4[2];
size = instance->container.uxDummy4[2];

/* Return maximum message size */
return (size);
Expand All @@ -144,19 +160,18 @@ uint32_t furi_message_queue_get_count(FuriMessageQueue* instance) {
uint32_t furi_message_queue_get_space(FuriMessageQueue* instance) {
furi_check(instance);

StaticQueue_t* mq = (StaticQueue_t*)instance;
uint32_t space;
uint32_t isrm;

if(furi_kernel_is_irq_or_masked() != 0U) {
isrm = taskENTER_CRITICAL_FROM_ISR();

/* space = pxQueue->uxLength - pxQueue->uxMessagesWaiting; */
space = mq->uxDummy4[1] - mq->uxDummy4[0];
space = instance->container.uxDummy4[1] - instance->container.uxDummy4[0];

taskEXIT_CRITICAL_FROM_ISR(isrm);
} else {
space = (uint32_t)uxQueueSpacesAvailable((QueueHandle_t)mq);
space = (uint32_t)uxQueueSpacesAvailable((QueueHandle_t)instance);
}

/* Return number of available slots */
Expand Down
2 changes: 1 addition & 1 deletion furi/core/message_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
extern "C" {
#endif

typedef void FuriMessageQueue;
typedef struct FuriMessageQueue FuriMessageQueue;

/** Allocate furi message queue
*
Expand Down

0 comments on commit 3f04d6d

Please sign in to comment.