From f12994ec9fc890947fb5f49d2450be4d8ae1aac4 Mon Sep 17 00:00:00 2001 From: Matheus Catarino Date: Tue, 13 Aug 2024 16:35:56 -0300 Subject: [PATCH] TaskCreate added --- main/app.c3 | 20 +++++++++++-- main/wrappers/idf.c3 | 70 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 86 insertions(+), 4 deletions(-) diff --git a/main/app.c3 b/main/app.c3 index 58a83c3..be274ff 100644 --- a/main/app.c3 +++ b/main/app.c3 @@ -8,10 +8,26 @@ fn void main() @export("app_main") defer idf::heap_caps_dump_all(); newlib::printf("Malloc test:\n"); - void* ptr = idf::heap_caps_malloc(100, idf::MALLOC_CAP_DMA); + void* ptr = idf::heap_caps_malloc(100, idf::MALLOC_CAP_DEFAULT); newlib::printf("Allocated 100 bytes at %p\n", ptr); idf::heap_caps_free(ptr); newlib::printf("Freeing memory...\n"); - + newlib::printf("Malloc dump all\n"); + + if(idf::xTaskCreate(&taskSample, "taskSample", 10000, null, 1, null) != idf::ESP_OK){ + // TODO: Handle error + newlib::printf("TaskSample cannot be run!!\n"); + } } + +fn void taskSample(void* arg) @export("taskSample") { + newlib::printf("TaskSample started\n"); + while(true){ + newlib::printf("TaskSample running, %d\n", counter); + idf::vTaskDelay(1000 / idf::TICK_PERIOD_MS); + counter++; + } +} + +int counter = 0; \ No newline at end of file diff --git a/main/wrappers/idf.c3 b/main/wrappers/idf.c3 index f0acf19..d42c4d1 100644 --- a/main/wrappers/idf.c3 +++ b/main/wrappers/idf.c3 @@ -1,5 +1,6 @@ module idf; +// typedefs def Esp_err_t = int; def Caps = isz; @@ -25,6 +26,7 @@ const Esp_err_t ESP_ERR_FLASH_BASE = 0x6000; const Esp_err_t ESP_ERR_HW_CRYPTO_BASE = 0xc000; const Esp_err_t ESP_ERR_MEMPROT_BASE = 0xd000; +// Memory capabilities const Caps MALLOC_CAP_EXEC = (1 << 0); //< Memory must be able to run executable code const Caps MALLOC_CAP_32BIT = (1 << 1); //< Memory must allow for aligned 32-bit data accesses const Caps MALLOC_CAP_8BIT = (1 << 2); //< Memory must allow for 8/16/...-bit data accesses @@ -44,13 +46,18 @@ const Caps MALLOC_CAP_RTCRAM = (1 << 15); //< Memory must be in RTC fast m const Caps MALLOC_CAP_TCM = (1 << 16); //< Memory must be in TCM memory const Caps MALLOC_CAP_INVALID = (1 << 31); //< Memory can't be used / list end marker -// Functions +// ========================Functions==================================== + +/// Get error extern fn char* esp_err_to_name(Esp_err_t err_code); extern fn char* esp_err_to_name_r(Esp_err_t err_code, char* buf, usz buflen); extern fn void _esp_error_check_failed(Esp_err_t err_code, char* file, int line, char* function, char* expression); extern fn void _esp_error_check_failed_without_abort(Esp_err_t err_code, char* file, int line, char* function, char* expression); + +/// Get IDF version string extern fn char* esp_get_idf_version(); +/// Memory allocation extern fn void* heap_caps_malloc(usz size, uint caps); extern fn void heap_caps_free(void* ptr); extern fn void* heap_caps_realloc(void* ptr, usz size, uint caps); @@ -66,4 +73,63 @@ extern fn Esp_err_t heap_caps_monitor_local_minimum_free_size_start(); extern fn Esp_err_t heap_caps_monitor_local_minimum_free_size_stop(); extern fn void heap_caps_dump(Caps caps); extern fn void heap_caps_dump_all(); -extern fn usz heap_caps_get_allocated_size(void* ptr); \ No newline at end of file +extern fn usz heap_caps_get_allocated_size(void* ptr); + +/// FreeRTOS tasks +def TaskHandle_t = void*; +def TaskFunction_t = fn void(void*); +def UBaseType_t = uint; +def TickType_t = uint; +def BaseType_t = int; +def StackType_t = char; + +struct StaticListItem_t +{ + uint xDummy2; + void*[4] pvDummy3; +} +struct StaticTask_t +{ + void* pxDummy1; + StaticListItem_t[2] xDummy3; + uint uxDummy5; + void* pxDummy6; + int[2] xDummy23; + char[16] ucDummy7; + void* pxDummy8; + uint[2] uxDummy12; + void*[2] pvDummy15; + void* xDummy17; + int[1] ulDummy18; + char[1] ucDummy19; + char uxDummy20; + char ucDummy21; +} + +const TickType_t TICK_RATE_HZ = 100; +const TickType_t TICK_PERIOD_MS = 1000 / TICK_RATE_HZ; +extern fn void vTaskDelay(TickType_t ticks); +extern fn void vTaskPrioritySet(TaskHandle_t xTask, UBaseType_t uxNewPriority); +extern fn void vTaskSuspend(TaskHandle_t xTaskToSuspend); +extern fn void vTaskResume(TaskHandle_t xTaskToResume); +extern fn BaseType_t xTaskResumeFromISR(TaskHandle_t xTaskToResume); +extern fn void vTaskPreemptionDisable(TaskHandle_t xTask); +extern fn void vTaskPreemptionEnable(TaskHandle_t xTask); +extern fn void vTaskStartScheduler(); +extern fn void vTaskEndScheduler(); +extern fn void vTaskSuspendAll(); +extern fn BaseType_t xTaskResumeAll(); +extern fn UBaseType_t xTaskGetTickCount(); +extern fn UBaseType_t xTaskGetTickCountFromISR(); +extern fn UBaseType_t uxTaskGetNumberOfTasks(); + +extern fn BaseType_t xTaskCreatePinnedToCore(TaskFunction_t pvTaskCode, char* pcName, uint usStackDepth, void* pvParameters, uint uxPriority, TaskHandle_t* pvCreatedTask, uint uxCoreID); +fn BaseType_t xTaskCreate(TaskFunction_t pvTaskCode, char* pcName, uint usStackDepth, void* pvParameters, uint uxPriority, TaskHandle_t* pvCreatedTask) +{ + return xTaskCreatePinnedToCore(pvTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pvCreatedTask, 2147483647); +} +extern fn BaseType_t xTaskCreateStatiicPinnedToCore(TaskFunction_t pvTaskCode, char* pcName, uint usStackDepth, void* pvParameters, uint uxPriority, TaskHandle_t* pvCreatedTask, StaticTask_t* pxTaskBuffer, StackType_t* pxStackBuffer, uint uxCoreID); +fn BaseType_t xTaskCreateStatic(TaskFunction_t pvTaskCode, char* pcName, uint usStackDepth, void* pvParameters, uint uxPriority, TaskHandle_t* pvCreatedTask, StaticTask_t* pxTaskBuffer, StackType_t* pxStackBuffer) +{ + return xTaskCreateStatiicPinnedToCore(pvTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pvCreatedTask, pxTaskBuffer, pxStackBuffer, 2147483647); +} \ No newline at end of file