2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
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
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
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 * main-blinky.c is included when the "Blinky" build configuration is used.
\r
31 * main-full.c is included when the "Full" build configuration is used.
\r
33 * main-blinky.c (this file) defines a very simple demo that creates two tasks,
\r
34 * one queue, and one timer. It also demonstrates how Cortex-M3 interrupts can
\r
35 * interact with FreeRTOS tasks/timers.
\r
37 * This simple demo project runs on the SK-FM3-100PMC evaluation board, which
\r
38 * is populated with an MB9B500 microcontroller.
\r
40 * The idle hook function:
\r
41 * The idle hook function demonstrates how to query the amount of FreeRTOS heap
\r
42 * space that is remaining (see vApplicationIdleHook() defined in this file).
\r
44 * The main() Function:
\r
45 * main() creates one software timer, one queue, and two tasks. It then starts
\r
48 * The Queue Send Task:
\r
49 * The queue send task is implemented by the prvQueueSendTask() function in
\r
50 * this file. prvQueueSendTask() sits in a loop that causes it to repeatedly
\r
51 * block for 200 milliseconds, before sending the value 100 to the queue that
\r
52 * was created within main(). Once the value is sent, the task loops back
\r
53 * around to block for another 200 milliseconds.
\r
55 * The Queue Receive Task:
\r
56 * The queue receive task is implemented by the prvQueueReceiveTask() function
\r
57 * in this file. prvQueueReceiveTask() sits in a loop that causes it to
\r
58 * repeatedly attempt to read data from the queue that was created within
\r
59 * main(). When data is received, the task checks the value of the data, and
\r
60 * if the value equals the expected 100, toggles an LED on the 7 segment
\r
61 * display. The 'block time' parameter passed to the queue receive function
\r
62 * specifies that the task should be held in the Blocked state indefinitely to
\r
63 * wait for data to be available on the queue. The queue receive task will only
\r
64 * leave the Blocked state when the queue send task writes to the queue. As the
\r
65 * queue send task writes to the queue every 200 milliseconds, the queue receive
\r
66 * task leaves the Blocked state every 200 milliseconds, and therefore toggles
\r
67 * the LED every 200 milliseconds.
\r
69 * The LED Software Timer and the Button Interrupt:
\r
70 * The user button SW2 is configured to generate an interrupt each time it is
\r
71 * pressed. The interrupt service routine switches an LED in the 7 segment
\r
72 * display on, and resets the LED software timer. The LED timer has a 5000
\r
73 * millisecond (5 second) period, and uses a callback function that is defined
\r
74 * to just turn the LED off again. Therefore, pressing the user button will
\r
75 * turn the LED on, and the LED will remain on until a full five seconds pass
\r
76 * without the button being pressed.
\r
79 /* Kernel includes. */
\r
80 #include "FreeRTOS.h"
\r
85 /* Fujitsu drivers/libraries. */
\r
86 #include "mb9bf506n.h"
\r
87 #include "system_mb9bf50x.h"
\r
89 /* Priorities at which the tasks are created. */
\r
90 #define mainQUEUE_RECEIVE_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
91 #define mainQUEUE_SEND_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
93 /* The rate at which data is sent to the queue, specified in milliseconds, and
\r
94 converted to ticks using the portTICK_PERIOD_MS constant. */
\r
95 #define mainQUEUE_SEND_FREQUENCY_MS ( 200 / portTICK_PERIOD_MS )
\r
97 /* The number of items the queue can hold. This is 1 as the receive task
\r
98 will remove items as they are added, meaning the send task should always find
\r
100 #define mainQUEUE_LENGTH ( 1 )
\r
102 /* The LED toggle by the queue receive task. */
\r
103 #define mainTASK_CONTROLLED_LED 0x8000UL
\r
105 /* The LED turned on by the button interrupt, and turned off by the LED timer. */
\r
106 #define mainTIMER_CONTROLLED_LED 0x8000UL
\r
108 /*-----------------------------------------------------------*/
\r
111 * Setup the NVIC, LED outputs, and button inputs.
\r
113 static void prvSetupHardware( void );
\r
116 * The tasks as described in the comments at the top of this file.
\r
118 static void prvQueueReceiveTask( void *pvParameters );
\r
119 static void prvQueueSendTask( void *pvParameters );
\r
122 * The LED timer callback function. This does nothing but switch off the
\r
123 * LED defined by the mainTIMER_CONTROLLED_LED constant.
\r
125 static void vLEDTimerCallback( TimerHandle_t xTimer );
\r
127 /*-----------------------------------------------------------*/
\r
129 /* The queue used by both tasks. */
\r
130 static QueueHandle_t xQueue = NULL;
\r
132 /* The LED software timer. This uses vLEDTimerCallback() as its callback
\r
134 static TimerHandle_t xLEDTimer = NULL;
\r
136 /*-----------------------------------------------------------*/
\r
140 /* Configure the NVIC, LED outputs and button inputs. */
\r
141 prvSetupHardware();
\r
143 /* Create the queue. */
\r
144 xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );
\r
146 if( xQueue != NULL )
\r
148 /* Start the two tasks as described in the comments at the top of this
\r
150 xTaskCreate( prvQueueReceiveTask, "Rx", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_RECEIVE_TASK_PRIORITY, NULL );
\r
151 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );
\r
153 /* Create the software timer that is responsible for turning off the LED
\r
154 if the button is not pushed within 5000ms, as described at the top of
\r
156 xLEDTimer = xTimerCreate( "LEDTimer", /* A text name, purely to help debugging. */
\r
157 ( 5000 / portTICK_PERIOD_MS ),/* The timer period, in this case 5000ms (5s). */
\r
158 pdFALSE, /* This is a one shot timer, so xAutoReload is set to pdFALSE. */
\r
159 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
160 vLEDTimerCallback /* The callback function that switches the LED off. */
\r
163 /* Start the tasks and timer running. */
\r
164 vTaskStartScheduler();
\r
167 /* If all is well, the scheduler will now be running, and the following line
\r
168 will never be reached. If the following line does execute, then there was
\r
169 insufficient FreeRTOS heap memory available for the idle and/or timer tasks
\r
170 to be created. See the memory management section on the FreeRTOS web site
\r
171 for more details. */
\r
174 /*-----------------------------------------------------------*/
\r
176 static void vLEDTimerCallback( TimerHandle_t xTimer )
\r
178 /* The timer has expired - so no button pushes have occurred in the last
\r
179 five seconds - turn the LED off. NOTE - accessing the LED port should use
\r
180 a critical section because it is accessed from multiple tasks, and the
\r
181 button interrupt - in this trivial case, for simplicity, the critical
\r
182 section is omitted. */
\r
183 FM3_GPIO->PDOR1 |= mainTIMER_CONTROLLED_LED;
\r
185 /*-----------------------------------------------------------*/
\r
187 /* The ISR executed when the user button is pushed. */
\r
188 void INT0_7_Handler( void )
\r
190 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
192 /* The button was pushed, so ensure the LED is on before resetting the
\r
193 LED timer. The LED timer will turn the LED off if the button is not
\r
194 pushed within 5000ms. */
\r
195 FM3_GPIO->PDOR1 &= ~mainTIMER_CONTROLLED_LED;
\r
197 /* This interrupt safe FreeRTOS function can be called from this interrupt
\r
198 because the interrupt priority is below the
\r
199 configMAX_SYSCALL_INTERRUPT_PRIORITY setting in FreeRTOSConfig.h. */
\r
200 xTimerResetFromISR( xLEDTimer, &xHigherPriorityTaskWoken );
\r
202 /* Clear the interrupt before leaving. This just clears all the interrupts
\r
203 for simplicity, as only one is actually used in this simple demo anyway. */
\r
204 FM3_EXTI->EICL = 0x0000;
\r
206 /* If calling xTimerResetFromISR() caused a task (in this case the timer
\r
207 service/daemon task) to unblock, and the unblocked task has a priority
\r
208 higher than or equal to the task that was interrupted, then
\r
209 xHigherPriorityTaskWoken will now be set to pdTRUE, and calling
\r
210 portEND_SWITCHING_ISR() will ensure the unblocked task runs next. */
\r
211 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
213 /*-----------------------------------------------------------*/
\r
215 static void prvQueueSendTask( void *pvParameters )
\r
217 TickType_t xNextWakeTime;
\r
218 const unsigned long ulValueToSend = 100UL;
\r
220 /* Initialise xNextWakeTime - this only needs to be done once. */
\r
221 xNextWakeTime = xTaskGetTickCount();
\r
225 /* Place this task in the blocked state until it is time to run again.
\r
226 The block time is specified in ticks, the constant used converts ticks
\r
227 to ms. While in the Blocked state this task will not consume any CPU
\r
229 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );
\r
231 /* Send to the queue - causing the queue receive task to unblock and
\r
232 toggle an LED. 0 is used as the block time so the sending operation
\r
233 will not block - it shouldn't need to block as the queue should always
\r
234 be empty at this point in the code. */
\r
235 xQueueSend( xQueue, &ulValueToSend, 0 );
\r
238 /*-----------------------------------------------------------*/
\r
240 static void prvQueueReceiveTask( void *pvParameters )
\r
242 unsigned long ulReceivedValue;
\r
246 /* Wait until something arrives in the queue - this task will block
\r
247 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in
\r
248 FreeRTOSConfig.h. */
\r
249 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );
\r
251 /* To get here something must have been received from the queue, but
\r
252 is it the expected value? If it is, toggle the LED. */
\r
253 if( ulReceivedValue == 100UL )
\r
255 /* NOTE - accessing the LED port should use a critical section
\r
256 because it is accessed from multiple tasks, and the button interrupt
\r
257 - in this trivial case, for simplicity, the critical section is
\r
259 if( ( FM3_GPIO->PDOR3 & mainTASK_CONTROLLED_LED ) != 0 )
\r
261 FM3_GPIO->PDOR3 &= ~mainTASK_CONTROLLED_LED;
\r
265 FM3_GPIO->PDOR3 |= mainTASK_CONTROLLED_LED;
\r
270 /*-----------------------------------------------------------*/
\r
272 static void prvSetupHardware( void )
\r
274 const unsigned short usButtonInputBit = 0x01U;
\r
275 const unsigned short usGPIOState = 0xFF00U;
\r
278 SystemCoreClockUpdate();
\r
280 /* Analog inputs are not used on the LED outputs. */
\r
281 FM3_GPIO->ADE = 0x00FF;
\r
283 /* LED seg1 to GPIO output (P18->P1F). */
\r
284 FM3_GPIO->DDR1 = 0xFF00;
\r
285 FM3_GPIO->PFR1 = 0x0000;
\r
287 /* LED seg2 to GPIO output (P30->P3F). */
\r
288 FM3_GPIO->DDR3 = 0xFF00;
\r
289 FM3_GPIO->PFR3 = 0x0000;
\r
291 /* Start with all LEDs off. */
\r
292 FM3_GPIO->PDOR3 = usGPIOState;
\r
293 FM3_GPIO->PDOR1 = usGPIOState;
\r
295 /* Set the switches to input (P18->P1F). */
\r
296 FM3_GPIO->DDR5 = 0x0000;
\r
297 FM3_GPIO->PFR5 = 0x0000;
\r
299 /* Assign the button input as GPIO. */
\r
300 FM3_GPIO->PFR1 |= usButtonInputBit;
\r
302 /* Button interrupt on falling edge. */
\r
303 FM3_EXTI->ELVR = 0x0003;
\r
305 /* Clear all external interrupts. */
\r
306 FM3_EXTI->EICL = 0x0000;
\r
308 /* Enable the button interrupt. */
\r
309 FM3_EXTI->ENIR |= usButtonInputBit;
\r
311 /* Setup the GPIO and the NVIC for the switch used in this simple demo. */
\r
312 NVIC_SetPriority( EXINT0_7_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
\r
313 NVIC_EnableIRQ( EXINT0_7_IRQn );
\r
315 /*-----------------------------------------------------------*/
\r
317 void vApplicationMallocFailedHook( void )
\r
319 /* Called if a call to pvPortMalloc() fails because there is insufficient
\r
320 free memory available in the FreeRTOS heap. pvPortMalloc() is called
\r
321 internally by FreeRTOS API functions that create tasks, queues, software
\r
322 timers, and semaphores. The size of the FreeRTOS heap is set by the
\r
323 configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */
\r
326 /*-----------------------------------------------------------*/
\r
328 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
330 ( void ) pcTaskName;
\r
333 /* Run time stack overflow checking is performed if
\r
334 configconfigCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
\r
335 function is called if a stack overflow is detected. */
\r
338 /*-----------------------------------------------------------*/
\r
340 void vApplicationTickHook( void )
\r
342 /* A tick hook is used by the "Full" build configuration. The Full and
\r
343 blinky build configurations share a FreeRTOSConfig.h header file, so this
\r
344 simple build configuration also has to define a tick hook - even though it
\r
345 does not actually use it for anything. */
\r
347 /*-----------------------------------------------------------*/
\r
349 void vApplicationIdleHook( void )
\r
351 volatile size_t xFreeHeapSpace;
\r
353 /* This function is called on each cycle of the idle task. In this case it
\r
354 does nothing useful, other than report the amount of FreeRTOS heap that
\r
355 remains unallocated. */
\r
356 xFreeHeapSpace = xPortGetFreeHeapSize();
\r
358 if( xFreeHeapSpace > 100 )
\r
360 /* By now, the kernel has allocated everything it is going to, so
\r
361 if there is a lot of heap remaining unallocated then
\r
362 the value of configTOTAL_HEAP_SIZE in FreeRTOSConfig.h can be
\r
363 reduced accordingly. */
\r
366 /*-----------------------------------------------------------*/
\r