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