]> begriffs open source - cmsis-freertos/blob - Demo/ARM7_STR75x_IAR/main.c
Set error state if no delay or already expired
[cmsis-freertos] / Demo / ARM7_STR75x_IAR / main.c
1 /*
2  * FreeRTOS Kernel V10.1.1
3  * Copyright (C) 2018 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 void 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 }
165 /*-----------------------------------------------------------*/
166
167 static void vCheckTask( void *pvParameters )
168 {
169 static unsigned long ulErrorDetected = pdFALSE;
170 TickType_t xLastExecutionTime;
171 unsigned char *cErrorMessage = "              FAIL";
172 unsigned char *cSuccessMessage = "              PASS";
173 unsigned portBASE_TYPE uxColumn = mainMAX_WRITE_COLUMN;
174 LCDMessage xMessage;
175
176         /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()
177         works correctly. */
178         xLastExecutionTime = xTaskGetTickCount();
179
180         for( ;; )
181         {
182                 /* Wait until it is time for the next cycle. */
183                 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_TASK_CYCLE_TIME );
184
185                 /* Has an error been found in any of the standard demo tasks? */
186
187                 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
188                 {
189                         ulErrorDetected = pdTRUE;
190                 }
191
192                 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
193                 {
194                         ulErrorDetected = pdTRUE;
195                 }
196
197                 if( xAreBlockingQueuesStillRunning() != pdTRUE )
198                 {
199                         ulErrorDetected = pdTRUE;
200                 }
201
202                 if( xAreComTestTasksStillRunning() != pdTRUE )
203                 {
204                         ulErrorDetected = pdTRUE;
205                 }
206
207                 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
208                 {
209                         ulErrorDetected = pdTRUE;
210                 }
211
212                 /* Calculate the LCD line on which we would like the message to
213                 be displayed.  The column variable is used for convenience as
214                 it is incremented each cycle anyway. */
215                 xMessage.ucLine = ( unsigned char ) ( uxColumn & 0x01 );
216
217                 /* The message displayed depends on whether an error was found or
218                 not.  Any discovered error is latched.  Here the column variable
219                 is used as an index into the text string as a simple way of moving
220                 the text from column to column. */
221                 if( ulErrorDetected == pdFALSE )
222                 {
223                         xMessage.pucString = cSuccessMessage + uxColumn;
224                 }
225                 else
226                 {
227                         xMessage.pucString = cErrorMessage + uxColumn;
228                 }
229
230                 /* Send the message to the print task for display. */
231                 xQueueSend( xLCDQueue, ( void * ) &xMessage, mainNO_DELAY );
232
233                 /* Make sure the message is printed in a different column the next
234                 time around. */
235                 uxColumn--;
236                 if( uxColumn == 0 )
237                 {
238                         uxColumn = mainMAX_WRITE_COLUMN;
239                 }
240         }
241 }
242 /*-----------------------------------------------------------*/
243
244 static void vPrintTask( void *pvParameters )
245 {
246 LCDMessage xMessage;
247
248         for( ;; )
249         {
250                 /* Wait until a message arrives. */
251                 while( xQueueReceive( xLCDQueue, ( void * ) &xMessage, portMAX_DELAY ) != pdPASS );
252
253                 /* The message contains the text to display, and the line on which the
254                 text should be displayed. */
255                 LCD_Clear();
256                 LCD_DisplayString( xMessage.ucLine, xMessage.pucString, BlackText );
257         }
258 }
259 /*-----------------------------------------------------------*/
260
261 static void prvSetupHardware(void)
262 {
263 ErrorStatus OSC4MStartUpStatus01;
264
265         /* ST provided routine. */
266
267         /* MRCC system reset */
268         MRCC_DeInit();
269
270         /* Wait for OSC4M start-up */
271         OSC4MStartUpStatus01 = MRCC_WaitForOSC4MStartUp();
272
273         if(OSC4MStartUpStatus01 == SUCCESS)
274         {
275                 /* Set HCLK to 60MHz */
276                 MRCC_HCLKConfig(MRCC_CKSYS_Div1);
277
278                 /* Set CKTIM to 60MHz */
279                 MRCC_CKTIMConfig(MRCC_HCLK_Div1);
280
281                 /* Set PCLK to 30MHz */
282                 MRCC_PCLKConfig(MRCC_CKTIM_Div2);
283
284                 /* Enable Flash Burst mode */
285                 CFG_FLASHBurstConfig(CFG_FLASHBurst_Enable);
286
287                 /* Set CK_SYS to 60 MHz */
288                 MRCC_CKSYSConfig(MRCC_CKSYS_OSC4MPLL, MRCC_PLL_Mul_15);
289         }
290
291         /* GPIO pins optimized for 3V3 operation */
292         MRCC_IOVoltageRangeConfig(MRCC_IOVoltageRange_3V3);
293
294         /* GPIO clock source enable */
295         MRCC_PeripheralClockConfig(MRCC_Peripheral_GPIO, ENABLE);
296
297         /* EXTIT clock source enable */
298         MRCC_PeripheralClockConfig(MRCC_Peripheral_EXTIT, ENABLE);
299         /* TB clock source enable */
300         MRCC_PeripheralClockConfig(MRCC_Peripheral_TB, ENABLE);
301
302         /* Initialize the demonstration menu */
303         LCD_Init();
304
305         LCD_DisplayString(Line1, "www.FreeRTOS.org", BlackText);
306         LCD_DisplayString(Line2, "  STR750 Demo  ", BlackText);
307
308         EIC_IRQCmd(ENABLE);
309 }
310 /*-----------------------------------------------------------*/