]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RX100-RSK_IAR/main_low_power.c
ee01bc3e528d09fbfdfed57a944f140e9d97147a
[freertos] / FreeRTOS / Demo / RX100-RSK_IAR / main_low_power.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /* ****************************************************************************\r
30  * When configCREATE_LOW_POWER_DEMO is set to 1 in FreeRTOSConfig.h main() will\r
31  * call main_low_power(), which is defined in this file.  main_low_power()\r
32  * demonstrates FreeRTOS tick suppression being used to allow the MCU to be\r
33  * placed into both the low power deep sleep mode and the low power software\r
34  * standby mode.  When configCREATE_LOW_POWER_DEMO is set to 0 main will\r
35  * instead call main_full(), which is a more comprehensive RTOS demonstration.\r
36  * ****************************************************************************\r
37  *\r
38  * This application demonstrates the FreeRTOS tickless idle mode (tick\r
39  * suppression).  See http://www.freertos.org/low-power-tickless-rtos.html\r
40  * The demo is configured to execute on the Renesas RX100 RSK.\r
41  *\r
42  *  Functionality:\r
43  *\r
44  *  + Two tasks are created, an Rx task and a Tx task.\r
45  *\r
46  *  + The Rx task repeatedly blocks on a queue to wait for data.  The Rx task\r
47  *    toggles LED 0 each time is receives a value from the queue.\r
48  *\r
49  *  + The Tx task repeatedly enters the Blocked state for an amount of time\r
50  *    that is set by the position of the potentiometer.  On exiting the blocked\r
51  *    state the Tx task sends a value through the queue to the Rx task (causing\r
52  *    the Rx task to exit the blocked state and toggle LED 0).\r
53  *\r
54  *    If the value read from the potentiometer is less than or equal to\r
55  *    mainSOFTWARE_STANDBY_DELAY then the Tx task blocks for the equivalent\r
56  *    number of milliseconds.  For example, if the sampled analog value is\r
57  *    2000, then the Tx task blocks for 2000ms.  Blocking for a finite period\r
58  *    allows the kernel to stop the tick interrupt and place the RX100 into\r
59  *    deep sleep mode.\r
60  *\r
61  *    If the value read form the potentiometer is greater than\r
62  *    mainSOFTWARE_STANDBY_DELAY then the Tx task blocks on a semaphore with\r
63  *    an infinite timeout.  Blocking with an infinite timeout allows the kernel\r
64  *    to stop the tick interrupt and place the RX100 into software standby\r
65  *    mode.  Pressing a button will generate an interrupt that causes the RX100\r
66  *    to exit software standby mode.  The interrupt service routine 'gives' the\r
67  *    semaphore to unblock the Tx task.\r
68  *\r
69  *\r
70  *  Using the Demo and Observed Behaviour:\r
71  *\r
72  *  1) Turn the potentiometer completely counter clockwise.\r
73  *\r
74  *  2) Program the RX100 with the application, then disconnect the programming/\r
75  *   debugging hardware to ensure power readings are not effected by any\r
76  *   connected interfaces.\r
77  *\r
78  *  3) Start the application running.  LED 0 will toggle quickly because the\r
79  *   potentiometer is turned to its lowest value.  LED 1 will be illuminated\r
80  *   when the RX100 is not in a power saving mode, but will appear to be off\r
81  *   because most execution time is spent in a sleep mode.  Led 2 will be\r
82  *   illuminated when the RX100 is in deep sleep mode, and will appear to be\r
83  *   always on, again because most execution time is spent in deep sleep mode.\r
84  *   The LEDs are turned on and off by the application defined pre and post\r
85  *   sleep macros (see the definitions of configPRE_SLEEP_PROCESSING() and\r
86  *   configPOST_SLEEP_PROCESSING() in FreeRTOSConfig.h).\r
87  *\r
88  *  4) Slowly turn the potentiometer in the clockwise direction.  This will\r
89  *   increase the value read from the potentiometer, which will increase the\r
90  *   time the Tx task spends in the Blocked state, which will therefore\r
91  *   decrease the frequency at which the Tx task sends data to the queue (and\r
92  *   the rate at which LED 0 is toggled).\r
93  *\r
94  *  5) Keep turning the potentiometer in the clockwise direction.  Eventually\r
95  *   the value read from the potentiometer will go above\r
96  *   mainSOFTWARE_STANDBY_DELAY, causing the Tx task to block on the semaphore\r
97  *   with an infinite timeout.  LED 0 will stop toggling because the Tx task is\r
98  *   no longer sending to the queue.  LED 1 and LED 2 will both be off because\r
99  *   the RX100 is neither running or in deep sleep mode (it is in software\r
100  *   standby mode).\r
101  *\r
102  *  6) Turn the potentiometer counter clockwise again to ensure its value goes\r
103  *   back below mainSOFTWARE_STANDBY_DELAY.\r
104  *\r
105  *  7) Press any of the three buttons to generate an interrupt.  The interrupt\r
106  *   will take the RX100 out of software standby mode, and the interrupt\r
107  *   service routine will unblock the Tx task by 'giving' the semaphore.  LED 0\r
108  *   will then start to toggle again.\r
109  *\r
110  */\r
111 \r
112 \r
113 /* Hardware specific includes. */\r
114 #include "platform.h"\r
115 #include "r_switches_if.h"\r
116 \r
117 /* Kernel includes. */\r
118 #include "FreeRTOS.h"\r
119 #include "task.h"\r
120 #include "queue.h"\r
121 #include "semphr.h"\r
122 \r
123 /* Common demo includes. */\r
124 #include "partest.h"\r
125 \r
126 /* Priorities at which the Rx and Tx tasks are created. */\r
127 #define configQUEUE_RECEIVE_TASK_PRIORITY       ( tskIDLE_PRIORITY + 1 )\r
128 #define configQUEUE_SEND_TASK_PRIORITY          ( tskIDLE_PRIORITY + 2 )\r
129 \r
130 /* The number of items the queue can hold.  This is 1 as the Rx task will\r
131 remove items as they are added so the Tx task should always find the queue\r
132 empty. */\r
133 #define mainQUEUE_LENGTH                                        ( 1 )\r
134 \r
135 /* The LED used to indicate that a value has been received on the queue. */\r
136 #define mainQUEUE_LED                                           ( 0 )\r
137 \r
138 /* The LED used to indicate that full power is being used (the MCU is not in\r
139 deep sleep or software standby mode). */\r
140 #define mainFULL_POWER_LED                                      ( 1 )\r
141 \r
142 /* The LED used to indicate that deep sleep mode is being used. */\r
143 #define mainDEEP_SLEEP_LED                                      ( 2 )\r
144 \r
145 /* The Tx task sends to the queue with a frequency that is set by the value\r
146 read from the potentiometer until the value goes above that set by the\r
147 mainSOFTWARE_STANDBY_DELAY constant - at which time the Tx task instead blocks\r
148 indefinitely on a semaphore. */\r
149 #define mainSOFTWARE_STANDBY_DELAY                      ( 3000UL )\r
150 \r
151 /* A block time of zero simply means "don't block". */\r
152 #define mainDONT_BLOCK                                          ( 0 )\r
153 \r
154 /* The value that is sent from the Tx task to the Rx task on the queue. */\r
155 #define mainQUEUED_VALUE                                        ( 100UL )\r
156 \r
157 /*-----------------------------------------------------------*/\r
158 \r
159 /*\r
160  * The Rx and Tx tasks as described at the top of this file.\r
161  */\r
162 static void prvQueueReceiveTask( void *pvParameters );\r
163 static void prvQueueSendTask( void *pvParameters );\r
164 \r
165 /*\r
166  * Reads and returns the value of the ADC connected to the potentiometer built\r
167  * onto the RSK.\r
168  */\r
169 static unsigned short prvReadPOT( void );\r
170 \r
171 /*\r
172  * The handler for the interrupt generated when any of the buttons are pressed.\r
173  */\r
174 __interrupt void vButtonInterrupt( void );\r
175 \r
176 /*-----------------------------------------------------------*/\r
177 \r
178 /* The queue to pass data from the Tx task to the Rx task. */\r
179 static QueueHandle_t xQueue = NULL;\r
180 \r
181 /* The semaphore that is 'given' by interrupts generated from button pushes. */\r
182 static SemaphoreHandle_t xSemaphore = NULL;\r
183 \r
184 /*-----------------------------------------------------------*/\r
185 \r
186 void main_low_power( void )\r
187 {\r
188         /* Create the queue. */\r
189         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
190         configASSERT( xQueue );\r
191 \r
192         /* Create the semaphore that is 'given' by an interrupt generated from a\r
193         button push. */\r
194         vSemaphoreCreateBinary( xSemaphore );\r
195         configASSERT( xSemaphore );\r
196 \r
197         /* Make sure the semaphore starts in the expected state - no button pushes\r
198         have yet occurred.  A block time of zero can be used as it is guaranteed\r
199         that the semaphore will be available because it has just been created. */\r
200         xSemaphoreTake( xSemaphore, mainDONT_BLOCK );\r
201 \r
202         /* Start the two tasks as described at the top of this file. */\r
203         xTaskCreate( prvQueueReceiveTask, "Rx", configMINIMAL_STACK_SIZE, NULL, configQUEUE_RECEIVE_TASK_PRIORITY, NULL );\r
204         xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, configQUEUE_SEND_TASK_PRIORITY, NULL );\r
205 \r
206         /* The CPU is currently running, not sleeping, so turn on the LED that\r
207         shows the CPU is not in a sleep mode. */\r
208         vParTestSetLED( mainFULL_POWER_LED, pdTRUE );\r
209 \r
210         /* Start the scheduler running running. */\r
211         vTaskStartScheduler();\r
212 \r
213         /* If all is well the next line of code will not be reached as the\r
214         scheduler will be running.  If the next line is reached then it is likely\r
215         there was insufficient FreeRTOS heap available for the idle task and/or\r
216         timer task to be created.  See http://www.freertos.org/a00111.html. */\r
217         for( ;; );\r
218 }\r
219 /*-----------------------------------------------------------*/\r
220 \r
221 static void prvQueueSendTask( void *pvParameters )\r
222 {\r
223 TickType_t xDelay;\r
224 const unsigned long ulValueToSend = mainQUEUED_VALUE;\r
225 \r
226         /* Remove compiler warning about unused parameter. */\r
227         ( void ) pvParameters;\r
228 \r
229         for( ;; )\r
230         {\r
231                 /* The delay period between successive sends to the queue is set by\r
232                 the potentiometer reading. */\r
233                 xDelay = ( TickType_t ) prvReadPOT();\r
234 \r
235                 /* If the block time is greater than 3000 milliseconds then block\r
236                 indefinitely waiting for a button push. */\r
237                 if( xDelay > mainSOFTWARE_STANDBY_DELAY )\r
238                 {\r
239                         /* As this is an indefinite delay the kernel will place the CPU\r
240                         into software standby mode the next time the idle task runs. */\r
241                         xSemaphoreTake( xSemaphore, portMAX_DELAY );\r
242                 }\r
243                 else\r
244                 {\r
245                         /* Convert a time in milliseconds to a time in ticks. */\r
246                         xDelay /= portTICK_PERIOD_MS;\r
247 \r
248                         /* Place this task in the blocked state until it is time to run\r
249                         again.  As this is not an indefinite sleep the kernel will place\r
250                         the CPU into the deep sleep state when the idle task next runs. */\r
251                         vTaskDelay( xDelay );\r
252                 }\r
253 \r
254                 /* Send to the queue - causing the queue receive task to flash its LED.\r
255                 It should not be necessary to block on the queue send because the Rx\r
256                 task will have removed the last queued item. */\r
257                 xQueueSend( xQueue, &ulValueToSend, mainDONT_BLOCK );\r
258         }\r
259 }\r
260 /*-----------------------------------------------------------*/\r
261 \r
262 static void prvQueueReceiveTask( void *pvParameters )\r
263 {\r
264 unsigned long ulReceivedValue;\r
265 \r
266         /* Remove compiler warning about unused parameter. */\r
267         ( void ) pvParameters;\r
268 \r
269         for( ;; )\r
270         {\r
271                 /* Wait until something arrives in the queue - this will block\r
272                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
273                 FreeRTOSConfig.h. */\r
274                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
275 \r
276                 /*  To get here something must have arrived, but is it the expected\r
277                 value?  If it is, toggle the LED. */\r
278                 if( ulReceivedValue == mainQUEUED_VALUE )\r
279                 {\r
280                         vParTestToggleLED( mainQUEUE_LED );\r
281                 }\r
282         }\r
283 }\r
284 /*-----------------------------------------------------------*/\r
285 \r
286 void vPreSleepProcessing( unsigned long ulExpectedIdleTime )\r
287 {\r
288         /* Called by the kernel before it places the MCU into a sleep mode because\r
289         configPRE_SLEEP_PROCESSING() is #defined to vPreSleepProcessing().\r
290 \r
291         NOTE:  Additional actions can be taken here to get the power consumption\r
292         even lower.  For example, the ADC input used by this demo could be turned\r
293         off here, and then back on again in the post sleep processing function.\r
294         For maximum power saving ensure all unused pins are in their lowest power\r
295         state. */\r
296 \r
297         /* Avoid compiler warnings about the unused parameter. */\r
298         ( void ) ulExpectedIdleTime;\r
299 \r
300         /* Is the MCU about to enter deep sleep mode or software standby mode? */\r
301         if( SYSTEM.SBYCR.BIT.SSBY == 0 )\r
302         {\r
303                 /* Turn on the LED that indicates deep sleep mode is being entered. */\r
304                 vParTestSetLED( mainDEEP_SLEEP_LED, pdTRUE );\r
305         }\r
306         else\r
307         {\r
308                 /* Software standby mode is being used, so no LEDs are illuminated to\r
309                 ensure minimum power readings are obtained.  Ensure the Queue LED is\r
310                 also off. */\r
311                 vParTestSetLED( mainQUEUE_LED, pdFALSE );\r
312         }\r
313 \r
314         /* Turn off the LED that indicates full power is being used. */\r
315         vParTestSetLED( mainFULL_POWER_LED, pdFALSE );\r
316 }\r
317 /*-----------------------------------------------------------*/\r
318 \r
319 void vPostSleepProcessing( unsigned long ulExpectedIdleTime )\r
320 {\r
321         /* Called by the kernel when the MCU exits a sleep mode because\r
322         configPOST_SLEEP_PROCESSING is #defined to vPostSleepProcessing(). */\r
323 \r
324         /* Avoid compiler warnings about the unused parameter. */\r
325         ( void ) ulExpectedIdleTime;\r
326 \r
327         /* Turn off the LED that indicates deep sleep mode, and turn on the LED\r
328         that indicates full power is being used. */\r
329         vParTestSetLED( mainDEEP_SLEEP_LED, pdFALSE );\r
330         vParTestSetLED( mainFULL_POWER_LED, pdTRUE );\r
331 }\r
332 /*-----------------------------------------------------------*/\r
333 \r
334 static unsigned short prvReadPOT( void )\r
335 {\r
336 unsigned short usADCValue;\r
337 const unsigned short usMinADCValue = 128;\r
338 \r
339         /* Start an ADC scan. */\r
340         S12AD.ADCSR.BIT.ADST = 1;\r
341         while( S12AD.ADCSR.BIT.ADST == 1 )\r
342         {\r
343                 /* Just waiting for the ADC scan to complete.  Inefficient\r
344                 polling! */\r
345         }\r
346 \r
347         usADCValue = S12AD.ADDR4;\r
348 \r
349         /* Don't let the ADC value get too small as the LED behaviour will look\r
350         erratic. */\r
351         if( usADCValue < usMinADCValue )\r
352         {\r
353                 usADCValue = usMinADCValue;\r
354         }\r
355 \r
356         return usADCValue;\r
357 }\r
358 /*-----------------------------------------------------------*/\r
359 \r
360 #pragma vector = VECT_ICU_IRQ0, VECT_ICU_IRQ1, VECT_ICU_IRQ4\r
361 __interrupt void vButtonInterrupt1( void )\r
362 {\r
363 long lHigherPriorityTaskWoken = pdFALSE;\r
364 \r
365         /* The semaphore is only created when the build is configured to create the\r
366         low power demo. */\r
367         if( xSemaphore != NULL )\r
368         {\r
369                 /* This interrupt will bring the CPU out of deep sleep and software\r
370                 standby modes.  Give the semaphore that was used to place the Tx task\r
371                 into an indefinite sleep. */\r
372                 if( uxQueueMessagesWaitingFromISR( xSemaphore ) == 0 )\r
373                 {\r
374                         xSemaphoreGiveFromISR( xSemaphore, &lHigherPriorityTaskWoken );\r
375                 }\r
376                 else\r
377                 {\r
378                         /* The semaphore was already available, so the task is not blocked\r
379                         on it and there is no point giving it. */\r
380                 }\r
381 \r
382                 /* If giving the semaphore caused a task to leave the Blocked state,\r
383                 and the task that left the Blocked state has a priority equal to or\r
384                 above the priority of the task that this interrupt interrupted, then\r
385                 lHigherPriorityTaskWoken will have been set to pdTRUE inside the call\r
386                 to xSemaphoreGiveFromISR(), and calling portYIELD_FROM_ISR() will cause\r
387                 a context switch to the unblocked task. */\r
388                 portYIELD_FROM_ISR( lHigherPriorityTaskWoken );\r
389         }\r
390 }\r
391 \r