From 8398761e7c05a4055a6bb57def621994ff632bf4 Mon Sep 17 00:00:00 2001 From: John Kenyon Date: Tue, 12 Nov 2024 20:46:24 -0800 Subject: [PATCH] Add more examples --- BUILD.md | 7 +- examples/ex2_read_input/CMakeLists.txt | 52 +++++++ .../ex2_read_input/include/FreeRTOSConfig.h | 143 +++++++++++++++++ examples/ex2_read_input/main.c | 45 ++++++ examples/ex3_producer_consumer/CMakeLists.txt | 52 +++++++ .../include/FreeRTOSConfig.h | 143 +++++++++++++++++ examples/ex3_producer_consumer/main.c | 90 +++++++++++ examples/ex4_mutex/CMakeLists.txt | 52 +++++++ examples/ex4_mutex/include/FreeRTOSConfig.h | 143 +++++++++++++++++ examples/ex4_mutex/main.c | 58 +++++++ examples/ex5_metrics/CMakeLists.txt | 52 +++++++ examples/ex5_metrics/include/FreeRTOSConfig.h | 145 ++++++++++++++++++ examples/ex5_metrics/main.c | 73 +++++++++ .../ex6_reboot_to_bootloader/CMakeLists.txt | 52 +++++++ .../include/FreeRTOSConfig.h | 145 ++++++++++++++++++ examples/ex6_reboot_to_bootloader/main.c | 80 ++++++++++ 16 files changed, 1331 insertions(+), 1 deletion(-) create mode 100644 examples/ex2_read_input/CMakeLists.txt create mode 100644 examples/ex2_read_input/include/FreeRTOSConfig.h create mode 100644 examples/ex2_read_input/main.c create mode 100644 examples/ex3_producer_consumer/CMakeLists.txt create mode 100644 examples/ex3_producer_consumer/include/FreeRTOSConfig.h create mode 100644 examples/ex3_producer_consumer/main.c create mode 100644 examples/ex4_mutex/CMakeLists.txt create mode 100644 examples/ex4_mutex/include/FreeRTOSConfig.h create mode 100644 examples/ex4_mutex/main.c create mode 100644 examples/ex5_metrics/CMakeLists.txt create mode 100644 examples/ex5_metrics/include/FreeRTOSConfig.h create mode 100644 examples/ex5_metrics/main.c create mode 100644 examples/ex6_reboot_to_bootloader/CMakeLists.txt create mode 100644 examples/ex6_reboot_to_bootloader/include/FreeRTOSConfig.h create mode 100644 examples/ex6_reboot_to_bootloader/main.c diff --git a/BUILD.md b/BUILD.md index 14a1537..05f44b6 100644 --- a/BUILD.md +++ b/BUILD.md @@ -1,7 +1,12 @@ - # IN PROGRESS The build process for this repo is still not stabilized. +First, be sure to init the submodules: `git submodule update --init --recursive` +Second, each example should be built individually + +`(cd examples/ex0_no_freertos/; rm -rf build; mkdir -p build; cd build; cmake ..; make -j 8; ls ex0_hello_world.uf2)` + +`(cd examples/ex1_task_hello/; rm -rf build; mkdir -p build; cd build; cmake ..; make -j 8; ls ex1_task_hello.uf2)` diff --git a/examples/ex2_read_input/CMakeLists.txt b/examples/ex2_read_input/CMakeLists.txt new file mode 100644 index 0000000..9442553 --- /dev/null +++ b/examples/ex2_read_input/CMakeLists.txt @@ -0,0 +1,52 @@ +cmake_minimum_required(VERSION 3.13) + +if(NOT DEFINED LIB_ROOT) + set(LIB_ROOT ${CMAKE_SOURCE_DIR}/../..) +endif(NOT DEFINED LIB_ROOT) + +set(PICO_SDK_PATH ${LIB_ROOT}/submodules/pico-sdk/) +include( ${LIB_ROOT}/submodules/pico-sdk/external/pico_sdk_import.cmake) + +# Pull in FreeRTOS +include(${LIB_ROOT}/submodules/FreeRTOS-Kernel/portable/ThirdParty/GCC/RP2040/FreeRTOS_Kernel_import.cmake) + +set(PICOTOOL_FETCH_FROM_GIT_PATH ${LIB_ROOT}/submodules/picotool/) + +# Include the Pico SDK (make sure to use the correct path to pico_sdk_import.cmake) +include(FetchContent) + +# Define the project +set(PROJECT ex2_read_input) +project(${PROJECT} C CXX ASM) + +# Initialize the Pico SDK +pico_sdk_init() + +include_directories( + ${LIB_ROOT}/submodules/pico-sdk/src/rp2040/hardware_structs/include/ + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/include/ + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/portable/ThirdParty/GCC/RP2040/include/ + include/ + ${LIB_ROOT}/submodules/pico-sdk/src/rp2_common/hardware_exception/include/ + ${LIB_ROOT}/submodules/pico-sdk/src/rp2_common/pico_multicore/include/ +) + +# Create the executable +add_executable(${PROJECT} main.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/tasks.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/queue.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/list.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/portable/ThirdParty/GCC/RP2040/port.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/portable/MemMang/heap_3.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/event_groups.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/timers.c + ${LIB_ROOT}/submodules/pico-sdk/src/rp2_common/hardware_exception/exception.c +) + +# Link against FreeRTOS, Pico SDK, and freertos_config +target_link_libraries(${PROJECT} pico_stdlib) +pico_add_extra_outputs(${PROJECT}) + +pico_enable_stdio_usb( ${PROJECT} 1) +pico_enable_stdio_uart(${PROJECT} 0) + diff --git a/examples/ex2_read_input/include/FreeRTOSConfig.h b/examples/ex2_read_input/include/FreeRTOSConfig.h new file mode 100644 index 0000000..b0b311b --- /dev/null +++ b/examples/ex2_read_input/include/FreeRTOSConfig.h @@ -0,0 +1,143 @@ +/* + * FreeRTOS V202212.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 + * + */ + +#ifndef FREERTOS_CONFIG_H +#define FREERTOS_CONFIG_H + +/*----------------------------------------------------------- + * Application specific definitions. + * + * These definitions should be adjusted for your particular hardware and + * application requirements. + * + * THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE + * FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE. + * + * See http://www.freertos.org/a00110.html + *----------------------------------------------------------*/ + +/* Scheduler Related */ +#define configUSE_PREEMPTION 1 +#define configUSE_TICKLESS_IDLE 0 +#define configUSE_IDLE_HOOK 0 +#define configUSE_TICK_HOOK 0 +#define configTICK_RATE_HZ ( ( TickType_t ) 1000 ) +#define configMAX_PRIORITIES 32 +#define configMINIMAL_STACK_SIZE ( configSTACK_DEPTH_TYPE ) 256 +#define configUSE_16_BIT_TICKS 0 + +#define configIDLE_SHOULD_YIELD 1 + +/* Synchronization Related */ +#define configUSE_MUTEXES 1 +#define configUSE_RECURSIVE_MUTEXES 1 +#define configUSE_APPLICATION_TASK_TAG 0 +#define configUSE_COUNTING_SEMAPHORES 1 +#define configQUEUE_REGISTRY_SIZE 8 +#define configUSE_QUEUE_SETS 1 +#define configUSE_TIME_SLICING 1 +#define configUSE_NEWLIB_REENTRANT 0 +#define configENABLE_BACKWARD_COMPATIBILITY 0 +#define configNUM_THREAD_LOCAL_STORAGE_POINTERS 5 + +/* System */ +#define configSTACK_DEPTH_TYPE uint32_t +#define configMESSAGE_BUFFER_LENGTH_TYPE size_t + +/* Memory allocation related definitions. */ +#define configSUPPORT_STATIC_ALLOCATION 0 +#define configSUPPORT_DYNAMIC_ALLOCATION 1 +#define configTOTAL_HEAP_SIZE (128*1024) +#define configAPPLICATION_ALLOCATED_HEAP 0 + +/* Hook function related definitions. */ +#define configCHECK_FOR_STACK_OVERFLOW 0 +#define configUSE_MALLOC_FAILED_HOOK 0 +#define configUSE_DAEMON_TASK_STARTUP_HOOK 0 + +/* Run time and task stats gathering related definitions. */ +#define configGENERATE_RUN_TIME_STATS 0 +#define configUSE_TRACE_FACILITY 1 +#define configUSE_STATS_FORMATTING_FUNCTIONS 0 + +/* Co-routine related definitions. */ +#define configUSE_CO_ROUTINES 0 +#define configMAX_CO_ROUTINE_PRIORITIES 1 + +/* Software timer related definitions. */ +#define configUSE_TIMERS 1 +#define configTIMER_TASK_PRIORITY ( configMAX_PRIORITIES - 1 ) +#define configTIMER_QUEUE_LENGTH 10 +#define configTIMER_TASK_STACK_DEPTH 1024 + +/* Interrupt nesting behaviour configuration. */ +/* +#define configKERNEL_INTERRUPT_PRIORITY [dependent of processor] +#define configMAX_SYSCALL_INTERRUPT_PRIORITY [dependent on processor and application] +#define configMAX_API_CALL_INTERRUPT_PRIORITY [dependent on processor and application] +*/ + +/* SMP port only */ +#define configNUMBER_OF_CORES 1 +#define configTICK_CORE 0 +#define configRUN_MULTIPLE_PRIORITIES 0 + +/* RP2040 specific */ +#define configSUPPORT_PICO_SYNC_INTEROP 1 +#define configSUPPORT_PICO_TIME_INTEROP 1 + +#include +/* Define to trap errors during development. */ +#define configASSERT(x) assert(x) + +/* Set the following definitions to 1 to include the API function, or zero +to exclude the API function. */ +#define INCLUDE_vTaskPrioritySet 1 +#define INCLUDE_uxTaskPriorityGet 1 +#define INCLUDE_vTaskDelete 1 +#define INCLUDE_vTaskSuspend 1 +#define INCLUDE_vTaskDelayUntil 1 +#define INCLUDE_vTaskDelay 1 +#define INCLUDE_xTaskGetSchedulerState 1 +#define INCLUDE_xTaskGetCurrentTaskHandle 1 +#define INCLUDE_uxTaskGetStackHighWaterMark 1 +#define INCLUDE_xTaskGetIdleTaskHandle 1 +#define INCLUDE_eTaskGetState 1 +#define INCLUDE_xTimerPendFunctionCall 1 +#define INCLUDE_xTaskAbortDelay 1 +#define INCLUDE_xTaskGetHandle 1 +#define INCLUDE_xTaskResumeFromISR 1 +#define INCLUDE_xQueueGetMutexHolder 1 +#define INCLUDE_vApplicationStackOverflowHook 0 +#define INCLUDE_vApplicationTickHook 1 +#define INCLUDE_vApplicationMallocFailedHook 1 + + +#define LIB_PICO_MULTICORE 1 +/* A header file that defines trace macro can be included here. */ + +#endif /* FREERTOS_CONFIG_H */ + diff --git a/examples/ex2_read_input/main.c b/examples/ex2_read_input/main.c new file mode 100644 index 0000000..bfc6584 --- /dev/null +++ b/examples/ex2_read_input/main.c @@ -0,0 +1,45 @@ +#include +#include "pico/stdlib.h" +#include "FreeRTOS.h" +#include "task.h" + +void usbInputTask(void *params) { + char inputBuffer[100]; + int bufferIndex = 0; + + while (1) { + // Check if a character is available from USB + if (stdio_usb_connected()) { // Make sure USB is connected + int ch = getchar_timeout_us(0); // Non-blocking read + if (ch != PICO_ERROR_TIMEOUT) { // Data available + if (ch == '\n' || ch == '\r') { + if (ch == '\r') { + putchar('\n'); + } + inputBuffer[bufferIndex] = '\0'; // Null-terminate the string + + // Parse the buffer as needed + int value; + if (sscanf(inputBuffer, "%d", &value) == 1) { + printf("Received integer: %d\n", value); + } + + bufferIndex = 0; // Reset buffer index for next input + } else if (bufferIndex < sizeof(inputBuffer) - 1) { + inputBuffer[bufferIndex++] = ch; + putchar(ch); + } + } + } + vTaskDelay(10 / portTICK_PERIOD_MS); // Small delay to prevent task hogging + } +} + +int main() { + stdio_init_all(); // Initialize stdio over USB + xTaskCreate(usbInputTask, "USB Input", 1024, NULL, 1, NULL); + vTaskStartScheduler(); // Start FreeRTOS scheduler + + for (;;); +} + diff --git a/examples/ex3_producer_consumer/CMakeLists.txt b/examples/ex3_producer_consumer/CMakeLists.txt new file mode 100644 index 0000000..2df9f16 --- /dev/null +++ b/examples/ex3_producer_consumer/CMakeLists.txt @@ -0,0 +1,52 @@ +cmake_minimum_required(VERSION 3.13) + +if(NOT DEFINED LIB_ROOT) + set(LIB_ROOT ${CMAKE_SOURCE_DIR}/../..) +endif(NOT DEFINED LIB_ROOT) + +set(PICO_SDK_PATH ${LIB_ROOT}/submodules/pico-sdk/) +include( ${LIB_ROOT}/submodules/pico-sdk/external/pico_sdk_import.cmake) + +# Pull in FreeRTOS +include(${LIB_ROOT}/submodules/FreeRTOS-Kernel/portable/ThirdParty/GCC/RP2040/FreeRTOS_Kernel_import.cmake) + +set(PICOTOOL_FETCH_FROM_GIT_PATH ${LIB_ROOT}/submodules/picotool/) + +# Include the Pico SDK (make sure to use the correct path to pico_sdk_import.cmake) +include(FetchContent) + +# Define the project +set(PROJECT ex3_queues) +project(${PROJECT} C CXX ASM) + +# Initialize the Pico SDK +pico_sdk_init() + +include_directories( + ${LIB_ROOT}/submodules/pico-sdk/src/rp2040/hardware_structs/include/ + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/include/ + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/portable/ThirdParty/GCC/RP2040/include/ + include/ + ${LIB_ROOT}/submodules/pico-sdk/src/rp2_common/hardware_exception/include/ + ${LIB_ROOT}/submodules/pico-sdk/src/rp2_common/pico_multicore/include/ +) + +# Create the executable +add_executable(${PROJECT} main.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/tasks.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/queue.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/list.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/portable/ThirdParty/GCC/RP2040/port.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/portable/MemMang/heap_3.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/event_groups.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/timers.c + ${LIB_ROOT}/submodules/pico-sdk/src/rp2_common/hardware_exception/exception.c +) + +# Link against FreeRTOS, Pico SDK, and freertos_config +target_link_libraries(${PROJECT} pico_stdlib) +pico_add_extra_outputs(${PROJECT}) + +pico_enable_stdio_usb( ${PROJECT} 1) +pico_enable_stdio_uart(${PROJECT} 0) + diff --git a/examples/ex3_producer_consumer/include/FreeRTOSConfig.h b/examples/ex3_producer_consumer/include/FreeRTOSConfig.h new file mode 100644 index 0000000..b0b311b --- /dev/null +++ b/examples/ex3_producer_consumer/include/FreeRTOSConfig.h @@ -0,0 +1,143 @@ +/* + * FreeRTOS V202212.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 + * + */ + +#ifndef FREERTOS_CONFIG_H +#define FREERTOS_CONFIG_H + +/*----------------------------------------------------------- + * Application specific definitions. + * + * These definitions should be adjusted for your particular hardware and + * application requirements. + * + * THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE + * FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE. + * + * See http://www.freertos.org/a00110.html + *----------------------------------------------------------*/ + +/* Scheduler Related */ +#define configUSE_PREEMPTION 1 +#define configUSE_TICKLESS_IDLE 0 +#define configUSE_IDLE_HOOK 0 +#define configUSE_TICK_HOOK 0 +#define configTICK_RATE_HZ ( ( TickType_t ) 1000 ) +#define configMAX_PRIORITIES 32 +#define configMINIMAL_STACK_SIZE ( configSTACK_DEPTH_TYPE ) 256 +#define configUSE_16_BIT_TICKS 0 + +#define configIDLE_SHOULD_YIELD 1 + +/* Synchronization Related */ +#define configUSE_MUTEXES 1 +#define configUSE_RECURSIVE_MUTEXES 1 +#define configUSE_APPLICATION_TASK_TAG 0 +#define configUSE_COUNTING_SEMAPHORES 1 +#define configQUEUE_REGISTRY_SIZE 8 +#define configUSE_QUEUE_SETS 1 +#define configUSE_TIME_SLICING 1 +#define configUSE_NEWLIB_REENTRANT 0 +#define configENABLE_BACKWARD_COMPATIBILITY 0 +#define configNUM_THREAD_LOCAL_STORAGE_POINTERS 5 + +/* System */ +#define configSTACK_DEPTH_TYPE uint32_t +#define configMESSAGE_BUFFER_LENGTH_TYPE size_t + +/* Memory allocation related definitions. */ +#define configSUPPORT_STATIC_ALLOCATION 0 +#define configSUPPORT_DYNAMIC_ALLOCATION 1 +#define configTOTAL_HEAP_SIZE (128*1024) +#define configAPPLICATION_ALLOCATED_HEAP 0 + +/* Hook function related definitions. */ +#define configCHECK_FOR_STACK_OVERFLOW 0 +#define configUSE_MALLOC_FAILED_HOOK 0 +#define configUSE_DAEMON_TASK_STARTUP_HOOK 0 + +/* Run time and task stats gathering related definitions. */ +#define configGENERATE_RUN_TIME_STATS 0 +#define configUSE_TRACE_FACILITY 1 +#define configUSE_STATS_FORMATTING_FUNCTIONS 0 + +/* Co-routine related definitions. */ +#define configUSE_CO_ROUTINES 0 +#define configMAX_CO_ROUTINE_PRIORITIES 1 + +/* Software timer related definitions. */ +#define configUSE_TIMERS 1 +#define configTIMER_TASK_PRIORITY ( configMAX_PRIORITIES - 1 ) +#define configTIMER_QUEUE_LENGTH 10 +#define configTIMER_TASK_STACK_DEPTH 1024 + +/* Interrupt nesting behaviour configuration. */ +/* +#define configKERNEL_INTERRUPT_PRIORITY [dependent of processor] +#define configMAX_SYSCALL_INTERRUPT_PRIORITY [dependent on processor and application] +#define configMAX_API_CALL_INTERRUPT_PRIORITY [dependent on processor and application] +*/ + +/* SMP port only */ +#define configNUMBER_OF_CORES 1 +#define configTICK_CORE 0 +#define configRUN_MULTIPLE_PRIORITIES 0 + +/* RP2040 specific */ +#define configSUPPORT_PICO_SYNC_INTEROP 1 +#define configSUPPORT_PICO_TIME_INTEROP 1 + +#include +/* Define to trap errors during development. */ +#define configASSERT(x) assert(x) + +/* Set the following definitions to 1 to include the API function, or zero +to exclude the API function. */ +#define INCLUDE_vTaskPrioritySet 1 +#define INCLUDE_uxTaskPriorityGet 1 +#define INCLUDE_vTaskDelete 1 +#define INCLUDE_vTaskSuspend 1 +#define INCLUDE_vTaskDelayUntil 1 +#define INCLUDE_vTaskDelay 1 +#define INCLUDE_xTaskGetSchedulerState 1 +#define INCLUDE_xTaskGetCurrentTaskHandle 1 +#define INCLUDE_uxTaskGetStackHighWaterMark 1 +#define INCLUDE_xTaskGetIdleTaskHandle 1 +#define INCLUDE_eTaskGetState 1 +#define INCLUDE_xTimerPendFunctionCall 1 +#define INCLUDE_xTaskAbortDelay 1 +#define INCLUDE_xTaskGetHandle 1 +#define INCLUDE_xTaskResumeFromISR 1 +#define INCLUDE_xQueueGetMutexHolder 1 +#define INCLUDE_vApplicationStackOverflowHook 0 +#define INCLUDE_vApplicationTickHook 1 +#define INCLUDE_vApplicationMallocFailedHook 1 + + +#define LIB_PICO_MULTICORE 1 +/* A header file that defines trace macro can be included here. */ + +#endif /* FREERTOS_CONFIG_H */ + diff --git a/examples/ex3_producer_consumer/main.c b/examples/ex3_producer_consumer/main.c new file mode 100644 index 0000000..e79eb82 --- /dev/null +++ b/examples/ex3_producer_consumer/main.c @@ -0,0 +1,90 @@ +#include +#include "pico/stdlib.h" +#include "FreeRTOS.h" +#include "task.h" +#include "queue.h" + +// Define the queue handle globally +QueueHandle_t dataQueue; + +// Task 1: Produces data and sends it to the queue +void producerTask(void *params) { + int counter = 0; + + while (1) { + // Send the counter value to the queue + if (xQueueSend(dataQueue, &counter, 0) == pdPASS) { + printf("Producer: Sent %d to queue\n", counter); + } else { + printf("Producer: Failed to send to queue\n"); + } + counter++; + + // Delay to simulate periodic data production + vTaskDelay(2000 / portTICK_PERIOD_MS); // 1 second delay + } +} + +// Task 2: Consumes data from the queue and processes it +void consumerTask(void *params) { + int receivedValue; + + while (1) { + // Receive data from the queue + if (xQueueReceive(dataQueue, &receivedValue, portMAX_DELAY) == pdPASS) { + printf("Consumer: Received %d from queue\n", receivedValue); + } + } +} + +void usbInputTask(void *params) { + char inputBuffer[100]; + int bufferIndex = 0; + + while (1) { + // Check if a character is available from USB + if (stdio_usb_connected()) { // Make sure USB is connected + int ch = getchar_timeout_us(0); // Non-blocking read + if (ch != PICO_ERROR_TIMEOUT) { // Data available + if (ch == '\n' || ch == '\r') { + if (ch == '\r') { + putchar('\n'); + } + inputBuffer[bufferIndex] = '\0'; // Null-terminate the string + + // Parse the buffer as needed + int value; + if (sscanf(inputBuffer, "%d", &value) == 1) { + printf("Received integer: %d\n", value); + } + + bufferIndex = 0; // Reset buffer index for next input + } else if (bufferIndex < sizeof(inputBuffer) - 1) { + inputBuffer[bufferIndex++] = ch; + putchar(ch); + } + } + } + vTaskDelay(10 / portTICK_PERIOD_MS); // Small delay to prevent task hogging + } +} + +int main() { + stdio_init_all(); // Initialize stdio over USB + + // Create a queue to hold integers, with a capacity of 5 items + dataQueue = xQueueCreate(5, sizeof(int)); + if (dataQueue == NULL) { + printf("Failed to create queue\n"); + while (1); // Loop indefinitely if queue creation fails + } + + xTaskCreate(producerTask, "Producer Task", 1024, NULL, 1, NULL); + xTaskCreate(consumerTask, "Consumer Task", 1024, NULL, 1, NULL); + xTaskCreate(usbInputTask, "USB Input", 1024, NULL, 1, NULL); + + vTaskStartScheduler(); // Start FreeRTOS scheduler + + for (;;); +} + diff --git a/examples/ex4_mutex/CMakeLists.txt b/examples/ex4_mutex/CMakeLists.txt new file mode 100644 index 0000000..efd54c5 --- /dev/null +++ b/examples/ex4_mutex/CMakeLists.txt @@ -0,0 +1,52 @@ +cmake_minimum_required(VERSION 3.13) + +if(NOT DEFINED LIB_ROOT) + set(LIB_ROOT ${CMAKE_SOURCE_DIR}/../..) +endif(NOT DEFINED LIB_ROOT) + +set(PICO_SDK_PATH ${LIB_ROOT}/submodules/pico-sdk/) +include( ${LIB_ROOT}/submodules/pico-sdk/external/pico_sdk_import.cmake) + +# Pull in FreeRTOS +include(${LIB_ROOT}/submodules/FreeRTOS-Kernel/portable/ThirdParty/GCC/RP2040/FreeRTOS_Kernel_import.cmake) + +set(PICOTOOL_FETCH_FROM_GIT_PATH ${LIB_ROOT}/submodules/picotool/) + +# Include the Pico SDK (make sure to use the correct path to pico_sdk_import.cmake) +include(FetchContent) + +# Define the project +set(PROJECT ex4_mutex) +project(${PROJECT} C CXX ASM) + +# Initialize the Pico SDK +pico_sdk_init() + +include_directories( + ${LIB_ROOT}/submodules/pico-sdk/src/rp2040/hardware_structs/include/ + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/include/ + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/portable/ThirdParty/GCC/RP2040/include/ + include/ + ${LIB_ROOT}/submodules/pico-sdk/src/rp2_common/hardware_exception/include/ + ${LIB_ROOT}/submodules/pico-sdk/src/rp2_common/pico_multicore/include/ +) + +# Create the executable +add_executable(${PROJECT} main.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/tasks.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/queue.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/list.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/portable/ThirdParty/GCC/RP2040/port.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/portable/MemMang/heap_3.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/event_groups.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/timers.c + ${LIB_ROOT}/submodules/pico-sdk/src/rp2_common/hardware_exception/exception.c +) + +# Link against FreeRTOS, Pico SDK, and freertos_config +target_link_libraries(${PROJECT} pico_stdlib) +pico_add_extra_outputs(${PROJECT}) + +pico_enable_stdio_usb( ${PROJECT} 1) +pico_enable_stdio_uart(${PROJECT} 0) + diff --git a/examples/ex4_mutex/include/FreeRTOSConfig.h b/examples/ex4_mutex/include/FreeRTOSConfig.h new file mode 100644 index 0000000..b0b311b --- /dev/null +++ b/examples/ex4_mutex/include/FreeRTOSConfig.h @@ -0,0 +1,143 @@ +/* + * FreeRTOS V202212.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 + * + */ + +#ifndef FREERTOS_CONFIG_H +#define FREERTOS_CONFIG_H + +/*----------------------------------------------------------- + * Application specific definitions. + * + * These definitions should be adjusted for your particular hardware and + * application requirements. + * + * THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE + * FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE. + * + * See http://www.freertos.org/a00110.html + *----------------------------------------------------------*/ + +/* Scheduler Related */ +#define configUSE_PREEMPTION 1 +#define configUSE_TICKLESS_IDLE 0 +#define configUSE_IDLE_HOOK 0 +#define configUSE_TICK_HOOK 0 +#define configTICK_RATE_HZ ( ( TickType_t ) 1000 ) +#define configMAX_PRIORITIES 32 +#define configMINIMAL_STACK_SIZE ( configSTACK_DEPTH_TYPE ) 256 +#define configUSE_16_BIT_TICKS 0 + +#define configIDLE_SHOULD_YIELD 1 + +/* Synchronization Related */ +#define configUSE_MUTEXES 1 +#define configUSE_RECURSIVE_MUTEXES 1 +#define configUSE_APPLICATION_TASK_TAG 0 +#define configUSE_COUNTING_SEMAPHORES 1 +#define configQUEUE_REGISTRY_SIZE 8 +#define configUSE_QUEUE_SETS 1 +#define configUSE_TIME_SLICING 1 +#define configUSE_NEWLIB_REENTRANT 0 +#define configENABLE_BACKWARD_COMPATIBILITY 0 +#define configNUM_THREAD_LOCAL_STORAGE_POINTERS 5 + +/* System */ +#define configSTACK_DEPTH_TYPE uint32_t +#define configMESSAGE_BUFFER_LENGTH_TYPE size_t + +/* Memory allocation related definitions. */ +#define configSUPPORT_STATIC_ALLOCATION 0 +#define configSUPPORT_DYNAMIC_ALLOCATION 1 +#define configTOTAL_HEAP_SIZE (128*1024) +#define configAPPLICATION_ALLOCATED_HEAP 0 + +/* Hook function related definitions. */ +#define configCHECK_FOR_STACK_OVERFLOW 0 +#define configUSE_MALLOC_FAILED_HOOK 0 +#define configUSE_DAEMON_TASK_STARTUP_HOOK 0 + +/* Run time and task stats gathering related definitions. */ +#define configGENERATE_RUN_TIME_STATS 0 +#define configUSE_TRACE_FACILITY 1 +#define configUSE_STATS_FORMATTING_FUNCTIONS 0 + +/* Co-routine related definitions. */ +#define configUSE_CO_ROUTINES 0 +#define configMAX_CO_ROUTINE_PRIORITIES 1 + +/* Software timer related definitions. */ +#define configUSE_TIMERS 1 +#define configTIMER_TASK_PRIORITY ( configMAX_PRIORITIES - 1 ) +#define configTIMER_QUEUE_LENGTH 10 +#define configTIMER_TASK_STACK_DEPTH 1024 + +/* Interrupt nesting behaviour configuration. */ +/* +#define configKERNEL_INTERRUPT_PRIORITY [dependent of processor] +#define configMAX_SYSCALL_INTERRUPT_PRIORITY [dependent on processor and application] +#define configMAX_API_CALL_INTERRUPT_PRIORITY [dependent on processor and application] +*/ + +/* SMP port only */ +#define configNUMBER_OF_CORES 1 +#define configTICK_CORE 0 +#define configRUN_MULTIPLE_PRIORITIES 0 + +/* RP2040 specific */ +#define configSUPPORT_PICO_SYNC_INTEROP 1 +#define configSUPPORT_PICO_TIME_INTEROP 1 + +#include +/* Define to trap errors during development. */ +#define configASSERT(x) assert(x) + +/* Set the following definitions to 1 to include the API function, or zero +to exclude the API function. */ +#define INCLUDE_vTaskPrioritySet 1 +#define INCLUDE_uxTaskPriorityGet 1 +#define INCLUDE_vTaskDelete 1 +#define INCLUDE_vTaskSuspend 1 +#define INCLUDE_vTaskDelayUntil 1 +#define INCLUDE_vTaskDelay 1 +#define INCLUDE_xTaskGetSchedulerState 1 +#define INCLUDE_xTaskGetCurrentTaskHandle 1 +#define INCLUDE_uxTaskGetStackHighWaterMark 1 +#define INCLUDE_xTaskGetIdleTaskHandle 1 +#define INCLUDE_eTaskGetState 1 +#define INCLUDE_xTimerPendFunctionCall 1 +#define INCLUDE_xTaskAbortDelay 1 +#define INCLUDE_xTaskGetHandle 1 +#define INCLUDE_xTaskResumeFromISR 1 +#define INCLUDE_xQueueGetMutexHolder 1 +#define INCLUDE_vApplicationStackOverflowHook 0 +#define INCLUDE_vApplicationTickHook 1 +#define INCLUDE_vApplicationMallocFailedHook 1 + + +#define LIB_PICO_MULTICORE 1 +/* A header file that defines trace macro can be included here. */ + +#endif /* FREERTOS_CONFIG_H */ + diff --git a/examples/ex4_mutex/main.c b/examples/ex4_mutex/main.c new file mode 100644 index 0000000..3de2841 --- /dev/null +++ b/examples/ex4_mutex/main.c @@ -0,0 +1,58 @@ +#include +#include "pico/stdlib.h" +#include "FreeRTOS.h" +#include "task.h" +#include "semphr.h" + +SemaphoreHandle_t xMutex; + +void taskA(void *params) { + while (1) { + // Attempt to take the mutex, waiting indefinitely if necessary + if (xSemaphoreTake(xMutex, portMAX_DELAY) == pdTRUE) { + // Critical section: Access the shared resource + printf("Task A: Accessing shared resource\n"); + vTaskDelay(500 / portTICK_PERIOD_MS); // Simulate work by delaying + + // Release the mutex after finishing work + xSemaphoreGive(xMutex); + } + + vTaskDelay(1000 / portTICK_PERIOD_MS); // Delay before trying to access again + } +} + +void taskB(void *params) { + while (1) { + // Attempt to take the mutex, waiting indefinitely if necessary + if (xSemaphoreTake(xMutex, portMAX_DELAY) == pdTRUE) { + // Critical section: Access the shared resource + printf("Task B: Accessing shared resource\n"); + vTaskDelay(500 / portTICK_PERIOD_MS); // Simulate work by delaying + + // Release the mutex after finishing work + xSemaphoreGive(xMutex); + } + + vTaskDelay(1500 / portTICK_PERIOD_MS); // Delay before trying to access again + } +} + +int main() { + stdio_init_all(); // Initialize stdio over USB + + // Create a semaphore + xMutex = xSemaphoreCreateMutex(); + if (xMutex == NULL) { + printf("Failed to create mutex\n"); + while (1); // Loop forever if mutex creation fails + } + + xTaskCreate(taskA, "Task A", 1024, NULL, 1, NULL); + xTaskCreate(taskB, "Task B", 1024, NULL, 1, NULL); + + vTaskStartScheduler(); // Start FreeRTOS scheduler + + for (;;); +} + diff --git a/examples/ex5_metrics/CMakeLists.txt b/examples/ex5_metrics/CMakeLists.txt new file mode 100644 index 0000000..30d97ef --- /dev/null +++ b/examples/ex5_metrics/CMakeLists.txt @@ -0,0 +1,52 @@ +cmake_minimum_required(VERSION 3.13) + +if(NOT DEFINED LIB_ROOT) + set(LIB_ROOT ${CMAKE_SOURCE_DIR}/../..) +endif(NOT DEFINED LIB_ROOT) + +set(PICO_SDK_PATH ${LIB_ROOT}/submodules/pico-sdk/) +include( ${LIB_ROOT}/submodules/pico-sdk/external/pico_sdk_import.cmake) + +# Pull in FreeRTOS +include(${LIB_ROOT}/submodules/FreeRTOS-Kernel/portable/ThirdParty/GCC/RP2040/FreeRTOS_Kernel_import.cmake) + +set(PICOTOOL_FETCH_FROM_GIT_PATH ${LIB_ROOT}/submodules/picotool/) + +# Include the Pico SDK (make sure to use the correct path to pico_sdk_import.cmake) +include(FetchContent) + +# Define the project +set(PROJECT ex5_metrics) +project(${PROJECT} C CXX ASM) + +# Initialize the Pico SDK +pico_sdk_init() + +include_directories( + ${LIB_ROOT}/submodules/pico-sdk/src/rp2040/hardware_structs/include/ + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/include/ + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/portable/ThirdParty/GCC/RP2040/include/ + include/ + ${LIB_ROOT}/submodules/pico-sdk/src/rp2_common/hardware_exception/include/ + ${LIB_ROOT}/submodules/pico-sdk/src/rp2_common/pico_multicore/include/ +) + +# Create the executable +add_executable(${PROJECT} main.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/tasks.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/queue.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/list.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/portable/ThirdParty/GCC/RP2040/port.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/portable/MemMang/heap_3.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/event_groups.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/timers.c + ${LIB_ROOT}/submodules/pico-sdk/src/rp2_common/hardware_exception/exception.c +) + +# Link against FreeRTOS, Pico SDK, and freertos_config +target_link_libraries(${PROJECT} pico_stdlib) +pico_add_extra_outputs(${PROJECT}) + +pico_enable_stdio_usb( ${PROJECT} 1) +pico_enable_stdio_uart(${PROJECT} 0) + diff --git a/examples/ex5_metrics/include/FreeRTOSConfig.h b/examples/ex5_metrics/include/FreeRTOSConfig.h new file mode 100644 index 0000000..fd356b4 --- /dev/null +++ b/examples/ex5_metrics/include/FreeRTOSConfig.h @@ -0,0 +1,145 @@ +/* + * FreeRTOS V202212.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 + * + */ + +#ifndef FREERTOS_CONFIG_H +#define FREERTOS_CONFIG_H + +/*----------------------------------------------------------- + * Application specific definitions. + * + * These definitions should be adjusted for your particular hardware and + * application requirements. + * + * THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE + * FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE. + * + * See http://www.freertos.org/a00110.html + *----------------------------------------------------------*/ + +/* Scheduler Related */ +#define configUSE_PREEMPTION 1 +#define configUSE_TICKLESS_IDLE 0 +#define configUSE_IDLE_HOOK 0 +#define configUSE_TICK_HOOK 0 +#define configTICK_RATE_HZ ( ( TickType_t ) 1000 ) +#define configMAX_PRIORITIES 32 +#define configMINIMAL_STACK_SIZE ( configSTACK_DEPTH_TYPE ) 256 +#define configUSE_16_BIT_TICKS 0 + +#define configIDLE_SHOULD_YIELD 1 + +/* Synchronization Related */ +#define configUSE_MUTEXES 1 +#define configUSE_RECURSIVE_MUTEXES 1 +#define configUSE_APPLICATION_TASK_TAG 0 +#define configUSE_COUNTING_SEMAPHORES 1 +#define configQUEUE_REGISTRY_SIZE 8 +#define configUSE_QUEUE_SETS 1 +#define configUSE_TIME_SLICING 1 +#define configUSE_NEWLIB_REENTRANT 0 +#define configENABLE_BACKWARD_COMPATIBILITY 0 +#define configNUM_THREAD_LOCAL_STORAGE_POINTERS 5 + +/* System */ +#define configSTACK_DEPTH_TYPE uint32_t +#define configMESSAGE_BUFFER_LENGTH_TYPE size_t + +/* Memory allocation related definitions. */ +#define configSUPPORT_STATIC_ALLOCATION 0 +#define configSUPPORT_DYNAMIC_ALLOCATION 1 +#define configTOTAL_HEAP_SIZE (128*1024) +#define configAPPLICATION_ALLOCATED_HEAP 0 + +/* Hook function related definitions. */ +#define configCHECK_FOR_STACK_OVERFLOW 0 +#define configUSE_MALLOC_FAILED_HOOK 0 +#define configUSE_DAEMON_TASK_STARTUP_HOOK 0 + +/* Run time and task stats gathering related definitions. */ +#define configGENERATE_RUN_TIME_STATS 1 +#define configUSE_STATS_FORMATTING_FUNCTIONS 1 +#define portGET_RUN_TIME_COUNTER_VALUE() (getRunTimeCounterValue()) +#define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() // No setup needed on RP2040 +#define configUSE_TRACE_FACILITY 1 + +/* Co-routine related definitions. */ +#define configUSE_CO_ROUTINES 0 +#define configMAX_CO_ROUTINE_PRIORITIES 1 + +/* Software timer related definitions. */ +#define configUSE_TIMERS 1 +#define configTIMER_TASK_PRIORITY ( configMAX_PRIORITIES - 1 ) +#define configTIMER_QUEUE_LENGTH 10 +#define configTIMER_TASK_STACK_DEPTH 1024 + +/* Interrupt nesting behaviour configuration. */ +/* +#define configKERNEL_INTERRUPT_PRIORITY [dependent of processor] +#define configMAX_SYSCALL_INTERRUPT_PRIORITY [dependent on processor and application] +#define configMAX_API_CALL_INTERRUPT_PRIORITY [dependent on processor and application] +*/ + +/* SMP port only */ +#define configNUMBER_OF_CORES 1 +#define configTICK_CORE 0 +#define configRUN_MULTIPLE_PRIORITIES 0 + +/* RP2040 specific */ +#define configSUPPORT_PICO_SYNC_INTEROP 1 +#define configSUPPORT_PICO_TIME_INTEROP 1 + +#include +/* Define to trap errors during development. */ +#define configASSERT(x) assert(x) + +/* Set the following definitions to 1 to include the API function, or zero +to exclude the API function. */ +#define INCLUDE_vTaskPrioritySet 1 +#define INCLUDE_uxTaskPriorityGet 1 +#define INCLUDE_vTaskDelete 1 +#define INCLUDE_vTaskSuspend 1 +#define INCLUDE_vTaskDelayUntil 1 +#define INCLUDE_vTaskDelay 1 +#define INCLUDE_xTaskGetSchedulerState 1 +#define INCLUDE_xTaskGetCurrentTaskHandle 1 +#define INCLUDE_uxTaskGetStackHighWaterMark 1 +#define INCLUDE_xTaskGetIdleTaskHandle 1 +#define INCLUDE_eTaskGetState 1 +#define INCLUDE_xTimerPendFunctionCall 1 +#define INCLUDE_xTaskAbortDelay 1 +#define INCLUDE_xTaskGetHandle 1 +#define INCLUDE_xTaskResumeFromISR 1 +#define INCLUDE_xQueueGetMutexHolder 1 +#define INCLUDE_vApplicationStackOverflowHook 0 +#define INCLUDE_vApplicationTickHook 1 +#define INCLUDE_vApplicationMallocFailedHook 1 + + +#define LIB_PICO_MULTICORE 1 +/* A header file that defines trace macro can be included here. */ + +#endif /* FREERTOS_CONFIG_H */ + diff --git a/examples/ex5_metrics/main.c b/examples/ex5_metrics/main.c new file mode 100644 index 0000000..c175a32 --- /dev/null +++ b/examples/ex5_metrics/main.c @@ -0,0 +1,73 @@ +#include +#include "pico/stdlib.h" +#include "pico/time.h" +#include "FreeRTOS.h" +#include "task.h" +#include "semphr.h" + +SemaphoreHandle_t xMutex; + +uint32_t getRunTimeCounterValue() { + return to_ms_since_boot(get_absolute_time()); +} + +void taskA(void *params) { + while (1) { + // Attempt to take the mutex, waiting indefinitely if necessary + if (xSemaphoreTake(xMutex, portMAX_DELAY) == pdTRUE) { + // Critical section: Access the shared resource + printf("Task A: Accessing shared resource\n"); + vTaskDelay(500 / portTICK_PERIOD_MS); // Simulate work by delaying + + // Release the mutex after finishing work + xSemaphoreGive(xMutex); + } + + vTaskDelay(1000 / portTICK_PERIOD_MS); // Delay before trying to access again + } +} + +void taskB(void *params) { + while (1) { + // Attempt to take the mutex, waiting indefinitely if necessary + if (xSemaphoreTake(xMutex, portMAX_DELAY) == pdTRUE) { + // Critical section: Access the shared resource + printf("Task B: Accessing shared resource\n"); + vTaskDelay(500 / portTICK_PERIOD_MS); // Simulate work by delaying + + // Release the mutex after finishing work + xSemaphoreGive(xMutex); + } + + vTaskDelay(1500 / portTICK_PERIOD_MS); // Delay before trying to access again + } +} + +void printTaskStats(void *params) { + char buffer[256]; + while (1) { + vTaskGetRunTimeStats(buffer); + printf("Task run-time stats:\n%s\n", buffer); + vTaskDelay(pdMS_TO_TICKS(5000)); // Print stats every 5 seconds + } +} + +int main() { + stdio_init_all(); // Initialize stdio over USB + + // Create a semaphore + xMutex = xSemaphoreCreateMutex(); + if (xMutex == NULL) { + printf("Failed to create mutex\n"); + while (1); // Loop forever if mutex creation fails + } + + xTaskCreate(taskA, "Task A", 1024, NULL, 1, NULL); + xTaskCreate(taskB, "Task B", 1024, NULL, 1, NULL); + xTaskCreate(printTaskStats, "Stats Printer", 1024, NULL, 1, NULL); + + vTaskStartScheduler(); // Start FreeRTOS scheduler + + for (;;); +} + diff --git a/examples/ex6_reboot_to_bootloader/CMakeLists.txt b/examples/ex6_reboot_to_bootloader/CMakeLists.txt new file mode 100644 index 0000000..6ab4a7e --- /dev/null +++ b/examples/ex6_reboot_to_bootloader/CMakeLists.txt @@ -0,0 +1,52 @@ +cmake_minimum_required(VERSION 3.13) + +if(NOT DEFINED LIB_ROOT) + set(LIB_ROOT ${CMAKE_SOURCE_DIR}/../..) +endif(NOT DEFINED LIB_ROOT) + +set(PICO_SDK_PATH ${LIB_ROOT}/submodules/pico-sdk/) +include( ${LIB_ROOT}/submodules/pico-sdk/external/pico_sdk_import.cmake) + +# Pull in FreeRTOS +include(${LIB_ROOT}/submodules/FreeRTOS-Kernel/portable/ThirdParty/GCC/RP2040/FreeRTOS_Kernel_import.cmake) + +set(PICOTOOL_FETCH_FROM_GIT_PATH ${LIB_ROOT}/submodules/picotool/) + +# Include the Pico SDK (make sure to use the correct path to pico_sdk_import.cmake) +include(FetchContent) + +# Define the project +set(PROJECT ex6_reboot_to_bootloader) +project(${PROJECT} C CXX ASM) + +# Initialize the Pico SDK +pico_sdk_init() + +include_directories( + ${LIB_ROOT}/submodules/pico-sdk/src/rp2040/hardware_structs/include/ + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/include/ + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/portable/ThirdParty/GCC/RP2040/include/ + include/ + ${LIB_ROOT}/submodules/pico-sdk/src/rp2_common/hardware_exception/include/ + ${LIB_ROOT}/submodules/pico-sdk/src/rp2_common/pico_multicore/include/ +) + +# Create the executable +add_executable(${PROJECT} main.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/tasks.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/queue.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/list.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/portable/ThirdParty/GCC/RP2040/port.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/portable/MemMang/heap_3.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/event_groups.c + ${LIB_ROOT}/submodules/FreeRTOS-Kernel/timers.c + ${LIB_ROOT}/submodules/pico-sdk/src/rp2_common/hardware_exception/exception.c +) + +# Link against FreeRTOS, Pico SDK, and freertos_config +target_link_libraries(${PROJECT} pico_stdlib) +pico_add_extra_outputs(${PROJECT}) + +pico_enable_stdio_usb( ${PROJECT} 1) +pico_enable_stdio_uart(${PROJECT} 0) + diff --git a/examples/ex6_reboot_to_bootloader/include/FreeRTOSConfig.h b/examples/ex6_reboot_to_bootloader/include/FreeRTOSConfig.h new file mode 100644 index 0000000..fd356b4 --- /dev/null +++ b/examples/ex6_reboot_to_bootloader/include/FreeRTOSConfig.h @@ -0,0 +1,145 @@ +/* + * FreeRTOS V202212.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 + * + */ + +#ifndef FREERTOS_CONFIG_H +#define FREERTOS_CONFIG_H + +/*----------------------------------------------------------- + * Application specific definitions. + * + * These definitions should be adjusted for your particular hardware and + * application requirements. + * + * THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE + * FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE. + * + * See http://www.freertos.org/a00110.html + *----------------------------------------------------------*/ + +/* Scheduler Related */ +#define configUSE_PREEMPTION 1 +#define configUSE_TICKLESS_IDLE 0 +#define configUSE_IDLE_HOOK 0 +#define configUSE_TICK_HOOK 0 +#define configTICK_RATE_HZ ( ( TickType_t ) 1000 ) +#define configMAX_PRIORITIES 32 +#define configMINIMAL_STACK_SIZE ( configSTACK_DEPTH_TYPE ) 256 +#define configUSE_16_BIT_TICKS 0 + +#define configIDLE_SHOULD_YIELD 1 + +/* Synchronization Related */ +#define configUSE_MUTEXES 1 +#define configUSE_RECURSIVE_MUTEXES 1 +#define configUSE_APPLICATION_TASK_TAG 0 +#define configUSE_COUNTING_SEMAPHORES 1 +#define configQUEUE_REGISTRY_SIZE 8 +#define configUSE_QUEUE_SETS 1 +#define configUSE_TIME_SLICING 1 +#define configUSE_NEWLIB_REENTRANT 0 +#define configENABLE_BACKWARD_COMPATIBILITY 0 +#define configNUM_THREAD_LOCAL_STORAGE_POINTERS 5 + +/* System */ +#define configSTACK_DEPTH_TYPE uint32_t +#define configMESSAGE_BUFFER_LENGTH_TYPE size_t + +/* Memory allocation related definitions. */ +#define configSUPPORT_STATIC_ALLOCATION 0 +#define configSUPPORT_DYNAMIC_ALLOCATION 1 +#define configTOTAL_HEAP_SIZE (128*1024) +#define configAPPLICATION_ALLOCATED_HEAP 0 + +/* Hook function related definitions. */ +#define configCHECK_FOR_STACK_OVERFLOW 0 +#define configUSE_MALLOC_FAILED_HOOK 0 +#define configUSE_DAEMON_TASK_STARTUP_HOOK 0 + +/* Run time and task stats gathering related definitions. */ +#define configGENERATE_RUN_TIME_STATS 1 +#define configUSE_STATS_FORMATTING_FUNCTIONS 1 +#define portGET_RUN_TIME_COUNTER_VALUE() (getRunTimeCounterValue()) +#define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() // No setup needed on RP2040 +#define configUSE_TRACE_FACILITY 1 + +/* Co-routine related definitions. */ +#define configUSE_CO_ROUTINES 0 +#define configMAX_CO_ROUTINE_PRIORITIES 1 + +/* Software timer related definitions. */ +#define configUSE_TIMERS 1 +#define configTIMER_TASK_PRIORITY ( configMAX_PRIORITIES - 1 ) +#define configTIMER_QUEUE_LENGTH 10 +#define configTIMER_TASK_STACK_DEPTH 1024 + +/* Interrupt nesting behaviour configuration. */ +/* +#define configKERNEL_INTERRUPT_PRIORITY [dependent of processor] +#define configMAX_SYSCALL_INTERRUPT_PRIORITY [dependent on processor and application] +#define configMAX_API_CALL_INTERRUPT_PRIORITY [dependent on processor and application] +*/ + +/* SMP port only */ +#define configNUMBER_OF_CORES 1 +#define configTICK_CORE 0 +#define configRUN_MULTIPLE_PRIORITIES 0 + +/* RP2040 specific */ +#define configSUPPORT_PICO_SYNC_INTEROP 1 +#define configSUPPORT_PICO_TIME_INTEROP 1 + +#include +/* Define to trap errors during development. */ +#define configASSERT(x) assert(x) + +/* Set the following definitions to 1 to include the API function, or zero +to exclude the API function. */ +#define INCLUDE_vTaskPrioritySet 1 +#define INCLUDE_uxTaskPriorityGet 1 +#define INCLUDE_vTaskDelete 1 +#define INCLUDE_vTaskSuspend 1 +#define INCLUDE_vTaskDelayUntil 1 +#define INCLUDE_vTaskDelay 1 +#define INCLUDE_xTaskGetSchedulerState 1 +#define INCLUDE_xTaskGetCurrentTaskHandle 1 +#define INCLUDE_uxTaskGetStackHighWaterMark 1 +#define INCLUDE_xTaskGetIdleTaskHandle 1 +#define INCLUDE_eTaskGetState 1 +#define INCLUDE_xTimerPendFunctionCall 1 +#define INCLUDE_xTaskAbortDelay 1 +#define INCLUDE_xTaskGetHandle 1 +#define INCLUDE_xTaskResumeFromISR 1 +#define INCLUDE_xQueueGetMutexHolder 1 +#define INCLUDE_vApplicationStackOverflowHook 0 +#define INCLUDE_vApplicationTickHook 1 +#define INCLUDE_vApplicationMallocFailedHook 1 + + +#define LIB_PICO_MULTICORE 1 +/* A header file that defines trace macro can be included here. */ + +#endif /* FREERTOS_CONFIG_H */ + diff --git a/examples/ex6_reboot_to_bootloader/main.c b/examples/ex6_reboot_to_bootloader/main.c new file mode 100644 index 0000000..5d67cac --- /dev/null +++ b/examples/ex6_reboot_to_bootloader/main.c @@ -0,0 +1,80 @@ +#include +#include "pico/stdlib.h" +#include "pico/time.h" +#include "pico/bootrom.h" +#include "FreeRTOS.h" +#include "task.h" +#include "semphr.h" + +SemaphoreHandle_t xMutex; + +uint32_t getRunTimeCounterValue() { + return to_ms_since_boot(get_absolute_time()); +} + +void taskA(void *params) { + while (1) { + // Attempt to take the mutex, waiting indefinitely if necessary + if (xSemaphoreTake(xMutex, portMAX_DELAY) == pdTRUE) { + // Critical section: Access the shared resource + printf("Task A: Accessing shared resource\n"); + vTaskDelay(500 / portTICK_PERIOD_MS); // Simulate work by delaying + + // Release the mutex after finishing work + xSemaphoreGive(xMutex); + } + + vTaskDelay(1000 / portTICK_PERIOD_MS); // Delay before trying to access again + } +} + +void taskB(void *params) { + while (1) { + // Attempt to take the mutex, waiting indefinitely if necessary + if (xSemaphoreTake(xMutex, portMAX_DELAY) == pdTRUE) { + // Critical section: Access the shared resource + printf("Task B: Accessing shared resource\n"); + vTaskDelay(500 / portTICK_PERIOD_MS); // Simulate work by delaying + + // Release the mutex after finishing work + xSemaphoreGive(xMutex); + } + + vTaskDelay(1500 / portTICK_PERIOD_MS); // Delay before trying to access again + } +} + +void printTaskStats(void *params) { + char buffer[256]; + while (1) { + vTaskGetRunTimeStats(buffer); + printf("Task run-time stats:\n%s\n", buffer); + vTaskDelay(pdMS_TO_TICKS(5000)); // Print stats every 5 seconds + } +} + +void theFinalCountdown(void *params) { + vTaskDelay(15000 / portTICK_PERIOD_MS); + reset_usb_boot(0,0); +} + +int main() { + stdio_init_all(); // Initialize stdio over USB + + // Create a semaphore + xMutex = xSemaphoreCreateMutex(); + if (xMutex == NULL) { + printf("Failed to create mutex\n"); + while (1); // Loop forever if mutex creation fails + } + + xTaskCreate(taskA, "Task A", 1024, NULL, 1, NULL); + xTaskCreate(taskB, "Task B", 1024, NULL, 1, NULL); + xTaskCreate(printTaskStats, "Stats Printer", 1024, NULL, 1, NULL); + xTaskCreate(theFinalCountdown, "The Final Countdown", 1024, NULL, 1, NULL); + + vTaskStartScheduler(); // Start FreeRTOS scheduler + + for (;;); +} +