diff --git a/libraries/FreeRTOS/examples/ex_01_simple_multitask/FreeRTOSConfig.h b/libraries/FreeRTOS/examples/ex_01_simple_multitask/FreeRTOSConfig.h new file mode 100644 index 000000000..28ad3ed10 --- /dev/null +++ b/libraries/FreeRTOS/examples/ex_01_simple_multitask/FreeRTOSConfig.h @@ -0,0 +1,52 @@ +#ifndef FREERTOS_CONFIG_H +#define FREERTOS_CONFIG_H + +#include + +/*----------------------------------------------------------- + * 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. + *----------------------------------------------------------*/ + +#define configUSE_PREEMPTION 1 +#define configUSE_IDLE_HOOK 0 +#define configUSE_TICK_HOOK 0 +#define configCPU_CLOCK_HZ ( ( unsigned portLONG ) 8000000 ) /* Clock setup from main.c in the demo application. */ +#define configTICK_RATE_HZ ( ( portTickType ) 1000 ) +#define configMAX_PRIORITIES ( ( unsigned portBASE_TYPE ) 4 ) +#define configMINIMAL_STACK_SIZE ( ( unsigned portSHORT ) 200 ) +#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 6000 ) ) +#define configMAX_TASK_NAME_LEN ( 8 ) +#define configUSE_TRACE_FACILITY 0 +#define configUSE_16_BIT_TICKS 1 +#define configIDLE_SHOULD_YIELD 1 + +/* Co-routine definitions. */ +#define configUSE_CO_ROUTINES 0 +#define configMAX_CO_ROUTINE_PRIORITIES ( 2 ) + +/* Set the following definitions to 1 to include the API function, or zero +to exclude the API function. */ + +#define INCLUDE_vTaskPrioritySet 0 +#define INCLUDE_uxTaskPriorityGet 0 +#define INCLUDE_vTaskDelete 1 +#define INCLUDE_vTaskCleanUpResources 0 +#define INCLUDE_vTaskSuspend 1 +#define INCLUDE_vTaskDelayUntil 1 +#define INCLUDE_vTaskDelay 1 + + + + + + + +#endif /* FREERTOS_CONFIG_H */ diff --git a/libraries/FreeRTOS/examples/ex_01_simple_multitask/ex_01_simple_multitask.pde b/libraries/FreeRTOS/examples/ex_01_simple_multitask/ex_01_simple_multitask.pde new file mode 100644 index 000000000..c04e24413 --- /dev/null +++ b/libraries/FreeRTOS/examples/ex_01_simple_multitask/ex_01_simple_multitask.pde @@ -0,0 +1,25 @@ +/***************************************************************************** + * + * FreeRTOS Simple Multitask example #01 + * + * Based on code from: + * http://senstools.gforge.inria.fr/doku.php?id=os:freertos:examples#simple_multitask + * + * Ported to Maple by maniacbug + * + * This first example is a simple application consisting of two independent + * tasks running concurrently. The goal is to show how to setup a multitask + * environment, define and create the tasks, start a preemptive scheduler, + * as well as to show some task time control functions. + * + * In this design, there are two independent tasks: the first called ’LED’ + * just blinks the LEDs at a fixed rate, and the second called ’Temperature’ + * periodically checks a sensor for a measurement and displays the result + * on the USB Serial. Both tasks will run at the same priority, and the + * scheduler might preempt the tasks for letting the other one execute. + * + * See .cpp files for the code (PDE is just here to appease the IDE). + */ + +#include "MapleFreeRTOS.h" +// vim:cin:ai:sts=4 sw=4 ft=cpp diff --git a/libraries/FreeRTOS/examples/ex_01_simple_multitask/setup.cpp b/libraries/FreeRTOS/examples/ex_01_simple_multitask/setup.cpp new file mode 100644 index 000000000..f80f723a7 --- /dev/null +++ b/libraries/FreeRTOS/examples/ex_01_simple_multitask/setup.cpp @@ -0,0 +1,103 @@ +/** + * \file setup.cpp + */ +#include +#include +#include +#include +#include +#include + +/* Scheduler includes. */ +#include "MapleFreeRTOS.h" + +/* Declarations */ +static void vLEDTask(void* pvParameters); +static void vTempTask(void* pvParameters); + +/* Pins used */ +const int sensor_pin = 12; + +/** + * The setup function. + */ +void setup( void ) +{ + // Set up the LED to steady on + pinMode(BOARD_LED_PIN, OUTPUT); + digitalWrite(BOARD_LED_PIN, HIGH); + + // Setup the button as input + pinMode(BOARD_BUTTON_PIN, INPUT); + digitalWrite(BOARD_BUTTON_PIN, HIGH); + + SerialUSB.begin(); + SerialUSB.println("Press any key to begin"); + SerialUSB.read(); + SerialUSB.println("FreeRTOS Simple Multitask example #01"); + + pinMode(sensor_pin,INPUT_ANALOG); + + /* Add the two tasks to the scheduler */ + xTaskCreate(vLEDTask, (const signed char*)"LED", configMINIMAL_STACK_SIZE, NULL, 1, NULL ); + xTaskCreate(vTempTask, (const signed char*)"Temperature", configMINIMAL_STACK_SIZE, NULL, 1, NULL ); + + /* Start the scheduler. */ + vTaskStartScheduler(); +} + +void loop(void) +{ +} + +/** + * The LED task function. + * It increments a variable and displays its value on the 3 LEDs, + * then waits for a given delay and start over again. + * \param pvParameter NULL is passed as parameter. + */ +static void vLEDTask(void* pvParameters) +{ + /* The LEDs are updated every 200 ticks, about 200 ms */ + const portTickType blinkDelay = 200; + + /* Infinite loop */ + while (1) + { + toggleLED(); + + /* Block the task for the defined time */ + vTaskDelay(blinkDelay); + } +} +/** + * The analog measurement task function. + * It reads the analog value from sensor pin and print it + * on the serial port. + * \param pvParameters NULL is passed, unused here. + */ +static void vTempTask(void* pvParameters) +{ + uint16_t samplecount = 0; + portTickType xLastWakeTime = xTaskGetTickCount(); + /* The display period is 1000 ticks, about 1s */ + const portTickType xWakePeriod = 1000; + + /* Infinite loop */ + while(1) + { + /* Read the sensor and increment the sample count */ + samplecount++; + + /* Print the result on the uart port */ + //iprintf("Sample #%u: reading = %u\r\n", samplecount, analogRead(sensor_pin)); + SerialUSB.print("Sample #"); + SerialUSB.print(samplecount); + SerialUSB.print(": reading = "); + SerialUSB.println(analogRead(sensor_pin)); + + /* Block until xWakePeriod ticks since previous call */ + vTaskDelayUntil(&xLastWakeTime, xWakePeriod); + } +} +// vim:cin:ai:sts=4 sw=4 diff --git a/libraries/FreeRTOS/examples/ex_02_queue_multitask/FreeRTOSConfig.h b/libraries/FreeRTOS/examples/ex_02_queue_multitask/FreeRTOSConfig.h new file mode 100644 index 000000000..28ad3ed10 --- /dev/null +++ b/libraries/FreeRTOS/examples/ex_02_queue_multitask/FreeRTOSConfig.h @@ -0,0 +1,52 @@ +#ifndef FREERTOS_CONFIG_H +#define FREERTOS_CONFIG_H + +#include + +/*----------------------------------------------------------- + * 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. + *----------------------------------------------------------*/ + +#define configUSE_PREEMPTION 1 +#define configUSE_IDLE_HOOK 0 +#define configUSE_TICK_HOOK 0 +#define configCPU_CLOCK_HZ ( ( unsigned portLONG ) 8000000 ) /* Clock setup from main.c in the demo application. */ +#define configTICK_RATE_HZ ( ( portTickType ) 1000 ) +#define configMAX_PRIORITIES ( ( unsigned portBASE_TYPE ) 4 ) +#define configMINIMAL_STACK_SIZE ( ( unsigned portSHORT ) 200 ) +#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 6000 ) ) +#define configMAX_TASK_NAME_LEN ( 8 ) +#define configUSE_TRACE_FACILITY 0 +#define configUSE_16_BIT_TICKS 1 +#define configIDLE_SHOULD_YIELD 1 + +/* Co-routine definitions. */ +#define configUSE_CO_ROUTINES 0 +#define configMAX_CO_ROUTINE_PRIORITIES ( 2 ) + +/* Set the following definitions to 1 to include the API function, or zero +to exclude the API function. */ + +#define INCLUDE_vTaskPrioritySet 0 +#define INCLUDE_uxTaskPriorityGet 0 +#define INCLUDE_vTaskDelete 1 +#define INCLUDE_vTaskCleanUpResources 0 +#define INCLUDE_vTaskSuspend 1 +#define INCLUDE_vTaskDelayUntil 1 +#define INCLUDE_vTaskDelay 1 + + + + + + + +#endif /* FREERTOS_CONFIG_H */ diff --git a/libraries/FreeRTOS/examples/ex_02_queue_multitask/ex_02_queue_multitask.pde b/libraries/FreeRTOS/examples/ex_02_queue_multitask/ex_02_queue_multitask.pde new file mode 100644 index 000000000..797d7a200 --- /dev/null +++ b/libraries/FreeRTOS/examples/ex_02_queue_multitask/ex_02_queue_multitask.pde @@ -0,0 +1,30 @@ +/***************************************************************************** + * + * FreeRTOS Intertask Communication example #02 + * + * Based on code from: + * http://senstools.gforge.inria.fr/doku.php?id=os:freertos:examples#intertask_communication + * + * Ported to Maple by maniacbug + * + * The second example is a simple application that consists of two tasks + * running concurrently and communicating with each other. The goal is to show + * how to make a task send data to another using a queue. + * + * In this design, there are two tasks. The first, called 'Temperature' is + * really similar to the homonym from the previous example, but instead of + * printing the measure value every time it reads the sensor, it will send + * the data to the other task, called 'Print'. This task will just wait + * until some data arrives, and print it on the USB Serial link. + * + * The 'Print' task enters its infinite loop where it waits for data to be + * available on the queue, and prints it each time it reads some. The + * 'Temperature' task starts the sampling, and enters the loop where it + * reads the measure value, places it in the queue, and waits for a given + * time. + * + * See .cpp files for the code (PDE is just here to appease the IDE). + */ + +#include "MapleFreeRTOS.h" +// vim:cin:ai:sts=4 sw=4 ft=cpp diff --git a/libraries/FreeRTOS/examples/ex_02_queue_multitask/setup.cpp b/libraries/FreeRTOS/examples/ex_02_queue_multitask/setup.cpp new file mode 100644 index 000000000..03c256a08 --- /dev/null +++ b/libraries/FreeRTOS/examples/ex_02_queue_multitask/setup.cpp @@ -0,0 +1,111 @@ +/** + * \file setup.cpp + */ +#include +#include +#include +#include +#include + +/* Scheduler includes. */ +#include "MapleFreeRTOS.h" + +/* Function Prototypes */ +static void vPrintTask(void* pvParameters); +static void vTempTask(void* pvParameters); + +/* Global Variables */ +xQueueHandle xQueue; + +const int sensor_pin = 12; + +/** + * The main function. + */ +void setup( void ) +{ + // Setup the LED to steady on + pinMode(BOARD_LED_PIN, OUTPUT); + digitalWrite(BOARD_LED_PIN, HIGH); + + // Setup the button as input + pinMode(BOARD_BUTTON_PIN, INPUT); + digitalWrite(BOARD_BUTTON_PIN, HIGH); + + // Setup the sensor pin as an analog input + pinMode(sensor_pin,INPUT_ANALOG); + + SerialUSB.begin(); + SerialUSB.println("Press any key to continue"); + SerialUSB.read(); + SerialUSB.println("FreeRTOS Intertask Communication example #02"); + + /* Create the Queue for communication between the tasks */ + xQueue = xQueueCreate( 5, sizeof(uint16_t) ); + + /* Add the two tasks to the scheduler */ + xTaskCreate(vPrintTask, (const signed char*)"Print", configMINIMAL_STACK_SIZE, NULL, 1, NULL ); + xTaskCreate(vTempTask, (const signed char*)"Temperature", configMINIMAL_STACK_SIZE, NULL, 1, NULL ); + + /* Start the scheduler. */ + vTaskStartScheduler(); +} + +void loop( void ) +{ +} + +/** + * The Print task function. + * It waits until a sensor value has been put in the queue, + * and prints its value on the uart port. + * \param pvParameter NULL is passed as parameter. + */ +static void vPrintTask(void* pvParameters) +{ + uint16_t temp_meas; + uint16_t samplecount = 0; + + /* Infinite loop */ + while (1) + { + /* Wait until an element is received from the queue */ + if (xQueueReceive(xQueue, &temp_meas, portMAX_DELAY)) + { + samplecount++; + /* Print the result on the uart port */ + //iprintf("Sample #%u: reading = %u\r\n", samplecount, temp_meas); + SerialUSB.print("Sample #"); + SerialUSB.print(samplecount); + SerialUSB.print(": reading = "); + SerialUSB.println(temp_meas); + } + } +} + +/** + * The sensor measurement task function. + * It reads the value from the sensor and puts it on the queue + * \param pvParameters NULL is passed, unused here. + */ +static void vTempTask(void* pvParameters) +{ + portTickType xLastWakeTime = xTaskGetTickCount(); + + /* The sample period is 1000 ticks, about 1s */ + const portTickType xWakePeriod = 1000; + + /* Infinite loop */ + while(1) + { + /* Read the sensor */ + uint16_t msb = analogRead(sensor_pin); + + /* Put the read value on the queue */ + xQueueSendToBack(xQueue, &msb, 0); + + /* Block until xWakePeriod(=1000) ticks since previous call */ + vTaskDelayUntil(&xLastWakeTime, xWakePeriod); + } +} +// vim:cin:ai:sts=4 sw=4 diff --git a/libraries/FreeRTOS/examples/ex_03_semphr_synchro/FreeRTOSConfig.h b/libraries/FreeRTOS/examples/ex_03_semphr_synchro/FreeRTOSConfig.h new file mode 100644 index 000000000..28ad3ed10 --- /dev/null +++ b/libraries/FreeRTOS/examples/ex_03_semphr_synchro/FreeRTOSConfig.h @@ -0,0 +1,52 @@ +#ifndef FREERTOS_CONFIG_H +#define FREERTOS_CONFIG_H + +#include + +/*----------------------------------------------------------- + * 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. + *----------------------------------------------------------*/ + +#define configUSE_PREEMPTION 1 +#define configUSE_IDLE_HOOK 0 +#define configUSE_TICK_HOOK 0 +#define configCPU_CLOCK_HZ ( ( unsigned portLONG ) 8000000 ) /* Clock setup from main.c in the demo application. */ +#define configTICK_RATE_HZ ( ( portTickType ) 1000 ) +#define configMAX_PRIORITIES ( ( unsigned portBASE_TYPE ) 4 ) +#define configMINIMAL_STACK_SIZE ( ( unsigned portSHORT ) 200 ) +#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 6000 ) ) +#define configMAX_TASK_NAME_LEN ( 8 ) +#define configUSE_TRACE_FACILITY 0 +#define configUSE_16_BIT_TICKS 1 +#define configIDLE_SHOULD_YIELD 1 + +/* Co-routine definitions. */ +#define configUSE_CO_ROUTINES 0 +#define configMAX_CO_ROUTINE_PRIORITIES ( 2 ) + +/* Set the following definitions to 1 to include the API function, or zero +to exclude the API function. */ + +#define INCLUDE_vTaskPrioritySet 0 +#define INCLUDE_uxTaskPriorityGet 0 +#define INCLUDE_vTaskDelete 1 +#define INCLUDE_vTaskCleanUpResources 0 +#define INCLUDE_vTaskSuspend 1 +#define INCLUDE_vTaskDelayUntil 1 +#define INCLUDE_vTaskDelay 1 + + + + + + + +#endif /* FREERTOS_CONFIG_H */ diff --git a/libraries/FreeRTOS/examples/ex_03_semphr_synchro/ex_03_semphr_synchro.pde b/libraries/FreeRTOS/examples/ex_03_semphr_synchro/ex_03_semphr_synchro.pde new file mode 100644 index 000000000..7edd82678 --- /dev/null +++ b/libraries/FreeRTOS/examples/ex_03_semphr_synchro/ex_03_semphr_synchro.pde @@ -0,0 +1,24 @@ +/***************************************************************************** + * + * FreeRTOS Task Syncrhonization example #03 + * + * Based on code from: + * http://senstools.gforge.inria.fr/doku.php?id=os:freertos:examples#task_synchronization + * + * Ported to Maple by maniacbug + * + * The third example is a simple application consisting of a single task that + * synchronizes with an interrupt service routine. The goal is to show how to + * use interrupts to synchronize FreeRTOS tasks. + * + * In this design, there is a single task, called 'LED' that will update the + * LEDs every time a the Button is pressed. The button pin is registered + * as an external intterupt, so an interrupt service routine will be called + * each time the button is pressed. This example shows how to synchronize + * the task with the callback function being called. + * + * See .cpp files for the code (PDE is just here to appease the IDE). + */ + +#include "MapleFreeRTOS.h" +// vim:cin:ai:sts=4 sw=4 ft=cpp diff --git a/libraries/FreeRTOS/examples/ex_03_semphr_synchro/setup.cpp b/libraries/FreeRTOS/examples/ex_03_semphr_synchro/setup.cpp new file mode 100644 index 000000000..840b6a44c --- /dev/null +++ b/libraries/FreeRTOS/examples/ex_03_semphr_synchro/setup.cpp @@ -0,0 +1,83 @@ +/** + * \file setup.cpp + */ +#include +#include +#include +#include +#include + +/* Scheduler includes. */ +#include "MapleFreeRTOS.h" + +/* Function Prototypes */ +static void vLEDTask(void* pvParameters); +static void irq_button(void); + +/* Global Variables */ +xSemaphoreHandle xSemaphore; + +/** + * The main function. + */ +void setup( void ) +{ + // Set up the LED to steady on + pinMode(BOARD_LED_PIN, OUTPUT); + digitalWrite(BOARD_LED_PIN, HIGH); + + // Setup the button as input + pinMode(BOARD_BUTTON_PIN, INPUT); + digitalWrite(BOARD_BUTTON_PIN, HIGH); + + // Wait until the user is watching + SerialUSB.begin(); + SerialUSB.println("Press any key to continue"); + SerialUSB.read(); + SerialUSB.println("FreeRTOS Task Syncrhonization example #03"); + + /* Get called when the button is pressed */ + attachInterrupt(BOARD_BUTTON_PIN,irq_button,FALLING); + + /* Create the Semaphore for synchronization between UART and LED task */ + vSemaphoreCreateBinary( xSemaphore) + + /* Add the only task to the scheduler */ + xTaskCreate(vLEDTask, (const signed char*)"LED", configMINIMAL_STACK_SIZE, NULL, 1, NULL ); + + /* Start the scheduler. */ + vTaskStartScheduler(); +} + +/** + * The LEDs task function. + * It waits the semaphore is given, then takes it and update the LEDs + * \param pvParameters NULL is passed, unused here. + */ +static void vLEDTask(void* pvParameters) +{ + uint16_t leds_state = 0; + + /* Infinite loop */ + while(1) + { + /* Increment the LED state */ + leds_state ++; + /* Block until the semaphore is given */ + xSemaphoreTake(xSemaphore, portMAX_DELAY); + /* update the LEDs and loop */ + toggleLED(); + } +} + +/** + * The irq callback function called when the button is pressed + * It gives the semaphore. + */ +static void irq_button(void) +{ + static portBASE_TYPE xHigherPriorityTaskWoken; + /* Give the semaphore */ + xSemaphoreGiveFromISR(xSemaphore, &xHigherPriorityTaskWoken); +} +// vim:cin:ai:sts=4 sw=4