Hi, Max,
based on the example located at:
D:\Freescale\KSDK_1.2.0\examples\frdmk64f\driver_examples\gpio\kds
I have modified the example as following, I added the SW3 button function. I have tested on FRDM-K64F, it worked fine, after I pressed the SW3, the LED could toggle, the same function as SW2.
Hope it can help you.
BR
Xiangjun Rong
// Standard C Included Files
#include <stdio.h>
// SDK Included Files
#include "board.h"
#include "gpio_pins.h"
#include "fsl_debug_console.h"
///////////////////////////////////////////////////////////////////////////////
// Variables
///////////////////////////////////////////////////////////////////////////////
volatile bool isButtonPress = false;
////////////////////////////////////////////////////////////////////////////////
// Code
////////////////////////////////////////////////////////////////////////////////
/*!
* @brief Uses a switch to controll a LED.
*
* This function toogles LED1 when press the switch.
*/
int main(void)
{
// Define gpio input pin config structure SW.
gpio_input_pin_user_config_t inputPin[] = {
{
.pinName = BOARD_SW_GPIO,
.config.isPullEnable = true,
#if FSL_FEATURE_PORT_HAS_PULL_SELECTION
.config.pullSelect = kPortPullUp,
#endif
#if FSL_FEATURE_PORT_HAS_PASSIVE_FILTER
.config.isPassiveFilterEnabled = false,
#endif
#if FSL_FEATURE_PORT_HAS_DIGITAL_FILTER
.config.isDigitalFilterEnabled = false,
#endif
.config.interrupt = kPortIntFallingEdge,
},
{
.pinName = kGpioSW3,
.config.isPullEnable = true,
#if FSL_FEATURE_PORT_HAS_PULL_SELECTION
.config.pullSelect = kPortPullUp,
#endif
#if FSL_FEATURE_PORT_HAS_PASSIVE_FILTER
.config.isPassiveFilterEnabled = false,
#endif
#if FSL_FEATURE_PORT_HAS_DIGITAL_FILTER
.config.isDigitalFilterEnabled = false,
#endif
.config.interrupt = kPortIntFallingEdge,
},
{
.pinName = GPIO_PINS_OUT_OF_RANGE,
}
};
// Define gpio output pin config structure LED1.
gpio_output_pin_user_config_t outputPin[] = {
{
.pinName = kGpioLED1,
.config.outputLogic = 0,
#if FSL_FEATURE_PORT_HAS_SLEW_RATE
.config.slewRate = kPortFastSlewRate,
#endif
#if FSL_FEATURE_PORT_HAS_DRIVE_STRENGTH
.config.driveStrength = kPortHighDriveStrength,
#endif
},
{
.pinName = GPIO_PINS_OUT_OF_RANGE,
}
};
// Init hardware
hardware_init();
// Print a note to terminal.
PRINTF("\r\n GPIO PD Driver example\r\n");
PRINTF("\r\n Press %s to turn on/off a LED1\r\n",BOARD_SW_NAME);
// Init LED1, Switch.
GPIO_DRV_Init(inputPin, outputPin);
// Turn LED1 on.
GPIO_DRV_ClearPinOutput(kGpioLED1);
while(1)
{
if(isButtonPress)
{
PRINTF(" %s is pressed \r\n",BOARD_SW_NAME);
// Reset state of button.
isButtonPress=false;
}
}
}
/*!
* @brief Interrupt service fuction of switch.
*
* This function toogles LED1
*/
void BOARD_SW_IRQ_HANDLER(void)
{
// Clear external interrupt flag.
GPIO_DRV_ClearPinIntFlag(BOARD_SW_GPIO);
// Change state of button.
isButtonPress = true;
// Toggle LED1.
GPIO_DRV_TogglePinOutput(kGpioLED1);
}
void PORTA_IRQHandler(void)
{
GPIO_DRV_ClearPinIntFlag(kGpioSW3);
// Toggle LED1.
GPIO_DRV_TogglePinOutput(kGpioLED1);
}
/*******************************************************************************
* EOF
******************************************************************************/