2020-08-17 09:45:53 +00:00
|
|
|
|
#include "cmsis_os.h"
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <stdio.h>
|
2020-08-24 15:31:22 +00:00
|
|
|
|
#include <pthread.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <signal.h>
|
2020-09-01 10:34:23 +00:00
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/ipc.h>
|
|
|
|
|
#include <sys/msg.h>
|
2020-08-17 09:45:53 +00:00
|
|
|
|
|
|
|
|
|
void osDelay(uint32_t ms) {
|
2020-08-26 01:56:33 +00:00
|
|
|
|
// printf("[DELAY] %d ms\n", ms);
|
2020-08-24 15:31:22 +00:00
|
|
|
|
usleep(ms * 1000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// temporary struct to pass function ptr and param to wrapper
|
|
|
|
|
typedef struct {
|
|
|
|
|
TaskFunction_t func;
|
2020-09-29 23:18:30 +00:00
|
|
|
|
void* param;
|
2020-08-24 15:31:22 +00:00
|
|
|
|
} PthreadTask;
|
|
|
|
|
|
|
|
|
|
void* pthread_wrapper(void* p) {
|
|
|
|
|
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0x00);
|
|
|
|
|
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0x00);
|
|
|
|
|
|
|
|
|
|
PthreadTask* task = (PthreadTask*)p;
|
2020-09-29 23:18:30 +00:00
|
|
|
|
|
2020-08-24 15:31:22 +00:00
|
|
|
|
task->func(task->param);
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-02 06:32:11 +00:00
|
|
|
|
TaskHandle_t xTaskCreateStatic(
|
|
|
|
|
TaskFunction_t pxTaskCode,
|
|
|
|
|
const char* const pcName,
|
|
|
|
|
const uint32_t ulStackDepth,
|
|
|
|
|
void* const pvParameters,
|
|
|
|
|
UBaseType_t uxPriority,
|
|
|
|
|
StackType_t* const puxStackBuffer,
|
|
|
|
|
StaticTask_t* const pxTaskBuffer) {
|
2020-08-24 15:31:22 +00:00
|
|
|
|
TaskHandle_t thread = malloc(sizeof(TaskHandle_t));
|
|
|
|
|
PthreadTask* task = malloc(sizeof(PthreadTask));
|
|
|
|
|
|
|
|
|
|
task->func = pxTaskCode;
|
|
|
|
|
task->param = pvParameters;
|
|
|
|
|
|
|
|
|
|
pthread_create(thread, NULL, pthread_wrapper, (void*)task);
|
|
|
|
|
|
|
|
|
|
return thread;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void vTaskDelete(TaskHandle_t xTask) {
|
|
|
|
|
if(xTask == NULL) {
|
|
|
|
|
// kill itself
|
|
|
|
|
pthread_exit(NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// maybe thread already join
|
2020-09-29 23:18:30 +00:00
|
|
|
|
if(pthread_kill(*xTask, 0) == ESRCH) return;
|
2020-08-24 15:31:22 +00:00
|
|
|
|
|
|
|
|
|
// send thread_child signal to stop it сигнал, который ее завершает
|
|
|
|
|
pthread_cancel(*xTask);
|
|
|
|
|
|
|
|
|
|
// wait for join and close descriptor
|
|
|
|
|
pthread_join(*xTask, 0x00);
|
|
|
|
|
|
|
|
|
|
// cleanup thread handler
|
|
|
|
|
*xTask = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TaskHandle_t xTaskGetCurrentTaskHandle(void) {
|
|
|
|
|
TaskHandle_t thread = malloc(sizeof(TaskHandle_t));
|
|
|
|
|
*thread = pthread_self();
|
|
|
|
|
return thread;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-29 23:18:30 +00:00
|
|
|
|
BaseType_t xQueueSend(QueueHandle_t xQueue, const void* pvItemToQueue, TickType_t xTicksToWait) {
|
2020-09-01 10:34:23 +00:00
|
|
|
|
// TODO: add implementation
|
|
|
|
|
return pdTRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-29 23:18:30 +00:00
|
|
|
|
BaseType_t xQueueReceive(QueueHandle_t xQueue, void* pvBuffer, TickType_t xTicksToWait) {
|
2020-09-01 10:34:23 +00:00
|
|
|
|
// TODO: add implementation
|
|
|
|
|
osDelay(100);
|
|
|
|
|
|
|
|
|
|
return pdFALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static uint32_t queue_global_id = 0;
|
|
|
|
|
|
2020-10-02 06:32:11 +00:00
|
|
|
|
QueueHandle_t xQueueCreateStatic(
|
|
|
|
|
UBaseType_t uxQueueLength,
|
|
|
|
|
UBaseType_t uxItemSize,
|
|
|
|
|
uint8_t* pucQueueStorageBuffer,
|
|
|
|
|
StaticQueue_t* pxQueueBuffer) {
|
2020-09-01 10:34:23 +00:00
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-08 14:37:19 +00:00
|
|
|
|
QueueHandle_t xQueueCreate(UBaseType_t uxQueueLength, UBaseType_t uxItemSize) {
|
|
|
|
|
return xQueueCreateStatic(uxQueueLength, uxItemSize, 0, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-02 06:32:11 +00:00
|
|
|
|
SemaphoreHandle_t xSemaphoreCreateCountingStatic(
|
|
|
|
|
UBaseType_t uxMaxCount,
|
|
|
|
|
UBaseType_t uxInitialCount,
|
|
|
|
|
StaticSemaphore_t* pxSemaphoreBuffer) {
|
2020-09-07 14:35:18 +00:00
|
|
|
|
pxSemaphoreBuffer->type = SemaphoreTypeCounting;
|
2020-09-01 10:34:23 +00:00
|
|
|
|
pxSemaphoreBuffer->take_counter = 0;
|
|
|
|
|
pxSemaphoreBuffer->give_counter = 0;
|
|
|
|
|
return pxSemaphoreBuffer;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 14:35:18 +00:00
|
|
|
|
SemaphoreHandle_t xSemaphoreCreateMutexStatic(StaticSemaphore_t* pxMutexBuffer) {
|
|
|
|
|
pxMutexBuffer->type = SemaphoreTypeMutex;
|
|
|
|
|
pthread_mutex_init(&pxMutexBuffer->mutex, NULL);
|
|
|
|
|
pxMutexBuffer->take_counter = 0;
|
|
|
|
|
pxMutexBuffer->give_counter = 0;
|
|
|
|
|
return pxMutexBuffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BaseType_t xSemaphoreTake(volatile SemaphoreHandle_t xSemaphore, TickType_t xTicksToWait) {
|
|
|
|
|
if(xSemaphore == NULL) return pdFALSE;
|
|
|
|
|
|
2020-09-29 23:18:30 +00:00
|
|
|
|
if(xSemaphore->type == SemaphoreTypeMutex) {
|
|
|
|
|
if(xTicksToWait == portMAX_DELAY) {
|
|
|
|
|
if(pthread_mutex_lock(&xSemaphore->mutex) == 0) {
|
2020-09-07 14:35:18 +00:00
|
|
|
|
return pdTRUE;
|
|
|
|
|
} else {
|
|
|
|
|
return pdFALSE;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
TickType_t ticks = xTicksToWait;
|
2020-09-29 23:18:30 +00:00
|
|
|
|
while(ticks >= 0) {
|
|
|
|
|
if(pthread_mutex_trylock(&xSemaphore->mutex) == 0) {
|
2020-09-07 14:35:18 +00:00
|
|
|
|
return pdTRUE;
|
|
|
|
|
}
|
2020-09-29 23:18:30 +00:00
|
|
|
|
if(ticks > 0) {
|
2020-09-07 14:35:18 +00:00
|
|
|
|
osDelay(1);
|
|
|
|
|
}
|
|
|
|
|
ticks--;
|
|
|
|
|
}
|
|
|
|
|
return pdFALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-01 10:34:23 +00:00
|
|
|
|
// TODO: need to add inter-process sync or use POSIX primitives
|
|
|
|
|
xSemaphore->take_counter++;
|
|
|
|
|
|
|
|
|
|
TickType_t ticks = xTicksToWait;
|
|
|
|
|
|
2020-09-29 23:18:30 +00:00
|
|
|
|
while(xSemaphore->take_counter != xSemaphore->give_counter &&
|
|
|
|
|
(ticks > 0 || xTicksToWait == portMAX_DELAY)) {
|
2020-09-01 10:34:23 +00:00
|
|
|
|
osDelay(1);
|
|
|
|
|
ticks--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(xTicksToWait != 0 && ticks == 0) return pdFALSE;
|
|
|
|
|
|
|
|
|
|
return pdTRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BaseType_t xSemaphoreGive(SemaphoreHandle_t xSemaphore) {
|
2020-09-07 14:35:18 +00:00
|
|
|
|
if(xSemaphore == NULL) return pdFALSE;
|
|
|
|
|
|
2020-09-29 23:18:30 +00:00
|
|
|
|
if(xSemaphore->type == SemaphoreTypeMutex) {
|
|
|
|
|
if(pthread_mutex_unlock(&xSemaphore->mutex) == 0) {
|
2020-09-07 14:35:18 +00:00
|
|
|
|
return pdTRUE;
|
|
|
|
|
} else {
|
|
|
|
|
return pdFALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-01 10:34:23 +00:00
|
|
|
|
|
|
|
|
|
// TODO: need to add inter-process sync or use POSIX primitives
|
|
|
|
|
xSemaphore->give_counter++;
|
|
|
|
|
|
|
|
|
|
return pdTRUE;
|
2020-09-10 19:52:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define TLS_ITEM_COUNT 1
|
|
|
|
|
static pthread_key_t tls_keys[TLS_ITEM_COUNT];
|
|
|
|
|
static pthread_once_t tls_keys_once = PTHREAD_ONCE_INIT;
|
|
|
|
|
|
|
|
|
|
static void create_tls_keys() {
|
2020-09-29 23:18:30 +00:00
|
|
|
|
for(size_t i = 0; i < TLS_ITEM_COUNT; i++) {
|
2020-09-10 19:52:58 +00:00
|
|
|
|
pthread_key_create(&tls_keys[i], NULL);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-29 23:18:30 +00:00
|
|
|
|
void* pvTaskGetThreadLocalStoragePointer(TaskHandle_t xTaskToQuery, BaseType_t xIndex) {
|
2020-09-10 19:52:58 +00:00
|
|
|
|
// Non-current task TLS access is not allowed
|
2020-09-29 23:18:30 +00:00
|
|
|
|
if(xTaskToQuery != NULL) {
|
2020-09-10 19:52:58 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-29 23:18:30 +00:00
|
|
|
|
if(xIndex >= TLS_ITEM_COUNT) {
|
2020-09-10 19:52:58 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pthread_once(&tls_keys_once, create_tls_keys);
|
|
|
|
|
|
|
|
|
|
return pthread_getspecific(tls_keys[xIndex]);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-29 23:18:30 +00:00
|
|
|
|
void vTaskSetThreadLocalStoragePointer(TaskHandle_t xTaskToSet, BaseType_t xIndex, void* pvValue) {
|
2020-09-10 19:52:58 +00:00
|
|
|
|
// Non-current task TLS access is not allowed
|
2020-09-29 23:18:30 +00:00
|
|
|
|
if(xTaskToSet != NULL) {
|
2020-09-10 19:52:58 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-29 23:18:30 +00:00
|
|
|
|
if(xIndex >= TLS_ITEM_COUNT) {
|
2020-09-10 19:52:58 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pthread_once(&tls_keys_once, create_tls_keys);
|
|
|
|
|
|
|
|
|
|
pthread_setspecific(tls_keys[xIndex], pvValue);
|
|
|
|
|
}
|
2020-10-08 14:37:19 +00:00
|
|
|
|
|
2020-10-13 08:22:43 +00:00
|
|
|
|
|
|
|
|
|
osMutexId_t osMutexNew(const osMutexAttr_t *attr) {
|
|
|
|
|
StaticSemaphore_t* pxMutexBuffer = malloc(sizeof(StaticSemaphore_t));
|
|
|
|
|
xSemaphoreCreateMutexStatic(pxMutexBuffer);
|
|
|
|
|
|
|
|
|
|
return (osMutexId_t)pxMutexBuffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
osStatus_t osMutexAcquire(osMutexId_t mutex_id, uint32_t timeout) {
|
|
|
|
|
if(xSemaphoreTake((SemaphoreHandle_t)mutex_id, (TickType_t)timeout) == pdTRUE) {
|
|
|
|
|
return osOK;
|
|
|
|
|
} else {
|
|
|
|
|
return osErrorTimeout;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
osStatus_t osMutexRelease (osMutexId_t mutex_id) {
|
|
|
|
|
if(xSemaphoreGive((SemaphoreHandle_t)mutex_id) == pdTRUE) {
|
|
|
|
|
return osOK;
|
|
|
|
|
} else {
|
|
|
|
|
return osError;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-15 18:23:18 +00:00
|
|
|
|
|
|
|
|
|
osStatus_t osMutexDelete (osMutexId_t mutex_id) {
|
|
|
|
|
osMutexRelease(mutex_id);
|
|
|
|
|
|
|
|
|
|
int res = 0;
|
|
|
|
|
if((res = pthread_mutex_destroy(&mutex_id->mutex)) == 0) {
|
|
|
|
|
return osOK;
|
|
|
|
|
} else {
|
|
|
|
|
printf("res = %d\n", res);
|
|
|
|
|
return osError;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-26 09:26:15 +00:00
|
|
|
|
|
|
|
|
|
osSemaphoreId_t osSemaphoreNew(uint32_t max_count, uint32_t initial_count, const osSemaphoreAttr_t *attr) {
|
|
|
|
|
if(max_count != 1) {
|
|
|
|
|
// Non-binary semaphors are not supported at the moment
|
2020-10-29 07:58:19 +00:00
|
|
|
|
return NULL;
|
2020-10-26 09:26:15 +00:00
|
|
|
|
}
|
|
|
|
|
if(attr != NULL) {
|
|
|
|
|
// Attributes are not supported at the moment
|
2020-10-29 07:58:19 +00:00
|
|
|
|
return NULL;
|
2020-10-26 09:26:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SemaphoreHandle_t handle = osMutexNew(NULL);
|
|
|
|
|
if(handle == NULL) return NULL;
|
|
|
|
|
|
|
|
|
|
if(initial_count == 0) {
|
|
|
|
|
xSemaphoreTake(handle, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
osStatus_t osSemaphoreAcquire(osSemaphoreId_t semaphore_id, uint32_t timeout) {
|
|
|
|
|
if(semaphore_id == NULL) {
|
|
|
|
|
return osErrorParameter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(xSemaphoreTake(semaphore_id, timeout) == pdTRUE) {
|
|
|
|
|
return osOK;
|
|
|
|
|
} else {
|
|
|
|
|
return osErrorTimeout;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
osStatus_t osSemaphoreRelease(osSemaphoreId_t semaphore_id) {
|
|
|
|
|
if(semaphore_id == NULL) {
|
|
|
|
|
return osErrorParameter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(xSemaphoreGive(semaphore_id) == pdTRUE) {
|
|
|
|
|
return osOK;
|
|
|
|
|
} else {
|
|
|
|
|
return osErrorTimeout;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
osStatus_t osSemaphoreDelete(osSemaphoreId_t semaphore_id) {
|
|
|
|
|
if(semaphore_id == NULL) {
|
|
|
|
|
return osErrorParameter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return osMutexDelete(semaphore_id);
|
|
|
|
|
}
|