]> begriffs open source - cmsis-freertos/blob - Demo/NEC_78K0R_IAR/ButtonTask.c
This is a FreeRTOS header, not RTX.
[cmsis-freertos] / Demo / NEC_78K0R_IAR / ButtonTask.c
1 /*
2  * FreeRTOS Kernel V10.0.1
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28 /*
29  * This file defines the button push task and ISR as described at the top of
30  * main.c.  The ISR is called from a wrapper function defined in ButtonISR.s26.
31  */
32
33 /* Kernel includes. */
34 #include "FreeRTOS.h"
35 #include "task.h"
36 #include "semphr.h"
37
38 /* The LED output used by the button push task. */
39 #define butLED1   P7_bit.no7
40
41 /* A short delay used for button debouncing. */
42 #define butDEBOUNCE_DELAY       ( 200 / portTICK_PERIOD_MS )
43
44 /* The semaphore used to synchronise the button push task with the interrupt. */
45 static SemaphoreHandle_t xButtonSemaphore;
46
47 /*
48  * The definition of the button task itself.  See the comments at the top of
49  * main.c.
50  */
51 void vButtonTask( void *pvParameters )
52 {
53         /* Ensure the semaphore is created before it gets used. */
54         vSemaphoreCreateBinary( xButtonSemaphore );
55
56         for( ;; )
57         {
58                 /* Block on the semaphore to wait for an interrupt event.  The semaphore
59                 is 'given' from vButtonISRHandler() below.  Using portMAX_DELAY as the
60                 block time will cause the task to block indefinitely provided
61                 INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h. */
62                 xSemaphoreTake( xButtonSemaphore, portMAX_DELAY );
63
64                 /* The button must have been pushed for this line to be executed.
65                 Simply toggle the LED. */
66                 butLED1 = !butLED1;
67                 
68                 /* Wait a short time then clear any pending button pushes as a crude
69                 method of debouncing the switch.  xSemaphoreTake() uses a block time of
70                 zero this time so it returns immediately rather than waiting for the
71                 interrupt to occur. */
72                 vTaskDelay( butDEBOUNCE_DELAY );
73                 xSemaphoreTake( xButtonSemaphore, 0 );
74         }
75 }
76 /*-----------------------------------------------------------*/
77
78 /*
79  * The C portion of the interrupt handler.  Interrupts are triggered by pushing
80  * the button on the target board.  This interrupt can cause a context switch
81  * so has an assembly file wrapper defined within ButtonISR.s26.
82  */
83 void vButtonISRHandler( void )
84 {
85 short sHigherPriorityTaskWoken = pdFALSE;
86
87         /* 'Give' the semaphore to unblock the button task. */
88         xSemaphoreGiveFromISR( xButtonSemaphore, &sHigherPriorityTaskWoken );
89         
90         /* If giving the semaphore unblocked a task, and the unblocked task has a
91         priority that is higher than the currently running task, then
92         sHigherPriorityTaskWoken will have been set to pdTRUE.  Passing a pdTRUE
93         value to portYIELD_FROM_ISR() will cause this interrupt to return directly
94         to the higher priority unblocked task. */
95         portYIELD_FROM_ISR( sHigherPriorityTaskWoken );
96 }
97 /*-----------------------------------------------------------*/