]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M0+_Atmel_SAMD20_XPlained/RTOSDemo/src/main.c
b916b791e2f72c84d51cbdbe8da72701537000bf
[freertos] / FreeRTOS / Demo / CORTEX_M0+_Atmel_SAMD20_XPlained / RTOSDemo / src / main.c
1 /*\r
2     FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 /* FreeRTOS includes. */\r
66 #include "FreeRTOS.h"\r
67 #include "task.h"\r
68 \r
69 /* Demo app includes. */\r
70 #include "UARTCommandConsole.h"\r
71 \r
72 /* Library includes. */\r
73 #include <asf.h>\r
74 \r
75 /*-----------------------------------------------------------*/\r
76 \r
77 /*\r
78  * Hardware and driver initialisation can be done in this function.\r
79  */\r
80 static void prvSetupHardware( void );\r
81 \r
82 /* \r
83  * Prototypes for the FreeRTOS hook/callback functions.  See the comments in\r
84  * the implementation of each function for more information.\r
85  */\r
86 void vApplicationMallocFailedHook( void );\r
87 void vApplicationIdleHook( void );\r
88 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName );\r
89 void vApplicationTickHook( void );\r
90 \r
91 /*-----------------------------------------------------------*/\r
92 \r
93 /* Used in the run time stats calculations. */\r
94 static unsigned long ulClocksPer10thOfAMilliSecond = 0UL;\r
95 \r
96 /*-----------------------------------------------------------*/\r
97 \r
98 int main (void)\r
99 {\r
100         prvSetupHardware();\r
101         vUARTCommandConsoleStart( ( configMINIMAL_STACK_SIZE * 3 ), tskIDLE_PRIORITY ); \r
102         \r
103         /* Start the scheduler. */\r
104         vTaskStartScheduler();\r
105 \r
106         /* If all is well, the scheduler will now be running, and the following line\r
107         will never be reached.  If the following line does execute, then there was\r
108         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
109         to be created.  See the memory management section on the FreeRTOS web site\r
110         for more details. */\r
111         for( ;; );\r
112 }\r
113 /*-----------------------------------------------------------*/\r
114 \r
115 static void prvSetupHardware( void )\r
116 {\r
117         system_init();\r
118 }\r
119 /*-----------------------------------------------------------*/\r
120 \r
121 void vApplicationMallocFailedHook( void )\r
122 {\r
123         /* vApplicationMallocFailedHook() will only be called if\r
124         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
125         function that will get called if a call to pvPortMalloc() fails.\r
126         pvPortMalloc() is called internally by the kernel whenever a task, queue,\r
127         timer or semaphore is created.  It is also called by various parts of the\r
128         demo application.  If heap_1.c or heap_2.c are used, then the size of the\r
129         heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in\r
130         FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used\r
131         to query the size of free heap space that remains (although it does not\r
132         provide information on how the remaining heap might be fragmented). */\r
133         taskDISABLE_INTERRUPTS();\r
134         for( ;; );\r
135 }\r
136 /*-----------------------------------------------------------*/\r
137 \r
138 void vApplicationIdleHook( void )\r
139 {\r
140         /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set\r
141         to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle\r
142         task.  It is essential that code added to this hook function never attempts\r
143         to block in any way (for example, call xQueueReceive() with a block time\r
144         specified, or call vTaskDelay()).  If the application makes use of the\r
145         vTaskDelete() API function (as this demo application does) then it is also\r
146         important that vApplicationIdleHook() is permitted to return to its calling\r
147         function, because it is the responsibility of the idle task to clean up\r
148         memory allocated by the kernel to any task that has since been deleted. */\r
149 }\r
150 /*-----------------------------------------------------------*/\r
151 \r
152 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )\r
153 {\r
154         ( void ) pcTaskName;\r
155         ( void ) pxTask;\r
156 \r
157         /* Run time stack overflow checking is performed if\r
158         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
159         function is called if a stack overflow is detected. */\r
160         taskDISABLE_INTERRUPTS();\r
161         for( ;; );\r
162 }\r
163 /*-----------------------------------------------------------*/\r
164 \r
165 void vApplicationTickHook( void )\r
166 {\r
167         /* This function will be called by each tick interrupt if\r
168         configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be\r
169         added here, but the tick hook is called from an interrupt context, so\r
170         code must not attempt to block, and only the interrupt safe FreeRTOS API\r
171         functions can be used (those that end in FromISR()). */\r
172 }\r
173 /*-----------------------------------------------------------*/\r
174 \r
175 void vMainConfigureTimerForRunTimeStats( void )\r
176 {\r
177         /* How many clocks are there per tenth of a millisecond? */\r
178         ulClocksPer10thOfAMilliSecond = configCPU_CLOCK_HZ / 10000UL;\r
179 }\r
180 /*-----------------------------------------------------------*/\r
181 \r
182 unsigned long ulMainGetRunTimeCounterValue( void )\r
183 {\r
184 unsigned long ulSysTickCounts, ulTickCount, ulReturn;\r
185 const unsigned long ulSysTickReloadValue = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;\r
186 volatile unsigned long * const pulCurrentSysTickCount = ( ( volatile unsigned long *) 0xe000e018 );\r
187 volatile unsigned long * const pulInterruptCTRLState = ( ( volatile unsigned long *) 0xe000ed04 );\r
188 const unsigned long ulSysTickPendingBit = 0x04000000UL;\r
189 \r
190         /* NOTE: There are potentially race conditions here.  However, it is used\r
191         anyway to keep the examples simple, and to avoid reliance on a separate\r
192         timer peripheral. */\r
193 \r
194 \r
195         /* The SysTick is a down counter.  How many clocks have passed since it was\r
196         last reloaded? */\r
197         ulSysTickCounts = ulSysTickReloadValue - *pulCurrentSysTickCount;\r
198         \r
199         /* How many times has it overflowed? */\r
200         ulTickCount = xTaskGetTickCountFromISR();\r
201 \r
202         /* This is called from the context switch, so will be called from a\r
203         critical section.  xTaskGetTickCountFromISR() contains its own critical\r
204         section, and the ISR safe critical sections are not designed to nest,\r
205         so reset the critical section. */\r
206         portSET_INTERRUPT_MASK_FROM_ISR();\r
207         \r
208         /* Is there a SysTick interrupt pending? */\r
209         if( ( *pulInterruptCTRLState & ulSysTickPendingBit ) != 0UL )\r
210         {\r
211                 /* There is a SysTick interrupt pending, so the SysTick has overflowed\r
212                 but the tick count not yet incremented. */\r
213                 ulTickCount++;\r
214                 \r
215                 /* Read the SysTick again, as the overflow might have occurred since\r
216                 it was read last. */\r
217                 ulSysTickCounts = ulSysTickReloadValue - *pulCurrentSysTickCount;\r
218         }       \r
219         \r
220         /* Convert the tick count into tenths of a millisecond.  THIS ASSUMES\r
221         configTICK_RATE_HZ is 1000! */\r
222         ulReturn = ( ulTickCount * 10UL ) ;\r
223                 \r
224         /* Add on the number of tenths of a millisecond that have passed since the\r
225         tick count last got updated. */\r
226         ulReturn += ( ulSysTickCounts / ulClocksPer10thOfAMilliSecond );\r
227         \r
228         return ulReturn;        \r
229 }\r
230 \r