Skip to content

More useful examples for FreeRTOS #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#ifndef FREERTOS_CONFIG_H
#define FREERTOS_CONFIG_H

#include <io.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.
*----------------------------------------------------------*/

#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 */
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*
* 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
103 changes: 103 additions & 0 deletions libraries/FreeRTOS/examples/ex_01_simple_multitask/setup.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* \file setup.cpp
*/
#include <stdio.h>
#include <io.h>
#include <signal.h>
#include <stdint.h>
#include <stdlib.h>
#include <WProgram.h>

/* 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
52 changes: 52 additions & 0 deletions libraries/FreeRTOS/examples/ex_02_queue_multitask/FreeRTOSConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#ifndef FREERTOS_CONFIG_H
#define FREERTOS_CONFIG_H

#include <io.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.
*----------------------------------------------------------*/

#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 */
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*
* 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
111 changes: 111 additions & 0 deletions libraries/FreeRTOS/examples/ex_02_queue_multitask/setup.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**
* \file setup.cpp
*/
#include <stdio.h>
#include <io.h>
#include <signal.h>
#include <stdint.h>
#include <WProgram.h>

/* 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
Loading