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