summaryrefslogtreecommitdiff
path: root/FreeRTOS/Demo/AVR_ATMega4809_Atmel_Studio/RTOSDemo/main_minimal.c
blob: 937bd939f44f86c76aa74f0321cf00e16b3892f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <avr/eeprom.h>

#include "FreeRTOS.h"
#include "task.h"
#include "croutine.h"
#include "PollQ.h"
#include "integer.h"
#include "serial.h"
#include "comtest.h"
#include "crflash.h"
#include "partest.h"
#include "regtest.h"

/* Priority definitions for most of the tasks in the demo application.  Some
tasks just use the idle priority. */
#define mainCOM_TEST_PRIORITY       ( tskIDLE_PRIORITY + 2 )
#define mainQUEUE_POLL_PRIORITY     ( tskIDLE_PRIORITY + 2 )
#define mainCHECK_TASK_PRIORITY     ( tskIDLE_PRIORITY + 3 )

/* Baud rate used by the serial port tasks. */
#define mainCOM_TEST_BAUD_RATE      ( ( unsigned long ) 9600 )

/* LED used by the serial port tasks.  This is toggled on each character Tx,
and mainCOM_TEST_LED + 1 is toggles on each character Rx. */
#define mainCOM_TEST_LED            ( 6 )

/* LED that is toggled by the check task.  The check task periodically checks
that all the other tasks are operating without error.  If no errors are found
the LED is toggled.  If an error is found at any time the LED is never toggles
again. */
#define mainCHECK_TASK_LED          ( 5 )

/* The period between executions of the check task. */
#define mainCHECK_PERIOD            ( ( TickType_t ) 1000 / portTICK_PERIOD_MS )

/* An address in the EEPROM used to count resets.  This is used to check that
the demo application is not unexpectedly resetting. */
#define mainRESET_COUNT_ADDRESS     ( 0x1400 )

/* The number of coroutines to create. */
#define mainNUM_FLASH_COROUTINES    ( 3 )

/*
 * The task function for the "Check" task.
 */
static void vErrorChecks( void *pvParameters );

/*
 * Checks the unique counts of other tasks to ensure they are still operational.
 * Flashes an LED if everything is okay.
 */
static void prvCheckOtherTasksAreStillRunning( void );

/*
 * Called on boot to increment a count stored in the EEPROM.  This is used to
 * ensure the CPU does not reset unexpectedly.
 */
static void prvIncrementResetCount( void );

void main_minimal( void )
{
    prvIncrementResetCount();

    /* Create the standard demo tasks. */
    vStartIntegerMathTasks( tskIDLE_PRIORITY );
    vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
    vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
    vStartRegTestTasks();
    
    /* Create the tasks defined within this file. */
    xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );

    /* Create the co-routines that flash the LED's. */
    vStartFlashCoRoutines( mainNUM_FLASH_COROUTINES );
    
    /* In this port, to use preemptive scheduler define configUSE_PREEMPTION
    as 1 in portmacro.h.  To use the cooperative scheduler define
    configUSE_PREEMPTION as 0. */
    vTaskStartScheduler();
}

void init_minimal( void )
{
    /* Configure UART pins: PB0 Rx, PB1 Tx */
    PORTB.DIR &= ~PIN1_bm;
    PORTB.DIR |= PIN0_bm;
    
    vParTestInitialise();
}

static void vErrorChecks( void *pvParameters )
{
static volatile unsigned long ulDummyVariable = 3UL;

    /* The parameters are not used. */
    ( void ) pvParameters;

    /* Cycle for ever, delaying then checking all the other tasks are still
    operating without error. */
    for( ;; )
    {
        vTaskDelay( mainCHECK_PERIOD );

        /* Perform a bit of 32bit maths to ensure the registers used by the
        integer tasks get some exercise.  The result here is not important -
        see the demo application documentation for more info. */
        ulDummyVariable *= 3;
        
        prvCheckOtherTasksAreStillRunning();
    }
}
/*-----------------------------------------------------------*/

static void prvCheckOtherTasksAreStillRunning( void )
{
static portBASE_TYPE xErrorHasOccurred = pdFALSE;

    if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
    {
        xErrorHasOccurred = pdTRUE;
    }

    if( xAreComTestTasksStillRunning() != pdTRUE )
    {
        xErrorHasOccurred = pdTRUE;
    }

    if( xArePollingQueuesStillRunning() != pdTRUE )
    {
        xErrorHasOccurred = pdTRUE;
    }

    if( xAreRegTestTasksStillRunning() != pdTRUE )
    {
        xErrorHasOccurred = pdTRUE;
    }
    
    if( xErrorHasOccurred == pdFALSE )
    {
        /* Toggle the LED if everything is okay so we know if an error occurs even if not
        using console IO. */
        vParTestToggleLED( mainCHECK_TASK_LED );
    }
}
/*-----------------------------------------------------------*/

static void prvIncrementResetCount( void )
{
unsigned char ucResetCount;

    eeprom_read_block( &ucResetCount, ( void * ) mainRESET_COUNT_ADDRESS, sizeof( ucResetCount ) );
    ucResetCount++;
    eeprom_write_byte( ( void * ) mainRESET_COUNT_ADDRESS, ucResetCount );
}
/*-----------------------------------------------------------*/

void vApplicationIdleHook( void )
{
    vCoRoutineSchedule();
}