Example ipc (#60)

* add blank example

* add ipc example code, need to change FURI API

* add ipc example code, need to change FURI API

* change core API, add context

* check handler at take

* fix important bugs in furi

* drawing example

* add posix mq

* fix unsigned demo counter

* create at open

* working local demo

* russian version of IPC example

* english version

* add gif
This commit is contained in:
coreglitch
2020-09-01 16:34:23 +06:00
committed by GitHub
parent f7882dbff4
commit 5b6ab7faf3
16 changed files with 538 additions and 33 deletions

View File

@@ -1,3 +1,5 @@
#pragma once
#include "main.h"
#include <stdbool.h>
@@ -5,12 +7,33 @@ void osDelay(uint32_t ms);
// some FreeRTOS types
typedef void(*TaskFunction_t)(void*);
typedef uint32_t UBaseType_t;
typedef size_t UBaseType_t;
typedef uint32_t StackType_t;
typedef uint32_t StaticTask_t;
typedef pthread_t* TaskHandle_t;
typedef uint32_t StaticSemaphore_t;
typedef void* SemaphoreHandle_t;
typedef enum {
SemaphoreTypeCounting
} SemaphoreType;
typedef struct {
SemaphoreType type;
uint8_t take_counter;
uint8_t give_counter;
} StaticSemaphore_t;
typedef StaticSemaphore_t* SemaphoreHandle_t;
typedef uint32_t StaticQueue_t;
typedef StaticQueue_t* QueueHandle_t;
#define portMAX_DELAY -1
typedef enum {
pdTRUE = 1,
pdFALSE = 0
} BaseType_t;
typedef int32_t TickType_t;
#define tskIDLE_PRIORITY 0
@@ -28,3 +51,24 @@ void vTaskDelete(TaskHandle_t xTask);
TaskHandle_t xTaskGetCurrentTaskHandle(void);
SemaphoreHandle_t xSemaphoreCreateMutexStatic(StaticSemaphore_t* pxMutexBuffer);
bool task_equal(TaskHandle_t a, TaskHandle_t b);
QueueHandle_t xQueueCreateStatic(
UBaseType_t uxQueueLength,
UBaseType_t uxItemSize,
uint8_t* pucQueueStorageBuffer,
StaticQueue_t* pxQueueBuffer
);
SemaphoreHandle_t xSemaphoreCreateCountingStatic(
UBaseType_t uxMaxCount,
UBaseType_t uxInitialCount,
StaticSemaphore_t *pxSemaphoreBuffer
);
BaseType_t xSemaphoreTake(SemaphoreHandle_t xSemaphore, TickType_t xTicksToWait);
BaseType_t xSemaphoreGive(SemaphoreHandle_t xSemaphore);
BaseType_t xQueueSend(
QueueHandle_t xQueue, const void * pvItemToQueue, TickType_t xTicksToWait
);
BaseType_t xQueueReceive(QueueHandle_t xQueue, void *pvBuffer, TickType_t xTicksToWait);

View File

@@ -31,7 +31,7 @@ C_SOURCES += Src/flipper_hal.c
C_SOURCES += Src/lo_os.c
C_SOURCES += Src/lo_hal.c
C_DEFS += -DFURI_DEBUG
# C_DEFS += -DFURI_DEBUG
# Core
@@ -63,6 +63,11 @@ C_SOURCES += ../applications/examples/uart_write.c
C_DEFS += -DEXAMPLE_UART_WRITE
endif
ifeq ($(EXAMPLE_IPC), 1)
C_SOURCES += ../applications/examples/ipc.c
C_DEFS += -DEXAMPLE_IPC
endif
# User application
# Add C_SOURCES +=, C_DEFS += or CPP_SOURCES += here
@@ -139,19 +144,24 @@ rust_lib:
$(RUST_LIB_CMD)
example_blink:
rm -f $(BUILD_DIR)/app.o
EXAMPLE_BLINK=1 make
rm $(BUILD_DIR)/app.o
$(BUILD_DIR)/$(TARGET)
example_uart_write:
rm -f $(BUILD_DIR)/app.o
EXAMPLE_UART_WRITE=1 make
rm $(BUILD_DIR)/app.o
$(BUILD_DIR)/$(TARGET)
example_ipc:
rm -f $(BUILD_DIR)/app.o
EXAMPLE_IPC=1 make
$(BUILD_DIR)/$(TARGET)
test:
rm -f $(BUILD_DIR)/app.o
TEST=1 make
rm $(BUILD_DIR)/app.o
$(BUILD_DIR)/$(TARGET)
.PHONY: all rust_lib example_blink example_uart_write test

View File

@@ -4,6 +4,9 @@
#include <pthread.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
void osDelay(uint32_t ms) {
// printf("[DELAY] %d ms\n", ms);
@@ -82,4 +85,79 @@ bool task_equal(TaskHandle_t a, TaskHandle_t b) {
SemaphoreHandle_t xSemaphoreCreateMutexStatic(StaticSemaphore_t* pxMutexBuffer) {
// TODO add posix mutex init
return NULL;
}
BaseType_t xQueueSend(
QueueHandle_t xQueue, const void * pvItemToQueue, TickType_t xTicksToWait
) {
// TODO: add implementation
return pdTRUE;
}
BaseType_t xQueueReceive(
QueueHandle_t xQueue, void *pvBuffer, TickType_t xTicksToWait
) {
// TODO: add implementation
osDelay(100);
return pdFALSE;
}
static uint32_t queue_global_id = 0;
QueueHandle_t xQueueCreateStatic(
UBaseType_t uxQueueLength,
UBaseType_t uxItemSize,
uint8_t* pucQueueStorageBuffer,
StaticQueue_t *pxQueueBuffer
) {
// TODO: check this implementation
int* msgid = malloc(sizeof(int));
key_t key = queue_global_id;
queue_global_id++;
*msgid = msgget(key, IPC_CREAT);
return (QueueHandle_t)msgid;
}
SemaphoreHandle_t xSemaphoreCreateCountingStatic(
UBaseType_t uxMaxCount,
UBaseType_t uxInitialCount,
StaticSemaphore_t* pxSemaphoreBuffer
) {
pxSemaphoreBuffer->take_counter = 0;
pxSemaphoreBuffer->give_counter = 0;
return pxSemaphoreBuffer;
}
BaseType_t xSemaphoreTake(SemaphoreHandle_t xSemaphore, TickType_t xTicksToWait) {
if(xSemaphore == NULL) return false;
// TODO: need to add inter-process sync or use POSIX primitives
xSemaphore->take_counter++;
TickType_t ticks = xTicksToWait;
while(
xSemaphore->take_counter != xSemaphore->give_counter
&& (ticks > 0 || xTicksToWait == portMAX_DELAY)
) {
osDelay(1);
ticks--;
}
if(xTicksToWait != 0 && ticks == 0) return pdFALSE;
return pdTRUE;
}
BaseType_t xSemaphoreGive(SemaphoreHandle_t xSemaphore) {
if(xSemaphore == NULL) return false;
// TODO: need to add inter-process sync or use POSIX primitives
xSemaphore->give_counter++;
return pdTRUE;
}