]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_CEC_MEC_17xx_51xx_Keil_GCC/main.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / CORTEX_M4F_CEC_MEC_17xx_51xx_Keil_GCC / main.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 \r
30 /*\r
31  * This is a simple example that creates two tasks and one queue.  One task\r
32  * periodically sends a value to the other, which then prints out a message.\r
33  * Normally such a simple example would toggle an LED, so the message that is\r
34  * printed out is "toggle".\r
35  *\r
36  * The demo configures the kernel to be as simple as possible; FreeRTOSConfig.h\r
37  * excludes most features, including dynamic memory allocation.\r
38  */\r
39 \r
40 /* Microchip includes. */\r
41 #include "common.h"\r
42 \r
43 /* Scheduler includes. */\r
44 #include "FreeRTOS.h"\r
45 #include "task.h"\r
46 #include "queue.h"\r
47 \r
48 /* Priorities at which the tasks are created. */\r
49 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
50 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
51 \r
52 /* The rate at which data is sent to the queue.  The 200ms value is converted\r
53 to ticks using the portTICK_PERIOD_MS constant. */\r
54 #define mainQUEUE_SEND_FREQUENCY_MS                     ( pdMS_TO_TICKS( 1000UL ) )\r
55 \r
56 /* The number of items the queue can hold.  This is 1 as the receive task\r
57 will remove items as they are added, meaning the send task should always find\r
58 the queue empty. */\r
59 #define mainQUEUE_LENGTH_IN_ITEMS                       ( 1 )\r
60 \r
61 /*-----------------------------------------------------------*/\r
62 \r
63 /*\r
64  * Configures the clocks ready to run the demo.\r
65  */\r
66 static void prvSetupHardware( void );\r
67 \r
68 /*\r
69  * Simple routine to print a string to ITM for viewing in the Keil serial debug\r
70  * viewer.\r
71  */\r
72 static void prvITMPrintString( const char *pcString );\r
73 \r
74 /*\r
75  * The tasks as described in the comments at the top of this file.\r
76  */\r
77 static void prvQueueReceiveTask( void *pvParameters );\r
78 static void prvQueueSendTask( void *pvParameters );\r
79 \r
80 /*-----------------------------------------------------------*/\r
81 \r
82 /* configSUPPORT_STATIC_ALLOCATION is 1 and configSUPPORT_DYNAMIC_ALLOCATION is\r
83 0 so the queue structure and the queue storage area can only be statically\r
84 allocated.  See http://TBD for more information.\r
85 The queue storage area is dimensioned to hold just one 32-bit value. */\r
86 static StaticQueue_t xStaticQueue;\r
87 static uint8_t ucQueueStorageArea[ mainQUEUE_LENGTH_IN_ITEMS * sizeof( uint32_t ) ];\r
88 \r
89 /* Holds the handle of the created queue. */\r
90 static QueueHandle_t xQueue = NULL;\r
91 \r
92 /* configSUPPORT_STATIC_ALLOCATION is 1 and configSUPPORT_DYNAMIC_ALLOCATION is\r
93 0 so the task structure and the stacks used by the tasks can only be statically\r
94 allocated.  See http://TBD for more information. */\r
95 StaticTask_t xRxTCBBuffer, xTxTCBBuffer;\r
96 static StackType_t uxRxStackBuffer[ configMINIMAL_STACK_SIZE ], uxTxStackBuffer[ configMINIMAL_STACK_SIZE ];\r
97 \r
98 /*-----------------------------------------------------------*/\r
99 \r
100 int main( void )\r
101 {\r
102         /* Set up the hardware ready to run the demo. */\r
103         prvSetupHardware();\r
104         prvITMPrintString( "Starting\r\n" );\r
105 \r
106         /* Create the queue.  xQueueCreateStatic() has two more parameters than the\r
107         xQueueCreate() function.  The first new parameter is a pointer to the\r
108         pre-allocated queue storage area.  The second new parameter is a pointer to\r
109         the StaticQueue_t structure that will hold the queue state information in\r
110         an anonymous way. */\r
111         xQueue = xQueueCreateStatic( mainQUEUE_LENGTH_IN_ITEMS, /* The maximum number of items the queue can hold. */\r
112                                                                  sizeof( uint32_t ),            /* The size of each item. */\r
113                                                                  ucQueueStorageArea,            /* The buffer used to hold items within the queue. */\r
114                                                                  &xStaticQueue );                       /* The static queue structure that will hold the state of the queue. */\r
115 \r
116         /* Create the two tasks as described in the comments at the top of this\r
117         file. */\r
118         xTaskCreateStatic(      prvQueueReceiveTask,                    /* Function that implements the task. */\r
119                                                 "Rx",                                                   /* Human readable name for the task. */\r
120                                                 configMINIMAL_STACK_SIZE,               /* Task's stack size, in words (not bytes!). */\r
121                                                 NULL,                                                   /* Parameter to pass into the task. */\r
122                                                 mainQUEUE_RECEIVE_TASK_PRIORITY,/* The priority of the task. */\r
123                                                 &( uxRxStackBuffer[ 0 ] ),              /* The buffer to use as the task's stack. */\r
124                                                 &xRxTCBBuffer );                                /* The variable that will hold that task's TCB. */\r
125 \r
126         xTaskCreateStatic(      prvQueueSendTask,                               /* Function that implements the task. */\r
127                                                 "Tx",                                                   /* Human readable name for the task. */\r
128                                                 configMINIMAL_STACK_SIZE,               /* Task's stack size, in words (not bytes!). */\r
129                                                 NULL,                                                   /* Parameter to pass into the task. */\r
130                                                 mainQUEUE_SEND_TASK_PRIORITY,   /* The priority of the task. */\r
131                                                 &( uxTxStackBuffer[ 0 ] ),              /* The buffer to use as the task's stack. */\r
132                                                 &xTxTCBBuffer );                                /* The variable that will hold that task's TCB. */\r
133 \r
134         /* Start the scheduler. */\r
135         vTaskStartScheduler();\r
136 \r
137         /* If dynamic memory allocation was used then the following code line would\r
138         be reached if there was insufficient heap memory available to create either\r
139         the timer or idle tasks.  As this project is using static memory allocation\r
140         then the following line should never be reached. */\r
141         for( ;; );\r
142 }\r
143 /*-----------------------------------------------------------*/\r
144 \r
145 static void prvQueueSendTask( void *pvParameters )\r
146 {\r
147 TickType_t xNextWakeTime;\r
148 const unsigned long ulValueToSend = 100UL;\r
149 \r
150         /* Initialise xNextWakeTime - this only needs to be done once. */\r
151         xNextWakeTime = xTaskGetTickCount();\r
152 \r
153         for( ;; )\r
154         {\r
155                 /* Place this task in the blocked state until it is time to run again.\r
156                 The block time is specified in ticks, the constant used converts ticks\r
157                 to ms.  While in the Blocked state this task will not consume any CPU\r
158                 time. */\r
159                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
160 \r
161                 /* Send to the queue - causing the queue receive task to unblock and\r
162                 toggle the LED.  0 is used as the block time so the sending operation\r
163                 will not block - it shouldn't need to block as the queue should always\r
164                 be empty at this point in the code. */\r
165                 xQueueSend( xQueue, &ulValueToSend, 0U );\r
166         }\r
167 }\r
168 /*-----------------------------------------------------------*/\r
169 \r
170 static void prvQueueReceiveTask( void *pvParameters )\r
171 {\r
172 unsigned long ulReceivedValue;\r
173 \r
174         for( ;; )\r
175         {\r
176                 /* Wait until something arrives in the queue - this task will block\r
177                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
178                 FreeRTOSConfig.h. */\r
179                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
180 \r
181                 /*  To get here something must have been received from the queue, but\r
182                 is it the expected value?  If it is, toggle the LED. */\r
183                 if( ulReceivedValue == 100UL )\r
184                 {\r
185                         /* Output a string in lieu of using an LED. */\r
186                         prvITMPrintString( "Toggle!\r\n" );\r
187                 }\r
188         }\r
189 }\r
190 /*-----------------------------------------------------------*/\r
191 \r
192 static void prvSetupHardware( void )\r
193 {\r
194         SystemInit();\r
195         SystemCoreClockUpdate();\r
196 }\r
197 /*-----------------------------------------------------------*/\r
198 \r
199 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
200 {\r
201         /* If configCHECK_FOR_STACK_OVERFLOW is set to either 1 or 2 then this\r
202         function will automatically get called if a task overflows its stack. */\r
203         ( void ) pxTask;\r
204         ( void ) pcTaskName;\r
205         for( ;; );\r
206 }\r
207 /*-----------------------------------------------------------*/\r
208 \r
209 /* configUSE_STATIC_ALLOCATION is set to 1, so the application must provide an\r
210 implementation of vApplicationGetIdleTaskMemory() to provide the memory that is\r
211 used by the Idle task. */\r
212 void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )\r
213 {\r
214 /* If the buffers to be provided to the Idle task are declared inside this\r
215 function then they must be declared static - otherwise they will be allocated on\r
216 the stack and so not exists after this function exits. */\r
217 static StaticTask_t xIdleTaskTCB;\r
218 static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];\r
219 \r
220         /* Pass out a pointer to the StaticTask_t structure in which the Idle task's\r
221         state will be stored. */\r
222         *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;\r
223 \r
224         /* Pass out the array that will be used as the Idle task's stack. */\r
225         *ppxIdleTaskStackBuffer = uxIdleTaskStack;\r
226 \r
227         /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.\r
228         Note that, as the array is necessarily of type StackType_t,\r
229         configMINIMAL_STACK_SIZE is specified in words, not bytes. */\r
230         *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;\r
231 }\r
232 /*-----------------------------------------------------------*/\r
233 \r
234 /* configUSE_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the\r
235 application must provide an implementation of vApplicationGetTimerTaskMemory()\r
236 to provide the memory that is used by the Timer service task. */\r
237 void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize )\r
238 {\r
239 /* If the buffers to be provided to the Timer task are declared inside this\r
240 function then they must be declared static - otherwise they will be allocated on\r
241 the stack and so not exists after this function exits. */\r
242 static StaticTask_t xTimerTaskTCB;\r
243 static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];\r
244 \r
245         /* Pass out a pointer to the StaticTask_t structure in which the Timer\r
246         task's state will be stored. */\r
247         *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;\r
248 \r
249         /* Pass out the array that will be used as the Timer task's stack. */\r
250         *ppxTimerTaskStackBuffer = uxTimerTaskStack;\r
251 \r
252         /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer.\r
253         Note that, as the array is necessarily of type StackType_t,\r
254         configMINIMAL_STACK_SIZE is specified in words, not bytes. */\r
255         *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;\r
256 }\r
257 /*-----------------------------------------------------------*/\r
258 \r
259 static void prvITMPrintString( const char *pcString )\r
260 {\r
261         while( *pcString != 0x00 )\r
262         {\r
263                 ITM_SendChar( *pcString );\r
264                 pcString++;\r
265         }\r
266 }\r
267 /*-----------------------------------------------------------*/\r
268 \r