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