]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_STM32L152_Discovery_IAR/main_full.c
172f724d309e02f866d8c90e83c143ed096e6a5b
[freertos] / FreeRTOS / Demo / CORTEX_STM32L152_Discovery_IAR / main_full.c
1 /*\r
2     FreeRTOS V8.2.2 - Copyright (C) 2015 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /******************************************************************************\r
71  * NOTE 1:  This project provides two demo applications.  A low power tickless\r
72  * project, and a more comprehensive test and demo application.  The\r
73  * configCREATE_LOW_POWER_DEMO setting in FreeRTOSConfig.h is used to\r
74  * select between the two.  See the notes on using\r
75  * configCREATE_LOW_POWER_DEMO in FreeRTOSConfig.h.  This file implements\r
76  * the comprehensive test and demo version.\r
77  *\r
78  * NOTE 2:  This file only contains the source code that is specific to the\r
79  * full demo.  Generic functions, such FreeRTOS hook functions, and functions\r
80  * required to configure the hardware, are defined in main.c.\r
81  ******************************************************************************\r
82  *\r
83  * main_full() creates all the demo application tasks and a software timer, then\r
84  * starts the scheduler.  The web documentation provides more details of the\r
85  * standard demo application tasks, which provide no particular functionality,\r
86  * but do provide a good example of how to use the FreeRTOS API.\r
87  *\r
88  * In addition to the standard demo tasks, the following tasks and tests are\r
89  * defined and/or created within this file:\r
90  *\r
91  * "Check" timer - The check software timer period is initially set to three\r
92  * seconds.  The callback function associated with the check software timer\r
93  * checks that all the standard demo tasks are not only still executing, but\r
94  * are executing without reporting any errors.  If the check software timer\r
95  * discovers that a task has either stalled, or reported an error, then it\r
96  * changes its own execution period from the initial three seconds, to just\r
97  * 200ms.  The check software timer callback function also toggles an LED each\r
98  * time it is called.  This provides a visual indication of the system status:\r
99  * If the LED toggles every three seconds, then no issues have been discovered.\r
100  * If the LED toggles every 200ms, then an issue has been discovered with at\r
101  * least one task.\r
102  *\r
103  * See the documentation page for this demo on the FreeRTOS.org web site for\r
104  * full information, including hardware setup requirements.\r
105  */\r
106 \r
107 /* Standard includes. */\r
108 #include <stdio.h>\r
109 \r
110 /* Kernel includes. */\r
111 #include "FreeRTOS.h"\r
112 #include "task.h"\r
113 #include "timers.h"\r
114 #include "semphr.h"\r
115 \r
116 /* Standard demo application includes. */\r
117 #include "PollQ.h"\r
118 #include "semtest.h"\r
119 #include "dynamic.h"\r
120 #include "BlockQ.h"\r
121 #include "blocktim.h"\r
122 #include "countsem.h"\r
123 #include "GenQTest.h"\r
124 #include "recmutex.h"\r
125 \r
126 /* ST library functions. */\r
127 #include "stm32l1xx.h"\r
128 #include "discover_board.h"\r
129 #include "stm32l_discovery_lcd.h"\r
130 \r
131 /* Priorities for the demo application tasks. */\r
132 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2UL )\r
133 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1UL )\r
134 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2UL )\r
135 \r
136 /* A block time of zero simply means "don't block". */\r
137 #define mainDONT_BLOCK                                          ( 0UL )\r
138 \r
139 /* The period after which the check timer will expire providing no errors\r
140 have been reported by any of the standard demo tasks.  ms are converted to the\r
141 equivalent in ticks using the portTICK_PERIOD_MS constant. */\r
142 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_PERIOD_MS )\r
143 \r
144 /* The period at which the check timer will expire, in ms, if an error has been\r
145 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
146 in ticks using the portTICK_PERIOD_MS constant. */\r
147 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_PERIOD_MS )\r
148 \r
149 /*-----------------------------------------------------------*/\r
150 \r
151 /*\r
152  * The check timer callback function, as described at the top of this file.\r
153  */\r
154 static void prvCheckTimerCallback( TimerHandle_t xTimer );\r
155 \r
156 /*\r
157  * Configure the LCD, then write welcome message.\r
158  */\r
159 static void prvConfigureLCD( void );\r
160 \r
161 /*-----------------------------------------------------------*/\r
162 \r
163 void main_full( void )\r
164 {\r
165 TimerHandle_t xCheckTimer = NULL;\r
166 \r
167         /* The LCD is only used in the Full demo. */\r
168         prvConfigureLCD();\r
169 \r
170         /* Start all the other standard demo/test tasks.  They have no particular\r
171         functionality, but do demonstrate how to use the FreeRTOS API and test the\r
172         kernel port. */\r
173         vStartDynamicPriorityTasks();\r
174         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
175         vCreateBlockTimeTasks();\r
176         vStartCountingSemaphoreTasks();\r
177         vStartGenericQueueTasks( tskIDLE_PRIORITY );\r
178         vStartRecursiveMutexTasks();\r
179         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
180         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
181 \r
182         /* Create the software timer that performs the 'check' functionality,\r
183         as described at the top of this file. */\r
184         xCheckTimer = xTimerCreate( "CheckTimer",                                       /* A text name, purely to help debugging. */\r
185                                                                 ( mainCHECK_TIMER_PERIOD_MS ),  /* The timer period, in this case 3000ms (3s). */\r
186                                                                 pdTRUE,                                                 /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
187                                                                 ( void * ) 0,                                   /* The ID is not used, so can be set to anything. */\r
188                                                                 prvCheckTimerCallback                   /* The callback function that inspects the status of all the other tasks. */\r
189                                                           );\r
190 \r
191         if( xCheckTimer != NULL )\r
192         {\r
193                 xTimerStart( xCheckTimer, mainDONT_BLOCK );\r
194         }\r
195 \r
196         /* Start the scheduler. */\r
197         vTaskStartScheduler();\r
198 \r
199         /* If all is well, the scheduler will now be running, and the following line\r
200         will never be reached.  If the following line does execute, then there was\r
201         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
202         to be created.  See the memory management section on the FreeRTOS web site\r
203         for more details. */\r
204         for( ;; );\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 static void prvCheckTimerCallback( TimerHandle_t xTimer )\r
209 {\r
210 static long lChangedTimerPeriodAlready = pdFALSE;\r
211 unsigned long ulErrorFound = pdFALSE;\r
212 \r
213         /* Check all the demo tasks to ensure they are all still running, and that\r
214         none have detected an error. */\r
215 \r
216         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
217         {\r
218                 ulErrorFound = pdTRUE;\r
219         }\r
220 \r
221         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
222         {\r
223                 ulErrorFound = pdTRUE;\r
224         }\r
225 \r
226         if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
227         {\r
228                 ulErrorFound = pdTRUE;\r
229         }\r
230 \r
231         if ( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
232         {\r
233                 ulErrorFound = pdTRUE;\r
234         }\r
235 \r
236         if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
237         {\r
238                 ulErrorFound = pdTRUE;\r
239         }\r
240 \r
241         if( xArePollingQueuesStillRunning() != pdTRUE )\r
242         {\r
243                 ulErrorFound = pdTRUE;\r
244         }\r
245 \r
246         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
247         {\r
248                 ulErrorFound = pdTRUE;\r
249         }\r
250 \r
251         /* Toggle the check LED to give an indication of the system status.  If\r
252         the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then\r
253         everything is ok.  A faster toggle indicates an error. */\r
254         GPIO_TOGGLE( LD_GPIO_PORT, LD_GREEN_GPIO_PIN );\r
255 \r
256         /* Have any errors been latch in ulErrorFound?  If so, shorten the\r
257         period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.\r
258         This will result in an increase in the rate at which mainCHECK_LED\r
259         toggles. */\r
260         if( ulErrorFound != pdFALSE )\r
261         {\r
262                 if( lChangedTimerPeriodAlready == pdFALSE )\r
263                 {\r
264                         lChangedTimerPeriodAlready = pdTRUE;\r
265 \r
266                         /* This call to xTimerChangePeriod() uses a zero block time.\r
267                         Functions called from inside of a timer callback function must\r
268                         *never* attempt to block. */\r
269                         xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
270                 }\r
271         }\r
272 }\r
273 /*-----------------------------------------------------------*/\r
274 \r
275 static void prvConfigureLCD( void )\r
276 {\r
277 GPIO_InitTypeDef GPIO_InitStructure;\r
278 \r
279         /* Enable necessary clocks. */\r
280         RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB | RCC_AHBPeriph_GPIOC, ENABLE );\r
281         RCC_APB1PeriphClockCmd( RCC_APB1Periph_LCD, ENABLE );\r
282         PWR_RTCAccessCmd( ENABLE );\r
283         RCC_LSEConfig( ENABLE );\r
284         RCC_RTCCLKConfig( RCC_RTCCLKSource_LSE );\r
285         RCC_RTCCLKCmd( ENABLE );\r
286 \r
287         /* Configure Port A LCD Output pins as alternate function. */\r
288         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_8 | GPIO_Pin_9 |GPIO_Pin_10 |GPIO_Pin_15;\r
289         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;\r
290         GPIO_Init( GPIOA, &GPIO_InitStructure );\r
291 \r
292         /* Select LCD alternate function for Port A LCD Output pins. */\r
293         GPIO_PinAFConfig( GPIOA, GPIO_PinSource1, GPIO_AF_LCD );\r
294         GPIO_PinAFConfig( GPIOA, GPIO_PinSource2, GPIO_AF_LCD );\r
295         GPIO_PinAFConfig( GPIOA, GPIO_PinSource3, GPIO_AF_LCD );\r
296         GPIO_PinAFConfig( GPIOA, GPIO_PinSource8, GPIO_AF_LCD );\r
297         GPIO_PinAFConfig( GPIOA, GPIO_PinSource9, GPIO_AF_LCD );\r
298         GPIO_PinAFConfig( GPIOA, GPIO_PinSource10, GPIO_AF_LCD );\r
299         GPIO_PinAFConfig( GPIOA, GPIO_PinSource15, GPIO_AF_LCD );\r
300 \r
301         /* Configure Port B LCD Output pins as alternate function */\r
302         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;\r
303         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;\r
304         GPIO_Init( GPIOB, &GPIO_InitStructure );\r
305 \r
306         /* Select LCD alternate function for Port B LCD Output pins */\r
307         GPIO_PinAFConfig( GPIOB, GPIO_PinSource3, GPIO_AF_LCD );\r
308         GPIO_PinAFConfig( GPIOB, GPIO_PinSource4, GPIO_AF_LCD );\r
309         GPIO_PinAFConfig( GPIOB, GPIO_PinSource5, GPIO_AF_LCD );\r
310         GPIO_PinAFConfig( GPIOB, GPIO_PinSource8, GPIO_AF_LCD );\r
311         GPIO_PinAFConfig( GPIOB, GPIO_PinSource9, GPIO_AF_LCD );\r
312         GPIO_PinAFConfig( GPIOB, GPIO_PinSource10, GPIO_AF_LCD );\r
313         GPIO_PinAFConfig( GPIOB, GPIO_PinSource11, GPIO_AF_LCD );\r
314         GPIO_PinAFConfig( GPIOB, GPIO_PinSource12, GPIO_AF_LCD );\r
315         GPIO_PinAFConfig( GPIOB, GPIO_PinSource13, GPIO_AF_LCD );\r
316         GPIO_PinAFConfig( GPIOB, GPIO_PinSource14, GPIO_AF_LCD );\r
317         GPIO_PinAFConfig( GPIOB, GPIO_PinSource15, GPIO_AF_LCD );\r
318 \r
319         /* Configure Port C LCD Output pins as alternate function */\r
320         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 |GPIO_Pin_11 ;\r
321         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;\r
322         GPIO_Init( GPIOC, &GPIO_InitStructure );\r
323 \r
324         /* Select LCD alternate function for Port B LCD Output pins */\r
325         GPIO_PinAFConfig( GPIOC, GPIO_PinSource0, GPIO_AF_LCD );\r
326         GPIO_PinAFConfig( GPIOC, GPIO_PinSource1, GPIO_AF_LCD );\r
327         GPIO_PinAFConfig( GPIOC, GPIO_PinSource2, GPIO_AF_LCD );\r
328         GPIO_PinAFConfig( GPIOC, GPIO_PinSource3, GPIO_AF_LCD );\r
329         GPIO_PinAFConfig( GPIOC, GPIO_PinSource6, GPIO_AF_LCD );\r
330         GPIO_PinAFConfig( GPIOC, GPIO_PinSource7, GPIO_AF_LCD );\r
331         GPIO_PinAFConfig( GPIOC, GPIO_PinSource8, GPIO_AF_LCD );\r
332         GPIO_PinAFConfig( GPIOC, GPIO_PinSource9, GPIO_AF_LCD );\r
333         GPIO_PinAFConfig( GPIOC, GPIO_PinSource10, GPIO_AF_LCD );\r
334         GPIO_PinAFConfig( GPIOC, GPIO_PinSource11, GPIO_AF_LCD );\r
335 \r
336         LCD_GLASS_Init();\r
337         LCD_GLASS_DisplayString( "F'RTOS" );\r
338 }\r
339 \r