]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RX100-RSK_IAR/main_low_power.c
Commit 3 RX100 low power demos.
[freertos] / FreeRTOS / Demo / RX100-RSK_IAR / main_low_power.c
1 /*\r
2     FreeRTOS V7.4.0 - 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 itcan 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 both the low power deep sleep mode and the low power software\r
80  * standby mode.  When configCREATE_LOW_POWER_DEMO is set to 0 main will\r
81  * instead call main_full(), which is a more comprehensive 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 Renesas RX100 RSK.\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 repeatedly blocks on a queue to wait for data.  The Rx task\r
93  *    toggles LED 0 each time is receives a value from the queue.\r
94  *\r
95  *  + The Tx task repeatedly enters the Blocked state for an amount of time\r
96  *    that is set by the position of the potentiometer.  On exiting the blocked\r
97  *    state the Tx task sends a value through the queue to the Rx task (causing\r
98  *    the Rx task to exit the blocked state and toggle LED 0).\r
99  *\r
100  *    If the value read from the potentiometer is less than or equal to\r
101  *    mainSOFTWARE_STANDBY_DELAY then the Tx task blocks for the equivalent\r
102  *    number of milliseconds.  For example, if the sampled analog value is\r
103  *    2000, then the Tx task blocks for 2000ms.  Blocking for a finite period\r
104  *    allows the kernel to stop the tick interrupt and place the RX100 into\r
105  *    deep sleep mode.\r
106  *\r
107  *    If the value read form the potentiometer is greater than\r
108  *    mainSOFTWARE_STANDBY_DELAY then the Tx task blocks on a semaphore with\r
109  *    an infinite timeout.  Blocking with an infinite timeout allows the kernel\r
110  *    to stop the tick interrupt and place the RX100 into software standby\r
111  *    mode.  Pressing a button will generate an interrupt that causes the RX100\r
112  *    to exit software standby mode.  The interrupt service routine 'gives' the\r
113  *    semaphore to unblock the Tx task.\r
114  *\r
115  *\r
116  *  Using the Demo and Observed Behaviour:\r
117  *\r
118  *  1) Turn the potentiometer completely counter clockwise.\r
119  *\r
120  *  2) Program the RX100 with the application, then disconnect the programming/\r
121  *   debugging hardware to ensure power readings are not effected by any\r
122  *   connected interfaces.\r
123  *\r
124  *  3) Start the application running.  LED 0 will toggle quickly because the\r
125  *   potentiometer is turned to its lowest value.  LED 1 will be illuminated\r
126  *   when the RX100 is not in a power saving mode, but will appear to be off\r
127  *   because most execution time is spent in a sleep mode.  Led 2 will be\r
128  *   illuminated when the RX100 is in deep sleep mode, and will appear to be\r
129  *   always on, again because most execution time is spent in deep sleep mode.\r
130  *   The LEDs are turned on and off by the application defined pre and post\r
131  *   sleep macros (see the definitions of configPRE_SLEEP_PROCESSING() and\r
132  *   configPOST_SLEEP_PROCESSING() in FreeRTOSConfig.h).\r
133  *\r
134  *  4) Slowly turn the potentiometer in the clockwise direction.  This will\r
135  *   increase the value read from the potentiometer, which will increase the\r
136  *   time the Tx task spends in the Blocked state, which will therefore\r
137  *   decrease the frequency at which the Tx task sends data to the queue (and\r
138  *   the rate at which LED 0 is toggled).\r
139  *\r
140  *  5) Keep turning the potentiometer in the clockwise direction.  Eventually\r
141  *   the value read from the potentiometer will go above\r
142  *   mainSOFTWARE_STANDBY_DELAY, causing the Tx task to block on the semaphore\r
143  *   with an infinite timeout.  LED 0 will stop toggling because the Tx task is\r
144  *   no longer sending to the queue.  LED 1 and LED 2 will both be off because\r
145  *   the RX100 is neither running or in deep sleep mode (it is in software\r
146  *   standby mode).\r
147  *\r
148  *  6) Turn the potentiometer counter clockwise again to ensure its value goes\r
149  *   back below mainSOFTWARE_STANDBY_DELAY.\r
150  *\r
151  *  7) Press any of the three buttons to generate an interrupt.  The interrupt\r
152  *   will take the RX100 out of software standby mode, and the interrupt\r
153  *   service routine will unblock the Tx task by 'giving' the semaphore.  LED 0\r
154  *   will then start to toggle again.\r
155  *\r
156  */\r
157 \r
158 \r
159 /* Hardware specific includes. */\r
160 #include "platform.h"\r
161 #include "r_switches_if.h"\r
162 \r
163 /* Kernel includes. */\r
164 #include "FreeRTOS.h"\r
165 #include "task.h"\r
166 #include "queue.h"\r
167 #include "semphr.h"\r
168 \r
169 /* Common demo includes. */\r
170 #include "partest.h"\r
171 \r
172 /* Priorities at which the Rx and Tx tasks are created. */\r
173 #define configQUEUE_RECEIVE_TASK_PRIORITY       ( tskIDLE_PRIORITY + 1 )\r
174 #define configQUEUE_SEND_TASK_PRIORITY          ( tskIDLE_PRIORITY + 2 )\r
175 \r
176 /* The number of items the queue can hold.  This is 1 as the Rx task will\r
177 remove items as they are added so the Tx task should always find the queue\r
178 empty. */\r
179 #define mainQUEUE_LENGTH                                        ( 1 )\r
180 \r
181 /* The LED used to indicate that a value has been received on the queue. */\r
182 #define mainQUEUE_LED                                           ( 0 )\r
183 \r
184 /* The LED used to indicate that full power is being used (the MCU is not in\r
185 deep sleep or software standby mode). */\r
186 #define mainFULL_POWER_LED                                      ( 1 )\r
187 \r
188 /* The LED used to indicate that deep sleep mode is being used. */\r
189 #define mainDEEP_SLEEP_LED                                      ( 2 )\r
190 \r
191 /* The Tx task sends to the queue with a frequency that is set by the value\r
192 read from the potentiometer until the value goes above that set by the\r
193 mainSOFTWARE_STANDBY_DELAY constant - at which time the Tx task instead blocks\r
194 indefinitely on a semaphore. */\r
195 #define mainSOFTWARE_STANDBY_DELAY                      ( 3000UL )\r
196 \r
197 /* A block time of zero simply means "don't block". */\r
198 #define mainDONT_BLOCK                                          ( 0 )\r
199 \r
200 /* The value that is sent from the Tx task to the Rx task on the queue. */\r
201 #define mainQUEUED_VALUE                                        ( 100UL )\r
202 \r
203 /*-----------------------------------------------------------*/\r
204 \r
205 /*\r
206  * The Rx and Tx tasks as described at the top of this file.\r
207  */\r
208 static void prvQueueReceiveTask( void *pvParameters );\r
209 static void prvQueueSendTask( void *pvParameters );\r
210 \r
211 /*\r
212  * Reads and returns the value of the ADC connected to the potentiometer built\r
213  * onto the RSK.\r
214  */\r
215 static unsigned short prvReadPOT( void );\r
216 \r
217 /*\r
218  * The handler for the interrupt generated when any of the buttons are pressed.\r
219  */\r
220 __interrupt void vButtonInterrupt( void );\r
221 \r
222 /*-----------------------------------------------------------*/\r
223 \r
224 /* The queue to pass data from the Tx task to the Rx task. */\r
225 static xQueueHandle xQueue = NULL;\r
226 \r
227 /* The semaphore that is 'given' by interrupts generated from button pushes. */\r
228 static xSemaphoreHandle xSemaphore = NULL;\r
229 \r
230 /*-----------------------------------------------------------*/\r
231 \r
232 void main_low_power( void )\r
233 {\r
234         /* Create the queue. */\r
235         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
236         configASSERT( xQueue );\r
237 \r
238         /* Create the semaphore that is 'given' by an interrupt generated from a\r
239         button push. */\r
240         vSemaphoreCreateBinary( xSemaphore );\r
241         configASSERT( xSemaphore );\r
242 \r
243         /* Make sure the semaphore starts in the expected state - no button pushes\r
244         have yet occurred.  A block time of zero can be used as it is guaranteed\r
245         that the semaphore will be available because it has just been created. */\r
246         xSemaphoreTake( xSemaphore, mainDONT_BLOCK );\r
247 \r
248         /* Start the two tasks as described at the top of this file. */\r
249         xTaskCreate( prvQueueReceiveTask, "Rx", configMINIMAL_STACK_SIZE, NULL, configQUEUE_RECEIVE_TASK_PRIORITY, NULL );\r
250         xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, configQUEUE_SEND_TASK_PRIORITY, NULL );\r
251 \r
252         /* The CPU is currently running, not sleeping, so turn on the LED that\r
253         shows the CPU is not in a sleep mode. */\r
254         vParTestSetLED( mainFULL_POWER_LED, pdTRUE );\r
255 \r
256         /* Start the scheduler running running. */\r
257         vTaskStartScheduler();\r
258 \r
259         /* If all is well the next line of code will not be reached as the\r
260         scheduler will be running.  If the next line is reached then it is likely\r
261         there was insufficient FreeRTOS heap available for the idle task and/or\r
262         timer task to be created.  See http://www.freertos.org/a00111.html. */\r
263         for( ;; );\r
264 }\r
265 /*-----------------------------------------------------------*/\r
266 \r
267 static void prvQueueSendTask( void *pvParameters )\r
268 {\r
269 portTickType xDelay;\r
270 const unsigned long ulValueToSend = mainQUEUED_VALUE;\r
271 \r
272         /* Remove compiler warning about unused parameter. */\r
273         ( void ) pvParameters;\r
274 \r
275         for( ;; )\r
276         {\r
277                 /* The delay period between successive sends to the queue is set by\r
278                 the potentiometer reading. */\r
279                 xDelay = ( portTickType ) prvReadPOT();\r
280 \r
281                 /* If the block time is greater than 3000 milliseconds then block\r
282                 indefinitely waiting for a button push. */\r
283                 if( xDelay > mainSOFTWARE_STANDBY_DELAY )\r
284                 {\r
285                         /* As this is an indefinite delay the kernel will place the CPU\r
286                         into software standby mode the next time the idle task runs. */\r
287                         xSemaphoreTake( xSemaphore, portMAX_DELAY );\r
288                 }\r
289                 else\r
290                 {\r
291                         /* Convert a time in milliseconds to a time in ticks. */\r
292                         xDelay /= portTICK_RATE_MS;\r
293 \r
294                         /* Place this task in the blocked state until it is time to run\r
295                         again.  As this is not an indefinite sleep the kernel will place\r
296                         the CPU into the deep sleep state when the idle task next runs. */\r
297                         vTaskDelay( xDelay );\r
298                 }\r
299 \r
300                 /* Send to the queue - causing the queue receive task to flash its LED.\r
301                 It should not be necessary to block on the queue send because the Rx\r
302                 task will have removed the last queued item. */\r
303                 xQueueSend( xQueue, &ulValueToSend, mainDONT_BLOCK );\r
304         }\r
305 }\r
306 /*-----------------------------------------------------------*/\r
307 \r
308 static void prvQueueReceiveTask( void *pvParameters )\r
309 {\r
310 unsigned long ulReceivedValue;\r
311 \r
312         /* Remove compiler warning about unused parameter. */\r
313         ( void ) pvParameters;\r
314 \r
315         for( ;; )\r
316         {\r
317                 /* Wait until something arrives in the queue - this will block\r
318                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
319                 FreeRTOSConfig.h. */\r
320                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
321 \r
322                 /*  To get here something must have arrived, but is it the expected\r
323                 value?  If it is, toggle the LED. */\r
324                 if( ulReceivedValue == mainQUEUED_VALUE )\r
325                 {\r
326                         vParTestToggleLED( mainQUEUE_LED );\r
327                 }\r
328         }\r
329 }\r
330 /*-----------------------------------------------------------*/\r
331 \r
332 void vPreSleepProcessing( unsigned long ulExpectedIdleTime )\r
333 {\r
334         /* Called by the kernel before it places the MCU into a sleep mode because\r
335         configPRE_SLEEP_PROCESSING() is #defined to vPreSleepProcessing().\r
336 \r
337         NOTE:  Additional actions can be taken here to get the power consumption\r
338         even lower.  For example, the ADC input used by this demo could be turned\r
339         off here, and then back on again in the post sleep processing function.\r
340         For maximum power saving ensure all unused pins are in their lowest power\r
341         state. */\r
342 \r
343         /* Avoid compiler warnings about the unused parameter. */\r
344         ( void ) ulExpectedIdleTime;\r
345 \r
346         /* Is the MCU about to enter deep sleep mode or software standby mode? */\r
347         if( SYSTEM.SBYCR.BIT.SSBY == 0 )\r
348         {\r
349                 /* Turn on the LED that indicates deep sleep mode is being entered. */\r
350                 vParTestSetLED( mainDEEP_SLEEP_LED, pdTRUE );\r
351         }\r
352         else\r
353         {\r
354                 /* Software standby mode is being used, so no LEDs are illuminated to\r
355                 ensure minimum power readings are obtained.  Ensure the Queue LED is\r
356                 also off. */\r
357                 vParTestSetLED( mainQUEUE_LED, pdFALSE );\r
358         }\r
359 \r
360         /* Turn off the LED that indicates full power is being used. */\r
361         vParTestSetLED( mainFULL_POWER_LED, pdFALSE );\r
362 }\r
363 /*-----------------------------------------------------------*/\r
364 \r
365 void vPostSleepProcessing( unsigned long ulExpectedIdleTime )\r
366 {\r
367         /* Called by the kernel when the MCU exits a sleep mode because\r
368         configPOST_SLEEP_PROCESSING is #defined to vPostSleepProcessing(). */\r
369 \r
370         /* Avoid compiler warnings about the unused parameter. */\r
371         ( void ) ulExpectedIdleTime;\r
372 \r
373         /* Turn off the LED that indicates deep sleep mode, and turn on the LED\r
374         that indicates full power is being used. */\r
375         vParTestSetLED( mainDEEP_SLEEP_LED, pdFALSE );\r
376         vParTestSetLED( mainFULL_POWER_LED, pdTRUE );\r
377 }\r
378 /*-----------------------------------------------------------*/\r
379 \r
380 static unsigned short prvReadPOT( void )\r
381 {\r
382 unsigned short usADCValue;\r
383 const unsigned short usMinADCValue = 128;\r
384 \r
385         /* Start an ADC scan. */\r
386         S12AD.ADCSR.BIT.ADST = 1;\r
387         while( S12AD.ADCSR.BIT.ADST == 1 )\r
388         {\r
389                 /* Just waiting for the ADC scan to complete.  Inefficient\r
390                 polling! */\r
391         }\r
392 \r
393         usADCValue = S12AD.ADDR4;\r
394 \r
395         /* Don't let the ADC value get too small as the LED behaviour will look\r
396         erratic. */\r
397         if( usADCValue < usMinADCValue )\r
398         {\r
399                 usADCValue = usMinADCValue;\r
400         }\r
401 \r
402         return usADCValue;\r
403 }\r
404 /*-----------------------------------------------------------*/\r
405 \r
406 #pragma vector = VECT_ICU_IRQ0, VECT_ICU_IRQ1, VECT_ICU_IRQ4\r
407 __interrupt void vButtonInterrupt1( void )\r
408 {\r
409 long lHigherPriorityTaskWoken = pdFALSE;\r
410 \r
411         /* The semaphore is only created when the build is configured to create the\r
412         low power demo. */\r
413         if( xSemaphore != NULL )\r
414         {\r
415                 /* This interrupt will bring the CPU out of deep sleep and software\r
416                 standby modes.  Give the semaphore that was used to place the Tx task\r
417                 into an indefinite sleep. */\r
418                 if( uxQueueMessagesWaitingFromISR( xSemaphore ) == 0 )\r
419                 {\r
420                         xSemaphoreGiveFromISR( xSemaphore, &lHigherPriorityTaskWoken );\r
421                 }\r
422                 else\r
423                 {\r
424                         /* The semaphore was already available, so the task is not blocked\r
425                         on it and there is no point giving it. */\r
426                 }\r
427 \r
428                 /* If giving the semaphore caused a task to leave the Blocked state,\r
429                 and the task that left the Blocked state has a priority equal to or\r
430                 above the priority of the task that this interrupt interrupted, then\r
431                 lHigherPriorityTaskWoken will have been set to pdTRUE inside the call\r
432                 to xSemaphoreGiveFromISR(), and calling portYIELD_FROM_ISR() will cause\r
433                 a context switch to the unblocked task. */\r
434                 portYIELD_FROM_ISR( lHigherPriorityTaskWoken );\r
435         }\r
436 }\r
437 \r