From 5d7a977d3342b0b8496c9b87ce6016c7c28aaa4e Mon Sep 17 00:00:00 2001 From: cfif Date: Mon, 26 May 2025 14:41:45 +0300 Subject: [PATCH] Init --- CmsisRtosThreadUtils.c | 74 +++++++++++++++++++++++++++++++++++++++++ CmsisRtosThreadUtils.h | 75 ++++++++++++++++++++++++++++++++++++++++++ modular.json | 17 ++++++++++ 3 files changed, 166 insertions(+) create mode 100644 CmsisRtosThreadUtils.c create mode 100644 CmsisRtosThreadUtils.h create mode 100644 modular.json diff --git a/CmsisRtosThreadUtils.c b/CmsisRtosThreadUtils.c new file mode 100644 index 0000000..d91dc7a --- /dev/null +++ b/CmsisRtosThreadUtils.c @@ -0,0 +1,74 @@ +// +// Created by xemon on 23.11.22. +// + +#include "CmsisRtosThreadUtils.h" + +void InitThreadAtr( + osThreadAttr_t *attr, + const char *name, ///< name of the thread + + void *cb_mem, ///< memory for control block + uint32_t cb_size, ///< size of provided memory for control block + + void *stack_mem, ///< memory for stack + uint32_t stack_size, ///< size of stack + + osPriority_t priority ///< initial thread priority (default: osPriorityNormal) +) { + + attr->name = name; + + attr->cb_mem = cb_mem; + attr->cb_size = cb_size; + + attr->stack_mem = stack_mem; + attr->stack_size = stack_size; + + attr->priority = priority; + + attr->tz_module = 0; + attr->reserved = 0; +} + +void InitQueueAtr( + osMessageQueueAttr_t *attr, + const char *name, ///< name of the thread + + void *cb_mem, ///< memory for control block + uint32_t cb_size, ///< size of provided memory for control block + + void *mem, ///< memory for data + uint32_t mem_size ///< size of mem +) { + + attr->name = name; + + attr->cb_mem = cb_mem; + attr->cb_size = cb_size; + + attr->mq_mem = mem; + attr->mq_size = mem_size; +} + +void utils_thread_block_start(void *env, osThreadId_t *id, osThreadAttr_t *attr, osThreadFunc_t func) { + if (!*id) { + *id = osThreadNew((osThreadFunc_t) func, (void *) (env), attr); + } else { + osThreadResume(*id); + } +} + +void utils_thread_block_pause(osThreadId_t id) { + if (id) { + osThreadSuspend(id); + } +} + +void utils_thread_block_stop(osThreadId_t *id) { + if (*id) { + if (osThreadTerminate(*id) == osOK) { + *id = NULL; + } + } +} \ No newline at end of file diff --git a/CmsisRtosThreadUtils.h b/CmsisRtosThreadUtils.h new file mode 100644 index 0000000..254d266 --- /dev/null +++ b/CmsisRtosThreadUtils.h @@ -0,0 +1,75 @@ +// +// Created by xemon on 08.09.22. +// + +#ifndef CMSISRTOSTHREADSUTILS_CMSISRTOSTHREADSUTILS_H +#define CMSISRTOSTHREADSUTILS_CMSISRTOSTHREADSUTILS_H + +#include "cmsis_os.h" + +typedef struct { + osThreadId_t id; + StaticTask_t controlBlock; +} tThreadData; + +#define tStaticThreadBlock(WORDS_LEN) struct { osThreadId_t id; osThreadAttr_t attr; StaticTask_t controlBlock; uint32_t stack[WORDS_LEN]; } +#define tStaticThreadDynBlock struct { osThreadId_t id; osThreadAttr_t attr; } + +#define THREAD_ATTR_CREATE(PRIORITY, TASK_NAME) osThreadAttr_t TASK_NAME##TaskAttr ={ \ + .name = #TASK_NAME, \ + .cb_mem = &threads.TASK_NAME.controlBlock, \ + .cb_size = sizeof(threads.TASK_NAME.controlBlock), \ + \ + .stack_mem = &threadsBuffers.TASK_NAME[0], \ + .stack_size = sizeof(threadsBuffers.TASK_NAME), \ + \ + .priority = (osPriority_t) (PRIORITY) \ +} + +#define THREAD_ADD(THREAD_NAME, ENV, FUNC) threads.THREAD_NAME.id = osThreadNew((osThreadFunc_t)(FUNC), (void *) (ENV), &THREAD_NAME##TaskAttr) + + +void InitThreadAtr( + osThreadAttr_t *attr, + const char *name, ///< name of the thread + + void *cb_mem, ///< memory for control block + uint32_t cb_size, ///< size of provided memory for control block + + void *stack_mem, ///< memory for stack + uint32_t stack_size, ///< size of stack + + osPriority_t priority ///< initial thread priority (default: osPriorityNormal) +); + +void InitQueueAtr( + osMessageQueueAttr_t *attr, + const char *name, ///< name of the thread + + void *cb_mem, ///< memory for control block + uint32_t cb_size, ///< size of provided memory for control block + + void *mem, ///< memory for data + uint32_t mem_size ///< size of mem +); + +#define InitThreadAtrStatic(ATTR, NAME, CB, STACK, PRIOR) InitThreadAtr(ATTR,NAME,&CB,sizeof(CB),STACK,sizeof(STACK),PRIOR) +#define InitQueueAtrStatic(ATTR, NAME, CB, MEM) InitQueueAtr(ATTR,NAME,&CB,sizeof(CB),MEM,sizeof(MEM)) + +#define InitThreadBlock(BLOCK, NAME, PRIOR) (BLOCK).id = 0; InitThreadAtr(&(BLOCK).attr,NAME,&(BLOCK).controlBlock,sizeof((BLOCK).controlBlock),(BLOCK).stack,sizeof((BLOCK).stack),PRIOR) +#define InitThreadDynBlock(BLOCK, NAME, STACK, PRIOR) (BLOCK).id = 0; InitThreadAtr(&(BLOCK).attr,NAME,NULL,0,NULL,STACK,PRIOR) + + +void utils_thread_block_start(void *env, osThreadId_t *id, osThreadAttr_t *attr, osThreadFunc_t func); + +void utils_thread_block_pause(osThreadId_t id); + +void utils_thread_block_stop(osThreadId_t *id); + +#define ThreadBlock_Start(THREAD, ENV, FUNC) utils_thread_block_start(ENV,&(THREAD).id,&(THREAD).attr, (osThreadFunc_t)(FUNC)) +#define ThreadBlock_Pause(THREAD) utils_thread_block_pause((THREAD).id) +#define ThreadBlock_Stop(THREAD) utils_thread_block_stop(&(THREAD).id) + + +#define PinThreadToCore(BLOCK, CORE) (BLOCK).attr.pinToCore = 1; (BLOCK).attr.core = CORE +#endif //CMSISRTOSTHREADSUTILS_CMSISRTOSTHREADSUTILS_H diff --git a/modular.json b/modular.json new file mode 100644 index 0000000..35c7fce --- /dev/null +++ b/modular.json @@ -0,0 +1,17 @@ +{ + "dep": [ + { + "type": "git", + "provider": "GONEC_NEW", + "repo": "CmsisRtosInterface" + } + ], + "cmake": { + "inc_dirs": [ + "./" + ], + "srcs": [ + "./*.c" + ] + } +} \ No newline at end of file