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