2020-10-13 11:22:43 +03:00
|
|
|
#pragma once
|
|
|
|
|
[FL-140] Core api dynamic records (#296)
* SYSTEM: tickless mode with deep sleep.
* Move FreeRTOS ticks to lptim2
* API: move all sumbodules init routines to one place. Timebase: working lptim2 at tick source.
* API Timebase: lp-timer routines, timer access safe zones prediction and synchronization. FreeRTOS: adjust configuration for tickless mode.
* NFC: support for tickless mode.
* API Timebase: improve tick error handling in IRQ. Apploader: use insomnia mode to run applications.
* BLE: prevent sleep while core2 starting
* HAL: nap while in insomnia mode
* init records work
* try to implement record delete
* tests and flapp
* flapp subsystem
* new core functions to get app stat, simplify core code
* fix thread termination
* add strdup to core
* fix tests
* Refactoring: remove all unusued parts, update API usage, aggreagate API sources and headers, new record storage
* Refactoring: update furi record api usage, cleanup code
* Fix broken merge for freertos apps
* Core, Target: fix compilation warnings
* Drop firmware target local
* HAL Timebase, Power, Clock: semaphore guarded access to clock and power modes, better sleep mode.
* SD-Filesystem: wait for all deps to arrive before adding widget. Core, BLE: disable debug dump to serial.
* delete old app example-ipc
* delete old app fatfs list
* fix strobe app, add input header
* delete old display driver
* comment old app qr-code
* fix sd-card test, add forced widget update
* remove unused new core test
* increase heap to 128k
* comment and assert old core tests
* fix syntax
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2021-01-20 19:09:26 +03:00
|
|
|
#include <stdbool.h>
|
2022-06-20 17:54:48 +03:00
|
|
|
#include "mutex.h"
|
[FL-140] Core api dynamic records (#296)
* SYSTEM: tickless mode with deep sleep.
* Move FreeRTOS ticks to lptim2
* API: move all sumbodules init routines to one place. Timebase: working lptim2 at tick source.
* API Timebase: lp-timer routines, timer access safe zones prediction and synchronization. FreeRTOS: adjust configuration for tickless mode.
* NFC: support for tickless mode.
* API Timebase: improve tick error handling in IRQ. Apploader: use insomnia mode to run applications.
* BLE: prevent sleep while core2 starting
* HAL: nap while in insomnia mode
* init records work
* try to implement record delete
* tests and flapp
* flapp subsystem
* new core functions to get app stat, simplify core code
* fix thread termination
* add strdup to core
* fix tests
* Refactoring: remove all unusued parts, update API usage, aggreagate API sources and headers, new record storage
* Refactoring: update furi record api usage, cleanup code
* Fix broken merge for freertos apps
* Core, Target: fix compilation warnings
* Drop firmware target local
* HAL Timebase, Power, Clock: semaphore guarded access to clock and power modes, better sleep mode.
* SD-Filesystem: wait for all deps to arrive before adding widget. Core, BLE: disable debug dump to serial.
* delete old app example-ipc
* delete old app fatfs list
* fix strobe app, add input header
* delete old display driver
* comment old app qr-code
* fix sd-card test, add forced widget update
* remove unused new core test
* increase heap to 128k
* comment and assert old core tests
* fix syntax
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2021-01-20 19:09:26 +03:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2020-10-13 11:22:43 +03:00
|
|
|
|
2021-03-24 12:35:33 +03:00
|
|
|
/**
|
|
|
|
* == ValueMutex ==
|
2020-10-13 11:22:43 +03:00
|
|
|
|
2021-03-24 12:35:33 +03:00
|
|
|
* The most simple concept is ValueMutex.
|
|
|
|
* It is wrapper around mutex and value pointer.
|
|
|
|
* You can take and give mutex to work with value and read and write value.
|
|
|
|
*/
|
2020-10-13 11:22:43 +03:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
void* value;
|
|
|
|
size_t size;
|
2022-07-20 13:56:33 +03:00
|
|
|
FuriMutex* mutex;
|
2020-10-13 11:22:43 +03:00
|
|
|
} ValueMutex;
|
|
|
|
|
2021-03-24 12:35:33 +03:00
|
|
|
/**
|
|
|
|
* Creates ValueMutex.
|
|
|
|
*/
|
2020-10-13 11:22:43 +03:00
|
|
|
bool init_mutex(ValueMutex* valuemutex, void* value, size_t size);
|
|
|
|
|
2021-03-24 12:35:33 +03:00
|
|
|
/**
|
|
|
|
* Free resources allocated by `init_mutex`.
|
|
|
|
* This function doesn't free the memory occupied by `ValueMutex` itself.
|
|
|
|
*/
|
2020-10-26 12:26:15 +03:00
|
|
|
bool delete_mutex(ValueMutex* valuemutex);
|
|
|
|
|
2021-03-24 12:35:33 +03:00
|
|
|
/**
|
|
|
|
* Call for work with data stored in mutex.
|
|
|
|
* @return pointer to data if success, NULL otherwise.
|
|
|
|
*/
|
2020-10-13 11:22:43 +03:00
|
|
|
void* acquire_mutex(ValueMutex* valuemutex, uint32_t timeout);
|
|
|
|
|
2021-03-24 12:35:33 +03:00
|
|
|
/**
|
|
|
|
* Helper: infinitly wait for mutex
|
|
|
|
*/
|
2020-10-13 11:22:43 +03:00
|
|
|
static inline void* acquire_mutex_block(ValueMutex* valuemutex) {
|
2022-07-20 13:56:33 +03:00
|
|
|
return acquire_mutex(valuemutex, FuriWaitForever);
|
2020-10-13 11:22:43 +03:00
|
|
|
}
|
|
|
|
|
2021-03-24 12:35:33 +03:00
|
|
|
/**
|
2020-10-26 20:00:17 +03:00
|
|
|
* With statement for value mutex, acts as lambda
|
|
|
|
* @param name a resource name, const char*
|
|
|
|
* @param function_body a (){} lambda declaration,
|
|
|
|
* executed within you parent function context.
|
2021-03-24 12:35:33 +03:00
|
|
|
*/
|
2020-10-26 20:00:17 +03:00
|
|
|
#define with_value_mutex(value_mutex, function_body) \
|
|
|
|
{ \
|
|
|
|
void* p = acquire_mutex_block(value_mutex); \
|
2020-10-29 09:27:17 +03:00
|
|
|
furi_check(p); \
|
2020-10-26 20:00:17 +03:00
|
|
|
({ void __fn__ function_body __fn__; })(p); \
|
|
|
|
release_mutex(value_mutex, p); \
|
|
|
|
}
|
|
|
|
|
2021-03-24 12:35:33 +03:00
|
|
|
/**
|
|
|
|
* Release mutex after end of work with data.
|
|
|
|
* Call `release_mutex` and pass ValueData instance and pointer to data.
|
|
|
|
*/
|
2020-11-19 15:25:32 +03:00
|
|
|
bool release_mutex(ValueMutex* valuemutex, const void* value);
|
2020-10-13 11:22:43 +03:00
|
|
|
|
2021-03-24 12:35:33 +03:00
|
|
|
/**
|
|
|
|
* Instead of take-access-give sequence you can use `read_mutex` and `write_mutex` functions.
|
|
|
|
* Both functions return true in case of success, false otherwise.
|
|
|
|
*/
|
2020-10-13 11:22:43 +03:00
|
|
|
bool read_mutex(ValueMutex* valuemutex, void* data, size_t len, uint32_t timeout);
|
|
|
|
|
|
|
|
bool write_mutex(ValueMutex* valuemutex, void* data, size_t len, uint32_t timeout);
|
|
|
|
|
|
|
|
inline static bool write_mutex_block(ValueMutex* valuemutex, void* data, size_t len) {
|
2022-07-20 13:56:33 +03:00
|
|
|
return write_mutex(valuemutex, data, len, FuriWaitForever);
|
2020-10-13 11:22:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
inline static bool read_mutex_block(ValueMutex* valuemutex, void* data, size_t len) {
|
2022-07-20 13:56:33 +03:00
|
|
|
return read_mutex(valuemutex, data, len, FuriWaitForever);
|
2020-10-13 11:22:43 +03:00
|
|
|
}
|
|
|
|
|
[FL-140] Core api dynamic records (#296)
* SYSTEM: tickless mode with deep sleep.
* Move FreeRTOS ticks to lptim2
* API: move all sumbodules init routines to one place. Timebase: working lptim2 at tick source.
* API Timebase: lp-timer routines, timer access safe zones prediction and synchronization. FreeRTOS: adjust configuration for tickless mode.
* NFC: support for tickless mode.
* API Timebase: improve tick error handling in IRQ. Apploader: use insomnia mode to run applications.
* BLE: prevent sleep while core2 starting
* HAL: nap while in insomnia mode
* init records work
* try to implement record delete
* tests and flapp
* flapp subsystem
* new core functions to get app stat, simplify core code
* fix thread termination
* add strdup to core
* fix tests
* Refactoring: remove all unusued parts, update API usage, aggreagate API sources and headers, new record storage
* Refactoring: update furi record api usage, cleanup code
* Fix broken merge for freertos apps
* Core, Target: fix compilation warnings
* Drop firmware target local
* HAL Timebase, Power, Clock: semaphore guarded access to clock and power modes, better sleep mode.
* SD-Filesystem: wait for all deps to arrive before adding widget. Core, BLE: disable debug dump to serial.
* delete old app example-ipc
* delete old app fatfs list
* fix strobe app, add input header
* delete old display driver
* comment old app qr-code
* fix sd-card test, add forced widget update
* remove unused new core test
* increase heap to 128k
* comment and assert old core tests
* fix syntax
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2021-01-20 19:09:26 +03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-10-13 11:22:43 +03:00
|
|
|
/*
|
|
|
|
|
|
|
|
Usage example
|
|
|
|
|
|
|
|
```C
|
|
|
|
// MANIFEST
|
|
|
|
// name="example-provider-app"
|
|
|
|
// stack=128
|
|
|
|
|
|
|
|
void provider_app(void* _p) {
|
|
|
|
// create record with mutex
|
|
|
|
uint32_t example_value = 0;
|
|
|
|
ValueMutex example_mutex;
|
|
|
|
// call `init_mutex`.
|
|
|
|
if(!init_mutex(&example_mutex, (void*)&example_value, sizeof(uint32_t))) {
|
|
|
|
printf("critical error\n");
|
|
|
|
flapp_exit(NULL);
|
|
|
|
}
|
|
|
|
|
[FL-140] Core api dynamic records (#296)
* SYSTEM: tickless mode with deep sleep.
* Move FreeRTOS ticks to lptim2
* API: move all sumbodules init routines to one place. Timebase: working lptim2 at tick source.
* API Timebase: lp-timer routines, timer access safe zones prediction and synchronization. FreeRTOS: adjust configuration for tickless mode.
* NFC: support for tickless mode.
* API Timebase: improve tick error handling in IRQ. Apploader: use insomnia mode to run applications.
* BLE: prevent sleep while core2 starting
* HAL: nap while in insomnia mode
* init records work
* try to implement record delete
* tests and flapp
* flapp subsystem
* new core functions to get app stat, simplify core code
* fix thread termination
* add strdup to core
* fix tests
* Refactoring: remove all unusued parts, update API usage, aggreagate API sources and headers, new record storage
* Refactoring: update furi record api usage, cleanup code
* Fix broken merge for freertos apps
* Core, Target: fix compilation warnings
* Drop firmware target local
* HAL Timebase, Power, Clock: semaphore guarded access to clock and power modes, better sleep mode.
* SD-Filesystem: wait for all deps to arrive before adding widget. Core, BLE: disable debug dump to serial.
* delete old app example-ipc
* delete old app fatfs list
* fix strobe app, add input header
* delete old display driver
* comment old app qr-code
* fix sd-card test, add forced widget update
* remove unused new core test
* increase heap to 128k
* comment and assert old core tests
* fix syntax
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2021-01-20 19:09:26 +03:00
|
|
|
furi_record_create("provider/example", (void*)&example_mutex);
|
2020-10-13 11:22:43 +03:00
|
|
|
|
|
|
|
// we are ready to provide record to other apps
|
|
|
|
flapp_ready();
|
|
|
|
|
|
|
|
// get value and increment it
|
|
|
|
while(1) {
|
|
|
|
uint32_t* value = acquire_mutex(&example_mutex, OsWaitForever);
|
|
|
|
if(value != NULL) {
|
|
|
|
value++;
|
|
|
|
}
|
|
|
|
release_mutex(&example_mutex, value);
|
|
|
|
|
2022-07-20 13:56:33 +03:00
|
|
|
furi_delay_ms(100);
|
2020-10-13 11:22:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MANIFEST
|
|
|
|
// name="example-consumer-app"
|
|
|
|
// stack=128
|
|
|
|
// require="example-provider-app"
|
|
|
|
void consumer_app(void* _p) {
|
|
|
|
// this app run after flapp_ready call in all requirements app
|
|
|
|
|
|
|
|
// open mutex value
|
[FL-140] Core api dynamic records (#296)
* SYSTEM: tickless mode with deep sleep.
* Move FreeRTOS ticks to lptim2
* API: move all sumbodules init routines to one place. Timebase: working lptim2 at tick source.
* API Timebase: lp-timer routines, timer access safe zones prediction and synchronization. FreeRTOS: adjust configuration for tickless mode.
* NFC: support for tickless mode.
* API Timebase: improve tick error handling in IRQ. Apploader: use insomnia mode to run applications.
* BLE: prevent sleep while core2 starting
* HAL: nap while in insomnia mode
* init records work
* try to implement record delete
* tests and flapp
* flapp subsystem
* new core functions to get app stat, simplify core code
* fix thread termination
* add strdup to core
* fix tests
* Refactoring: remove all unusued parts, update API usage, aggreagate API sources and headers, new record storage
* Refactoring: update furi record api usage, cleanup code
* Fix broken merge for freertos apps
* Core, Target: fix compilation warnings
* Drop firmware target local
* HAL Timebase, Power, Clock: semaphore guarded access to clock and power modes, better sleep mode.
* SD-Filesystem: wait for all deps to arrive before adding widget. Core, BLE: disable debug dump to serial.
* delete old app example-ipc
* delete old app fatfs list
* fix strobe app, add input header
* delete old display driver
* comment old app qr-code
* fix sd-card test, add forced widget update
* remove unused new core test
* increase heap to 128k
* comment and assert old core tests
* fix syntax
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2021-01-20 19:09:26 +03:00
|
|
|
ValueMutex* counter_mutex = furi_record_open("provider/example");
|
2020-10-13 11:22:43 +03:00
|
|
|
if(counter_mutex == NULL) {
|
|
|
|
printf("critical error\n");
|
|
|
|
flapp_exit(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// continously read value every 1s
|
|
|
|
uint32_t counter;
|
|
|
|
while(1) {
|
|
|
|
if(read_mutex(counter_mutex, &counter, sizeof(counter), OsWaitForever)) {
|
|
|
|
printf("counter value: %d\n", counter);
|
|
|
|
}
|
|
|
|
|
2022-07-20 13:56:33 +03:00
|
|
|
furi_delay_ms(1000);
|
2020-10-13 11:22:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
2022-05-06 16:37:10 +03:00
|
|
|
*/
|