diff --git a/FreeRTOS/Demo/CORTEX_M3_MPS2_QEMU_GCC/FreeRTOSConfig.h b/FreeRTOS/Demo/CORTEX_M3_MPS2_QEMU_GCC/FreeRTOSConfig.h index cec63a52..2d046e95 100644 --- a/FreeRTOS/Demo/CORTEX_M3_MPS2_QEMU_GCC/FreeRTOSConfig.h +++ b/FreeRTOS/Demo/CORTEX_M3_MPS2_QEMU_GCC/FreeRTOSConfig.h @@ -51,7 +51,7 @@ extern void vAssertCalled( void ); #define configUSE_IDLE_HOOK 1 #define configUSE_TICK_HOOK 1 #define configUSE_DAEMON_TASK_STARTUP_HOOK 0 -#define configCPU_CLOCK_HZ ( ( unsigned long ) 20000000 ) +#define configCPU_CLOCK_HZ ( ( unsigned long ) 62500000 ) // icount shift 4 #define configTICK_RATE_HZ ( ( TickType_t ) 1000 ) #define configMINIMAL_STACK_SIZE ( ( unsigned short ) 2000 ) #define configTOTAL_HEAP_SIZE ( ( size_t ) ( 900 ) ) @@ -61,7 +61,7 @@ extern void vAssertCalled( void ); #define configIDLE_SHOULD_YIELD 1 #define configUSE_CO_ROUTINES 0 -#define configMAX_PRIORITIES ( 10 ) +#define configMAX_PRIORITIES ( 15 ) #define configMAX_CO_ROUTINE_PRIORITIES ( 2 ) #define configTIMER_QUEUE_LENGTH 20 #define configTIMER_TASK_PRIORITY ( configMAX_PRIORITIES - 1 ) diff --git a/FreeRTOS/Demo/CORTEX_M3_MPS2_QEMU_GCC/Makefile b/FreeRTOS/Demo/CORTEX_M3_MPS2_QEMU_GCC/Makefile index 18b1f9ec..e8aa5ff4 100644 --- a/FreeRTOS/Demo/CORTEX_M3_MPS2_QEMU_GCC/Makefile +++ b/FreeRTOS/Demo/CORTEX_M3_MPS2_QEMU_GCC/Makefile @@ -88,6 +88,11 @@ ifeq ($(LIFT_DEMO), 1) SOURCE_FILES += main_lift.c CFLAGS := -DmainCREATE_LIFT_DEMO=1 +else +ifeq ($(WATERS_DEMO), 1) + SOURCE_FILES += main_waters.c + + CFLAGS := -DmainCREATE_WATERS_DEMO=1 else SOURCE_FILES += main_blinky.c @@ -99,6 +104,7 @@ endif endif endif endif +endif DEFINES := -DQEMU_SOC_MPS2 -DHEAP3 diff --git a/FreeRTOS/Demo/CORTEX_M3_MPS2_QEMU_GCC/arbitrary_loads.c b/FreeRTOS/Demo/CORTEX_M3_MPS2_QEMU_GCC/arbitrary_loads.c new file mode 100644 index 00000000..4bb8a671 --- /dev/null +++ b/FreeRTOS/Demo/CORTEX_M3_MPS2_QEMU_GCC/arbitrary_loads.c @@ -0,0 +1,24 @@ +#ifndef ARB_LOAD_C +#define ARB_LOAD_C 1 + +volatile long _NONSENSE_VAR = 0; +#define NS_PER_INS (1000000000 / configCPU_CLOCK_HZ) // 20MHz +#define INS_PER_LOOP 5 // got this multiplier by testing with the goal of getting a 100 ms or 100 tick period +#define INS_PER_PRINT 117 // got this multiplier by testing with the goal of getting a 100 ms or 100 tick period, 2 digits +#define GLOBAL_WCET_MULT 1 // Multiplier to increase all waiting periods to make the schedule more tight an force preemptions +#define WASTE_TIME(X) for (int i=0; i<=((X*GLOBAL_WCET_MULT)/(NS_PER_INS*INS_PER_LOOP)); i++) {_NONSENSE_VAR+=i;} +#define PRINT_TIME(X,Y) for (int i=0; i<=((X*GLOBAL_WCET_MULT)/((NS_PER_INS*INS_PER_PRINT))); i++) {puts(Y);} +// #define PRINT_TIME(X,Y) WASTE_TIME(X) +#define CLAMP_FLOOR(X,LB) ((X>LB)*X+(X<=LB)*LB) +#define CLAMP_CEILING(X,UB) ((X>UB)*UB+(X<=UB)*X) +#define CLAMP(X,LB,UB) CLAMP_CEILING(CLAMP_FLOOR(X,LB),UB) // branch-less clamping + +#define A (unsigned int)2000000011 // prime +#define M (unsigned int)4000000007 // prime +#define C (unsigned int)1283612343 +static unsigned int rng_seed = 2345745; +#define RNG rng_seed+=((A*((rng_seed+=C)-C)+C) % M) +#define RNG_FROM(X) ((A*X+C) % M) +#define CHANCE_1_IN_POWOF2(X,Y) (RNG_FROM(X)<(M>>Y)) + +#endif \ No newline at end of file diff --git a/FreeRTOS/Demo/CORTEX_M3_MPS2_QEMU_GCC/main.c b/FreeRTOS/Demo/CORTEX_M3_MPS2_QEMU_GCC/main.c index 90112004..d9b0bf9c 100644 --- a/FreeRTOS/Demo/CORTEX_M3_MPS2_QEMU_GCC/main.c +++ b/FreeRTOS/Demo/CORTEX_M3_MPS2_QEMU_GCC/main.c @@ -87,6 +87,10 @@ int main() { main_lift(); } + #elif ( mainCREATE_WATERS_DEMO == 1 ) + { + main_waters(); + } #else { #error "Invalid Selection...\nPlease Select a Demo application from the main command" @@ -147,6 +151,10 @@ void vApplicationIdleHook( void ) * blinky demo does not use the idle task hook. */ vFullDemoIdleFunction(); } + #elif ( mainCREATE_WATERS_DEMO == 1 ) + { + vWatersIdleFunction(); + } #endif } /*-----------------------------------------------------------*/ diff --git a/FreeRTOS/Demo/CORTEX_M3_MPS2_QEMU_GCC/main_waters.c b/FreeRTOS/Demo/CORTEX_M3_MPS2_QEMU_GCC/main_waters.c new file mode 100644 index 00000000..81e69708 --- /dev/null +++ b/FreeRTOS/Demo/CORTEX_M3_MPS2_QEMU_GCC/main_waters.c @@ -0,0 +1,376 @@ +/* + * FreeRTOS V202111.00 + * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + * + */ + +#include +#include +#include +#include +/* +TMR Demo with retry +prvSamplerTask will read 4 Bytes of Input into a buffer, unlocks xMutexInput +prvReplicateA and prvReplicateB wait on xMutexInput to average the Inputs and +sum up all numbers up to the Input. +ReplicateA will fail if mod 11 = 0, but only once +ReplicateB will fail if mod 12 = 0 +ReplicateC also exists and will never fail, does not run by default +Each Replicate outputs to it's own queue +prvVoterTask will wait on ReplicateA&B +If they disagree ReplicateC will be started by mutex. +If all the Replicates disagree now the sampler will be engaged once more +*/ +// include tacle benches + +#include "arbitrary_loads.c" + +__attribute__((noinline)) static void trigger_Qemu_break( void ) +{ + puts("Trigger"); + while (1) { + } +} + +// Begin Input Stuff +volatile unsigned char FUZZ_INPUT[4096] = {0xa,0xb,0xc,0xd,0xe,0xf}; +volatile uint32_t FUZZ_LENGTH = 4096; +volatile uint32_t FUZZ_POINTER = 0; +// Read the Byte of Input, if the Input is exausted trigger the breakpoint instead +static unsigned char fuzz_char_next(void) { + // printf("Get next Input from %lx \n",FUZZ_INPUT); + if (FUZZ_POINTER < FUZZ_LENGTH) { + FUZZ_POINTER++; + // printf("Input no. %d %x\n",FUZZ_POINTER-1,FUZZ_INPUT[FUZZ_POINTER-1]); + return FUZZ_INPUT[FUZZ_POINTER-1]; + } else { + // puts("End of Input"); + // Exausted inputs early + trigger_Qemu_break(); + } +} +static uint16_t fuzz_short_next(void) { + unsigned char field[2]; + field[0]=fuzz_char_next(); + field[1]=fuzz_char_next(); + uint16_t* sf = (uint16_t*) field; + return *sf; +} +static uint32_t fuzz_long_next(void) { + unsigned char field[4]; + field[0]=fuzz_char_next(); + field[1]=fuzz_char_next(); + field[2]=fuzz_char_next(); + field[3]=fuzz_char_next(); + uint32_t* sf = (uint32_t*) field; + return *sf; +} +// End Input Stuff +static void prvTask31( void * pvParameters ); +static void prvTask78( void * pvParameters ); +static void prvTask90( void * pvParameters ); +static void prvTask397( void * pvParameters ); +static void prvTask400( void * pvParameters ); +static void prvTask416( void * pvParameters ); +static void prvTask579( void * pvParameters ); +static void prvTask1009( void * pvParameters ); +static void prvTask1107( void * pvParameters ); +static void prvTask1129( void * pvParameters ); + +// Priorities using rate-monotonic scheduling +// ties are decided favoring short wcets +#define mainTASK_31_PRIO ( tskIDLE_PRIORITY + 1 ) +#define mainTASK_78_PRIO ( tskIDLE_PRIORITY + 5 ) +#define mainTASK_90_PRIO ( tskIDLE_PRIORITY + 8 ) +#define mainTASK_397_PRIO ( tskIDLE_PRIORITY + 10 ) +#define mainTASK_400_PRIO ( tskIDLE_PRIORITY + 9 ) +#define mainTASK_416_PRIO ( tskIDLE_PRIORITY + 4 ) +#define mainTASK_579_PRIO ( tskIDLE_PRIORITY + 7 ) +#define mainTASK_1009_PRIO ( tskIDLE_PRIORITY + 6 ) +#define mainTASK_1107_PRIO ( tskIDLE_PRIORITY + 2 ) +#define mainTASK_1129_PRIO ( tskIDLE_PRIORITY + 3 ) + +#define TASK_31_MESSAGE "01" +#define TASK_78_MESSAGE "05" +#define TASK_90_MESSAGE "08" +#define TASK_397_MESSAGE "10" +#define TASK_400_MESSAGE "09" +#define TASK_416_MESSAGE "04" +#define TASK_579_MESSAGE "07" +#define TASK_1009_MESSAGE "06" +#define TASK_1107_MESSAGE "02" +#define TASK_1129_MESSAGE "03" + +// Handles for direct messages +static TaskHandle_t xTask31 = NULL; +static TaskHandle_t xTask78 = NULL; +static TaskHandle_t xTask90 = NULL; +static TaskHandle_t xTask397 = NULL; +static TaskHandle_t xTask400 = NULL; +static TaskHandle_t xTask416 = NULL; +static TaskHandle_t xTask579 = NULL; +static TaskHandle_t xTask1009 = NULL; +static TaskHandle_t xTask1107 = NULL; +static TaskHandle_t xTask1129 = NULL; + +void main_waters( void ) +{ + // puts("Main function"); + /* Start the two tasks as described in the comments at the top of this + * file. */ + xTaskCreate( prvTask31, /* The function that implements the task. */ + "31", /* The text name assigned to the task - for debug only as it is not used by the kernel. */ + configMINIMAL_STACK_SIZE, /* The size of the stack to allocate to the task. */ + NULL, /* The parameter passed to the task - not used in this case. */ + mainTASK_31_PRIO, /* The priority assigned to the task. */ + &xTask31 ); /* The task handle is not required, so NULL is passed. */ + + xTaskCreate( prvTask78, /* The function that implements the task. */ + "78", /* The text name assigned to the task - for debug only as it is not used by the kernel. */ + configMINIMAL_STACK_SIZE, /* The size of the stack to allocate to the task. */ + NULL, /* The parameter passed to the task - not used in this case. */ + mainTASK_78_PRIO, /* The priority assigned to the task. */ + &xTask78 ); /* The task handle is not required, so NULL is passed. */ + + xTaskCreate( prvTask90, + "90", + configMINIMAL_STACK_SIZE, + NULL, + mainTASK_90_PRIO, + &xTask90 ); + + // xTaskCreate( prvTask397, + // "397", + // configMINIMAL_STACK_SIZE, + // NULL, + // mainTASK_397_PRIO, + // &xTask397 ); + + xTaskCreate( prvTask400, + "400", + configMINIMAL_STACK_SIZE, + NULL, + mainTASK_400_PRIO, + &xTask400 ); + + xTaskCreate( prvTask416, + "416", + configMINIMAL_STACK_SIZE, + NULL, + mainTASK_416_PRIO, + &xTask416 ); + + xTaskCreate( prvTask579, + "579", + configMINIMAL_STACK_SIZE, + NULL, + mainTASK_579_PRIO, + &xTask579 ); + + xTaskCreate( prvTask1009, + "1009", + configMINIMAL_STACK_SIZE, + NULL, + mainTASK_1009_PRIO, + &xTask1009 ); + + xTaskCreate( prvTask1107, + "1107", + configMINIMAL_STACK_SIZE, + NULL, + mainTASK_1107_PRIO, + &xTask1107 ); + + xTaskCreate( prvTask1129, + "1129", + configMINIMAL_STACK_SIZE, + NULL, + mainTASK_1129_PRIO, + &xTask1129 ); + + + /* Start the tasks and timer running. */ + // puts("Start scheduler"); + vTaskStartScheduler(); + + /* If all is well, the scheduler will now be running, and the following + * line will never be reached. If the following line does execute, then + * there was insufficient FreeRTOS heap memory available for the Idle and/or + * timer tasks to be created. See the memory management section on the + * FreeRTOS web site for more details on the FreeRTOS heap + * http://www.freertos.org/a00111.html. */ + for( ; ; ) + { + } +} + +static void prvTask31( void * pvParameters ) { + TickType_t xLastWakeTime = xTaskGetTickCount(); + const TickType_t xFrequency = 100 / portTICK_PERIOD_MS; + for( ;; ){ + // Actions -------------------------------------- + // Exectime: f(x) = c + x + // Output: g(x) = x % 4 + PRINT_TIME(48940, TASK_31_MESSAGE) + // uint16_t x = fuzz_short_next(); + // PRINT_TIME(CLAMP(x, 0, 2<<11), TASK_579_MESSAGE) + // xTaskNotify(xTask1009, x % 4, eSetValueWithOverwrite); + // --------------------------------------------- + vTaskDelayUntil( &xLastWakeTime, xFrequency );}// Wait for the next cycle. +} + +static void prvTask78( void * pvParameters ) { + TickType_t xLastWakeTime = xTaskGetTickCount(); + const TickType_t xFrequency = 10 / portTICK_PERIOD_MS; + for( ;; ){ + // Actions -------------------------------------- + // Exectime: f(x) = c + x + // Output: g(x) = x % 4 + PRINT_TIME(76035, TASK_78_MESSAGE) + // uint16_t x = fuzz_short_next(); + // PRINT_TIME(CLAMP(x, 0, 2<<11), TASK_579_MESSAGE) + // xTaskNotify(xTask1009, x % 4, eSetValueWithOverwrite); + // --------------------------------------------- + vTaskDelayUntil( &xLastWakeTime, xFrequency );} +} + +static void prvTask90( void * pvParameters ) { + TickType_t xLastWakeTime = xTaskGetTickCount(); + const TickType_t xFrequency = 2 / portTICK_PERIOD_MS; + for( ;; ){ + // Actions -------------------------------------- + // Exectime: f(x) = c + x + // Output: g(x) = x % 4 + PRINT_TIME(20045, TASK_90_MESSAGE) + // uint16_t x = fuzz_short_next(); + // PRINT_TIME(CLAMP(x, 0, 2<<11), TASK_579_MESSAGE) + // xTaskNotify(xTask1009, x % 4, eSetValueWithOverwrite); + // --------------------------------------------- + vTaskDelayUntil( &xLastWakeTime, xFrequency );} +} + +static void prvTask397( void * pvParameters ) { + for( ;; ){ + ulTaskNotifyTake(pdTRUE,portMAX_DELAY); + PRINT_TIME(5830, TASK_397_MESSAGE);} +} + +static void prvTask400( void * pvParameters ) { + TickType_t xLastWakeTime = xTaskGetTickCount(); + const TickType_t xFrequency = 2 / portTICK_PERIOD_MS; + for( ;; ){ + // Actions -------------------------------------- + // Exectime: f(x) = c + x + // Output: g(x) = x % 4 + PRINT_TIME(1765, TASK_400_MESSAGE) + // uint16_t x = fuzz_short_next(); + // PRINT_TIME(CLAMP(x, 0, 2<<11), TASK_579_MESSAGE) + // xTaskNotify(xTask1009, x % 4, eSetValueWithOverwrite); + // --------------------------------------------- + vTaskDelayUntil( &xLastWakeTime, xFrequency );} +} + +static void prvTask416( void * pvParameters ) { + TickType_t xLastWakeTime = xTaskGetTickCount(); + const TickType_t xFrequency = 10 / portTICK_PERIOD_MS; + for( ;; ){ + // Actions -------------------------------------- + // Exectime: f(x) = c + x + // Output: g(x) = x % 4 + PRINT_TIME(126955, TASK_416_MESSAGE) + // uint16_t x = fuzz_short_next(); + // PRINT_TIME(CLAMP(x, 0, 2<<11), TASK_579_MESSAGE) + // xTaskNotify(xTask1009, x % 4, eSetValueWithOverwrite); + // --------------------------------------------- + vTaskDelayUntil( &xLastWakeTime, xFrequency );} +} + +static void prvTask579( void * pvParameters ) { + TickType_t xLastWakeTime = xTaskGetTickCount(); + const TickType_t xFrequency = 10 / portTICK_PERIOD_MS; + for( ;; ){ + // Actions -------------------------------------- + // Exectime: f(x) = c + x + // Output: g(x) = x % 4 + PRINT_TIME(2460, TASK_579_MESSAGE) + // uint16_t x = fuzz_short_next(); + // PRINT_TIME(CLAMP(x, 0, 2<<11), TASK_579_MESSAGE) + // xTaskNotify(xTask1009, x % 4, eSetValueWithOverwrite); + // --------------------------------------------- + vTaskDelayUntil( &xLastWakeTime, xFrequency );} +} + +static void prvTask1009( void * pvParameters ) { + TickType_t xLastWakeTime = xTaskGetTickCount(); + const TickType_t xFrequency = 10 / portTICK_PERIOD_MS; + for( ;; ){ + // Actions -------------------------------------- + // int y = ulTaskNotifyTake(pdFALSE, 0); + PRINT_TIME(51485, TASK_1009_MESSAGE); + // --------------------------------------------- + vTaskDelayUntil( &xLastWakeTime, xFrequency );} +} + +static void prvTask1107( void * pvParameters ) { + TickType_t xLastWakeTime = xTaskGetTickCount(); + const TickType_t xFrequency = 50 / portTICK_PERIOD_MS; + for( ;; ){ + // Actions -------------------------------------- + // Exectime: f(x) = c + x + // Output: g(x) = x % 4 + PRINT_TIME(76865, TASK_1107_MESSAGE) + // uint16_t x = fuzz_short_next(); + // PRINT_TIME(CLAMP(x, 0, 2<<11), TASK_579_MESSAGE) + // xTaskNotify(xTask1009, x % 4, eSetValueWithOverwrite); + // --------------------------------------------- + vTaskDelayUntil( &xLastWakeTime, xFrequency );} +} + +static void prvTask1129( void * pvParameters ) { + int period_counter = 2; + TickType_t xLastWakeTime = xTaskGetTickCount(); + const TickType_t xFrequency = 10 / portTICK_PERIOD_MS; + for( ;; ){ + // Actions -------------------------------------- + // Exectime: f(x) = c + x + // Output: g(x) = x % 4 + PRINT_TIME(145040, TASK_1129_MESSAGE) + if (--period_counter==0) {puts("End");} + // uint16_t x = fuzz_short_next(); + // PRINT_TIME(CLAMP(x, 0, 2<<11), TASK_579_MESSAGE) + // xTaskNotify(xTask1009, x % 4, eSetValueWithOverwrite); + // --------------------------------------------- + vTaskDelayUntil( &xLastWakeTime, xFrequency );} +} + +void vWatersIdleFunction() { + for (int i; i<1000; i++) { + puts("0 "); + } +} + +void isr_starter( void ) +{ +} + +/*-----------------------------------------------------------*/