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