1 | /*
|
2 | * Copyright (c) 2015, Freescale Semiconductor, Inc.
|
3 | * All rights reserved.
|
4 | *
|
5 | * Redistribution and use in source and binary forms, with or without modification,
|
6 | * are permitted provided that the following conditions are met:
|
7 | *
|
8 | * o Redistributions of source code must retain the above copyright notice, this list
|
9 | * of conditions and the following disclaimer.
|
10 | *
|
11 | * o Redistributions in binary form must reproduce the above copyright notice, this
|
12 | * list of conditions and the following disclaimer in the documentation and/or
|
13 | * other materials provided with the distribution.
|
14 | *
|
15 | * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
|
16 | * contributors may be used to endorse or promote products derived from this
|
17 | * software without specific prior written permission.
|
18 | *
|
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
21 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
23 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
26 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
28 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
29 | */
|
30 |
|
31 | #include "fsl_debug_console.h"
|
32 | #include "fsl_port.h"
|
33 | #include "fsl_gpio.h"
|
34 | #include "fsl_common.h"
|
35 | #include "board.h"
|
36 | #include "pin_mux.h"
|
37 | #include "fsl_device_registers.h"
|
38 | #include <stdbool.h>
|
39 | #include "clock_config.h"
|
40 | /*******************************************************************************
|
41 | * Definitions
|
42 | ******************************************************************************/
|
43 | #define BOARD_LED_GPIO BOARD_LED_RED_GPIO
|
44 | #define BOARD_LED_GPIO_PIN BOARD_LED_RED_GPIO_PIN
|
45 |
|
46 | #define BOARD_SW_GPIO BOARD_SW3_GPIO
|
47 | #define BOARD_SW_PORT BOARD_SW3_PORT
|
48 | #define BOARD_SW_GPIO_PIN BOARD_SW3_GPIO_PIN
|
49 | #define BOARD_SW_IRQ BOARD_SW3_IRQ
|
50 | #define BOARD_SW_IRQ_HANDLER BOARD_SW3_IRQ_HANDLER
|
51 | #define BOARD_SW_NAME BOARD_SW3_NAME
|
52 |
|
53 | /*******************************************************************************
|
54 | * Prototypes
|
55 | ******************************************************************************/
|
56 |
|
57 | /*******************************************************************************
|
58 | * Variables
|
59 | ******************************************************************************/
|
60 | /* Whether the SW button is pressed */
|
61 | volatile bool g_ButtonPress = false;
|
62 |
|
63 | /*******************************************************************************
|
64 | * Code
|
65 | ******************************************************************************/
|
66 | /*!
|
67 | * @brief Interrupt service fuction of switch.
|
68 | *
|
69 | * This function toggles the LED
|
70 | */
|
71 | void SW_IRQ_HANDLER(void)
|
72 | {
|
73 | /* Clear external interrupt flag. */
|
74 | GPIO_ClearPinsInterruptFlags(BOARD_SW_GPIO, 1U << BOARD_SW_GPIO_PIN);
|
75 | /* Change state of button. */
|
76 | g_ButtonPress = true;
|
77 | PRINTF("\r\n Interrupt Occurred\r\n");
|
78 | /* Toggle LED. */
|
79 | GPIO_TogglePinsOutput(BOARD_LED_GPIO, 1U << BOARD_LED_GPIO_PIN);
|
80 | }
|
81 |
|
82 | /*!
|
83 | * @brief Main function
|
84 | */
|
85 | int main(void)
|
86 | {
|
87 | /* Define the init structure for the input switch pin */
|
88 | gpio_pin_config_t sw_config = {
|
89 | kGPIO_DigitalInput, 0,
|
90 | };
|
91 |
|
92 | /* Define the init structure for the output LED pin */
|
93 | gpio_pin_config_t led_config = {
|
94 | kGPIO_DigitalOutput, 0,
|
95 | };
|
96 |
|
97 | BOARD_InitPins();
|
98 | BOARD_BootClockRUN();
|
99 | BOARD_InitDebugConsole();
|
100 |
|
101 | CLOCK_EnableClock(kCLOCK_PortA);
|
102 | CLOCK_EnableClock(kCLOCK_PortB);
|
103 | CLOCK_EnableClock(kCLOCK_PortC);
|
104 |
|
105 | /* Print a note to terminal. */
|
106 | PRINTF("\r\n GPIO Interrupt Test\r\n");
|
107 | PRINTF("\r\n Press %s to turn on/off a LED \r\n", BOARD_SW_NAME);
|
108 |
|
109 | /* Init input switch GPIO. */
|
110 | PORT_SetPinInterruptConfig(BOARD_SW_PORT, BOARD_SW_GPIO_PIN, kPORT_InterruptFallingEdge);
|
111 | PORT_SetPinMux(BOARD_SW_PORT, BOARD_SW_GPIO_PIN, kPORT_MuxAsGpio);
|
112 |
|
113 | EnableIRQ(BOARD_SW_IRQ);
|
114 | NVIC_SetPriority(BOARD_SW_IRQ, 3); /* set Priority for SW3 Interrupt to lowest interrupt */
|
115 | InstallIRQHandler(BOARD_SW_IRQ, (uint32_t)SW_IRQ_HANDLER);
|
116 |
|
117 | GPIO_PinInit(BOARD_SW_GPIO, BOARD_SW_GPIO_PIN, &sw_config);
|
118 |
|
119 | /* Init output LED GPIO. */
|
120 | PORT_SetPinMux(BOARD_LED_RED_GPIO_PORT, BOARD_LED_RED_GPIO_PIN, kPORT_MuxAsGpio);
|
121 | LED_RED_INIT(LOGIC_LED_OFF);
|
122 |
|
123 | while (1)
|
124 | {
|
125 | if (g_ButtonPress)
|
126 | {
|
127 | PRINTF(" %s pressed \r\n", BOARD_SW_NAME);
|
128 | LED_RED_TOGGLE();
|
129 | /* Reset state of button. */
|
130 | g_ButtonPress = false;
|
131 | }
|
132 | }
|
133 | }
|