2 FreeRTOS V8.0.0:rc1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
66 /* ****************************************************************************
\r
67 * When configCREATE_LOW_POWER_DEMO is set to 1 in FreeRTOSConfig.h main() will
\r
68 * call main_low_power(), which is defined in this file. main_low_power()
\r
69 * demonstrates FreeRTOS tick suppression being used to allow the MCU to be
\r
70 * placed into both the low power deep sleep mode and the low power software
\r
71 * standby mode. When configCREATE_LOW_POWER_DEMO is set to 0 main will
\r
72 * instead call main_full(), which is a more comprehensive RTOS demonstration.
\r
73 * ****************************************************************************
\r
75 * This application demonstrates the FreeRTOS tickless idle mode (tick
\r
76 * suppression). See http://www.freertos.org/low-power-tickless-rtos.html
\r
77 * The demo is configured to execute on the Renesas RX100 RSK.
\r
81 * + Two tasks are created, an Rx task and a Tx task.
\r
83 * + The Rx task repeatedly blocks on a queue to wait for data. The Rx task
\r
84 * toggles LED 0 each time is receives a value from the queue.
\r
86 * + The Tx task repeatedly enters the Blocked state for an amount of time
\r
87 * that is set by the position of the potentiometer. On exiting the blocked
\r
88 * state the Tx task sends a value through the queue to the Rx task (causing
\r
89 * the Rx task to exit the blocked state and toggle LED 0).
\r
91 * If the value read from the potentiometer is less than or equal to
\r
92 * mainSOFTWARE_STANDBY_DELAY then the Tx task blocks for the equivalent
\r
93 * number of milliseconds. For example, if the sampled analog value is
\r
94 * 2000, then the Tx task blocks for 2000ms. Blocking for a finite period
\r
95 * allows the kernel to stop the tick interrupt and place the RX100 into
\r
98 * If the value read form the potentiometer is greater than
\r
99 * mainSOFTWARE_STANDBY_DELAY then the Tx task blocks on a semaphore with
\r
100 * an infinite timeout. Blocking with an infinite timeout allows the kernel
\r
101 * to stop the tick interrupt and place the RX100 into software standby
\r
102 * mode. Pressing a button will generate an interrupt that causes the RX100
\r
103 * to exit software standby mode. The interrupt service routine 'gives' the
\r
104 * semaphore to unblock the Tx task.
\r
107 * Using the Demo and Observed Behaviour:
\r
109 * 1) Turn the potentiometer completely counter clockwise.
\r
111 * 2) Program the RX100 with the application, then disconnect the programming/
\r
112 * debugging hardware to ensure power readings are not effected by any
\r
113 * connected interfaces.
\r
115 * 3) Start the application running. LED 0 will toggle quickly because the
\r
116 * potentiometer is turned to its lowest value. LED 1 will be illuminated
\r
117 * when the RX100 is not in a power saving mode, but will appear to be off
\r
118 * because most execution time is spent in a sleep mode. Led 2 will be
\r
119 * illuminated when the RX100 is in deep sleep mode, and will appear to be
\r
120 * always on, again because most execution time is spent in deep sleep mode.
\r
121 * The LEDs are turned on and off by the application defined pre and post
\r
122 * sleep macros (see the definitions of configPRE_SLEEP_PROCESSING() and
\r
123 * configPOST_SLEEP_PROCESSING() in FreeRTOSConfig.h).
\r
125 * 4) Slowly turn the potentiometer in the clockwise direction. This will
\r
126 * increase the value read from the potentiometer, which will increase the
\r
127 * time the Tx task spends in the Blocked state, which will therefore
\r
128 * decrease the frequency at which the Tx task sends data to the queue (and
\r
129 * the rate at which LED 0 is toggled).
\r
131 * 5) Keep turning the potentiometer in the clockwise direction. Eventually
\r
132 * the value read from the potentiometer will go above
\r
133 * mainSOFTWARE_STANDBY_DELAY, causing the Tx task to block on the semaphore
\r
134 * with an infinite timeout. LED 0 will stop toggling because the Tx task is
\r
135 * no longer sending to the queue. LED 1 and LED 2 will both be off because
\r
136 * the RX100 is neither running or in deep sleep mode (it is in software
\r
139 * 6) Turn the potentiometer counter clockwise again to ensure its value goes
\r
140 * back below mainSOFTWARE_STANDBY_DELAY.
\r
142 * 7) Press any of the three buttons to generate an interrupt. The interrupt
\r
143 * will take the RX100 out of software standby mode, and the interrupt
\r
144 * service routine will unblock the Tx task by 'giving' the semaphore. LED 0
\r
145 * will then start to toggle again.
\r
150 /* Hardware specific includes. */
\r
151 #include "platform.h"
\r
152 #include "r_switches_if.h"
\r
154 /* Kernel includes. */
\r
155 #include "FreeRTOS.h"
\r
158 #include "semphr.h"
\r
160 /* Common demo includes. */
\r
161 #include "partest.h"
\r
163 /* Priorities at which the Rx and Tx tasks are created. */
\r
164 #define configQUEUE_RECEIVE_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
165 #define configQUEUE_SEND_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
167 /* The number of items the queue can hold. This is 1 as the Rx task will
\r
168 remove items as they are added so the Tx task should always find the queue
\r
170 #define mainQUEUE_LENGTH ( 1 )
\r
172 /* The LED used to indicate that a value has been received on the queue. */
\r
173 #define mainQUEUE_LED ( 0 )
\r
175 /* The LED used to indicate that full power is being used (the MCU is not in
\r
176 deep sleep or software standby mode). */
\r
177 #define mainFULL_POWER_LED ( 1 )
\r
179 /* The LED used to indicate that deep sleep mode is being used. */
\r
180 #define mainDEEP_SLEEP_LED ( 2 )
\r
182 /* The Tx task sends to the queue with a frequency that is set by the value
\r
183 read from the potentiometer until the value goes above that set by the
\r
184 mainSOFTWARE_STANDBY_DELAY constant - at which time the Tx task instead blocks
\r
185 indefinitely on a semaphore. */
\r
186 #define mainSOFTWARE_STANDBY_DELAY ( 3000UL )
\r
188 /* A block time of zero simply means "don't block". */
\r
189 #define mainDONT_BLOCK ( 0 )
\r
191 /* The value that is sent from the Tx task to the Rx task on the queue. */
\r
192 #define mainQUEUED_VALUE ( 100UL )
\r
194 /*-----------------------------------------------------------*/
\r
197 * The Rx and Tx tasks as described at the top of this file.
\r
199 static void prvQueueReceiveTask( void *pvParameters );
\r
200 static void prvQueueSendTask( void *pvParameters );
\r
203 * Reads and returns the value of the ADC connected to the potentiometer built
\r
206 static unsigned short prvReadPOT( void );
\r
209 * The handler for the interrupt generated when any of the buttons are pressed.
\r
211 __interrupt void vButtonInterrupt( void );
\r
213 /*-----------------------------------------------------------*/
\r
215 /* The queue to pass data from the Tx task to the Rx task. */
\r
216 static QueueHandle_t xQueue = NULL;
\r
218 /* The semaphore that is 'given' by interrupts generated from button pushes. */
\r
219 static SemaphoreHandle_t xSemaphore = NULL;
\r
221 /*-----------------------------------------------------------*/
\r
223 void main_low_power( void )
\r
225 /* Create the queue. */
\r
226 xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );
\r
227 configASSERT( xQueue );
\r
229 /* Create the semaphore that is 'given' by an interrupt generated from a
\r
231 vSemaphoreCreateBinary( xSemaphore );
\r
232 configASSERT( xSemaphore );
\r
234 /* Make sure the semaphore starts in the expected state - no button pushes
\r
235 have yet occurred. A block time of zero can be used as it is guaranteed
\r
236 that the semaphore will be available because it has just been created. */
\r
237 xSemaphoreTake( xSemaphore, mainDONT_BLOCK );
\r
239 /* Start the two tasks as described at the top of this file. */
\r
240 xTaskCreate( prvQueueReceiveTask, "Rx", configMINIMAL_STACK_SIZE, NULL, configQUEUE_RECEIVE_TASK_PRIORITY, NULL );
\r
241 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, configQUEUE_SEND_TASK_PRIORITY, NULL );
\r
243 /* The CPU is currently running, not sleeping, so turn on the LED that
\r
244 shows the CPU is not in a sleep mode. */
\r
245 vParTestSetLED( mainFULL_POWER_LED, pdTRUE );
\r
247 /* Start the scheduler running running. */
\r
248 vTaskStartScheduler();
\r
250 /* If all is well the next line of code will not be reached as the
\r
251 scheduler will be running. If the next line is reached then it is likely
\r
252 there was insufficient FreeRTOS heap available for the idle task and/or
\r
253 timer task to be created. See http://www.freertos.org/a00111.html. */
\r
256 /*-----------------------------------------------------------*/
\r
258 static void prvQueueSendTask( void *pvParameters )
\r
261 const unsigned long ulValueToSend = mainQUEUED_VALUE;
\r
263 /* Remove compiler warning about unused parameter. */
\r
264 ( void ) pvParameters;
\r
268 /* The delay period between successive sends to the queue is set by
\r
269 the potentiometer reading. */
\r
270 xDelay = ( TickType_t ) prvReadPOT();
\r
272 /* If the block time is greater than 3000 milliseconds then block
\r
273 indefinitely waiting for a button push. */
\r
274 if( xDelay > mainSOFTWARE_STANDBY_DELAY )
\r
276 /* As this is an indefinite delay the kernel will place the CPU
\r
277 into software standby mode the next time the idle task runs. */
\r
278 xSemaphoreTake( xSemaphore, portMAX_DELAY );
\r
282 /* Convert a time in milliseconds to a time in ticks. */
\r
283 xDelay /= portTICK_PERIOD_MS;
\r
285 /* Place this task in the blocked state until it is time to run
\r
286 again. As this is not an indefinite sleep the kernel will place
\r
287 the CPU into the deep sleep state when the idle task next runs. */
\r
288 vTaskDelay( xDelay );
\r
291 /* Send to the queue - causing the queue receive task to flash its LED.
\r
292 It should not be necessary to block on the queue send because the Rx
\r
293 task will have removed the last queued item. */
\r
294 xQueueSend( xQueue, &ulValueToSend, mainDONT_BLOCK );
\r
297 /*-----------------------------------------------------------*/
\r
299 static void prvQueueReceiveTask( void *pvParameters )
\r
301 unsigned long ulReceivedValue;
\r
303 /* Remove compiler warning about unused parameter. */
\r
304 ( void ) pvParameters;
\r
308 /* Wait until something arrives in the queue - this will block
\r
309 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in
\r
310 FreeRTOSConfig.h. */
\r
311 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );
\r
313 /* To get here something must have arrived, but is it the expected
\r
314 value? If it is, toggle the LED. */
\r
315 if( ulReceivedValue == mainQUEUED_VALUE )
\r
317 vParTestToggleLED( mainQUEUE_LED );
\r
321 /*-----------------------------------------------------------*/
\r
323 void vPreSleepProcessing( unsigned long ulExpectedIdleTime )
\r
325 /* Called by the kernel before it places the MCU into a sleep mode because
\r
326 configPRE_SLEEP_PROCESSING() is #defined to vPreSleepProcessing().
\r
328 NOTE: Additional actions can be taken here to get the power consumption
\r
329 even lower. For example, the ADC input used by this demo could be turned
\r
330 off here, and then back on again in the post sleep processing function.
\r
331 For maximum power saving ensure all unused pins are in their lowest power
\r
334 /* Avoid compiler warnings about the unused parameter. */
\r
335 ( void ) ulExpectedIdleTime;
\r
337 /* Is the MCU about to enter deep sleep mode or software standby mode? */
\r
338 if( SYSTEM.SBYCR.BIT.SSBY == 0 )
\r
340 /* Turn on the LED that indicates deep sleep mode is being entered. */
\r
341 vParTestSetLED( mainDEEP_SLEEP_LED, pdTRUE );
\r
345 /* Software standby mode is being used, so no LEDs are illuminated to
\r
346 ensure minimum power readings are obtained. Ensure the Queue LED is
\r
348 vParTestSetLED( mainQUEUE_LED, pdFALSE );
\r
351 /* Turn off the LED that indicates full power is being used. */
\r
352 vParTestSetLED( mainFULL_POWER_LED, pdFALSE );
\r
354 /*-----------------------------------------------------------*/
\r
356 void vPostSleepProcessing( unsigned long ulExpectedIdleTime )
\r
358 /* Called by the kernel when the MCU exits a sleep mode because
\r
359 configPOST_SLEEP_PROCESSING is #defined to vPostSleepProcessing(). */
\r
361 /* Avoid compiler warnings about the unused parameter. */
\r
362 ( void ) ulExpectedIdleTime;
\r
364 /* Turn off the LED that indicates deep sleep mode, and turn on the LED
\r
365 that indicates full power is being used. */
\r
366 vParTestSetLED( mainDEEP_SLEEP_LED, pdFALSE );
\r
367 vParTestSetLED( mainFULL_POWER_LED, pdTRUE );
\r
369 /*-----------------------------------------------------------*/
\r
371 static unsigned short prvReadPOT( void )
\r
373 unsigned short usADCValue;
\r
374 const unsigned short usMinADCValue = 128;
\r
376 /* Start an ADC scan. */
\r
377 S12AD.ADCSR.BIT.ADST = 1;
\r
378 while( S12AD.ADCSR.BIT.ADST == 1 )
\r
380 /* Just waiting for the ADC scan to complete. Inefficient
\r
384 usADCValue = S12AD.ADDR4;
\r
386 /* Don't let the ADC value get too small as the LED behaviour will look
\r
388 if( usADCValue < usMinADCValue )
\r
390 usADCValue = usMinADCValue;
\r
395 /*-----------------------------------------------------------*/
\r
397 #pragma vector = VECT_ICU_IRQ0, VECT_ICU_IRQ1, VECT_ICU_IRQ4
\r
398 __interrupt void vButtonInterrupt1( void )
\r
400 long lHigherPriorityTaskWoken = pdFALSE;
\r
402 /* The semaphore is only created when the build is configured to create the
\r
404 if( xSemaphore != NULL )
\r
406 /* This interrupt will bring the CPU out of deep sleep and software
\r
407 standby modes. Give the semaphore that was used to place the Tx task
\r
408 into an indefinite sleep. */
\r
409 if( uxQueueMessagesWaitingFromISR( xSemaphore ) == 0 )
\r
411 xSemaphoreGiveFromISR( xSemaphore, &lHigherPriorityTaskWoken );
\r
415 /* The semaphore was already available, so the task is not blocked
\r
416 on it and there is no point giving it. */
\r
419 /* If giving the semaphore caused a task to leave the Blocked state,
\r
420 and the task that left the Blocked state has a priority equal to or
\r
421 above the priority of the task that this interrupt interrupted, then
\r
422 lHigherPriorityTaskWoken will have been set to pdTRUE inside the call
\r
423 to xSemaphoreGiveFromISR(), and calling portYIELD_FROM_ISR() will cause
\r
424 a context switch to the unblocked task. */
\r
425 portYIELD_FROM_ISR( lHigherPriorityTaskWoken );
\r