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