]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_CEC_MEC_17xx_Keil_GCC/main.c
Change name of the CEC and MEC directory to CORTEX_MPU_CEC_MEC_17xx_51xx_Keil_GCC...
[freertos] / FreeRTOS / Demo / CORTEX_M4F_CEC_MEC_17xx_Keil_GCC / main.c
1 /*\r
2     FreeRTOS V9.0.1 - Copyright (C) 2017 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 /*\r
72  * This is a simple example that creates two tasks and one queue.  One task\r
73  * periodically sends a value to the other, which then prints out a message.\r
74  * Normally such a simple example would toggle an LED, so the message that is\r
75  * printed out is "toggle".\r
76  *\r
77  * The demo configures the kernel to be as simple as possible; FreeRTOSConfig.h\r
78  * excludes most features, including dynamic memory allocation.\r
79  */\r
80 \r
81 /* Microchip includes. */\r
82 #include "common.h"\r
83 \r
84 /* Scheduler includes. */\r
85 #include "FreeRTOS.h"\r
86 #include "task.h"\r
87 #include "queue.h"\r
88 \r
89 /* Priorities at which the tasks are created. */\r
90 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
91 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
92 \r
93 /* The rate at which data is sent to the queue.  The 200ms value is converted\r
94 to ticks using the portTICK_PERIOD_MS constant. */\r
95 #define mainQUEUE_SEND_FREQUENCY_MS                     ( pdMS_TO_TICKS( 1000UL ) )\r
96 \r
97 /* The number of items the queue can hold.  This is 1 as the receive task\r
98 will remove items as they are added, meaning the send task should always find\r
99 the queue empty. */\r
100 #define mainQUEUE_LENGTH_IN_ITEMS                       ( 1 )\r
101 \r
102 /*-----------------------------------------------------------*/\r
103 \r
104 /*\r
105  * Configures the clocks ready to run the demo.\r
106  */\r
107 static void prvSetupHardware( void );\r
108 \r
109 /*\r
110  * Simple routine to print a string to ITM for viewing in the Keil serial debug\r
111  * viewer.\r
112  */\r
113 static void prvITMPrintString( const char *pcString );\r
114 \r
115 /*\r
116  * The tasks as described in the comments at the top of this file.\r
117  */\r
118 static void prvQueueReceiveTask( void *pvParameters );\r
119 static void prvQueueSendTask( void *pvParameters );\r
120 \r
121 /*-----------------------------------------------------------*/\r
122 \r
123 /* configSUPPORT_STATIC_ALLOCATION is 1 and configSUPPORT_DYNAMIC_ALLOCATION is\r
124 0 so the queue structure and the queue storage area can only be statically\r
125 allocated.  See http://TBD for more information.\r
126 The queue storage area is dimensioned to hold just one 32-bit value. */\r
127 static StaticQueue_t xStaticQueue;\r
128 static uint8_t ucQueueStorageArea[ mainQUEUE_LENGTH_IN_ITEMS * sizeof( uint32_t ) ];\r
129 \r
130 /* Holds the handle of the created queue. */\r
131 static QueueHandle_t xQueue = NULL;\r
132 \r
133 /* configSUPPORT_STATIC_ALLOCATION is 1 and configSUPPORT_DYNAMIC_ALLOCATION is\r
134 0 so the task structure and the stacks used by the tasks can only be statically\r
135 allocated.  See http://TBD for more information. */\r
136 StaticTask_t xRxTCBBuffer, xTxTCBBuffer;\r
137 static StackType_t uxRxStackBuffer[ configMINIMAL_STACK_SIZE ], uxTxStackBuffer[ configMINIMAL_STACK_SIZE ];\r
138 \r
139 /*-----------------------------------------------------------*/\r
140 \r
141 int main( void )\r
142 {\r
143         /* Set up the hardware ready to run the demo. */\r
144         prvSetupHardware();\r
145         prvITMPrintString( "Starting\r\n" );\r
146 \r
147         /* Create the queue.  xQueueCreateStatic() has two more parameters than the\r
148         xQueueCreate() function.  The first new parameter is a pointer to the\r
149         pre-allocated queue storage area.  The second new parameter is a pointer to\r
150         the StaticQueue_t structure that will hold the queue state information in\r
151         an anonymous way. */\r
152         xQueue = xQueueCreateStatic( mainQUEUE_LENGTH_IN_ITEMS, /* The maximum number of items the queue can hold. */\r
153                                                                  sizeof( uint32_t ),            /* The size of each item. */\r
154                                                                  ucQueueStorageArea,            /* The buffer used to hold items within the queue. */\r
155                                                                  &xStaticQueue );                       /* The static queue structure that will hold the state of the queue. */\r
156 \r
157         /* Create the two tasks as described in the comments at the top of this\r
158         file. */\r
159         xTaskCreateStatic(      prvQueueReceiveTask,                    /* Function that implements the task. */\r
160                                                 "Rx",                                                   /* Human readable name for the task. */\r
161                                                 configMINIMAL_STACK_SIZE,               /* Task's stack size, in words (not bytes!). */\r
162                                                 NULL,                                                   /* Parameter to pass into the task. */\r
163                                                 mainQUEUE_RECEIVE_TASK_PRIORITY,/* The priority of the task. */\r
164                                                 &( uxRxStackBuffer[ 0 ] ),              /* The buffer to use as the task's stack. */\r
165                                                 &xRxTCBBuffer );                                /* The variable that will hold that task's TCB. */\r
166 \r
167         xTaskCreateStatic(      prvQueueSendTask,                               /* Function that implements the task. */\r
168                                                 "Tx",                                                   /* Human readable name for the task. */\r
169                                                 configMINIMAL_STACK_SIZE,               /* Task's stack size, in words (not bytes!). */\r
170                                                 NULL,                                                   /* Parameter to pass into the task. */\r
171                                                 mainQUEUE_SEND_TASK_PRIORITY,   /* The priority of the task. */\r
172                                                 &( uxTxStackBuffer[ 0 ] ),              /* The buffer to use as the task's stack. */\r
173                                                 &xTxTCBBuffer );                                /* The variable that will hold that task's TCB. */\r
174 \r
175         /* Start the scheduler. */\r
176         vTaskStartScheduler();\r
177 \r
178         /* If dynamic memory allocation was used then the following code line would\r
179         be reached if there was insufficient heap memory available to create either\r
180         the timer or idle tasks.  As this project is using static memory allocation\r
181         then the following line should never be reached. */\r
182         for( ;; );\r
183 }\r
184 /*-----------------------------------------------------------*/\r
185 \r
186 static void prvQueueSendTask( void *pvParameters )\r
187 {\r
188 TickType_t xNextWakeTime;\r
189 const unsigned long ulValueToSend = 100UL;\r
190 \r
191         /* Initialise xNextWakeTime - this only needs to be done once. */\r
192         xNextWakeTime = xTaskGetTickCount();\r
193 \r
194         for( ;; )\r
195         {\r
196                 /* Place this task in the blocked state until it is time to run again.\r
197                 The block time is specified in ticks, the constant used converts ticks\r
198                 to ms.  While in the Blocked state this task will not consume any CPU\r
199                 time. */\r
200                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
201 \r
202                 /* Send to the queue - causing the queue receive task to unblock and\r
203                 toggle the LED.  0 is used as the block time so the sending operation\r
204                 will not block - it shouldn't need to block as the queue should always\r
205                 be empty at this point in the code. */\r
206                 xQueueSend( xQueue, &ulValueToSend, 0U );\r
207         }\r
208 }\r
209 /*-----------------------------------------------------------*/\r
210 \r
211 static void prvQueueReceiveTask( void *pvParameters )\r
212 {\r
213 unsigned long ulReceivedValue;\r
214 \r
215         for( ;; )\r
216         {\r
217                 /* Wait until something arrives in the queue - this task will block\r
218                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
219                 FreeRTOSConfig.h. */\r
220                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
221 \r
222                 /*  To get here something must have been received from the queue, but\r
223                 is it the expected value?  If it is, toggle the LED. */\r
224                 if( ulReceivedValue == 100UL )\r
225                 {\r
226                         /* Output a string in lieu of using an LED. */\r
227                         prvITMPrintString( "Toggle!\r\n" );\r
228                 }\r
229         }\r
230 }\r
231 /*-----------------------------------------------------------*/\r
232 \r
233 static void prvSetupHardware( void )\r
234 {\r
235         SystemInit();\r
236         SystemCoreClockUpdate();\r
237 }\r
238 /*-----------------------------------------------------------*/\r
239 \r
240 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
241 {\r
242         /* If configCHECK_FOR_STACK_OVERFLOW is set to either 1 or 2 then this\r
243         function will automatically get called if a task overflows its stack. */\r
244         ( void ) pxTask;\r
245         ( void ) pcTaskName;\r
246         for( ;; );\r
247 }\r
248 /*-----------------------------------------------------------*/\r
249 \r
250 /* configUSE_STATIC_ALLOCATION is set to 1, so the application must provide an\r
251 implementation of vApplicationGetIdleTaskMemory() to provide the memory that is\r
252 used by the Idle task. */\r
253 void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )\r
254 {\r
255 /* If the buffers to be provided to the Idle task are declared inside this\r
256 function then they must be declared static - otherwise they will be allocated on\r
257 the stack and so not exists after this function exits. */\r
258 static StaticTask_t xIdleTaskTCB;\r
259 static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];\r
260 \r
261         /* Pass out a pointer to the StaticTask_t structure in which the Idle task's\r
262         state will be stored. */\r
263         *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;\r
264 \r
265         /* Pass out the array that will be used as the Idle task's stack. */\r
266         *ppxIdleTaskStackBuffer = uxIdleTaskStack;\r
267 \r
268         /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.\r
269         Note that, as the array is necessarily of type StackType_t,\r
270         configMINIMAL_STACK_SIZE is specified in words, not bytes. */\r
271         *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;\r
272 }\r
273 /*-----------------------------------------------------------*/\r
274 \r
275 /* configUSE_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the\r
276 application must provide an implementation of vApplicationGetTimerTaskMemory()\r
277 to provide the memory that is used by the Timer service task. */\r
278 void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize )\r
279 {\r
280 /* If the buffers to be provided to the Timer task are declared inside this\r
281 function then they must be declared static - otherwise they will be allocated on\r
282 the stack and so not exists after this function exits. */\r
283 static StaticTask_t xTimerTaskTCB;\r
284 static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];\r
285 \r
286         /* Pass out a pointer to the StaticTask_t structure in which the Timer\r
287         task's state will be stored. */\r
288         *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;\r
289 \r
290         /* Pass out the array that will be used as the Timer task's stack. */\r
291         *ppxTimerTaskStackBuffer = uxTimerTaskStack;\r
292 \r
293         /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer.\r
294         Note that, as the array is necessarily of type StackType_t,\r
295         configMINIMAL_STACK_SIZE is specified in words, not bytes. */\r
296         *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;\r
297 }\r
298 /*-----------------------------------------------------------*/\r
299 \r
300 static void prvITMPrintString( const char *pcString )\r
301 {\r
302         while( *pcString != 0x00 )\r
303         {\r
304                 ITM_SendChar( *pcString );\r
305                 pcString++;\r
306         }\r
307 }\r
308 /*-----------------------------------------------------------*/\r
309 \r