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