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