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