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