]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/NEC_78K0R_IAR/ButtonTask.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / NEC_78K0R_IAR / ButtonTask.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  * This file defines the button push task and ISR as described at the top of\r
31  * main.c.  The ISR is called from a wrapper function defined in ButtonISR.s26.\r
32  */\r
33 \r
34 /* Kernel includes. */\r
35 #include "FreeRTOS.h"\r
36 #include "task.h"\r
37 #include "semphr.h"\r
38 \r
39 /* The LED output used by the button push task. */\r
40 #define butLED1   P7_bit.no7\r
41 \r
42 /* A short delay used for button debouncing. */\r
43 #define butDEBOUNCE_DELAY       ( 200 / portTICK_PERIOD_MS )\r
44 \r
45 /* The semaphore used to synchronise the button push task with the interrupt. */\r
46 static SemaphoreHandle_t xButtonSemaphore;\r
47 \r
48 /*\r
49  * The definition of the button task itself.  See the comments at the top of\r
50  * main.c.\r
51  */\r
52 void vButtonTask( void *pvParameters )\r
53 {\r
54         /* Ensure the semaphore is created before it gets used. */\r
55         vSemaphoreCreateBinary( xButtonSemaphore );\r
56 \r
57         for( ;; )\r
58         {\r
59                 /* Block on the semaphore to wait for an interrupt event.  The semaphore\r
60                 is 'given' from vButtonISRHandler() below.  Using portMAX_DELAY as the\r
61                 block time will cause the task to block indefinitely provided\r
62                 INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h. */\r
63                 xSemaphoreTake( xButtonSemaphore, portMAX_DELAY );\r
64 \r
65                 /* The button must have been pushed for this line to be executed.\r
66                 Simply toggle the LED. */\r
67                 butLED1 = !butLED1;\r
68                 \r
69                 /* Wait a short time then clear any pending button pushes as a crude\r
70                 method of debouncing the switch.  xSemaphoreTake() uses a block time of\r
71                 zero this time so it returns immediately rather than waiting for the\r
72                 interrupt to occur. */\r
73                 vTaskDelay( butDEBOUNCE_DELAY );\r
74                 xSemaphoreTake( xButtonSemaphore, 0 );\r
75         }\r
76 }\r
77 /*-----------------------------------------------------------*/\r
78 \r
79 /*\r
80  * The C portion of the interrupt handler.  Interrupts are triggered by pushing\r
81  * the button on the target board.  This interrupt can cause a context switch\r
82  * so has an assembly file wrapper defined within ButtonISR.s26.\r
83  */\r
84 void vButtonISRHandler( void )\r
85 {\r
86 short sHigherPriorityTaskWoken = pdFALSE;\r
87 \r
88         /* 'Give' the semaphore to unblock the button task. */\r
89         xSemaphoreGiveFromISR( xButtonSemaphore, &sHigherPriorityTaskWoken );\r
90         \r
91         /* If giving the semaphore unblocked a task, and the unblocked task has a\r
92         priority that is higher than the currently running task, then\r
93         sHigherPriorityTaskWoken will have been set to pdTRUE.  Passing a pdTRUE\r
94         value to portYIELD_FROM_ISR() will cause this interrupt to return directly\r
95         to the higher priority unblocked task. */\r
96         portYIELD_FROM_ISR( sHigherPriorityTaskWoken );\r
97 }\r
98 /*-----------------------------------------------------------*/\r