]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_STM32F103_Primer_GCC/main.c
Prepare files for export (MicroBlaze project).
[freertos] / Demo / CORTEX_STM32F103_Primer_GCC / main.c
1 /*\r
2     FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /*\r
55  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
56  * documentation provides more details of the standard demo application tasks.\r
57  * In addition to the standard demo tasks, the following tasks and tests are\r
58  * defined and/or created within this file:\r
59  *\r
60  * "Fast Interrupt Test" - A high frequency periodic interrupt is generated\r
61  * using a free running timer to demonstrate the use of the\r
62  * configKERNEL_INTERRUPT_PRIORITY configuration constant.  The interrupt\r
63  * service routine measures the number of processor clocks that occur between\r
64  * each interrupt - and in so doing measures the jitter in the interrupt timing.\r
65  * The maximum measured jitter time is latched in the ulMaxJitter variable, and\r
66  * displayed on the LCD by the 'Check' task as described below.  The\r
67  * fast interrupt is configured and handled in the timertest.c source file.\r
68  *\r
69  * "LCD" task - the LCD task is a 'gatekeeper' task.  It is the only task that\r
70  * is permitted to access the display directly.  Other tasks wishing to write a\r
71  * message to the LCD send the message on a queue to the LCD task instead of\r
72  * accessing the LCD themselves.  The LCD task just blocks on the queue waiting\r
73  * for messages - waking and displaying the messages as they arrive.  Messages\r
74  * can either be a text string to display, or an instruction to update MEMS\r
75  * input.  The MEMS input is used to display a ball that can be moved around\r
76  * LCD by tilting the STM32 Primer.  45% is taken as the neutral position.\r
77  *\r
78  * "Check" task -  This only executes every five seconds but has the highest\r
79  * priority so is guaranteed to get processor time.  Its main function is to\r
80  * check that all the standard demo tasks are still operational.  Should any\r
81  * unexpected behaviour within a demo task be discovered the 'check' task will\r
82  * write an error to the LCD (via the LCD task).  If all the demo tasks are\r
83  * executing with their expected behaviour then the check task writes PASS\r
84  * along with the max jitter time to the LCD (again via the LCD task), as\r
85  * described above.\r
86  *\r
87  * Tick Hook - A tick hook is provided just for demonstration purposes.  In \r
88  * this case it is used to periodically send an instruction to updated the\r
89  * MEMS input to the LCD task.\r
90  *\r
91  */\r
92 \r
93 /* CircleOS includes.  Some of the CircleOS peripheral functionality is \r
94 utilised, although CircleOS itself is not used. */\r
95 #include "circle.h"\r
96 \r
97 /* Standard includes. */\r
98 #include <string.h>\r
99 \r
100 /* Scheduler includes. */\r
101 #include "FreeRTOS.h"\r
102 #include "task.h"\r
103 #include "queue.h"\r
104 \r
105 /* Demo app includes. */\r
106 #include "BlockQ.h"\r
107 #include "blocktim.h"\r
108 #include "GenQTest.h"\r
109 #include "partest.h"\r
110 #include "QPeek.h"\r
111 \r
112 /* The bitmap used to display the FreeRTOS.org logo is stored in 16bit format\r
113 and therefore takes up a large proportion of the Flash space.  Setting this\r
114 parameter to 0 excludes the bitmap from the build, freeing up Flash space for\r
115 extra code. */\r
116 #define mainINCLUDE_BITMAP                                      0\r
117 \r
118 #if mainINCLUDE_BITMAP == 1\r
119         #include "bitmap.h"\r
120 #endif\r
121 \r
122 /* Task priorities. */\r
123 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
124 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )\r
125 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
126 #define mainGEN_Q_PRIORITY                                      ( tskIDLE_PRIORITY + 0 )\r
127 #define mainFLASH_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
128 \r
129 /* Splash screen related constants. */\r
130 #define mainBITMAP_Y                                            ( 38 )\r
131 #define mainBITMAP_X                                            ( 18 )\r
132 #define mainURL_Y                                                       ( 8 )\r
133 #define mainURL_X                                                       ( 78 )\r
134 #define mainSPLASH_SCREEN_DELAY         ( 2000 / portTICK_RATE_MS )\r
135 \r
136 /* Text drawing related constants. */\r
137 #define mainLCD_CHAR_HEIGHT                     ( 13 )\r
138 #define mainLCD_MAX_Y                           ( 110 )\r
139 \r
140 /* The maximum number of message that can be waiting for display at any one\r
141 time. */\r
142 #define mainLCD_QUEUE_SIZE                                      ( 3 )\r
143 \r
144 /* The check task uses the sprintf function so requires a little more stack. */\r
145 #define mainCHECK_TASK_STACK_SIZE                       ( configMINIMAL_STACK_SIZE + 50 )\r
146 \r
147 /* The LCD task calls some of the CircleOS functions (for MEMS and LCD access),\r
148 these can require a larger stack. */\r
149 #define configLCD_TASK_STACK_SIZE                       ( configMINIMAL_STACK_SIZE + 50 )\r
150 \r
151 /* Dimensions the buffer into which the jitter time is written. */\r
152 #define mainMAX_MSG_LEN                                         25\r
153 \r
154 /* The time between cycles of the 'check' task. */\r
155 #define mainCHECK_DELAY                                         ( ( portTickType ) 5000 / portTICK_RATE_MS )\r
156 \r
157 /* The period at which the MEMS input should be updated. */\r
158 #define mainMEMS_DELAY                                          ( ( portTickType ) 100 / portTICK_RATE_MS )\r
159 \r
160 /* The rate at which the flash task toggles the LED. */\r
161 #define mainFLASH_DELAY                                         ( ( portTickType ) 1000 / portTICK_RATE_MS )\r
162 \r
163 /* The number of nano seconds between each processor clock. */\r
164 #define mainNS_PER_CLOCK ( ( unsigned portLONG ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )\r
165 \r
166 /* The two types of message that can be sent to the LCD task. */\r
167 #define mainUPDATE_BALL_MESSAGE                         ( 0 )\r
168 #define mainWRITE_STRING_MESSAGE                        ( 1 )\r
169 \r
170 /* Type of the message sent to the LCD task. */\r
171 typedef struct\r
172 {\r
173         portBASE_TYPE xMessageType;\r
174         signed char *pcMessage;\r
175 } xLCDMessage;\r
176 \r
177 /*-----------------------------------------------------------*/\r
178 \r
179 /*\r
180  * Configure the clocks, GPIO and other peripherals as required by the demo.\r
181  */\r
182 static void prvSetupHardware( void );\r
183 \r
184 /*\r
185  * The LCD is written two by more than one task so is controlled by a\r
186  * 'gatekeeper' task.  This is the only task that is actually permitted to\r
187  * access the LCD directly.  Other tasks wanting to display a message send\r
188  * the message to the gatekeeper.\r
189  */\r
190 static void prvLCDTask( void *pvParameters );\r
191 \r
192 /*\r
193  * Checks the status of all the demo tasks then prints a message to the\r
194  * display.  The message will be either PASS - and include in brackets the\r
195  * maximum measured jitter time (as described at the to of the file), or a\r
196  * message that describes which of the standard demo tasks an error has been\r
197  * discovered in.\r
198  *\r
199  * Messages are not written directly to the terminal, but passed to prvLCDTask\r
200  * via a queue.\r
201  *\r
202  * The check task also receives instructions to update the MEMS input, which\r
203  * in turn can also lead to the LCD being updated.\r
204  */\r
205 static void prvCheckTask( void *pvParameters );\r
206 \r
207 /*\r
208  * Configures the timers and interrupts for the fast interrupt test as\r
209  * described at the top of this file.\r
210  */\r
211 extern void vSetupTimerTest( void );\r
212 \r
213 /*\r
214  * A cut down version of sprintf() used to percent the HUGE GCC library\r
215  * equivalent from being included in the binary image. \r
216  */\r
217 extern int sprintf(char *out, const char *format, ...);\r
218 \r
219 /*\r
220  * Simple toggle the LED periodically for timing verification.\r
221  */\r
222 static void prvFlashTask( void *pvParameters );\r
223 \r
224 /*-----------------------------------------------------------*/\r
225 \r
226 /* The queue used to send messages to the LCD task. */\r
227 xQueueHandle xLCDQueue;\r
228 \r
229 /*-----------------------------------------------------------*/\r
230 \r
231 int main( void )\r
232 {\r
233         #ifdef DEBUG\r
234                 debug();\r
235         #endif\r
236 \r
237         prvSetupHardware();\r
238 \r
239         /* Create the queue used by the LCD task.  Messages for display on the LCD\r
240         are received via this queue. */\r
241         xLCDQueue = xQueueCreate( mainLCD_QUEUE_SIZE, sizeof( xLCDMessage ) );\r
242         \r
243         /* Start the standard demo tasks. */\r
244         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
245     vCreateBlockTimeTasks();\r
246         vStartGenericQueueTasks( mainGEN_Q_PRIORITY );\r
247         vStartQueuePeekTasks();\r
248         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
249 \r
250         /* Start the tasks defined within this file/specific to this demo. */\r
251     xTaskCreate( prvCheckTask, ( signed portCHAR * ) "Check", mainCHECK_TASK_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL ); \r
252         xTaskCreate( prvLCDTask, ( signed portCHAR * ) "LCD", configLCD_TASK_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
253         xTaskCreate( prvFlashTask, ( signed portCHAR * ) "Flash", configMINIMAL_STACK_SIZE, NULL, mainFLASH_TASK_PRIORITY, NULL );\r
254 \r
255         /* Configure the timers used by the fast interrupt timer test. */\r
256         vSetupTimerTest();\r
257         \r
258         /* Start the scheduler. */\r
259         vTaskStartScheduler();\r
260         \r
261         /* Will only get here if there was not enough heap space to create the\r
262         idle task. */\r
263         return 0;\r
264 }\r
265 /*-----------------------------------------------------------*/\r
266 \r
267 void prvLCDTask( void *pvParameters )\r
268 {\r
269 xLCDMessage xMessage;\r
270 portCHAR cY = mainLCD_CHAR_HEIGHT;\r
271 const portCHAR * const pcString = "www.FreeRTOS.org";\r
272 const portCHAR * const pcBlankLine = "                  ";\r
273 \r
274         DRAW_Init();\r
275 \r
276         #if mainINCLUDE_BITMAP == 1\r
277                 DRAW_SetImage( pucImage, mainBITMAP_Y, mainBITMAP_X, bmpBITMAP_HEIGHT, bmpBITMAP_WIDTH );\r
278         #endif\r
279 \r
280         LCD_SetScreenOrientation( V9 );\r
281         DRAW_DisplayString( mainURL_Y, mainURL_X, pcString, strlen( pcString ) );\r
282         vTaskDelay( mainSPLASH_SCREEN_DELAY );\r
283         LCD_FillRect( 0, 0, CHIP_SCREEN_WIDTH, CHIP_SCREEN_HEIGHT, RGB_WHITE );\r
284 \r
285         for( ;; )\r
286         {\r
287                 /* Wait for a message to arrive that requires displaying. */\r
288                 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );\r
289 \r
290                 /* Check the message type. */\r
291                 if( xMessage.xMessageType == mainUPDATE_BALL_MESSAGE )\r
292                 {\r
293                         /* Read the MEMS and update the ball display on the LCD if required. */\r
294                         MEMS_Handler();\r
295                         POINTER_Handler();\r
296                 }\r
297                 else\r
298                 {\r
299                         /* A text string was sent.  First blank off the old text string, then\r
300                         draw the new text on the next line down. */\r
301                         DRAW_DisplayString( 0, cY, pcBlankLine, strlen( pcBlankLine ) );\r
302 \r
303                         cY -= mainLCD_CHAR_HEIGHT;\r
304                         if( cY <= ( mainLCD_CHAR_HEIGHT - 1 ) )\r
305                         {                       \r
306                                 /* Wrap the line onto which we are going to write the text. */\r
307                                 cY = mainLCD_MAX_Y;\r
308                         }\r
309                         \r
310                         /* Display the message. */\r
311                         DRAW_DisplayString( 0, cY, xMessage.pcMessage, strlen( xMessage.pcMessage ) );\r
312                 }\r
313         }\r
314 }\r
315 /*-----------------------------------------------------------*/\r
316 \r
317 static void prvCheckTask( void *pvParameters )\r
318 {\r
319 portTickType xLastExecutionTime;\r
320 xLCDMessage xMessage;\r
321 static signed portCHAR cPassMessage[ mainMAX_MSG_LEN ];\r
322 extern unsigned portSHORT usMaxJitter;\r
323 \r
324         /* Initialise the xLastExecutionTime variable on task entry. */\r
325         xLastExecutionTime = xTaskGetTickCount();\r
326 \r
327         /* Setup the message we are going to send to the LCD task. */\r
328         xMessage.xMessageType = mainWRITE_STRING_MESSAGE;\r
329         xMessage.pcMessage = cPassMessage;\r
330         \r
331     for( ;; )\r
332         {\r
333                 /* Perform this check every mainCHECK_DELAY milliseconds. */\r
334                 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );\r
335 \r
336                 /* Has an error been found in any task?   If so then point the text\r
337                 we are going to send to the LCD task to an error message instead of\r
338                 the PASS message. */\r
339                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
340                 {\r
341                         xMessage.pcMessage = "ERROR IN GEN Q";\r
342                 }\r
343         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
344                 {\r
345                         xMessage.pcMessage = "ERROR IN BLOCK Q";\r
346                 }\r
347                 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
348                 {\r
349                         xMessage.pcMessage = "ERROR IN BLOCK TIME";\r
350                 }\r
351         else if( xArePollingQueuesStillRunning() != pdTRUE )\r
352         {\r
353             xMessage.pcMessage = "ERROR IN POLL Q";\r
354         }\r
355                 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
356                 {\r
357                         xMessage.pcMessage = "ERROR IN PEEK Q";\r
358                 }\r
359                 else\r
360                 {\r
361                         /* No errors were found in any task, so send a pass message\r
362                         with the max measured jitter time also included (as per the\r
363                         fast interrupt test described at the top of this file and on\r
364                         the online documentation page for this demo application). */\r
365                         sprintf( ( portCHAR * ) cPassMessage, "PASS [%uns]", ( ( unsigned portLONG ) usMaxJitter ) * mainNS_PER_CLOCK );\r
366                 }\r
367 \r
368                 /* Send the message to the LCD gatekeeper for display. */\r
369                 xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );\r
370         }\r
371 }\r
372 /*-----------------------------------------------------------*/\r
373 \r
374 void vApplicationTickHook( void )\r
375 {\r
376 static unsigned portLONG ulCallCount;\r
377 static const xLCDMessage xMemsMessage = { mainUPDATE_BALL_MESSAGE, NULL };\r
378 static portBASE_TYPE xHigherPriorityTaskWoken;\r
379 \r
380         /* Periodically send a message to the LCD task telling it to update\r
381         the MEMS input, and then if necessary the LCD. */\r
382         ulCallCount++;\r
383         if( ulCallCount >= mainMEMS_DELAY )\r
384         {\r
385                 ulCallCount = 0;\r
386                 xHigherPriorityTaskWoken = pdFALSE;\r
387                 xQueueSendFromISR( xLCDQueue, &xMemsMessage, &xHigherPriorityTaskWoken );\r
388         }\r
389 }\r
390 /*-----------------------------------------------------------*/\r
391 \r
392 static void prvSetupHardware( void )\r
393 {\r
394         /* Start with the clocks in their expected state. */\r
395         RCC_DeInit();\r
396 \r
397         /* Enable HSE (high speed external clock). */\r
398         RCC_HSEConfig( RCC_HSE_ON );\r
399 \r
400         /* Wait till HSE is ready. */\r
401         while( RCC_GetFlagStatus( RCC_FLAG_HSERDY ) == RESET )\r
402         {\r
403         }\r
404 \r
405         /* 2 wait states required on the flash. */\r
406         *( ( unsigned portLONG * ) 0x40022000 ) = 0x02;\r
407 \r
408         /* HCLK = SYSCLK */\r
409         RCC_HCLKConfig( RCC_SYSCLK_Div1 );\r
410 \r
411         /* PCLK2 = HCLK */\r
412         RCC_PCLK2Config( RCC_HCLK_Div1 );\r
413 \r
414         /* PCLK1 = HCLK/2 */\r
415         RCC_PCLK1Config( RCC_HCLK_Div2 );\r
416 \r
417         /* PLLCLK = 12MHz * 6 = 72 MHz. */\r
418         RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_6 );\r
419 \r
420         /* Enable PLL. */\r
421         RCC_PLLCmd( ENABLE );\r
422 \r
423         /* Wait till PLL is ready. */\r
424         while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)\r
425         {\r
426         }\r
427 \r
428         /* Select PLL as system clock source. */\r
429         RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );\r
430 \r
431         /* Wait till PLL is used as system clock source. */\r
432         while( RCC_GetSYSCLKSource() != 0x08 )\r
433         {\r
434         }\r
435 \r
436         /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */\r
437         RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC\r
438                                                         | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );\r
439 \r
440         /* SPI2 Periph clock enable */\r
441         RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );\r
442 \r
443 \r
444         /* Set the Vector Table base address at 0x08000000 */\r
445         NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );\r
446 \r
447         NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );\r
448         \r
449         /* Configure HCLK clock as SysTick clock source. */\r
450         SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );\r
451         \r
452         /* Misc initialisation, including some of the CircleOS features.  Note\r
453         that CircleOS itself is not used. */\r
454         vParTestInitialise();\r
455         MEMS_Init();\r
456         POINTER_Init();\r
457         POINTER_SetMode( POINTER_RESTORE_LESS );\r
458 }\r
459 /*-----------------------------------------------------------*/\r
460 \r
461 static void prvFlashTask( void *pvParameters )\r
462 {\r
463 portTickType xLastExecutionTime;\r
464 \r
465         /* Initialise the xLastExecutionTime variable on task entry. */\r
466         xLastExecutionTime = xTaskGetTickCount();\r
467 \r
468     for( ;; )\r
469         {\r
470                 /* Simple toggle the LED periodically.  This just provides some timing\r
471                 verification. */\r
472                 vTaskDelayUntil( &xLastExecutionTime, mainFLASH_DELAY );\r
473                 vParTestToggleLED( 0 );\r
474         }\r
475 }\r
476 /*-----------------------------------------------------------*/\r
477 \r
478 void starting_delay( unsigned long ul )\r
479 {\r
480         vTaskDelay( ( portTickType ) ul );\r
481 }\r
482 \r
483 \r
484 \r