FL-262 Interrupt manager (#270)
* interrupt manager * init interrupt manager * add usage to lf-rfid app * check ready flag * move interrupts code to target hal * fix path
This commit is contained in:
65
core/api-hal/api-interrupt-mgr.c
Normal file
65
core/api-hal/api-interrupt-mgr.c
Normal file
@@ -0,0 +1,65 @@
|
||||
#include "api-interrupt-mgr.h"
|
||||
|
||||
LIST_DEF(list_interrupt, InterruptCallbackItem, M_POD_OPLIST);
|
||||
list_interrupt_t interrupts;
|
||||
osMutexId_t interrupt_list_mutex;
|
||||
|
||||
bool api_interrupt_init() {
|
||||
interrupt_list_mutex = osMutexNew(NULL);
|
||||
return (interrupt_list_mutex != NULL);
|
||||
}
|
||||
|
||||
void api_interrupt_add(InterruptCallback callback, InterruptType type, void* context) {
|
||||
if(osMutexAcquire(interrupt_list_mutex, osWaitForever) == osOK) {
|
||||
// put uninitialized item to the list
|
||||
// M_POD_OPLIST provide memset(&(a), 0, sizeof (a)) constructor
|
||||
// so item will not be ready until we set ready flag
|
||||
InterruptCallbackItem* item = list_interrupt_push_new(interrupts);
|
||||
|
||||
// initialize item
|
||||
item->callback = callback;
|
||||
item->type = type;
|
||||
item->context = context;
|
||||
item->ready = true;
|
||||
|
||||
// TODO remove on app exit
|
||||
//flapp_on_exit(api_interrupt_remove, callback);
|
||||
|
||||
osMutexRelease(interrupt_list_mutex);
|
||||
}
|
||||
}
|
||||
|
||||
void api_interrupt_remove(InterruptCallback callback) {
|
||||
if(osMutexAcquire(interrupt_list_mutex, osWaitForever) == osOK) {
|
||||
// iterate over items
|
||||
list_interrupt_it_t it;
|
||||
for(list_interrupt_it(it, interrupts); !list_interrupt_end_p(it);
|
||||
list_interrupt_next(it)) {
|
||||
const InterruptCallbackItem* item = list_interrupt_cref(it);
|
||||
|
||||
// if the iterator is equal to our element
|
||||
if(item->callback == callback) {
|
||||
list_interrupt_remove(interrupts, it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
osMutexRelease(interrupt_list_mutex);
|
||||
}
|
||||
}
|
||||
|
||||
void api_interrupt_call(InterruptType type, void* hw) {
|
||||
// that executed in interrupt ctx so mutex don't needed
|
||||
// but we need to check ready flag
|
||||
|
||||
// iterate over items
|
||||
list_interrupt_it_t it;
|
||||
for(list_interrupt_it(it, interrupts); !list_interrupt_end_p(it); list_interrupt_next(it)) {
|
||||
const InterruptCallbackItem* item = list_interrupt_cref(it);
|
||||
|
||||
// if the iterator is equal to our element
|
||||
if(item->type == type && item->ready) {
|
||||
item->callback(hw, item->context);
|
||||
}
|
||||
}
|
||||
}
|
19
core/api-hal/api-interrupt-mgr.h
Normal file
19
core/api-hal/api-interrupt-mgr.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
#include "flipper_v2.h"
|
||||
|
||||
typedef void (*InterruptCallback)(void*, void*);
|
||||
|
||||
typedef enum {
|
||||
InterruptTypeComparatorTrigger = 0,
|
||||
} InterruptType;
|
||||
|
||||
typedef struct {
|
||||
InterruptCallback callback;
|
||||
InterruptType type;
|
||||
void* context;
|
||||
bool ready;
|
||||
} InterruptCallbackItem;
|
||||
|
||||
bool api_interrupt_init();
|
||||
void api_interrupt_add(InterruptCallback callback, InterruptType type, void* context);
|
||||
void api_interrupt_remove(InterruptCallback callback);
|
Reference in New Issue
Block a user