]> begriffs open source - cmsis-freertos/blob - Demo/ARM7_STR75x_GCC/main.c
osEventFlagsWait: Fix flag comparison
[cmsis-freertos] / Demo / ARM7_STR75x_GCC / main.c
1 /*
2  * FreeRTOS Kernel V10.2.1
3  * Copyright (C) 2019 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 demo application tasks.
31  *
32  * In addition to the standard demo tasks there are two tasks defined within
33  * this file:
34  *
35  * 1 - The check task
36  * The 'check' task is responsible for ensuring that all the standard demo
37  * tasks are executing as expected.  It only executes every three seconds, but
38  * has the highest priority within the system so is guaranteed to get execution
39  * time.  Any errors discovered by the check task are latched until the
40  * processor is reset.  At the end of each cycle the check task sends either
41  * a pass or fail message to the 'print' task for display on the LCD.
42  *
43  * 2 - The print task
44  * The print task is the LCD 'gatekeeper'.  That is, it is the only task that
45  * should access the LCD directly so is always guaranteed exclusive (and
46  * therefore consistent) access.  The print task simply blocks on a queue
47  * to wait for messages from other tasks wishing to display text on the LCD.
48  * When a message arrives it displays its contents on the LCD then blocks to
49  * wait again.
50  */
51
52 /* ST includes. */
53 #include "lcd.h"
54
55 /* Kernel includes. */
56 #include "FreeRTOS.h"
57 #include "task.h"
58 #include "queue.h"
59
60 /* Demo application includes. */
61 #include "partest.h"
62 #include "flash.h"
63 #include "integer.h"
64 #include "blocktim.h"
65 #include "BlockQ.h"
66 #include "comtest2.h"
67 #include "dynamic.h"
68
69 /* Demo application task priorities. */
70 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 4 )
71 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )
72 #define mainLED_TASK_PRIORITY           ( tskIDLE_PRIORITY + 1 )
73 #define mainCOM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 1 )
74 #define mainLCD_TASK_PRIORITY           ( tskIDLE_PRIORITY + 1 )
75
76 /* How often should we check the other tasks? */
77 #define mainCHECK_TASK_CYCLE_TIME       ( 3000 )
78
79 /* The maximum offset into the pass and fail strings sent to the LCD.  An
80 offset is used a simple method of using a different column each time a message
81 is written to the LCD. */
82 #define mainMAX_WRITE_COLUMN            ( 14 )
83
84 /* Baud rate used by the comtest tasks. */
85 #define mainCOM_TEST_BAUD_RATE          ( 19200 )
86
87 /* The LED used by the comtest tasks. See the comtest.c file for more
88 information. */
89 #define mainCOM_TEST_LED                        ( 3 )
90
91 /* The number of messages that can be queued for display on the LCD at any one
92 time. */
93 #define mainLCD_QUEUE_LENGTH            ( 2 )
94
95 /* The time to wait when sending to mainLCD_QUEUE_LENGTH. */
96 #define mainNO_DELAY                            ( 0 )
97
98 /*-----------------------------------------------------------*/
99
100 /* The type that is posted to the LCD queue. */
101 typedef struct LCD_MESSAGE
102 {
103         unsigned char *pucString; /* Points to the string to be displayed. */
104         unsigned char ucLine;     /* The line of the LCD that should be used. */
105 } LCDMessage;
106
107 /*-----------------------------------------------------------*/
108
109 /*
110  * The task that executes at the highest priority and checks the operation of
111  * all the other tasks in the system.  See the description at the top of the
112  * file.
113  */
114 static void vCheckTask( void *pvParameters );
115
116 /*
117  * ST provided routine to configure the processor.
118  */
119 static void prvSetupHardware(void);
120
121 /*
122  * The only task that should access the LCD.  Other tasks wanting to write
123  * to the LCD should send a message of type LCDMessage containing the
124  * information to display to the print task.  The print task simply blocks
125  * waiting for the arrival of such messages, displays the message, then blocks
126  * again.
127  */
128 static void vPrintTask( void *pvParameters );
129
130 /*-----------------------------------------------------------*/
131
132 /* The queue used to communicate with the LCD print task. */
133 static QueueHandle_t xLCDQueue;
134
135 /*-----------------------------------------------------------*/
136
137 /* Create all the demo application tasks, then start the scheduler. */
138 int main( void )
139 {
140         /* Perform any hardware setup necessary. */
141         prvSetupHardware();
142         vParTestInitialise();
143
144         /* Create the queue used to communicate with the LCD print task. */
145         xLCDQueue = xQueueCreate( mainLCD_QUEUE_LENGTH, sizeof( LCDMessage ) );
146
147         /* Create the standard demo application tasks.  See the WEB documentation
148         for more information on these tasks. */
149         vCreateBlockTimeTasks();
150         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
151         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
152         vStartDynamicPriorityTasks();
153         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
154         vStartIntegerMathTasks( tskIDLE_PRIORITY );
155
156         /* Create the tasks defined within this file. */
157         xTaskCreate( vPrintTask, "LCD", configMINIMAL_STACK_SIZE, NULL, mainLCD_TASK_PRIORITY, NULL );
158         xTaskCreate( vCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
159
160         vTaskStartScheduler();
161
162         /* Execution will only reach here if there was insufficient heap to
163         start the scheduler. */
164         return 0;
165 }
166 /*-----------------------------------------------------------*/
167
168 static void vCheckTask( void *pvParameters )
169 {
170 static unsigned long ulErrorDetected = pdFALSE;
171 TickType_t xLastExecutionTime;
172 unsigned char *ucErrorMessage = ( unsigned char * )"              FAIL";
173 unsigned char *ucSuccessMessage = ( unsigned char * )"              PASS";
174 unsigned portBASE_TYPE uxColumn = mainMAX_WRITE_COLUMN;
175 LCDMessage xMessage;
176
177         /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()
178         works correctly. */
179         xLastExecutionTime = xTaskGetTickCount();
180
181         for( ;; )
182         {
183                 /* Wait until it is time for the next cycle. */
184                 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_TASK_CYCLE_TIME );
185
186                 /* Has an error been found in any of the standard demo tasks? */
187
188                 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
189                 {
190                         ulErrorDetected = pdTRUE;
191                 }
192
193                 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
194                 {
195                         ulErrorDetected = pdTRUE;
196                 }
197
198                 if( xAreBlockingQueuesStillRunning() != pdTRUE )
199                 {
200                         ulErrorDetected = pdTRUE;
201                 }
202
203                 if( xAreComTestTasksStillRunning() != pdTRUE )
204                 {
205                         ulErrorDetected = pdTRUE;
206                 }
207
208                 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
209                 {
210                         ulErrorDetected = pdTRUE;
211                 }
212
213                 /* Calculate the LCD line on which we would like the message to
214                 be displayed.  The column variable is used for convenience as
215                 it is incremented each cycle anyway. */
216                 xMessage.ucLine = ( unsigned char ) ( uxColumn & 0x01 );
217
218                 /* The message displayed depends on whether an error was found or
219                 not.  Any discovered error is latched.  Here the column variable
220                 is used as an index into the text string as a simple way of moving
221                 the text from column to column. */
222                 if( ulErrorDetected == pdFALSE )
223                 {
224                         xMessage.pucString = ucSuccessMessage + uxColumn;
225                 }
226                 else
227                 {
228                         xMessage.pucString = ucErrorMessage + uxColumn;
229                 }
230
231                 /* Send the message to the print task for display. */
232                 xQueueSend( xLCDQueue, ( void * ) &xMessage, mainNO_DELAY );
233
234                 /* Make sure the message is printed in a different column the next
235                 time around. */
236                 uxColumn--;
237                 if( uxColumn == 0 )
238                 {
239                         uxColumn = mainMAX_WRITE_COLUMN;
240                 }
241         }
242 }
243
244 /*-----------------------------------------------------------*/
245
246 static void vPrintTask( void *pvParameters )
247 {
248 LCDMessage xMessage;
249
250         for( ;; )
251         {
252                 /* Wait until a message arrives. */
253                 while( xQueueReceive( xLCDQueue, ( void * ) &xMessage, portMAX_DELAY ) != pdPASS );
254
255                 /* The message contains the text to display, and the line on which the
256                 text should be displayed. */
257                 LCD_Clear();
258                 LCD_DisplayString( xMessage.ucLine, xMessage.pucString, BlackText );
259         }
260 }
261 /*-----------------------------------------------------------*/
262
263 static void prvSetupHardware(void)
264 {
265 ErrorStatus OSC4MStartUpStatus01;
266
267         /* ST provided routine. */
268
269         /* MRCC system reset */
270         MRCC_DeInit();
271
272         /* Wait for OSC4M start-up */
273         OSC4MStartUpStatus01 = MRCC_WaitForOSC4MStartUp();
274
275         if(OSC4MStartUpStatus01 == SUCCESS)
276         {
277                 /* Set HCLK to 60MHz */
278                 MRCC_HCLKConfig(MRCC_CKSYS_Div1);
279
280                 /* Set CKTIM to 60MHz */
281                 MRCC_CKTIMConfig(MRCC_HCLK_Div1);
282
283                 /* Set PCLK to 30MHz */
284                 MRCC_PCLKConfig(MRCC_CKTIM_Div2);
285
286                 /* Enable Flash Burst mode */
287                 CFG_FLASHBurstConfig(CFG_FLASHBurst_Enable);
288
289                 /* Set CK_SYS to 60 MHz */
290                 MRCC_CKSYSConfig(MRCC_CKSYS_OSC4MPLL, MRCC_PLL_Mul_15);
291         }
292
293         /* GPIO pins optimized for 3V3 operation */
294         MRCC_IOVoltageRangeConfig(MRCC_IOVoltageRange_3V3);
295
296         /* GPIO clock source enable */
297         MRCC_PeripheralClockConfig(MRCC_Peripheral_GPIO, ENABLE);
298
299         /* EXTIT clock source enable */
300         MRCC_PeripheralClockConfig(MRCC_Peripheral_EXTIT, ENABLE);
301         /* TB clock source enable */
302         MRCC_PeripheralClockConfig(MRCC_Peripheral_TB, ENABLE);
303
304         /* Initialize the demonstration menu */
305         LCD_Init();
306
307         LCD_DisplayString(Line1, ( unsigned char * ) "www.FreeRTOS.org", BlackText);
308         LCD_DisplayString(Line2, ( unsigned char * ) "  STR750 Demo  ", BlackText);
309
310         EIC_IRQCmd(ENABLE);
311 }
312 /*-----------------------------------------------------------*/
313