/** * \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 Main function. */ int main(void) { /* Set the sleep mode to initially lock. */ volatile enum sleepmgr_mode mode = SLEEPMGR_ACTIVE; /* Initialize the pins for input and output. */ board_init(); /* Initialize the clock and disable clock unused modules */ sysclk_init(); /* Initialize the IOPORT */ ioport_init(); delay_init(sysclk_get_cpu_hz()); /* Set the pin sense mode */ ioport_set_pin_sense_mode(BUTTON_PIN, IOPORT_SENSE_LEVEL); /* Enable external interrupt */ external_interrupt_enable(BUTTON_NUMBER); /* Turn off the LED */ LED_On(LED_PIN); /* Initialize the sleep manager, lock initial mode. */ sleepmgr_init(); sleepmgr_lock_mode(mode); Enable_global_interrupt(); do { /* Delay for 3 seconds to show the device is awake. */ delay_ms(3000); /* Turn off the LED and go to sleep. */ LED_Off(LED_PIN); sleepmgr_enter_sleep(); /* Turn on the LED on wake up */ LED_On(LED_PIN); /* Unlock current mode, then lock the next one. */ sleepmgr_unlock_mode(mode); if (++mode < SLEEPMGR_NR_OF_MODES) { sleepmgr_lock_mode(mode); } else { mode = SLEEPMGR_ACTIVE; sleepmgr_lock_mode(mode); } } while (1); }
void ext_int_init(ioport_pin_t pin, enum ioport_sense trigmode) { /* Disable the interrupt */ ext_int_disable(pin); /* Enable IRQ pin as input */ /* Enable the pullup for the IRQ pin */ ioport_configure_pin(pin, IOPORT_DIR_INPUT | IOPORT_PULL_UP); /* Setup interrupt sence control */ ioport_set_pin_sense_mode(pin,trigmode); /* Clear the INTn interrupt flag */ ext_int_clear_flag(pin); /* Enable the interrupt */ ext_int_enable(pin); }
/** * \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!"); }
/** * Initialize the PDCA transfer for the example. */ static void init_pdca(void) { /* PDCA channel options */ static const pdca_channel_config_t pdca_tx_configs = { .addr = (void *)event_string, .pid = PDCA_PID_USART2_TX, .size = sizeof(event_string), .r_addr = 0, .r_size = 0, .ring = false, .etrig = true, .transfer_size = PDCA_MR_SIZE_BYTE }; /* Enable PDCA module */ pdca_enable(PDCA); /* Init PDCA channel with the pdca_options.*/ pdca_channel_set_config(PEVC_ID_USER_PDCA_0, &pdca_tx_configs); /* Set callback for PDCA channel */ pdca_channel_set_callback(PEVC_ID_USER_PDCA_0, pdca_tranfer_done, PDCA_0_IRQn, 1, PDCA_IER_TRC | PDCA_IER_TERR); /* Enable PDCA channel */ pdca_channel_enable(PEVC_ID_USER_PDCA_0); } /** * Configure serial console. */ static void configure_console(void) { const usart_serial_options_t uart_serial_options = { .baudrate = CONF_UART_BAUDRATE, #ifdef CONF_UART_CHAR_LENGTH .charlength = CONF_UART_CHAR_LENGTH, #endif .paritytype = CONF_UART_PARITY, #ifdef CONF_UART_STOP_BITS .stopbits = CONF_UART_STOP_BITS, #endif }; /* Configure console. */ stdio_serial_init(CONF_UART, &uart_serial_options); } /** * \brief Main entry point for event 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-- Events example 2 --\r\n"); printf("-- %s\r\n", BOARD_NAME); printf("-- Compiled: %s %s --\r\n", __DATE__, __TIME__); /* Configure pin to trigger an enent on falling edge */ ioport_set_pin_mode(CONF_EXAMPLE_PIN_EVENT, IOPORT_MODE_PULLUP | IOPORT_MODE_MUX_C); ioport_disable_pin(CONF_EXAMPLE_PIN_EVENT); ioport_set_pin_sense_mode(CONF_EXAMPLE_PIN_EVENT, IOPORT_SENSE_FALLING); gpio_enable_pin_periph_event(CONF_EXAMPLE_PIN_EVENT); printf(CONF_EXAMPLE_EVENT_MSG); init_events(); init_pdca(); while (1) { /* Toggle LED0 every 500 ms */ LED_Toggle(LED0); delay_ms(500); } }
/* * Configure the SPI hardware, including SPI clock speed, mode, delays, chip select pins * It uses values listed in */ void AJ_WSL_SPI_InitializeSPIController(void) { uint32_t config; /* Initialize and enable DMA controller. */ pmc_enable_periph_clk(ID_DMAC); dmac_init(DMAC); dmac_set_priority_mode(DMAC, DMAC_PRIORITY_ROUND_ROBIN); dmac_enable(DMAC); /* Configure DMA TX channel. */ config = 0; config |= DMAC_CFG_DST_PER(AJ_SPI_TX_INDEX) | DMAC_CFG_DST_H2SEL | DMAC_CFG_SOD | DMAC_CFG_FIFOCFG_ALAP_CFG; dmac_channel_set_configuration(DMAC, AJ_DMA_TX_CHANNEL, config); /* Configure DMA RX channel. */ config = 0; config |= DMAC_CFG_SRC_PER(AJ_SPI_RX_INDEX) | DMAC_CFG_SRC_H2SEL | DMAC_CFG_SOD | DMAC_CFG_FIFOCFG_ALAP_CFG; dmac_channel_set_configuration(DMAC, AJ_DMA_RX_CHANNEL, config); /* Enable receive channel interrupt for DMAC. */ uint8_t* interruptEnableAddress = AJ_SPI_ISER1_IEN_ADDR; *interruptEnableAddress = AJ_SPI_DMAC_IEN_BIT; dmac_enable_interrupt(DMAC, (1 << AJ_DMA_RX_CHANNEL)); dmac_enable_interrupt(DMAC, (1 << AJ_DMA_TX_CHANNEL)); //AJ_WSL_DMA_Setup(); dmac_channel_disable(DMAC, AJ_DMA_TX_CHANNEL); dmac_channel_disable(DMAC, AJ_DMA_RX_CHANNEL); /* * Configure the hardware to enable SPI and some output pins */ { pmc_enable_periph_clk(ID_PIOA); pmc_enable_periph_clk(ID_PIOB); pmc_enable_periph_clk(ID_PIOC); pmc_enable_periph_clk(ID_PIOD); // make all of these pins controlled by the right I/O controller pio_configure_pin_group(PIOA, 0xFFFFFFFF, PIO_TYPE_PIO_PERIPH_A); pio_configure_pin_group(PIOB, 0xFFFFFFFF, PIO_TYPE_PIO_PERIPH_B); pio_configure_pin_group(PIOC, 0xFFFFFFFF, PIO_TYPE_PIO_PERIPH_C); pio_configure_pin_group(PIOD, 0xFFFFFFFF, PIO_TYPE_PIO_PERIPH_D); /* * Reset the device by toggling the CHIP_POWER */ ioport_set_pin_dir(AJ_WSL_SPI_CHIP_POWER_PIN, IOPORT_DIR_OUTPUT); ioport_set_pin_level(AJ_WSL_SPI_CHIP_POWER_PIN, IOPORT_PIN_LEVEL_LOW); AJ_Sleep(10); ioport_set_pin_level(AJ_WSL_SPI_CHIP_POWER_PIN, IOPORT_PIN_LEVEL_HIGH); /* * Reset the device by toggling the CHIP_PWD# signal */ ioport_set_pin_dir(AJ_WSL_SPI_CHIP_PWD_PIN, IOPORT_DIR_OUTPUT); ioport_set_pin_level(AJ_WSL_SPI_CHIP_PWD_PIN, IOPORT_PIN_LEVEL_LOW); AJ_Sleep(10); ioport_set_pin_level(AJ_WSL_SPI_CHIP_PWD_PIN, IOPORT_PIN_LEVEL_HIGH); /* configure the pin that detects SPI data ready from the target chip */ ioport_set_pin_dir(AJ_WSL_SPI_CHIP_SPI_INT_PIN, IOPORT_DIR_INPUT); ioport_set_pin_sense_mode(AJ_WSL_SPI_CHIP_SPI_INT_PIN, IOPORT_SENSE_LEVEL_LOW); pio_handler_set(PIOC, ID_PIOC, AJ_WSL_SPI_CHIP_SPI_INT_BIT, (PIO_PULLUP | PIO_IT_FALL_EDGE), &AJ_WSL_SPI_CHIP_SPI_ISR); pio_handler_set_priority(PIOD, (IRQn_Type) ID_PIOC, 0xB); pio_enable_interrupt(PIOC, AJ_WSL_SPI_CHIP_SPI_INT_BIT); } spi_enable_clock(AJ_WSL_SPI_DEVICE); spi_reset(AJ_WSL_SPI_DEVICE); spi_set_lastxfer(AJ_WSL_SPI_DEVICE); spi_set_master_mode(AJ_WSL_SPI_DEVICE); spi_disable_mode_fault_detect(AJ_WSL_SPI_DEVICE); spi_set_peripheral_chip_select_value(AJ_WSL_SPI_DEVICE, AJ_WSL_SPI_DEVICE_NPCS); spi_set_clock_polarity(AJ_WSL_SPI_DEVICE, AJ_WSL_SPI_DEVICE_NPCS, AJ_WSL_SPI_CLOCK_POLARITY); spi_set_clock_phase(AJ_WSL_SPI_DEVICE, AJ_WSL_SPI_DEVICE_NPCS, AJ_WSL_SPI_CLOCK_PHASE); spi_set_bits_per_transfer(AJ_WSL_SPI_DEVICE, AJ_WSL_SPI_DEVICE_NPCS, SPI_CSR_BITS_8_BIT); spi_set_baudrate_div(AJ_WSL_SPI_DEVICE, AJ_WSL_SPI_DEVICE_NPCS, (sysclk_get_cpu_hz() / AJ_WSL_SPI_CLOCK_RATE)); spi_set_transfer_delay(AJ_WSL_SPI_DEVICE, AJ_WSL_SPI_DEVICE_NPCS, AJ_WSL_SPI_DELAY_BEFORE_CLOCK, AJ_WSL_SPI_DELAY_BETWEEN_TRANSFERS); spi_set_fixed_peripheral_select(AJ_WSL_SPI_DEVICE); spi_configure_cs_behavior(AJ_WSL_SPI_DEVICE, AJ_WSL_SPI_DEVICE_NPCS, SPI_CS_RISE_FORCED); spi_enable_interrupt(AJ_WSL_SPI_DEVICE, SPI_IER_TDRE | SPI_IER_RDRF); spi_enable(AJ_WSL_SPI_DEVICE); }
// [main] int main(void) { //! [main_step_sys_init] /* Initialize the SAM system */ sysclk_init(); board_init(); //! [main_step_sys_init] #ifndef BOARD_NO_PUSHBUTTON_2 #if (SAMV71 || SAMV70 || SAMS70 || SAME70) if (GPIO_PUSH_BUTTON_2 == PIO_PB12_IDX) { matrix_set_system_io(matrix_get_system_io() | CCFG_SYSIO_SYSIO12); } ioport_set_pin_dir(GPIO_PUSH_BUTTON_2, IOPORT_DIR_INPUT); ioport_set_pin_mode(GPIO_PUSH_BUTTON_2, GPIO_PUSH_BUTTON_2_FLAGS); ioport_set_pin_sense_mode(GPIO_PUSH_BUTTON_2, GPIO_PUSH_BUTTON_2_SENSE); #endif #endif //! [main_step_console_init] /* Initialize the console uart */ configure_console(); //! [main_step_console_init] /* Output example information */ puts(STRING_HEADER); /* Configure systick for 1 ms */ puts("Configure system tick to get 1ms tick period.\r"); //! [main_step_systick_init] if (SysTick_Config(sysclk_get_cpu_hz() / 1000)) { puts("-F- Systick configuration error\r"); while (1); } //! [main_step_systick_init] #ifndef BOARD_NO_LED_1 puts("Configure TC.\r"); //! [main_step_tc_init] configure_tc(); //! [main_step_tc_init] #endif puts("Configure buttons with debouncing.\r"); //! [main_step_btn_init] configure_buttons(); //! [main_step_btn_init] printf("Press %s to Start/Stop the %s blinking.\r\n", PUSHBUTTON_1_NAME, LED_0_NAME); #ifndef BOARD_NO_PUSHBUTTON_2 printf("Press %s to Start/Stop the %s blinking.\r\n", PUSHBUTTON_2_NAME, LED_1_NAME); #endif //! [main_step_loop] while (1) { /* Wait for LED to be active */ while (!g_b_led0_active); /* Toggle LED state if active */ if (g_b_led0_active) { ioport_toggle_pin_level(LED0_GPIO); printf("1 "); } /* Wait for 500ms */ mdelay(500); } //! [main_step_loop] }
/*! \brief Install a sensor interrupt handler * * The Sensors Xplained add-on boards route sensor device I/O pins to GPIO * pins for the MCU installed on an Xplained platform board. Some sensor * devices can be configured to generate interrupts on these pins to indicate * the availability of new sensor data or the occurrence of configurable * events related to sensor data thresholds, for example. * * This routine will enable interrupts on the GPIO pin specified by the * \c gpio_pin parameter and call a user-defined callback \c handler when an * interrupt is detected. The \c arg parameter is used to pass the address * of user-defined input and output storage for the callback handler. Calling * the routine with the \c handler parameter set to 0 (the NULL pointer) will * fail with \c false returned to the caller. * * \param gpio_pin Board-specific GPIO pin interface to the MCU. * \param handler The address of a driver-defined interrupt handler. * \param arg An optional address passed to the interrupt handler. * * \return bool true if the call succeeds, else false. */ bool sensor_board_irq_connect(uint32_t gpio_pin, SENSOR_IRQ_HANDLER handler, void *arg) { bool status = false; #if XMEGA PORT_t *sensor_port; #endif /* Ensure that the caller has specified a function address. */ if (handler == NULL) { return status; } /* Save the interrupt flag state and disable MCU interrupts. */ irqflags_t const irq_flags = cpu_irq_save(); cpu_irq_disable(); /* Initialize an interrupt for a specified I/O pin. */ if (SENSOR_BOARD_PIN3 == gpio_pin) { sensor_pin3_handler = handler; sensor_pin3_arg = arg; #if UC3 # if defined(SENSOR_PIN3_EIC_LINE) eic_irq_connect(SENSOR_PIN3_EIC_LINE, SENSOR_PIN3_EIC_PIN, SENSOR_PIN3_EIC_FUNC, SENSOR_PIN3_EIC_IRQ, eic_pin3_handler); # else gpio_irq_connect(gpio_pin, SENSOR_PIN3_IRQ); # endif #elif XMEGA sensor_port = ioport_pin_to_port(SENSOR_BOARD_PIN3); sensor_port->INTCTRL = PORT_INT0LVL_LO_gc; sensor_port->INT0MASK |= ioport_pin_to_mask(SENSOR_BOARD_PIN3); /* Some Xplained kits have limited asynchronous sensing on most * pins, which requires them to be sensing on both edges. */ ioport_set_pin_sense_mode(SENSOR_BOARD_PIN3, IOPORT_SENSE_BOTHEDGES); #endif status = true; } else if (SENSOR_BOARD_PIN4 == gpio_pin) { sensor_pin4_handler = handler; sensor_pin4_arg = arg; #if UC3 # if defined(SENSOR_PIN4_EIC_LINE) eic_irq_connect(SENSOR_PIN4_EIC_LINE, SENSOR_PIN4_EIC_PIN, SENSOR_PIN4_EIC_FUNC, SENSOR_PIN4_EIC_IRQ, eic_pin4_handler); # else gpio_irq_connect(gpio_pin, SENSOR_PIN4_IRQ); # endif #elif XMEGA sensor_port = ioport_pin_to_port(SENSOR_BOARD_PIN4); sensor_port->INTCTRL = PORT_INT0LVL_LO_gc; sensor_port->INT0MASK |= ioport_pin_to_mask(SENSOR_BOARD_PIN4); /* Some Xplained kits have limited asynchronous sensing on most * pins, which requires them to be sensing on both edges. */ ioport_set_pin_sense_mode(SENSOR_BOARD_PIN4, IOPORT_SENSE_BOTHEDGES); #endif status = true; } else if (SENSOR_BOARD_PIN5 == gpio_pin) { sensor_pin5_handler = handler; sensor_pin5_arg = arg; #if UC3 # if defined(SENSOR_PIN5_EIC_LINE) eic_irq_connect(SENSOR_PIN5_EIC_LINE, SENSOR_PIN5_EIC_PIN, SENSOR_PIN5_EIC_FUNC, SENSOR_PIN5_EIC_IRQ, eic_pin5_handler); # else gpio_irq_connect(gpio_pin, SENSOR_PIN5_IRQ); # endif #elif XMEGA sensor_port = ioport_pin_to_port(SENSOR_BOARD_PIN5); sensor_port->INTCTRL = PORT_INT0LVL_LO_gc; sensor_port->INT0MASK |= ioport_pin_to_mask(SENSOR_BOARD_PIN5); /* Some Xplained kits have limited asynchronous sensing on most * pins, which requires them to be sensing on both edges. */ ioport_set_pin_sense_mode(SENSOR_BOARD_PIN5, IOPORT_SENSE_BOTHEDGES); #endif status = true; } /* Restore the MCU interrupt flag state. */ cpu_irq_restore(irq_flags); return status; }