]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4_ATSAM4L_Atmel_Studio/src/main_low_power.c
Update version numbers to V7.4.1.
[freertos] / FreeRTOS / Demo / CORTEX_M4_ATSAM4L_Atmel_Studio / src / main_low_power.c
1 /*\r
2     FreeRTOS V7.4.1 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not it can be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
67     Integrity Systems, who sell the code with commercial support,\r
68     indemnification and middleware, under the OpenRTOS brand.\r
69 \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
71     engineered and independently SIL3 certified version for use in safety and\r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /* ****************************************************************************\r
76  * When configCREATE_LOW_POWER_DEMO is set to 1 in FreeRTOSConfig.h main() will\r
77  * call main_low_power(), which is defined in this file.  main_low_power()\r
78  * demonstrates FreeRTOS tick suppression being used to allow the MCU to be\r
79  * placed into the Retention low power mode.  When configCREATE_LOW_POWER_DEMO\r
80  * is set to 0 main will instead call main_full(), which is a more comprehensive\r
81  * RTOS demonstration.\r
82  * ****************************************************************************\r
83  *\r
84  * This application demonstrates the FreeRTOS tickless idle mode (tick\r
85  * suppression).  See http://www.freertos.org/low-power-tickless-rtos.html\r
86  * The demo is configured to execute on the SAM4L-EK.\r
87  *\r
88  * Functionality:\r
89  *\r
90  *  + Two tasks are created, an Rx task and a Tx task.\r
91  *\r
92  *  + The Rx task blocks on a queue to wait for data, blipping an LED each time\r
93  *    data is received (turning it on and then off again) before returning to\r
94  *    block on the queue once more.\r
95  *\r
96  *  + The Tx task repeatedly enters the Blocked state for 500ms.  On exiting the\r
97  *    blocked state the Tx task sends a value through the queue to the Rx task\r
98  *    (causing the Rx task to exit the blocked state and blip the LED).\r
99  *\r
100  *    Blocking for a finite period allows the kernel to stop the tick interrupt\r
101  *    and place the SAM4L into Retention mode - the lowest power mode possible\r
102  *    that allows the CPU registers and RAM to retain their state.\r
103  *\r
104  * Observed behaviour:\r
105  *\r
106  * For correct results the SAM4L-EK must be connected (and powered) using only\r
107  * the JTAG USB connector, but the debugger must not be connected (the\r
108  * application must be executed 'stand alone').\r
109  *\r
110  * The MCU spends most of its time in the Retention low power state, during\r
111  * which times the current monitor (built onto the SAM4L-EK) will show a low\r
112  * current reading.\r
113  *\r
114  * Every 500ms the MCU will come out of the low power state to turn the LED on,\r
115  * then return to the low power state for 20ms before leaving the low power\r
116  * state again to turn the LED off.  This will be observed as a fast blipping\r
117  * on the LED, and two very brief dots appearing on the current monitor graph\r
118  * (often observed as a single dot).\r
119  *\r
120  * The RTOS tick is suppressed while the MCU is in its low power state.\r
121  *\r
122  */\r
123 \r
124 /* ASF includes. */\r
125 #include <asf.h>\r
126 \r
127 /* Kernel includes. */\r
128 #include "FreeRTOS.h"\r
129 #include "task.h"\r
130 #include "queue.h"\r
131 \r
132 /* Common demo includes. */\r
133 #include "partest.h"\r
134 \r
135 /* Priorities at which the Rx and Tx tasks are created. */\r
136 #define configQUEUE_RECEIVE_TASK_PRIORITY       ( tskIDLE_PRIORITY + 1 )\r
137 #define configQUEUE_SEND_TASK_PRIORITY          ( tskIDLE_PRIORITY + 2 )\r
138 \r
139 /* The number of items the queue can hold.  This is 1 as the Rx task will\r
140 remove items as they are added so the Tx task should always find the queue\r
141 empty. */\r
142 #define mainQUEUE_LENGTH                                        ( 1 )\r
143 \r
144 /* The LED used to indicate that a value has been received on the queue. */\r
145 #define mainQUEUE_LED                                           ( 0 )\r
146 \r
147 /* The rate at which the Tx task sends to the queue. */\r
148 #define mainTX_DELAY                                            ( 500UL / portTICK_RATE_MS )\r
149 \r
150 /* A block time of zero simply means "don't block". */\r
151 #define mainDONT_BLOCK                                          ( 0 )\r
152 \r
153 /* The value that is sent from the Tx task to the Rx task on the queue. */\r
154 #define mainQUEUED_VALUE                                        ( 100UL )\r
155 \r
156 /* The length of time the LED will remain on for.  It is on just long enough\r
157 to be able to see with the human eye so as not to distort the power readings too\r
158 much. */\r
159 #define mainLED_TOGGLE_DELAY                            ( 20 / portTICK_RATE_MS )\r
160 \r
161 /*-----------------------------------------------------------*/\r
162 \r
163 /*\r
164  * The Rx and Tx tasks as described at the top of this file.\r
165  */\r
166 static void prvQueueReceiveTask( void *pvParameters );\r
167 static void prvQueueSendTask( void *pvParameters );\r
168 \r
169 /*-----------------------------------------------------------*/\r
170 \r
171 /* The queue to pass data from the Tx task to the Rx task. */\r
172 static xQueueHandle xQueue = NULL;\r
173 \r
174 /*-----------------------------------------------------------*/\r
175 \r
176 void main_low_power( void )\r
177 {\r
178         /* Create the queue. */\r
179         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
180         configASSERT( xQueue );\r
181 \r
182         /* Start the two tasks as described at the top of this file. */\r
183         xTaskCreate( prvQueueReceiveTask, ( const signed char * const ) "Rx", configMINIMAL_STACK_SIZE, NULL, configQUEUE_RECEIVE_TASK_PRIORITY, NULL );\r
184         xTaskCreate( prvQueueSendTask, ( const signed char * const ) "TX", configMINIMAL_STACK_SIZE, NULL, configQUEUE_SEND_TASK_PRIORITY, NULL );\r
185 \r
186         /* Start the scheduler running running. */\r
187         vTaskStartScheduler();\r
188 \r
189         /* If all is well the next line of code will not be reached as the\r
190         scheduler will be running.  If the next line is reached then it is likely\r
191         there was insufficient FreeRTOS heap available for the idle task and/or\r
192         timer task to be created.  See http://www.freertos.org/a00111.html. */\r
193         for( ;; );\r
194 }\r
195 /*-----------------------------------------------------------*/\r
196 \r
197 static void prvQueueSendTask( void *pvParameters )\r
198 {\r
199 const unsigned long ulValueToSend = mainQUEUED_VALUE;\r
200 \r
201         /* Remove compiler warning about unused parameter. */\r
202         ( void ) pvParameters;\r
203 \r
204         for( ;; )\r
205         {\r
206                 /* Place this task into the blocked state until it is time to run again.\r
207                 The kernel will place the MCU into the Retention low power sleep state\r
208                 when the idle task next runs. */\r
209                 vTaskDelay( mainTX_DELAY );\r
210 \r
211                 /* Send to the queue - causing the queue receive task to flash its LED.\r
212                 It should not be necessary to block on the queue send because the Rx\r
213                 task will already have removed the last queued item. */\r
214                 xQueueSend( xQueue, &ulValueToSend, mainDONT_BLOCK );\r
215         }\r
216 }\r
217 /*-----------------------------------------------------------*/\r
218 \r
219 static void prvQueueReceiveTask( void *pvParameters )\r
220 {\r
221 unsigned long ulReceivedValue;\r
222 \r
223         /* Remove compiler warning about unused parameter. */\r
224         ( void ) pvParameters;\r
225 \r
226         for( ;; )\r
227         {\r
228                 /* Wait until something arrives in the queue. */\r
229                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
230 \r
231                 /*  To get here something must have arrived, but is it the expected\r
232                 value?  If it is, turn the LED on for a short while. */\r
233                 if( ulReceivedValue == mainQUEUED_VALUE )\r
234                 {\r
235                         vParTestSetLED( mainQUEUE_LED, pdTRUE );\r
236                         vTaskDelay( mainLED_TOGGLE_DELAY );\r
237                         vParTestSetLED( mainQUEUE_LED, pdFALSE );\r
238                 }\r
239         }\r
240 }\r
241 /*-----------------------------------------------------------*/\r
242 \r
243 void vPreSleepProcessing( unsigned long ulExpectedIdleTime )\r
244 {\r
245         /* Called by the kernel before it places the MCU into a sleep mode because\r
246         configPRE_SLEEP_PROCESSING() is #defined to vPreSleepProcessing().\r
247 \r
248         NOTE:  Additional actions can be taken here to get the power consumption\r
249         even lower.  For example, peripherals can be turned     off here, and then back\r
250         on again in the post sleep processing function.  For maximum power saving\r
251         ensure all unused pins are in their lowest power state. */\r
252 \r
253         /* Avoid compiler warnings about the unused parameter. */\r
254         ( void ) ulExpectedIdleTime;\r
255 }\r
256 /*-----------------------------------------------------------*/\r
257 \r
258 void vPostSleepProcessing( unsigned long ulExpectedIdleTime )\r
259 {\r
260         /* Called by the kernel when the MCU exits a sleep mode because\r
261         configPOST_SLEEP_PROCESSING is #defined to vPostSleepProcessing(). */\r
262 \r
263         /* Avoid compiler warnings about the unused parameter. */\r
264         ( void ) ulExpectedIdleTime;\r
265 }\r
266 /*-----------------------------------------------------------*/\r
267 \r