/** * \brief Main entry point for GPIO example. */ int main(void) { /* Initialize the SAM system */ sysclk_init(); board_init(); /* Initialize the console uart */ configure_console(); /* Output example information */ printf("\r\n\r\n-- GPIO interrupt and event example --\r\n"); printf("-- %s\r\n", BOARD_NAME); printf("-- Compiled: %s %s --\r\n", __DATE__, __TIME__); //! [config_button_0_trig_fall] /* Configure push button 0 to trigger an interrupt on falling edge */ //! [config_button_0_trig_fall_1] ioport_set_pin_dir(EXAMPLE_BUTTON_INT, IOPORT_DIR_INPUT); ioport_set_pin_mode(EXAMPLE_BUTTON_INT, IOPORT_MODE_PULLUP | IOPORT_MODE_GLITCH_FILTER); ioport_set_pin_sense_mode(EXAMPLE_BUTTON_INT, IOPORT_SENSE_FALLING); //! [config_button_0_trig_fall_1] //! [config_button_0_trig_fall_2] if (!gpio_set_pin_callback(EXAMPLE_BUTTON_INT, pb0_callback, 1)) { printf("Set pin callback failure!\r\n"); while (1) { } } //! [config_button_0_trig_fall_2] //! [enable_pin_interrupt] gpio_enable_pin_interrupt(EXAMPLE_BUTTON_INT); //! [enable_pin_interrupt] //! [config_button_0_trig_fall] printf("Press %s to trigger LED.\r\n", BUTTON_0_NAME); /* Configure pin to trigger an event on falling edge */ ioport_set_pin_mode(EXAMPLE_PIN_EVENT, IOPORT_MODE_PULLUP | IOPORT_MODE_MUX_C); ioport_disable_pin(EXAMPLE_PIN_EVENT); ioport_set_pin_sense_mode(EXAMPLE_PIN_EVENT, IOPORT_SENSE_FALLING); gpio_enable_pin_periph_event(EXAMPLE_PIN_EVENT); printf("Connect %s to %s to trigger an event.\r\n", EXAMPLE_PIN_NAME, EXAMPLE_GND_NAME); init_pevc(); init_pdca(); while (1) { } }
/** * \brief Test GPIO interrupt. * * \param test Current test case. */ static void run_gpio_int_test(const struct test_case *test) { /* Configure an input pin to trigger an interrupt on falling edge */ ioport_set_pin_dir(CONF_TEST_GPIO_IN, IOPORT_DIR_INPUT); ioport_set_pin_mode(CONF_TEST_GPIO_IN, IOPORT_MODE_PULLUP | IOPORT_MODE_GLITCH_FILTER); ioport_set_pin_sense_mode(CONF_TEST_GPIO_IN, IOPORT_SENSE_FALLING); gpio_set_pin_callback(CONF_TEST_GPIO_IN, gpio_pin_callback, 1); gpio_enable_pin_interrupt(CONF_TEST_GPIO_IN); /* Configure an output pin */ ioport_set_pin_dir(CONF_TEST_GPIO_OUT, IOPORT_DIR_OUTPUT); ioport_set_pin_level(CONF_TEST_GPIO_OUT, IOPORT_PIN_LEVEL_HIGH); /* Trigger an interrupt */ gpio_int_happened = false; ioport_set_pin_level(CONF_TEST_GPIO_OUT, IOPORT_PIN_LEVEL_LOW); delay_ms(5); test_assert_true(test, gpio_int_happened, "No interrupt has been triggered!"); }