321 lines
12 KiB
C
321 lines
12 KiB
C
|
/*
|
||
|
* FreeRTOS Kernel V10.2.1
|
||
|
* Copyright (C) 2019 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.
|
||
|
*
|
||
|
* http://www.FreeRTOS.org
|
||
|
* http://aws.amazon.com/freertos
|
||
|
*
|
||
|
* 1 tab == 4 spaces!
|
||
|
*/
|
||
|
|
||
|
/******************************************************************************
|
||
|
* NOTE 1: This project provides two demo applications. A simple blinky style
|
||
|
* project, and a more comprehensive test and demo application. The
|
||
|
* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select
|
||
|
* between the two. See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY
|
||
|
* in main.c. This file implements the comprehensive test and demo version.
|
||
|
*
|
||
|
* NOTE 2: This file only contains the source code that is specific to the
|
||
|
* full demo. Generic functions, such FreeRTOS hook functions, and functions
|
||
|
* required to configure the hardware, are defined in main.c.
|
||
|
*
|
||
|
******************************************************************************
|
||
|
*
|
||
|
* main_full() creates all the demo application tasks and software timers, then
|
||
|
* starts the scheduler. The web documentation provides more details of the
|
||
|
* standard demo application tasks, which provide no particular functionality,
|
||
|
* but do provide a good example of how to use the FreeRTOS API.
|
||
|
*
|
||
|
* In addition to the standard demo tasks, the following tasks and tests are
|
||
|
* defined and/or created within this file:
|
||
|
*
|
||
|
* "Reg test" tasks - These fill both the core registers with known values, then
|
||
|
* check that each register maintains its expected value for the lifetime of the
|
||
|
* task. Each task uses a different set of values. The reg test tasks execute
|
||
|
* with a very low priority, so get preempted very frequently. A register
|
||
|
* containing an unexpected value is indicative of an error in the context
|
||
|
* switching mechanism.
|
||
|
*
|
||
|
* "Check" task - The check executes every five seconds. It checks that all
|
||
|
* the standard demo tasks, and the register check tasks, are not only still
|
||
|
* executing, but are executing without reporting any errors. If the check task
|
||
|
* discovers that a task has either stalled, or reported an error, then it
|
||
|
* prints an error message to the UART, otherwise it prints "Pass" followed
|
||
|
* by an additional period (".") after each successful loop of its implementing
|
||
|
* function.
|
||
|
*/
|
||
|
|
||
|
/* Standard includes. */
|
||
|
#include <stdio.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
/* Kernel includes. */
|
||
|
#include "FreeRTOS.h"
|
||
|
#include "task.h"
|
||
|
#include "timers.h"
|
||
|
#include "semphr.h"
|
||
|
|
||
|
/* Standard demo application includes. */
|
||
|
#include "dynamic.h"
|
||
|
#include "blocktim.h"
|
||
|
#include "GenQTest.h"
|
||
|
#include "recmutex.h"
|
||
|
#include "TimerDemo.h"
|
||
|
#include "EventGroupsDemo.h"
|
||
|
#include "TaskNotify.h"
|
||
|
|
||
|
/* SiFive includes. */
|
||
|
#include "platform.h"
|
||
|
#include "encoding.h"
|
||
|
#include "unistd.h"
|
||
|
|
||
|
/* Priorities for the demo application tasks. */
|
||
|
#define mainCHECK_TASK_PRIORITY ( configMAX_PRIORITIES - 1 )
|
||
|
|
||
|
/* The period of the check task, in ms, converted to ticks using the
|
||
|
pdMS_TO_TICKS() macro. */
|
||
|
#define mainNO_ERROR_CHECK_TASK_PERIOD pdMS_TO_TICKS( 5000UL )
|
||
|
|
||
|
/* Parameters that are passed into the register check tasks solely for the
|
||
|
purpose of ensuring parameters are passed into tasks correctl5. */
|
||
|
#define mainREG_TEST_TASK_1_PARAMETER ( ( void * ) 0x12345678 )
|
||
|
#define mainREG_TEST_TASK_2_PARAMETER ( ( void * ) 0x87654321 )
|
||
|
|
||
|
/* The base period used by the timer test tasks. */
|
||
|
#define mainTIMER_TEST_PERIOD ( 50 )
|
||
|
|
||
|
/* The size of the stack allocated to the check task (as described in the
|
||
|
comments at the top of this file. */
|
||
|
#define mainCHECK_TASK_STACK_SIZE_WORDS 85
|
||
|
|
||
|
/* Size of the stacks to allocated for the register check tasks. */
|
||
|
#define mainREG_TEST_STACK_SIZE_WORDS 50
|
||
|
|
||
|
/*-----------------------------------------------------------*/
|
||
|
|
||
|
/*
|
||
|
* Called by main() to run the full demo (as opposed to the blinky demo) when
|
||
|
* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.
|
||
|
*/
|
||
|
void main_full( void );
|
||
|
|
||
|
/*
|
||
|
* The check task, as described at the top of this file.
|
||
|
*/
|
||
|
static void prvCheckTask( void *pvParameters );
|
||
|
|
||
|
/*
|
||
|
* Register check tasks as described at the top of this file. The nature of
|
||
|
* these files necessitates that they are written in an assembly file, but the
|
||
|
* entry points are kept in the C file for the convenience of checking the task
|
||
|
* parameter.
|
||
|
*/
|
||
|
static void prvRegTestTaskEntry1( void *pvParameters );
|
||
|
extern void vRegTest1Implementation( void );
|
||
|
static void prvRegTestTaskEntry2( void *pvParameters );
|
||
|
extern void vRegTest2Implementation( void );
|
||
|
|
||
|
/*
|
||
|
* Tick hook used by the full demo, which includes code that interacts with
|
||
|
* some of the tests.
|
||
|
*/
|
||
|
void vFullDemoTickHook( void );
|
||
|
|
||
|
/*-----------------------------------------------------------*/
|
||
|
|
||
|
/* The following two variables are used to communicate the status of the
|
||
|
register check tasks to the check task. If the variables keep incrementing,
|
||
|
then the register check tasks have not discovered any errors. If a variable
|
||
|
stops incrementing, then an error has been found. */
|
||
|
volatile uint32_t ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;
|
||
|
|
||
|
/*-----------------------------------------------------------*/
|
||
|
|
||
|
void main_full( void )
|
||
|
{
|
||
|
/* Create the register check tasks, as described at the top of this file.
|
||
|
Use xTaskCreateStatic() to create a task using only statically allocated
|
||
|
memory. */
|
||
|
xTaskCreate( prvRegTestTaskEntry1, /* The function that implements the task. */
|
||
|
"Reg1", /* The name of the task. */
|
||
|
mainREG_TEST_STACK_SIZE_WORDS, /* Size of stack to allocate for the task - in words not bytes!. */
|
||
|
mainREG_TEST_TASK_1_PARAMETER, /* Parameter passed into the task. */
|
||
|
tskIDLE_PRIORITY, /* Priority of the task. */
|
||
|
NULL ); /* Can be used to pass out a handle to the created task. */
|
||
|
xTaskCreate( prvRegTestTaskEntry2, "Reg2", mainREG_TEST_STACK_SIZE_WORDS, mainREG_TEST_TASK_2_PARAMETER, tskIDLE_PRIORITY, NULL );
|
||
|
|
||
|
/* Start all the other standard demo/test tasks. They have no particular
|
||
|
functionality, but do demonstrate how to use the FreeRTOS API and test the
|
||
|
kernel port. */
|
||
|
vStartDynamicPriorityTasks();
|
||
|
vCreateBlockTimeTasks();
|
||
|
vStartGenericQueueTasks( tskIDLE_PRIORITY );
|
||
|
vStartRecursiveMutexTasks();
|
||
|
vStartTimerDemoTask( mainTIMER_TEST_PERIOD );
|
||
|
vStartEventGroupTasks();
|
||
|
vStartTaskNotifyTask();
|
||
|
|
||
|
/* Create the task that performs the 'check' functionality, as described at
|
||
|
the top of this file. */
|
||
|
xTaskCreate( prvCheckTask, "Check", mainCHECK_TASK_STACK_SIZE_WORDS, NULL, mainCHECK_TASK_PRIORITY, NULL );
|
||
|
|
||
|
/* Start the 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 prvCheckTask( void *pvParameters )
|
||
|
{
|
||
|
const TickType_t xDelayPeriod = mainNO_ERROR_CHECK_TASK_PERIOD;
|
||
|
TickType_t xLastExecutionTime;
|
||
|
static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;
|
||
|
const char * const pcPassMessage = "Pass";
|
||
|
const char * pcStatusMessage = ".";
|
||
|
|
||
|
/* Just to stop compiler warnings. */
|
||
|
( void ) pvParameters;
|
||
|
|
||
|
write( STDOUT_FILENO, pcPassMessage, strlen( pcPassMessage ) );
|
||
|
|
||
|
/* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()
|
||
|
works correctly. */
|
||
|
xLastExecutionTime = xTaskGetTickCount();
|
||
|
|
||
|
/* Cycle for ever, delaying then checking all the other tasks are still
|
||
|
operating without error. The onboard LED is toggled on each iteration.
|
||
|
If an error is detected then the delay period is decreased from
|
||
|
mainNO_ERROR_CHECK_TASK_PERIOD to mainERROR_CHECK_TASK_PERIOD. This has the
|
||
|
effect of increasing the rate at which the onboard LED toggles, and in so
|
||
|
doing gives visual feedback of the system status. */
|
||
|
for( ;; )
|
||
|
{
|
||
|
/* Delay until it is time to execute again. */
|
||
|
vTaskDelayUntil( &xLastExecutionTime, xDelayPeriod );
|
||
|
|
||
|
/* Check all the demo tasks (other than the flash tasks) to ensure
|
||
|
that they are all still running, and that none have detected an error. */
|
||
|
if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
|
||
|
{
|
||
|
pcStatusMessage = "ERROR: Dynamic priority demo/tests.\r\n";
|
||
|
}
|
||
|
|
||
|
if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
|
||
|
{
|
||
|
pcStatusMessage = "ERROR: Block time demo/tests.\r\n";
|
||
|
}
|
||
|
|
||
|
if ( xAreGenericQueueTasksStillRunning() != pdTRUE )
|
||
|
{
|
||
|
pcStatusMessage = "ERROR: Generic queue demo/tests.\r\n";
|
||
|
}
|
||
|
|
||
|
if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
|
||
|
{
|
||
|
pcStatusMessage = "ERROR: Recursive mutex demo/tests.\r\n";
|
||
|
}
|
||
|
|
||
|
if( xAreTimerDemoTasksStillRunning( ( TickType_t ) xDelayPeriod ) != pdPASS )
|
||
|
{
|
||
|
pcStatusMessage = "ERROR: Timer demo/tests.\r\n";
|
||
|
}
|
||
|
|
||
|
if( xAreEventGroupTasksStillRunning() != pdPASS )
|
||
|
{
|
||
|
pcStatusMessage = "ERROR: Event group demo/tests.\r\n";
|
||
|
}
|
||
|
|
||
|
if( xAreTaskNotificationTasksStillRunning() != pdPASS )
|
||
|
{
|
||
|
pcStatusMessage = "ERROR: Task notification demo/tests.\r\n";
|
||
|
}
|
||
|
|
||
|
/* Check that the register test 1 task is still running. */
|
||
|
if( ulLastRegTest1Value == ulRegTest1LoopCounter )
|
||
|
{
|
||
|
pcStatusMessage = "ERROR: Register test 1.\r\n";
|
||
|
}
|
||
|
ulLastRegTest1Value = ulRegTest1LoopCounter;
|
||
|
|
||
|
/* Check that the register test 2 task is still running. */
|
||
|
if( ulLastRegTest2Value == ulRegTest2LoopCounter )
|
||
|
{
|
||
|
pcStatusMessage = "ERROR: Register test 2.\r\n";
|
||
|
}
|
||
|
ulLastRegTest2Value = ulRegTest2LoopCounter;
|
||
|
|
||
|
/* Write the status message to the UART. */
|
||
|
write( STDOUT_FILENO, pcStatusMessage, strlen( pcStatusMessage ) );
|
||
|
}
|
||
|
}
|
||
|
/*-----------------------------------------------------------*/
|
||
|
|
||
|
static void prvRegTestTaskEntry1( void *pvParameters )
|
||
|
{
|
||
|
/* Although the regtest task is written in assembler, its entry point is
|
||
|
written in C for convenience of checking the task parameter is being passed
|
||
|
in correctly. */
|
||
|
if( pvParameters == mainREG_TEST_TASK_1_PARAMETER )
|
||
|
{
|
||
|
/* Start the part of the test that is written in assembler. */
|
||
|
vRegTest1Implementation();
|
||
|
}
|
||
|
|
||
|
/* The following line will only execute if the task parameter is found to
|
||
|
be incorrect. The check task will detect that the regtest loop counter is
|
||
|
not being incremented and flag an error. */
|
||
|
vTaskDelete( NULL );
|
||
|
}
|
||
|
/*-----------------------------------------------------------*/
|
||
|
|
||
|
static void prvRegTestTaskEntry2( void *pvParameters )
|
||
|
{
|
||
|
/* Although the regtest task is written in assembler, its entry point is
|
||
|
written in C for convenience of checking the task parameter is being passed
|
||
|
in correctly. */
|
||
|
if( pvParameters == mainREG_TEST_TASK_2_PARAMETER )
|
||
|
{
|
||
|
/* Start the part of the test that is written in assembler. */
|
||
|
vRegTest2Implementation();
|
||
|
}
|
||
|
|
||
|
/* The following line will only execute if the task parameter is found to
|
||
|
be incorrect. The check task will detect that the regtest loop counter is
|
||
|
not being incremented and flag an error. */
|
||
|
vTaskDelete( NULL );
|
||
|
}
|
||
|
/*-----------------------------------------------------------*/
|
||
|
|
||
|
void vFullDemoTickHook( void )
|
||
|
{
|
||
|
/* Called from vApplicationTickHook() when the project is configured to
|
||
|
build the full demo. */
|
||
|
vTimerPeriodicISRTests();
|
||
|
vPeriodicEventGroupsProcessing();
|
||
|
xNotifyTaskFromISR();
|
||
|
}
|