]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4_ATSAM4L_Atmel_Studio/src/main_low_power.c
Update version number.
[freertos] / FreeRTOS / Demo / CORTEX_M4_ATSAM4L_Atmel_Studio / src / main_low_power.c
1 /*\r
2     FreeRTOS V7.5.1 - 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 /* ****************************************************************************\r
66  * When configCREATE_LOW_POWER_DEMO is set to 1 in FreeRTOSConfig.h main() will\r
67  * call main_low_power(), which is defined in this file.  main_low_power()\r
68  * demonstrates FreeRTOS tick suppression being used to allow the MCU to be\r
69  * placed into the Retention low power mode.  When configCREATE_LOW_POWER_DEMO\r
70  * is set to 0 main will instead call main_full(), which is a more comprehensive\r
71  * RTOS demonstration.\r
72  * ****************************************************************************\r
73  *\r
74  * This application demonstrates the FreeRTOS tickless idle mode (tick\r
75  * suppression).  See http://www.freertos.org/low-power-tickless-rtos.html\r
76  * The demo is configured to execute on the SAM4L-EK.\r
77  *\r
78  * Functionality:\r
79  *\r
80  *  + Two tasks are created, an Rx task and a Tx task.\r
81  *\r
82  *  + The Rx task blocks on a queue to wait for data, blipping an LED each time\r
83  *    data is received (turning it on and then off again) before returning to\r
84  *    block on the queue once more.\r
85  *\r
86  *  + The Tx task repeatedly enters the Blocked state for 500ms.  On exiting the\r
87  *    blocked state the Tx task sends a value through the queue to the Rx task\r
88  *    (causing the Rx task to exit the blocked state and blip the LED).\r
89  *\r
90  *    Blocking for a finite period allows the kernel to stop the tick interrupt\r
91  *    and place the SAM4L into Retention mode - the lowest power mode possible\r
92  *    that allows the CPU registers and RAM to retain their state.\r
93  *\r
94  * Observed behaviour:\r
95  *\r
96  * For correct results the SAM4L-EK must be connected (and powered) using only\r
97  * the JTAG USB connector, but the debugger must not be connected (the\r
98  * application must be executed 'stand alone').\r
99  *\r
100  * The MCU spends most of its time in the Retention low power state, during\r
101  * which times the current monitor (built onto the SAM4L-EK) will show a low\r
102  * current reading.\r
103  *\r
104  * Every 500ms the MCU will come out of the low power state to turn the LED on,\r
105  * then return to the low power state for 20ms before leaving the low power\r
106  * state again to turn the LED off.  This will be observed as a fast blipping\r
107  * on the LED, and two very brief dots appearing on the current monitor graph\r
108  * (often observed as a single dot).\r
109  *\r
110  * The RTOS tick is suppressed while the MCU is in its low power state.\r
111  *\r
112  */\r
113 \r
114 /* ASF includes. */\r
115 #include <asf.h>\r
116 \r
117 /* Kernel includes. */\r
118 #include "FreeRTOS.h"\r
119 #include "task.h"\r
120 #include "queue.h"\r
121 \r
122 /* Common demo includes. */\r
123 #include "partest.h"\r
124 \r
125 /* Priorities at which the Rx and Tx tasks are created. */\r
126 #define configQUEUE_RECEIVE_TASK_PRIORITY       ( tskIDLE_PRIORITY + 1 )\r
127 #define configQUEUE_SEND_TASK_PRIORITY          ( tskIDLE_PRIORITY + 2 )\r
128 \r
129 /* The number of items the queue can hold.  This is 1 as the Rx task will\r
130 remove items as they are added so the Tx task should always find the queue\r
131 empty. */\r
132 #define mainQUEUE_LENGTH                                        ( 1 )\r
133 \r
134 /* The LED used to indicate that a value has been received on the queue. */\r
135 #define mainQUEUE_LED                                           ( 0 )\r
136 \r
137 /* The rate at which the Tx task sends to the queue. */\r
138 #define mainTX_DELAY                                            ( 500UL / portTICK_RATE_MS )\r
139 \r
140 /* A block time of zero simply means "don't block". */\r
141 #define mainDONT_BLOCK                                          ( 0 )\r
142 \r
143 /* The value that is sent from the Tx task to the Rx task on the queue. */\r
144 #define mainQUEUED_VALUE                                        ( 100UL )\r
145 \r
146 /* The length of time the LED will remain on for.  It is on just long enough\r
147 to be able to see with the human eye so as not to distort the power readings too\r
148 much. */\r
149 #define mainLED_TOGGLE_DELAY                            ( 20 / portTICK_RATE_MS )\r
150 \r
151 /*-----------------------------------------------------------*/\r
152 \r
153 /*\r
154  * The Rx and Tx tasks as described at the top of this file.\r
155  */\r
156 static void prvQueueReceiveTask( void *pvParameters );\r
157 static void prvQueueSendTask( void *pvParameters );\r
158 \r
159 /*-----------------------------------------------------------*/\r
160 \r
161 /* The queue to pass data from the Tx task to the Rx task. */\r
162 static xQueueHandle xQueue = NULL;\r
163 \r
164 /*-----------------------------------------------------------*/\r
165 \r
166 void main_low_power( void )\r
167 {\r
168         /* Create the queue. */\r
169         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
170         configASSERT( xQueue );\r
171 \r
172         /* Start the two tasks as described at the top of this file. */\r
173         xTaskCreate( prvQueueReceiveTask, ( const signed char * const ) "Rx", configMINIMAL_STACK_SIZE, NULL, configQUEUE_RECEIVE_TASK_PRIORITY, NULL );\r
174         xTaskCreate( prvQueueSendTask, ( const signed char * const ) "TX", configMINIMAL_STACK_SIZE, NULL, configQUEUE_SEND_TASK_PRIORITY, NULL );\r
175 \r
176         /* Start the scheduler running running. */\r
177         vTaskStartScheduler();\r
178 \r
179         /* If all is well the next line of code will not be reached as the\r
180         scheduler will be running.  If the next line is reached then it is likely\r
181         there was insufficient FreeRTOS heap available for the idle task and/or\r
182         timer task to be created.  See http://www.freertos.org/a00111.html. */\r
183         for( ;; );\r
184 }\r
185 /*-----------------------------------------------------------*/\r
186 \r
187 static void prvQueueSendTask( void *pvParameters )\r
188 {\r
189 const unsigned long ulValueToSend = mainQUEUED_VALUE;\r
190 \r
191         /* Remove compiler warning about unused parameter. */\r
192         ( void ) pvParameters;\r
193 \r
194         for( ;; )\r
195         {\r
196                 /* Place this task into the blocked state until it is time to run again.\r
197                 The kernel will place the MCU into the Retention low power sleep state\r
198                 when the idle task next runs. */\r
199                 vTaskDelay( mainTX_DELAY );\r
200 \r
201                 /* Send to the queue - causing the queue receive task to flash its LED.\r
202                 It should not be necessary to block on the queue send because the Rx\r
203                 task will already have removed the last queued item. */\r
204                 xQueueSend( xQueue, &ulValueToSend, mainDONT_BLOCK );\r
205         }\r
206 }\r
207 /*-----------------------------------------------------------*/\r
208 \r
209 static void prvQueueReceiveTask( void *pvParameters )\r
210 {\r
211 unsigned long ulReceivedValue;\r
212 \r
213         /* Remove compiler warning about unused parameter. */\r
214         ( void ) pvParameters;\r
215 \r
216         for( ;; )\r
217         {\r
218                 /* Wait until something arrives in the queue. */\r
219                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
220 \r
221                 /*  To get here something must have arrived, but is it the expected\r
222                 value?  If it is, turn the LED on for a short while. */\r
223                 if( ulReceivedValue == mainQUEUED_VALUE )\r
224                 {\r
225                         vParTestSetLED( mainQUEUE_LED, pdTRUE );\r
226                         vTaskDelay( mainLED_TOGGLE_DELAY );\r
227                         vParTestSetLED( mainQUEUE_LED, pdFALSE );\r
228                 }\r
229         }\r
230 }\r
231 /*-----------------------------------------------------------*/\r
232 \r
233 void vPreSleepProcessing( unsigned long ulExpectedIdleTime )\r
234 {\r
235         /* Called by the kernel before it places the MCU into a sleep mode because\r
236         configPRE_SLEEP_PROCESSING() is #defined to vPreSleepProcessing().\r
237 \r
238         NOTE:  Additional actions can be taken here to get the power consumption\r
239         even lower.  For example, peripherals can be turned     off here, and then back\r
240         on again in the post sleep processing function.  For maximum power saving\r
241         ensure all unused pins are in their lowest power state. */\r
242 \r
243         /* Avoid compiler warnings about the unused parameter. */\r
244         ( void ) ulExpectedIdleTime;\r
245 }\r
246 /*-----------------------------------------------------------*/\r
247 \r
248 void vPostSleepProcessing( unsigned long ulExpectedIdleTime )\r
249 {\r
250         /* Called by the kernel when the MCU exits a sleep mode because\r
251         configPOST_SLEEP_PROCESSING is #defined to vPostSleepProcessing(). */\r
252 \r
253         /* Avoid compiler warnings about the unused parameter. */\r
254         ( void ) ulExpectedIdleTime;\r
255 }\r
256 /*-----------------------------------------------------------*/\r
257 \r