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