From 10d5720fad6ad95c5f7c857510a82e831ba5012a Mon Sep 17 00:00:00 2001 From: Dario Gogliandolo Date: Mon, 17 Jun 2019 11:43:53 +0200 Subject: [PATCH 1/5] Added possibility to run Arduino loop() as dedicated task So loop() can contain delay and blocking instructions. --- src/FreeRTOSConfig.h | 2 +- src/tasks.c | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/FreeRTOSConfig.h b/src/FreeRTOSConfig.h index fff58b1..527560f 100644 --- a/src/FreeRTOSConfig.h +++ b/src/FreeRTOSConfig.h @@ -85,7 +85,7 @@ #define configUSE_PREEMPTION 1 -#define configUSE_IDLE_HOOK 1 +#define configUSE_IDLE_HOOK 0 // 0 = loop() is a dedicated task | 1 = loop() is the FreeRTOS idle hook function #define configUSE_TICK_HOOK 0 #define configCPU_CLOCK_HZ ( ( unsigned long ) F_CPU ) #define configTICK_RATE_HZ ( ( TickType_t ) 1000 ) diff --git a/src/tasks.c b/src/tasks.c index 9e816ce..866be21 100644 --- a/src/tasks.c +++ b/src/tasks.c @@ -1540,6 +1540,17 @@ StackType_t *pxTopOfStack; #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */ /*-----------------------------------------------------------*/ +// this is referring to the loop function of your arduino project +extern void loop(void); + +void loopTask(void *pvParameters) +{ + while(1) + { + loop(); + } +} + void vTaskStartScheduler( void ) { BaseType_t xReturn; @@ -1547,6 +1558,9 @@ BaseType_t xReturn; /* Add the idle task at the lowest priority. */ #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) { + #if ( configUSE_IDLE_HOOK == 0) + xTaskCreate(loopTask, "loop", 512, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), NULL); + #endif /* Create the idle task, storing its handle in xIdleTaskHandle so it can be returned by the xTaskGetIdleTaskHandle() function. */ xReturn = xTaskCreate( prvIdleTask, "IDLE", tskIDLE_STACK_SIZE, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), &xIdleTaskHandle ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */ From 1fab6546e9af5d45fc65e87bb95e691b4c632ee9 Mon Sep 17 00:00:00 2001 From: Dario Gogliandolo Date: Mon, 8 Jul 2019 15:26:41 +0200 Subject: [PATCH 2/5] resized stack of loop() task --- src/tasks.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tasks.c b/src/tasks.c index 866be21..0d3b880 100644 --- a/src/tasks.c +++ b/src/tasks.c @@ -1559,7 +1559,7 @@ BaseType_t xReturn; #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) { #if ( configUSE_IDLE_HOOK == 0) - xTaskCreate(loopTask, "loop", 512, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), NULL); + xTaskCreate(loopTask, "loop", 256, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), NULL); #endif /* Create the idle task, storing its handle in xIdleTaskHandle so it can be returned by the xTaskGetIdleTaskHandle() function. */ From bb096a64dece93cd1ff9f1e95c4f3e3f23db43b2 Mon Sep 17 00:00:00 2001 From: Dario Gogliandolo Date: Mon, 15 Jul 2019 13:08:37 +0200 Subject: [PATCH 3/5] loop() as dedicated task with user defined stack size and priority --- src/FreeRTOSConfig.h | 3 ++- src/idle_hook.c | 21 ++++++++++++++++++++- src/tasks.c | 14 -------------- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/FreeRTOSConfig.h b/src/FreeRTOSConfig.h index 527560f..d38efea 100644 --- a/src/FreeRTOSConfig.h +++ b/src/FreeRTOSConfig.h @@ -85,7 +85,7 @@ #define configUSE_PREEMPTION 1 -#define configUSE_IDLE_HOOK 0 // 0 = loop() is a dedicated task | 1 = loop() is the FreeRTOS idle hook function +#define configUSE_IDLE_HOOK 1 // 0 = loop() is a dedicated task | 1 = loop() is the FreeRTOS idle hook function TODO #define configUSE_TICK_HOOK 0 #define configCPU_CLOCK_HZ ( ( unsigned long ) F_CPU ) #define configTICK_RATE_HZ ( ( TickType_t ) 1000 ) @@ -149,6 +149,7 @@ FreeRTOS/Source/tasks.c for limitations. */ header file. */ extern void rtosFatalError(void); +extern void runLoopAsTask(uint16_t stack, uint16_t priority); #define configASSERT( x ) \ if( ( x ) == 0 ) { taskDISABLE_INTERRUPTS(); rtosFatalError(); } diff --git a/src/idle_hook.c b/src/idle_hook.c index 1d1c725..b4a1b7e 100644 --- a/src/idle_hook.c +++ b/src/idle_hook.c @@ -2,11 +2,30 @@ #include #include +uint8_t loopAsTask = 0; +TaskHandle_t loopHandle; // this is referring to the loop function of your arduino project extern void loop(void); void __attribute__((weak)) vApplicationIdleHook( void ) { - loop(); //will use your projects loop function as the rtos idle loop + if (!loopAsTask) + { + loop(); //will use your projects loop function as the rtos idle loop + } +} + +void loopTask(void *pvParameters) +{ + while(1) + { + loop(); + } +} + +void runLoopAsTask(uint16_t stack, uint16_t priority) +{ + loopAsTask = 1; + xTaskCreate(loopTask, "loop", stack, ( void * ) NULL, ( (UBaseType_t)priority | portPRIVILEGE_BIT ) , &loopHandle); } diff --git a/src/tasks.c b/src/tasks.c index 0d3b880..9e816ce 100644 --- a/src/tasks.c +++ b/src/tasks.c @@ -1540,17 +1540,6 @@ StackType_t *pxTopOfStack; #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */ /*-----------------------------------------------------------*/ -// this is referring to the loop function of your arduino project -extern void loop(void); - -void loopTask(void *pvParameters) -{ - while(1) - { - loop(); - } -} - void vTaskStartScheduler( void ) { BaseType_t xReturn; @@ -1558,9 +1547,6 @@ BaseType_t xReturn; /* Add the idle task at the lowest priority. */ #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) { - #if ( configUSE_IDLE_HOOK == 0) - xTaskCreate(loopTask, "loop", 256, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), NULL); - #endif /* Create the idle task, storing its handle in xIdleTaskHandle so it can be returned by the xTaskGetIdleTaskHandle() function. */ xReturn = xTaskCreate( prvIdleTask, "IDLE", tskIDLE_STACK_SIZE, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), &xIdleTaskHandle ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */ From 436f54eaf927e1f63b16c7f9ec1e6aaf93ee2e4e Mon Sep 17 00:00:00 2001 From: andrea-83 Date: Fri, 9 Jul 2021 11:45:08 +0200 Subject: [PATCH 4/5] possibility to define ENABLE_CALLOC_REALLOC as compiler flags --- src/heap_4bis.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/heap_4bis.c b/src/heap_4bis.c index c6d3354..319f66b 100644 --- a/src/heap_4bis.c +++ b/src/heap_4bis.c @@ -435,7 +435,9 @@ uint8_t *puc; } // non standard contributed feature that can be enabled +#ifndef ENABLE_CALLOC_REALLOC #define ENABLE_CALLOC_REALLOC 0 +#endif #if ENABLE_CALLOC_REALLOC From f4ee1a34e81b070540d1bef7f9e66ec600db0080 Mon Sep 17 00:00:00 2001 From: andrea-83 Date: Thu, 15 Jul 2021 14:48:15 +0200 Subject: [PATCH 5/5] added task.h dependencies --- src/idle_hook.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/idle_hook.c b/src/idle_hook.c index b4a1b7e..ab5c23e 100644 --- a/src/idle_hook.c +++ b/src/idle_hook.c @@ -1,6 +1,8 @@ #include #include +//Arduino loop as task +#include "task.h" uint8_t loopAsTask = 0; TaskHandle_t loopHandle;