]> begriffs open source - cmsis-freertos/blob - Demo/dsPIC_MPLAB/main.c
Update README.md - branch main is now the base branch
[cmsis-freertos] / Demo / dsPIC_MPLAB / main.c
1 /*
2  * FreeRTOS V202111.00
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28 /*
29  * Creates all the demo application tasks, then starts the scheduler.  The WEB
30  * documentation provides more details of the standard demo application tasks.
31  * In addition to the standard demo tasks, the following tasks and tests are
32  * defined and/or created within this file:
33  *
34  * "Fast Interrupt Test" - A high frequency periodic interrupt is generated
35  * using a free running timer to demonstrate the use of the
36  * configKERNEL_INTERRUPT_PRIORITY configuration constant.  The interrupt
37  * service routine measures the number of processor clocks that occur between
38  * each interrupt - and in so doing measures the jitter in the interrupt
39  * timing.  The maximum measured jitter time is latched in the usMaxJitter
40  * variable, and displayed on the LCD by the 'Check' as described below.
41  * The fast interrupt is configured and handled in the timer_test.c source
42  * file.
43  *
44  * "LCD" task - the LCD task is a 'gatekeeper' task.  It is the only task that
45  * is permitted to access the LCD directly.  Other tasks wishing to write a
46  * message to the LCD send the message on a queue to the LCD task instead of
47  * accessing the LCD themselves.  The LCD task just blocks on the queue waiting
48  * for messages - waking and displaying the messages as they arrive.  The LCD
49  * task is defined in lcd.c.
50  *
51  * "Check" task -  This only executes every three seconds but has the highest
52  * priority so is guaranteed to get processor time.  Its main function is to
53  * check that all the standard demo tasks are still operational.  Should any
54  * unexpected behaviour within a demo task be discovered the 'check' task will
55  * write "FAIL #n" to the LCD (via the LCD task).  If all the demo tasks are
56  * executing with their expected behaviour then the check task writes the max
57  * jitter time to the LCD (again via the LCD task), as described above.
58  */
59
60 /* Standard includes. */
61 #include <stdio.h>
62
63 /* Scheduler includes. */
64 #include "FreeRTOS.h"
65 #include "task.h"
66 #include "queue.h"
67 #include "croutine.h"
68
69 /* Demo application includes. */
70 #include "BlockQ.h"
71 #include "crflash.h"
72 #include "blocktim.h"
73 #include "integer.h"
74 #include "comtest2.h"
75 #include "partest.h"
76 #include "lcd.h"
77 #include "timertest.h"
78
79 /* Demo task priorities. */
80 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )
81 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )
82 #define mainCOM_TEST_PRIORITY                           ( 2 )
83
84 /* The check task may require a bit more stack as it calls sprintf(). */
85 #define mainCHECK_TAKS_STACK_SIZE                       ( configMINIMAL_STACK_SIZE * 2 )
86
87 /* The execution period of the check task. */
88 #define mainCHECK_TASK_PERIOD                           ( ( TickType_t ) 3000 / portTICK_PERIOD_MS )
89
90 /* The number of flash co-routines to create. */
91 #define mainNUM_FLASH_COROUTINES                        ( 5 )
92
93 /* Baud rate used by the comtest tasks. */
94 #define mainCOM_TEST_BAUD_RATE                          ( 19200 )
95
96 /* The LED used by the comtest tasks.  mainCOM_TEST_LED + 1 is also used.
97 See the comtest.c file for more information. */
98 #define mainCOM_TEST_LED                                        ( 6 )
99
100 /* The frequency at which the "fast interrupt test" interrupt will occur. */
101 #define mainTEST_INTERRUPT_FREQUENCY            ( 20000 )
102
103 /* The number of processor clocks we expect to occur between each "fast
104 interrupt test" interrupt. */
105 #define mainEXPECTED_CLOCKS_BETWEEN_INTERRUPTS ( configCPU_CLOCK_HZ / mainTEST_INTERRUPT_FREQUENCY )
106
107 /* The number of nano seconds between each processor clock. */
108 #define mainNS_PER_CLOCK ( ( unsigned short ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )
109
110 /* Dimension the buffer used to hold the value of the maximum jitter time when
111 it is converted to a string. */
112 #define mainMAX_STRING_LENGTH                           ( 20 )
113
114 /*-----------------------------------------------------------*/
115
116 /*
117  * The check task as described at the top of this file.
118  */
119 static void vCheckTask( void *pvParameters );
120
121 /*
122  * Setup the processor ready for the demo.
123  */
124 static void prvSetupHardware( void );
125
126 /*-----------------------------------------------------------*/
127
128 /* The queue used to send messages to the LCD task. */
129 static QueueHandle_t xLCDQueue;
130
131 /*-----------------------------------------------------------*/
132
133 /*
134  * Create the demo tasks then start the scheduler.
135  */
136 int main( void )
137 {
138         /* Configure any hardware required for this demo. */
139         prvSetupHardware();
140
141         /* Create the standard demo tasks. */
142         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
143         vStartIntegerMathTasks( tskIDLE_PRIORITY );
144         vStartFlashCoRoutines( mainNUM_FLASH_COROUTINES );
145         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
146         vCreateBlockTimeTasks();
147
148         /* Create the test tasks defined within this file. */
149         xTaskCreate( vCheckTask, "Check", mainCHECK_TAKS_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
150
151         /* Start the task that will control the LCD.  This returns the handle
152         to the queue used to write text out to the task. */
153         xLCDQueue = xStartLCDTask();
154
155         /* Start the high frequency interrupt test. */
156         vSetupTimerTest( mainTEST_INTERRUPT_FREQUENCY );
157
158         /* Finally start the scheduler. */
159         vTaskStartScheduler();
160
161         /* Will only reach here if there is insufficient heap available to start
162         the scheduler. */
163         return 0;
164 }
165 /*-----------------------------------------------------------*/
166
167 static void prvSetupHardware( void )
168 {
169         vParTestInitialise();
170 }
171 /*-----------------------------------------------------------*/
172
173 static void vCheckTask( void *pvParameters )
174 {
175 /* Used to wake the task at the correct frequency. */
176 TickType_t xLastExecutionTime;
177
178 /* The maximum jitter time measured by the fast interrupt test. */
179 extern unsigned short usMaxJitter ;
180
181 /* Buffer into which the maximum jitter time is written as a string. */
182 static char cStringBuffer[ mainMAX_STRING_LENGTH ];
183
184 /* The message that is sent on the queue to the LCD task.  The first
185 parameter is the minimum time (in ticks) that the message should be
186 left on the LCD without being overwritten.  The second parameter is a pointer
187 to the message to display itself. */
188 xLCDMessage xMessage = { 0, cStringBuffer };
189
190 /* Set to pdTRUE should an error be detected in any of the standard demo tasks. */
191 unsigned short usErrorDetected = pdFALSE;
192
193         /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()
194         works correctly. */
195         xLastExecutionTime = xTaskGetTickCount();
196
197         for( ;; )
198         {
199                 /* Wait until it is time for the next cycle. */
200                 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_TASK_PERIOD );
201
202                 /* Has an error been found in any of the standard demo tasks? */
203
204                 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
205                 {
206                         usErrorDetected = pdTRUE;
207                         sprintf( cStringBuffer, "FAIL #1" );
208                 }
209
210                 if( xAreComTestTasksStillRunning() != pdTRUE )
211                 {
212                         usErrorDetected = pdTRUE;
213                         sprintf( cStringBuffer, "FAIL #2" );
214                 }
215
216                 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
217                 {
218                         usErrorDetected = pdTRUE;
219                         sprintf( cStringBuffer, "FAIL #3" );
220                 }
221
222                 if( xAreBlockingQueuesStillRunning() != pdTRUE )
223                 {
224                         usErrorDetected = pdTRUE;
225                         sprintf( cStringBuffer, "FAIL #4" );
226                 }
227
228                 if( usErrorDetected == pdFALSE )
229                 {
230                         /* No errors have been discovered, so display the maximum jitter
231                         timer discovered by the "fast interrupt test". */
232                         sprintf( cStringBuffer, "%dns max jitter", ( short ) ( usMaxJitter - mainEXPECTED_CLOCKS_BETWEEN_INTERRUPTS ) * mainNS_PER_CLOCK );
233                 }
234
235                 /* Send the message to the LCD gatekeeper for display. */
236                 xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );
237         }
238 }
239 /*-----------------------------------------------------------*/
240
241 void vApplicationIdleHook( void )
242 {
243         /* Schedule the co-routines from within the idle task hook. */
244         vCoRoutineSchedule();
245 }
246 /*-----------------------------------------------------------*/
247