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