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