]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RX100-RSK_Renesas_e2studio/RTOSDemo/main_low_power.c
Change version numbers in preparation for V7.6.0 release.
[freertos] / FreeRTOS / Demo / RX100-RSK_Renesas_e2studio / RTOSDemo / main_low_power.c
1 /*\r
2     FreeRTOS V7.6.0 - Copyright (C) 2013 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     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
28     >>! a combined work that includes FreeRTOS without being obliged to provide\r
29     >>! the source code for proprietary components outside of the FreeRTOS\r
30     >>! kernel.\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /* ****************************************************************************\r
67  * When configCREATE_LOW_POWER_DEMO is set to 1 in FreeRTOSConfig.h main() will\r
68  * call main_low_power(), which is defined in this file.  main_low_power()\r
69  * demonstrates FreeRTOS tick suppression being used to allow the MCU to be\r
70  * placed into both the low power deep sleep mode and the low power software\r
71  * standby mode.  When configCREATE_LOW_POWER_DEMO is set to 0 main will\r
72  * instead call main_full(), which is a more comprehensive RTOS demonstration.\r
73  * ****************************************************************************\r
74  *\r
75  * This application demonstrates the FreeRTOS tickless idle mode (tick\r
76  * suppression).  See http://www.freertos.org/low-power-tickless-rtos.html\r
77  * The demo is configured to execute on the Renesas RX100 RSK.\r
78  *\r
79  *  Functionality:\r
80  *\r
81  *  + Two tasks are created, an Rx task and a Tx task.\r
82  *\r
83  *  + The Rx task repeatedly blocks on a queue to wait for data.  The Rx task\r
84  *    toggles LED 0 each time is receives a value from the queue.\r
85  *\r
86  *  + The Tx task repeatedly enters the Blocked state for an amount of time\r
87  *    that is set by the position of the potentiometer.  On exiting the blocked\r
88  *    state the Tx task sends a value through the queue to the Rx task (causing\r
89  *    the Rx task to exit the blocked state and toggle LED 0).\r
90  *\r
91  *    If the value read from the potentiometer is less than or equal to\r
92  *    mainSOFTWARE_STANDBY_DELAY then the Tx task blocks for the equivalent\r
93  *    number of milliseconds.  For example, if the sampled analog value is\r
94  *    2000, then the Tx task blocks for 2000ms.  Blocking for a finite period\r
95  *    allows the kernel to stop the tick interrupt and place the RX100 into\r
96  *    deep sleep mode.\r
97  *\r
98  *    If the value read form the potentiometer is greater than\r
99  *    mainSOFTWARE_STANDBY_DELAY then the Tx task blocks on a semaphore with\r
100  *    an infinite timeout.  Blocking with an infinite timeout allows the kernel\r
101  *    to stop the tick interrupt and place the RX100 into software standby\r
102  *    mode.  Pressing a button will generate an interrupt that causes the RX100\r
103  *    to exit software standby mode.  The interrupt service routine 'gives' the\r
104  *    semaphore to unblock the Tx task.\r
105  *\r
106  *\r
107  *  Using the Demo and Observed Behaviour:\r
108  *\r
109  *  1) Turn the potentiometer completely counter clockwise.\r
110  *\r
111  *  2) Program the RX100 with the application, then disconnect the programming/\r
112  *   debugging hardware to ensure power readings are not effected by any\r
113  *   connected interfaces.\r
114  *\r
115  *  3) Start the application running.  LED 0 will toggle quickly because the\r
116  *   potentiometer is turned to its lowest value.  LED 1 will be illuminated\r
117  *   when the RX100 is not in a power saving mode, but will appear to be off\r
118  *   because most execution time is spent in a sleep mode.  Led 2 will be\r
119  *   illuminated when the RX100 is in deep sleep mode, and will appear to be\r
120  *   always on, again because most execution time is spent in deep sleep mode.\r
121  *   The LEDs are turned on and off by the application defined pre and post\r
122  *   sleep macros (see the definitions of configPRE_SLEEP_PROCESSING() and\r
123  *   configPOST_SLEEP_PROCESSING() in FreeRTOSConfig.h).\r
124  *\r
125  *  4) Slowly turn the potentiometer in the clockwise direction.  This will\r
126  *   increase the value read from the potentiometer, which will increase the\r
127  *   time the Tx task spends in the Blocked state, which will therefore\r
128  *   decrease the frequency at which the Tx task sends data to the queue (and\r
129  *   the rate at which LED 0 is toggled).\r
130  *\r
131  *  5) Keep turning the potentiometer in the clockwise direction.  Eventually\r
132  *   the value read from the potentiometer will go above\r
133  *   mainSOFTWARE_STANDBY_DELAY, causing the Tx task to block on the semaphore\r
134  *   with an infinite timeout.  LED 0 will stop toggling because the Tx task is\r
135  *   no longer sending to the queue.  LED 1 and LED 2 will both be off because\r
136  *   the RX100 is neither running or in deep sleep mode (it is in software\r
137  *   standby mode).\r
138  *\r
139  *  6) Turn the potentiometer counter clockwise again to ensure its value goes\r
140  *   back below mainSOFTWARE_STANDBY_DELAY.\r
141  *\r
142  *  7) Press any of the three buttons to generate an interrupt.  The interrupt\r
143  *   will take the RX100 out of software standby mode, and the interrupt\r
144  *   service routine will unblock the Tx task by 'giving' the semaphore.  LED 0\r
145  *   will then start to toggle again.\r
146  *\r
147  */\r
148 \r
149 \r
150 /* Hardware specific includes. */\r
151 #include "platform.h"\r
152 #include "r_switches_if.h"\r
153 \r
154 /* Kernel includes. */\r
155 #include "FreeRTOS.h"\r
156 #include "task.h"\r
157 #include "queue.h"\r
158 #include "semphr.h"\r
159 \r
160 /* Common demo includes. */\r
161 #include "partest.h"\r
162 \r
163 /* Priorities at which the Rx and Tx tasks are created. */\r
164 #define configQUEUE_RECEIVE_TASK_PRIORITY       ( tskIDLE_PRIORITY + 1 )\r
165 #define configQUEUE_SEND_TASK_PRIORITY          ( tskIDLE_PRIORITY + 2 )\r
166 \r
167 /* The number of items the queue can hold.  This is 1 as the Rx task will\r
168 remove items as they are added so the Tx task should always find the queue\r
169 empty. */\r
170 #define mainQUEUE_LENGTH                                        ( 1 )\r
171 \r
172 /* The LED used to indicate that a value has been received on the queue. */\r
173 #define mainQUEUE_LED                                           ( 0 )\r
174 \r
175 /* The LED used to indicate that full power is being used (the MCU is not in\r
176 deep sleep or software standby mode). */\r
177 #define mainFULL_POWER_LED                                      ( 1 )\r
178 \r
179 /* The LED used to indicate that deep sleep mode is being used. */\r
180 #define mainDEEP_SLEEP_LED                                      ( 2 )\r
181 \r
182 /* The Tx task sends to the queue with a frequency that is set by the value\r
183 read from the potentiometer until the value goes above that set by the\r
184 mainSOFTWARE_STANDBY_DELAY constant - at which time the Tx task instead blocks\r
185 indefinitely on a semaphore. */\r
186 #define mainSOFTWARE_STANDBY_DELAY                      ( 3000UL )\r
187 \r
188 /* A block time of zero simply means "don't block". */\r
189 #define mainDONT_BLOCK                                          ( 0 )\r
190 \r
191 /* The value that is sent from the Tx task to the Rx task on the queue. */\r
192 #define mainQUEUED_VALUE                                        ( 100UL )\r
193 \r
194 /*-----------------------------------------------------------*/\r
195 \r
196 /*\r
197  * The Rx and Tx tasks as described at the top of this file.\r
198  */\r
199 static void prvQueueReceiveTask( void *pvParameters );\r
200 static void prvQueueSendTask( void *pvParameters );\r
201 \r
202 /*\r
203  * Reads and returns the value of the ADC connected to the potentiometer built\r
204  * onto the RSK.\r
205  */\r
206 static unsigned short prvReadPOT( void );\r
207 \r
208 /*\r
209  * The handler for the interrupt generated when any of the buttons are pressed.\r
210  */\r
211 void vButtonInterruptCallback( void );\r
212 \r
213 /*-----------------------------------------------------------*/\r
214 \r
215 /* The queue to pass data from the Tx task to the Rx task. */\r
216 static xQueueHandle xQueue = NULL;\r
217 \r
218 /* The semaphore that is 'given' by interrupts generated from button pushes. */\r
219 static xSemaphoreHandle xSemaphore = NULL;\r
220 \r
221 /*-----------------------------------------------------------*/\r
222 \r
223 void main_low_power( void )\r
224 {\r
225         /* Create the queue. */\r
226         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
227         configASSERT( xQueue );\r
228 \r
229         /* Create the semaphore that is 'given' by an interrupt generated from a\r
230         button push. */\r
231         vSemaphoreCreateBinary( xSemaphore );\r
232         configASSERT( xSemaphore );\r
233 \r
234         /* Make sure the semaphore starts in the expected state - no button pushes\r
235         have yet occurred.  A block time of zero can be used as it is guaranteed\r
236         that the semaphore will be available because it has just been created. */\r
237         xSemaphoreTake( xSemaphore, mainDONT_BLOCK );\r
238 \r
239         /* Start the two tasks as described at the top of this file. */\r
240         xTaskCreate( prvQueueReceiveTask, "Rx", configMINIMAL_STACK_SIZE, NULL, configQUEUE_RECEIVE_TASK_PRIORITY, NULL );\r
241         xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, configQUEUE_SEND_TASK_PRIORITY, NULL );\r
242 \r
243         /* The CPU is currently running, not sleeping, so turn on the LED that\r
244         shows the CPU is not in a sleep mode. */\r
245         vParTestSetLED( mainFULL_POWER_LED, pdTRUE );\r
246 \r
247         /* Start the scheduler running running. */\r
248         vTaskStartScheduler();\r
249 \r
250         /* If all is well the next line of code will not be reached as the\r
251         scheduler will be running.  If the next line is reached then it is likely\r
252         there was insufficient FreeRTOS heap available for the idle task and/or\r
253         timer task to be created.  See http://www.freertos.org/a00111.html. */\r
254         for( ;; );\r
255 }\r
256 /*-----------------------------------------------------------*/\r
257 \r
258 static void prvQueueSendTask( void *pvParameters )\r
259 {\r
260 portTickType xDelay;\r
261 const unsigned long ulValueToSend = mainQUEUED_VALUE;\r
262 \r
263         /* Remove compiler warning about unused parameter. */\r
264         ( void ) pvParameters;\r
265 \r
266         for( ;; )\r
267         {\r
268                 /* The delay period between successive sends to the queue is set by\r
269                 the potentiometer reading. */\r
270                 xDelay = ( portTickType ) prvReadPOT();\r
271 \r
272                 /* If the block time is greater than 3000 milliseconds then block\r
273                 indefinitely waiting for a button push. */\r
274                 if( xDelay > mainSOFTWARE_STANDBY_DELAY )\r
275                 {\r
276                         /* As this is an indefinite delay the kernel will place the CPU\r
277                         into software standby mode the next time the idle task runs. */\r
278                         xSemaphoreTake( xSemaphore, portMAX_DELAY );\r
279                 }\r
280                 else\r
281                 {\r
282                         /* Convert a time in milliseconds to a time in ticks. */\r
283                         xDelay /= portTICK_RATE_MS;\r
284 \r
285                         /* Place this task in the blocked state until it is time to run\r
286                         again.  As this is not an indefinite sleep the kernel will place\r
287                         the CPU into the deep sleep state when the idle task next runs. */\r
288                         vTaskDelay( xDelay );\r
289                 }\r
290 \r
291                 /* Send to the queue - causing the queue receive task to flash its LED.\r
292                 It should not be necessary to block on the queue send because the Rx\r
293                 task will have removed the last queued item. */\r
294                 xQueueSend( xQueue, &ulValueToSend, mainDONT_BLOCK );\r
295         }\r
296 }\r
297 /*-----------------------------------------------------------*/\r
298 \r
299 static void prvQueueReceiveTask( void *pvParameters )\r
300 {\r
301 unsigned long ulReceivedValue;\r
302 \r
303         /* Remove compiler warning about unused parameter. */\r
304         ( void ) pvParameters;\r
305 \r
306         for( ;; )\r
307         {\r
308                 /* Wait until something arrives in the queue - this will block\r
309                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
310                 FreeRTOSConfig.h. */\r
311                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
312 \r
313                 /*  To get here something must have arrived, but is it the expected\r
314                 value?  If it is, toggle the LED. */\r
315                 if( ulReceivedValue == mainQUEUED_VALUE )\r
316                 {\r
317                         vParTestToggleLED( mainQUEUE_LED );\r
318                 }\r
319         }\r
320 }\r
321 /*-----------------------------------------------------------*/\r
322 \r
323 void vPreSleepProcessing( unsigned long ulExpectedIdleTime )\r
324 {\r
325         /* Called by the kernel before it places the MCU into a sleep mode because\r
326         configPRE_SLEEP_PROCESSING() is #defined to vPreSleepProcessing().\r
327 \r
328         NOTE:  Additional actions can be taken here to get the power consumption\r
329         even lower.  For example, the ADC input used by this demo could be turned\r
330         off here, and then back on again in the power sleep processing function.\r
331         For maximum power saving ensure all unused pins are in their lowest power\r
332         state. */\r
333 \r
334         /* Avoid compiler warnings about the unused parameter. */\r
335         ( void ) ulExpectedIdleTime;\r
336 \r
337         /* Is the MCU about to enter deep sleep mode or software standby mode? */\r
338         if( SYSTEM.SBYCR.BIT.SSBY == 0 )\r
339         {\r
340                 /* Turn on the LED that indicates deep sleep mode is being entered. */\r
341                 vParTestSetLED( mainDEEP_SLEEP_LED, pdTRUE );\r
342         }\r
343         else\r
344         {\r
345                 /* Software standby mode is being used, so no LEDs are illuminated to\r
346                 ensure minimum power readings are obtained.  Ensure the Queue LED is\r
347                 also off. */\r
348                 vParTestSetLED( mainQUEUE_LED, pdFALSE );\r
349         }\r
350 \r
351         /* Turn off the LED that indicates full power is being used. */\r
352         vParTestSetLED( mainFULL_POWER_LED, pdFALSE );\r
353 }\r
354 /*-----------------------------------------------------------*/\r
355 \r
356 void vPostSleepProcessing( unsigned long ulExpectedIdleTime )\r
357 {\r
358         /* Called by the kernel when the MCU exits a sleep mode because\r
359         configPOST_SLEEP_PROCESSING is #defined to vPostSleepProcessing(). */\r
360 \r
361         /* Avoid compiler warnings about the unused parameter. */\r
362         ( void ) ulExpectedIdleTime;\r
363 \r
364         /* Turn off the LED that indicates deep sleep mode, and turn on the LED\r
365         that indicates full power is being used. */\r
366         vParTestSetLED( mainDEEP_SLEEP_LED, pdFALSE );\r
367         vParTestSetLED( mainFULL_POWER_LED, pdTRUE );\r
368 }\r
369 /*-----------------------------------------------------------*/\r
370 \r
371 static unsigned short prvReadPOT( void )\r
372 {\r
373 unsigned short usADCValue;\r
374 const unsigned short usMinADCValue = 128;\r
375 \r
376         /* Start an ADC scan. */\r
377         S12AD.ADCSR.BIT.ADST = 1;\r
378         while( S12AD.ADCSR.BIT.ADST == 1 )\r
379         {\r
380                 /* Just waiting for the ADC scan to complete.  Inefficient\r
381                 polling! */\r
382         }\r
383 \r
384         usADCValue = S12AD.ADDR4;\r
385 \r
386         /* Don't let the ADC value get too small as the LED behaviour will look\r
387         erratic. */\r
388         if( usADCValue < usMinADCValue )\r
389         {\r
390                 usADCValue = usMinADCValue;\r
391         }\r
392 \r
393         return usADCValue;\r
394 }\r
395 /*-----------------------------------------------------------*/\r
396 \r
397 void vButtonInterruptCallback( void )\r
398 {\r
399 long lHigherPriorityTaskWoken = pdFALSE;\r
400 \r
401         /* The semaphore is only created when the build is configured to create the\r
402         low power demo. */\r
403         if( xSemaphore != NULL )\r
404         {\r
405                 /* This interrupt will bring the CPU out of deep sleep and software\r
406                 standby modes.  Give the semaphore that was used to place the Tx task\r
407                 into an indefinite sleep. */\r
408                 if( uxQueueMessagesWaitingFromISR( xSemaphore ) == 0 )\r
409                 {\r
410                         xSemaphoreGiveFromISR( xSemaphore, &lHigherPriorityTaskWoken );\r
411                 }\r
412                 else\r
413                 {\r
414                         /* The semaphore was already available, so the task is not blocked\r
415                         on it and there is no point giving it. */\r
416                 }\r
417 \r
418                 /* If giving the semaphore caused a task to leave the Blocked state,\r
419                 and the task that left the Blocked state has a priority equal to or\r
420                 above the priority of the task that this interrupt interrupted, then\r
421                 lHigherPriorityTaskWoken will have been set to pdTRUE inside the call\r
422                 to xSemaphoreGiveFromISR(), and calling portYIELD_FROM_ISR() will cause\r
423                 a context switch to the unblocked task. */\r
424                 portYIELD_FROM_ISR( lHigherPriorityTaskWoken );\r
425         }\r
426 }\r
427 \r