]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M0+_Atmel_SAMD20_XPlained/RTOSDemo/src/main.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / CORTEX_M0+_Atmel_SAMD20_XPlained / RTOSDemo / src / 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  * This project provides two demo applications.  A simple blinky style project,\r
31  * and a more comprehensive test and demo application.  The\r
32  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting (defined in this file) is used to\r
33  * select between the two.  The simply blinky demo is implemented and described\r
34  * in main_blinky.c.  The more comprehensive test and demo application is\r
35  * implemented and described in main_full.c.\r
36  *\r
37  * This file implements the code that is not demo specific, including the\r
38  * hardware setup and FreeRTOS hook functions.  It also contains a dummy\r
39  * interrupt service routine called Dummy_IRQHandler() that is provided as an\r
40  * example of how to use interrupt safe FreeRTOS API functions (those that end\r
41  * in "FromISR").\r
42  *\r
43  *****************************************************************************/\r
44 \r
45 /* FreeRTOS includes. */\r
46 #include "FreeRTOS.h"\r
47 #include "task.h"\r
48 \r
49 /* Demo app includes. */\r
50 #include "UARTCommandConsole.h"\r
51 \r
52 /* Demo application include. */\r
53 #include "QueueSet.h"\r
54 \r
55 /* Library includes. */\r
56 #include <asf.h>\r
57 \r
58 /* Set mainCREATE_SIMPLE_BLINKY_DEMO_ONLY to one to run the simple blinky demo,\r
59 or 0 to run the more comprehensive test and demo application. */\r
60 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY      1\r
61 \r
62 /*-----------------------------------------------------------*/\r
63 \r
64 /*\r
65  * Perform any application specific hardware configuration.  \r
66  */\r
67 static void prvSetupHardware( void );\r
68 \r
69 /* \r
70  * Prototypes for the FreeRTOS hook/callback functions.  See the comments in\r
71  * the implementation of each function for more information.\r
72  */\r
73 void vApplicationMallocFailedHook( void );\r
74 void vApplicationIdleHook( void );\r
75 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
76 void vApplicationTickHook( void );\r
77 \r
78 /*\r
79  * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
80  * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.\r
81  */\r
82 extern void main_blinky( void );\r
83 extern void main_full( void );\r
84 \r
85 /*-----------------------------------------------------------*/\r
86 \r
87 /* Used in the run time stats calculations. */\r
88 static unsigned long ulClocksPer10thOfAMilliSecond = 0UL;\r
89 \r
90 /*-----------------------------------------------------------*/\r
91 \r
92 int main (void)\r
93 {\r
94         /* Prepare the hardware for the demo. */\r
95         prvSetupHardware();\r
96 \r
97         /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top\r
98         of this file. */\r
99         #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )\r
100         {\r
101                 main_blinky();\r
102         }\r
103         #else\r
104         {\r
105                 main_full();\r
106         }\r
107         #endif\r
108 }\r
109 /*-----------------------------------------------------------*/\r
110 \r
111 static void prvSetupHardware( void )\r
112 {\r
113         /* Initialisation is performed by the Atmel board support package. */\r
114         system_init();\r
115 }\r
116 /*-----------------------------------------------------------*/\r
117 \r
118 void vApplicationMallocFailedHook( void )\r
119 {\r
120         /* vApplicationMallocFailedHook() will only be called if\r
121         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
122         function that will get called if a call to pvPortMalloc() fails.\r
123         pvPortMalloc() is called internally by the kernel whenever a task, queue,\r
124         timer or semaphore is created.  It is also called by various parts of the\r
125         demo application.  If heap_1.c or heap_2.c are used, then the size of the\r
126         heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in\r
127         FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used\r
128         to query the size of free heap space that remains (although it does not\r
129         provide information on how the remaining heap might be fragmented). */\r
130         taskDISABLE_INTERRUPTS();\r
131         for( ;; );\r
132 }\r
133 /*-----------------------------------------------------------*/\r
134 \r
135 void vApplicationIdleHook( void )\r
136 {\r
137         /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set\r
138         to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle\r
139         task.  It is essential that code added to this hook function never attempts\r
140         to block in any way (for example, call xQueueReceive() with a block time\r
141         specified, or call vTaskDelay()).  If the application makes use of the\r
142         vTaskDelete() API function (as this demo application does) then it is also\r
143         important that vApplicationIdleHook() is permitted to return to its calling\r
144         function, because it is the responsibility of the idle task to clean up\r
145         memory allocated by the kernel to any task that has since been deleted. */\r
146 }\r
147 /*-----------------------------------------------------------*/\r
148 \r
149 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
150 {\r
151         ( void ) pcTaskName;\r
152         ( void ) pxTask;\r
153 \r
154         /* Run time stack overflow checking is performed if\r
155         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
156         function is called if a stack overflow is detected. */\r
157         taskDISABLE_INTERRUPTS();\r
158         for( ;; );\r
159 }\r
160 /*-----------------------------------------------------------*/\r
161 \r
162 void vApplicationTickHook( void )\r
163 {\r
164         /* This function will be called by each tick interrupt if\r
165         configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be\r
166         added here, but the tick hook is called from an interrupt context, so\r
167         code must not attempt to block, and only the interrupt safe FreeRTOS API\r
168         functions can be used (those that end in FromISR()). */\r
169 \r
170         /* The semaphore and associated task are not created when the simple blinky\r
171         demo is used. */\r
172         #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 0\r
173         {\r
174                 /* Write to a queue that is in use as part of the queue set demo to\r
175                 demonstrate using queue sets from an ISR. */\r
176                 vQueueSetAccessQueueSetFromISR();\r
177         }\r
178         #endif /* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY */\r
179 }\r
180 /*-----------------------------------------------------------*/\r
181 \r
182 void vMainConfigureTimerForRunTimeStats( void )\r
183 {\r
184         /* Used by the optional run-time stats gathering functionality. */\r
185         \r
186         /* How many clocks are there per tenth of a millisecond? */\r
187         ulClocksPer10thOfAMilliSecond = configCPU_CLOCK_HZ / 10000UL;\r
188 }\r
189 /*-----------------------------------------------------------*/\r
190 \r
191 unsigned long ulMainGetRunTimeCounterValue( void )\r
192 {\r
193 unsigned long ulSysTickCounts, ulTickCount, ulReturn;\r
194 const unsigned long ulSysTickReloadValue = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;\r
195 volatile unsigned long * const pulCurrentSysTickCount = ( ( volatile unsigned long *) 0xe000e018 );\r
196 volatile unsigned long * const pulInterruptCTRLState = ( ( volatile unsigned long *) 0xe000ed04 );\r
197 const unsigned long ulSysTickPendingBit = 0x04000000UL;\r
198 \r
199         /* Used by the optional run-time stats gathering functionality. */\r
200 \r
201 \r
202         /* NOTE: There are potentially race conditions here.  However, it is used\r
203         anyway to keep the examples simple, and to avoid reliance on a separate\r
204         timer peripheral. */\r
205 \r
206 \r
207         /* The SysTick is a down counter.  How many clocks have passed since it was\r
208         last reloaded? */\r
209         ulSysTickCounts = ulSysTickReloadValue - *pulCurrentSysTickCount;\r
210         \r
211         /* How many times has it overflowed? */\r
212         ulTickCount = xTaskGetTickCountFromISR();\r
213 \r
214         /* This is called from the context switch, so will be called from a\r
215         critical section.  xTaskGetTickCountFromISR() contains its own critical\r
216         section, and the ISR safe critical sections are not designed to nest,\r
217         so reset the critical section. */\r
218         portSET_INTERRUPT_MASK_FROM_ISR();\r
219         \r
220         /* Is there a SysTick interrupt pending? */\r
221         if( ( *pulInterruptCTRLState & ulSysTickPendingBit ) != 0UL )\r
222         {\r
223                 /* There is a SysTick interrupt pending, so the SysTick has overflowed\r
224                 but the tick count not yet incremented. */\r
225                 ulTickCount++;\r
226                 \r
227                 /* Read the SysTick again, as the overflow might have occurred since\r
228                 it was read last. */\r
229                 ulSysTickCounts = ulSysTickReloadValue - *pulCurrentSysTickCount;\r
230         }       \r
231         \r
232         /* Convert the tick count into tenths of a millisecond.  THIS ASSUMES\r
233         configTICK_RATE_HZ is 1000! */\r
234         ulReturn = ( ulTickCount * 10UL ) ;\r
235                 \r
236         /* Add on the number of tenths of a millisecond that have passed since the\r
237         tick count last got updated. */\r
238         ulReturn += ( ulSysTickCounts / ulClocksPer10thOfAMilliSecond );\r
239         \r
240         return ulReturn;        \r
241 }\r
242 /*-----------------------------------------------------------*/\r
243 \r
244 #ifdef JUST_AN_EXAMPLE_ISR\r
245 \r
246 void Dummy_IRQHandler(void)\r
247 {\r
248 long lHigherPriorityTaskWoken = pdFALSE;\r
249 \r
250         /* Clear the interrupt if necessary. */\r
251         Dummy_ClearITPendingBit();\r
252 \r
253         /* This interrupt does nothing more than demonstrate how to synchronise a\r
254         task with an interrupt.  A semaphore is used for this purpose.  Note\r
255         lHigherPriorityTaskWoken is initialised to zero. Only FreeRTOS API functions\r
256         that end in "FromISR" can be called from an ISR. */\r
257         xSemaphoreGiveFromISR( xTestSemaphore, &lHigherPriorityTaskWoken );\r
258 \r
259         /* If there was a task that was blocked on the semaphore, and giving the\r
260         semaphore caused the task to unblock, and the unblocked task has a priority\r
261         higher than the current Running state task (the task that this interrupt\r
262         interrupted), then lHigherPriorityTaskWoken will have been set to pdTRUE\r
263         internally within xSemaphoreGiveFromISR().  Passing pdTRUE into the\r
264         portEND_SWITCHING_ISR() macro will result in a context switch being pended to\r
265         ensure this interrupt returns directly to the unblocked, higher priority,\r
266         task.  Passing pdFALSE into portEND_SWITCHING_ISR() has no effect. */\r
267         portEND_SWITCHING_ISR( lHigherPriorityTaskWoken );\r
268 }\r
269 \r
270 #endif /* JUST_AN_EXAMPLE_ISR */\r
271 \r