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