]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_EFM32_Gecko_Starter_Kit_Simplicity_Studio/Source/Low_Power_Demo/main_low_power.c
894f64d4665b51e8dac4aaba38335cc332a80dd9
[freertos] / FreeRTOS / Demo / CORTEX_EFM32_Gecko_Starter_Kit_Simplicity_Studio / Source / Low_Power_Demo / main_low_power.c
1 /*\r
2     FreeRTOS V8.2.3 - Copyright (C) 2015 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  * NOTE 1:  This project provides two demo applications.  A simple blinky demo\r
72  * that demonstrates tickless low power operation, and a more comprehensive test\r
73  * and demo application.  The mainCREATE_LOW_POWER_DEMO setting in main.c is\r
74  * used to select between the two.  See the notes on using\r
75  * mainCREATE_LOW_POWER_DEMO in main.c.  This file implements the low power\r
76  * version.\r
77  *\r
78  * NOTE 2:  This file only contains the source code that is specific to the\r
79  * low power demo.  Generic functions, such FreeRTOS hook functions, and\r
80  * functions required to configure the hardware are defined in main.c.\r
81  ******************************************************************************\r
82  *\r
83  * main_low_power() creates one queue, and two tasks.  It then starts the\r
84  * scheduler.\r
85  *\r
86  * The Queue Send Task:\r
87  * The queue send task is implemented by the prvQueueSendTask() function in\r
88  * this file.  prvQueueSendTask() sits in a loop that causes it to repeatedly\r
89  * block for 200 milliseconds, before sending the value 100 to the queue that\r
90  * was created within main_low_power().  Once the value is sent, the task loops\r
91  * back around to block for another 200 milliseconds...and so on.\r
92  *\r
93  * The Queue Receive Task:\r
94  * The queue receive task is implemented by the prvQueueReceiveTask() function\r
95  * in this file.  prvQueueReceiveTask() sits in a loop where it repeatedly\r
96  * blocks on attempts to read data from the queue that was created within\r
97  * main_low_power().  When data is received, the task checks the value of the\r
98  * data, and if the value equals the expected 100, toggles an LED.  The 'block\r
99  * time' parameter passed to the queue receive function specifies that the\r
100  * task should be held in the Blocked state indefinitely to wait for data to\r
101  * be available on the queue.  The queue receive task will only leave the\r
102  * Blocked state when the queue send task writes to the queue.  As the queue\r
103  * send task writes to the queue every 200 milliseconds, the queue receive\r
104  * task leaves the Blocked state every 200 milliseconds, and therefore toggles\r
105  * the LED every 200 milliseconds.\r
106  */\r
107 \r
108 #warning Description of demo in comments above is not correct.\r
109 \r
110 /* Kernel includes. */\r
111 #include "FreeRTOS.h"\r
112 #include "task.h"\r
113 #include "semphr.h"\r
114 \r
115 /* SiLabs includes. */\r
116 #include "bsp.h"\r
117 \r
118 /* Priorities at which the tasks are created. */\r
119 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
120 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
121 \r
122 /* The rate at which data is sent to the queue.  The 200ms value is converted\r
123 to ticks using the portTICK_PERIOD_MS constant. */\r
124 #define mainQUEUE_SEND_FREQUENCY_MS                     ( 200 / portTICK_PERIOD_MS )\r
125 \r
126 /* The number of items the queue can hold.  This is 1 as the receive task\r
127 will remove items as they are added, meaning the send task should always find\r
128 the queue empty. */\r
129 #define mainQUEUE_LENGTH                                        ( 1 )\r
130 \r
131 /* The LED toggled by the Rx task. */\r
132 #define mainTASK_LED                                            ( 0 )\r
133 \r
134 /*-----------------------------------------------------------*/\r
135 \r
136 /*\r
137  * Called by main when mainCREATE_LOW_POWER_DEMO is set to 1 in\r
138  * main.c.\r
139  */\r
140 void main_low_power( void );\r
141 \r
142 /*\r
143  * The tasks as described in the comments at the top of this file.\r
144  */\r
145 static void prvQueueReceiveTask( void *pvParameters );\r
146 static void prvQueueSendTask( void *pvParameters );\r
147 \r
148 /*-----------------------------------------------------------*/\r
149 \r
150 /* The queue used by both tasks. */\r
151 static QueueHandle_t xQueue = NULL;\r
152 \r
153 /*-----------------------------------------------------------*/\r
154 \r
155 void main_low_power( void )\r
156 {\r
157         /* Create the queue. */\r
158         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( uint32_t ) );\r
159 \r
160         if( xQueue != NULL )\r
161         {\r
162                 /* Start the two tasks as described in the comments at the top of this\r
163                 file. */\r
164                 xTaskCreate( prvQueueReceiveTask,                               /* The function that implements the task. */\r
165                                         "Rx",                                                           /* The text name assigned to the task - for debug only as it is not used by the kernel. */\r
166                                         configMINIMAL_STACK_SIZE,                       /* The size of the stack to allocate to the task. */\r
167                                         NULL,                                                           /* The parameter passed to the task - not used in this case. */\r
168                                         mainQUEUE_RECEIVE_TASK_PRIORITY,        /* The priority assigned to the task. */\r
169                                         NULL );                                                         /* The task handle is not required, so NULL is passed. */\r
170 \r
171                 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
172 \r
173                 /* Start the tasks and timer running. */\r
174                 vTaskStartScheduler();\r
175         }\r
176 \r
177         /* If all is well, the scheduler will now be running, and the following\r
178         line will never be reached.  If the following line does execute, then\r
179         there was insufficient FreeRTOS heap memory available for the Idle and/or\r
180         timer tasks to be created.  See the memory management section on the\r
181         FreeRTOS web site for more details on the FreeRTOS heap\r
182         http://www.freertos.org/a00111.html. */\r
183         for( ;; );\r
184 }\r
185 /*-----------------------------------------------------------*/\r
186 \r
187 static void prvQueueSendTask( void *pvParameters )\r
188 {\r
189 TickType_t xNextWakeTime;\r
190 const unsigned long ulValueToSend = 100UL;\r
191 \r
192         /* Remove compiler warning about unused parameter. */\r
193         ( void ) pvParameters;\r
194 \r
195         /* Initialise xNextWakeTime - this only needs to be done once. */\r
196         xNextWakeTime = xTaskGetTickCount();\r
197 \r
198         for( ;; )\r
199         {\r
200                 /* Place this task in the blocked state until it is time to run again. */\r
201                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
202 \r
203                 /* Send to the queue - causing the queue receive task to unblock and\r
204                 toggle the LED.  0 is used as the block time so the sending operation\r
205                 will not block - it shouldn't need to block as the queue should always\r
206                 be empty at this point in the code. */\r
207                 xQueueSend( xQueue, &ulValueToSend, 0U );\r
208         }\r
209 }\r
210 /*-----------------------------------------------------------*/\r
211 \r
212 static void prvQueueReceiveTask( void *pvParameters )\r
213 {\r
214 unsigned long ulReceivedValue;\r
215 const unsigned long ulExpectedValue = 100UL;\r
216 \r
217         /* Remove compiler warning about unused parameter. */\r
218         ( void ) pvParameters;\r
219 \r
220         for( ;; )\r
221         {\r
222                 /* Wait until something arrives in the queue - this task will block\r
223                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
224                 FreeRTOSConfig.h. */\r
225                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
226 \r
227                 /*  To get here something must have been received from the queue, but\r
228                 is it the expected value?  If it is, toggle the LED. */\r
229                 if( ulReceivedValue == ulExpectedValue )\r
230                 {\r
231                         BSP_LedToggle( mainTASK_LED );\r
232                         ulReceivedValue = 0U;\r
233                 }\r
234         }\r
235 }\r
236 /*-----------------------------------------------------------*/\r
237 \r