]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_MSP432_LaunchPad_IAR_CCS_Keil/SimplyBlinkyDemo/main_blinky.c
Final V8.2.1 release ready for tagging:
[freertos] / FreeRTOS / Demo / CORTEX_M4F_MSP432_LaunchPad_IAR_CCS_Keil / SimplyBlinkyDemo / main_blinky.c
1 /*\r
2     FreeRTOS V8.2.1 - 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 style\r
72  * project, and a more comprehensive test and demo application.  The\r
73  * configCREATE_SIMPLE_TICKLESS_DEMO setting in FreeRTOSConfig.h is used to\r
74  * select between the two.  See the notes on using\r
75  * configCREATE_SIMPLE_TICKLESS_DEMO in main.c.  This file implements the\r
76  * simply blinky style version.\r
77  *\r
78  * The blinky demo uses FreeRTOS's tickless idle mode to reduce power \r
79  * consumption.  See the notes on the web page below regarding the difference\r
80  * in power saving that can be achieved between using the generic tickless\r
81  * implementation (as used by the blinky demo) and a tickless implementation\r
82  * that is tailored specifically to the MSP432.\r
83  * \r
84  * See http://www.FreeRTOS.org/TI_MSP432_Free_RTOS_Demo.html for instructions.\r
85  *\r
86  * NOTE 2:  This file only contains the source code that is specific to the\r
87  * basic demo.  Generic functions, such FreeRTOS hook functions, and functions\r
88  * required to configure the hardware, are defined in main.c.\r
89  ******************************************************************************\r
90  *\r
91  * main_blinky() creates one queue, and two tasks.  It then starts the\r
92  * scheduler.\r
93  *\r
94  * The Queue Send Task:\r
95  * The queue send task is implemented by the prvQueueSendTask() function in\r
96  * this file.  prvQueueSendTask() sits in a loop that causes it to repeatedly\r
97  * block for 200 milliseconds, before sending the value 100 to the queue that\r
98  * was created within main_blinky().  Once the value is sent, the task loops\r
99  * back around to block for another 200 milliseconds.\r
100  *\r
101  * The Queue Receive Task:\r
102  * The queue receive task is implemented by the prvQueueReceiveTask() function\r
103  * in this file.  prvQueueReceiveTask() sits in a loop where it repeatedly\r
104  * blocks on attempts to read data from the queue that was created within\r
105  * main_blinky().  When data is received, the task checks the value of the\r
106  * data, and if the value equals the expected 100, toggles the LED.  The 'block\r
107  * time' parameter passed to the queue receive function specifies that the\r
108  * task should be held in the Blocked state indefinitely to wait for data to\r
109  * be available on the queue.  The queue receive task will only leave the\r
110  * Blocked state when the queue send task writes to the queue.  As the queue\r
111  * send task writes to the queue every 200 milliseconds, the queue receive\r
112  * task leaves the Blocked state every 200 milliseconds, and therefore toggles\r
113  * the LED every 200 milliseconds.\r
114  */\r
115 \r
116 /* Standard includes. */\r
117 #include <stdio.h>\r
118 \r
119 /* Kernel includes. */\r
120 #include "FreeRTOS.h"\r
121 #include "task.h"\r
122 #include "semphr.h"\r
123 \r
124 /* Priorities at which the tasks are created. */\r
125 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
126 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
127 \r
128 /* The rate at which data is sent to the queue.  The 200ms value is converted\r
129 to ticks using the portTICK_PERIOD_MS constant. */\r
130 #define mainQUEUE_SEND_FREQUENCY_MS                     ( pdMS_TO_TICKS( 1000UL ) )\r
131 \r
132 /* The number of items the queue can hold.  This is 1 as the receive task\r
133 will remove items as they are added, meaning the send task should always find\r
134 the queue empty. */\r
135 #define mainQUEUE_LENGTH                                        ( 1 )\r
136 \r
137 /* Values passed to the two tasks just to check the task parameter\r
138 functionality. */\r
139 #define mainQUEUE_SEND_PARAMETER                        ( 0x1111UL )\r
140 #define mainQUEUE_RECEIVE_PARAMETER                     ( 0x22UL )\r
141 \r
142 /*-----------------------------------------------------------*/\r
143 \r
144 /*\r
145  * The tasks as described in the comments at the top of this file.\r
146  */\r
147 static void prvQueueReceiveTask( void *pvParameters );\r
148 static void prvQueueSendTask( void *pvParameters );\r
149 \r
150 /*\r
151  * Called by main() to create the simply blinky style application if\r
152  * configCREATE_SIMPLE_TICKLESS_DEMO is set to 1.\r
153  */\r
154 void main_blinky( void );\r
155 \r
156 /*\r
157  * The full demo configures the clocks for maximum frequency, wheras this blinky\r
158  * demo uses a slower clock as it also uses low power features.\r
159  */\r
160 static void prvConfigureClocks( void );\r
161 \r
162 /*-----------------------------------------------------------*/\r
163 \r
164 /* The queue used by both tasks. */\r
165 static QueueHandle_t xQueue = NULL;\r
166 \r
167 /*-----------------------------------------------------------*/\r
168 \r
169 void main_blinky( void )\r
170 {\r
171         /* See http://www.FreeRTOS.org/TI_MSP432_Free_RTOS_Demo.html for \r
172         instructions and notes regarding the difference in power saving that can be \r
173         achieved between using the generic tickless RTOS implementation (as used by \r
174         the blinky demo) and a tickless RTOS implementation that is tailored \r
175         specifically to the MSP432. */\r
176 \r
177         /* The full demo configures the clocks for maximum frequency, wheras this\r
178         blinky demo uses a slower clock as it also uses low power features. */\r
179         prvConfigureClocks();\r
180 \r
181         /* Create the queue. */\r
182         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( uint32_t ) );\r
183 \r
184         if( xQueue != NULL )\r
185         {\r
186                 /* Start the two tasks as described in the comments at the top of this\r
187                 file. */\r
188                 xTaskCreate( prvQueueReceiveTask,                                       /* The function that implements the task. */\r
189                                         "Rx",                                                                   /* The text name assigned to the task - for debug only as it is not used by the kernel. */\r
190                                         configMINIMAL_STACK_SIZE,                               /* The size of the stack to allocate to the task. */\r
191                                         ( void * ) mainQUEUE_RECEIVE_PARAMETER, /* The parameter passed to the task - just to check the functionality. */\r
192                                         mainQUEUE_RECEIVE_TASK_PRIORITY,                /* The priority assigned to the task. */\r
193                                         NULL );                                                                 /* The task handle is not required, so NULL is passed. */\r
194 \r
195                 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, ( void * ) mainQUEUE_SEND_PARAMETER, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
196 \r
197                 /* Start the tasks and timer running. */\r
198                 vTaskStartScheduler();\r
199         }\r
200 \r
201         /* If all is well, the scheduler will now be running, and the following\r
202         line will never be reached.  If the following line does execute, then\r
203         there was insufficient FreeRTOS heap memory available for the idle and/or\r
204         timer tasks     to be created.  See the memory management section on the\r
205         FreeRTOS web site for more details. */\r
206         for( ;; );\r
207 }\r
208 /*-----------------------------------------------------------*/\r
209 \r
210 static void prvQueueSendTask( void *pvParameters )\r
211 {\r
212 TickType_t xNextWakeTime;\r
213 const unsigned long ulValueToSend = 100UL;\r
214 \r
215         /* Check the task parameter is as expected. */\r
216         configASSERT( ( ( unsigned long ) pvParameters ) == mainQUEUE_SEND_PARAMETER );\r
217 \r
218         /* Initialise xNextWakeTime - this only needs to be done once. */\r
219         xNextWakeTime = xTaskGetTickCount();\r
220 \r
221         for( ;; )\r
222         {\r
223                 /* Place this task in the blocked state until it is time to run again.\r
224                 The block time is specified in ticks, the constant used converts ticks\r
225                 to ms.  While in the Blocked state this task will not consume any CPU\r
226                 time. */\r
227                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
228 \r
229                 /* Send to the queue - causing the queue receive task to unblock and\r
230                 toggle the LED.  0 is used as the block time so the sending operation\r
231                 will not block - it shouldn't need to block as the queue should always\r
232                 be empty at this point in the code. */\r
233                 xQueueSend( xQueue, &ulValueToSend, 0U );\r
234         }\r
235 }\r
236 /*-----------------------------------------------------------*/\r
237 \r
238 static void prvQueueReceiveTask( void *pvParameters )\r
239 {\r
240 unsigned long ulReceivedValue;\r
241 static const TickType_t xShortBlock = pdMS_TO_TICKS( 50 );\r
242 \r
243         /* Check the task parameter is as expected. */\r
244         configASSERT( ( ( unsigned long ) pvParameters ) == mainQUEUE_RECEIVE_PARAMETER );\r
245 \r
246         for( ;; )\r
247         {\r
248                 /* Wait until something arrives in the queue - this task will block\r
249                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
250                 FreeRTOSConfig.h. */\r
251                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
252 \r
253                 /*  To get here something must have been received from the queue, but\r
254                 is it the expected value?  If it is, toggle the LED. */\r
255                 if( ulReceivedValue == 100UL )\r
256                 {\r
257                         /* Blip the LED for a short while so as not to use too much\r
258                         power. */\r
259                         configTOGGLE_LED();\r
260                         vTaskDelay( xShortBlock );\r
261                         configTOGGLE_LED();\r
262                         ulReceivedValue = 0U;\r
263                 }\r
264         }\r
265 }\r
266 /*-----------------------------------------------------------*/\r
267 \r
268 static void prvConfigureClocks( void )\r
269 {\r
270         /* The full demo configures the clocks for maximum frequency, wheras this\r
271         blinky demo uses a slower clock as it also uses low power features.\r
272 \r
273         From the datashee:  For AM_LDO_VCORE0 and AM_DCDC_VCORE0 modes, the maximum\r
274         CPU operating frequency is 24 MHz and maximum input clock frequency for\r
275         peripherals is 12 MHz. */\r
276         CS_setDCOCenteredFrequency( CS_DCO_FREQUENCY_3 );\r
277         CS_initClockSignal( CS_HSMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1 );\r
278         CS_initClockSignal( CS_SMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1 );\r
279         CS_initClockSignal( CS_MCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1 );\r
280         CS_initClockSignal( CS_ACLK, CS_REFOCLK_SELECT, CS_CLOCK_DIVIDER_1 );\r
281 \r
282         /* The lower frequency allows the use of CVORE level 0. */\r
283         PCM_setCoreVoltageLevel( PCM_VCORE0 );\r
284 }\r
285 /*-----------------------------------------------------------*/\r
286 \r
287 void vPreSleepProcessing( uint32_t ulExpectedIdleTime )\r
288 {\r
289 \r
290 }\r
291 /*-----------------------------------------------------------*/\r
292 \r
293 #if( configCREATE_SIMPLE_TICKLESS_DEMO == 1 )\r
294 \r
295         void vApplicationTickHook( void )\r
296         {\r
297                 /* This function will be called by each tick interrupt if\r
298                 configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be\r
299                 added here, but the tick hook is called from an interrupt context, so\r
300                 code must not attempt to block, and only the interrupt safe FreeRTOS API\r
301                 functions can be used (those that end in FromISR()). */\r
302 \r
303                 /* Only the full demo uses the tick hook so there is no code is\r
304                 executed here. */\r
305         }\r
306 \r
307 #endif\r
308 \r