/** * \brief Configure the Pushbuttons * * Configure the PIO as inputs and generate corresponding interrupt when * pressed or released. */ static void configure_buttons(void) { // [main_button1_configure] /* Configure Pushbutton 1 */ pmc_enable_periph_clk(PIN_PUSHBUTTON_1_ID); pio_set_debounce_filter(PIN_PUSHBUTTON_1_PIO, PIN_PUSHBUTTON_1_MASK, 10); /* Interrupt on rising edge */ pio_handler_set(PIN_PUSHBUTTON_1_PIO, PIN_PUSHBUTTON_1_ID, PIN_PUSHBUTTON_1_MASK, PIN_PUSHBUTTON_1_ATTR, Button1_Handler); NVIC_EnableIRQ((IRQn_Type) PIN_PUSHBUTTON_1_ID); pio_handler_set_priority(PIN_PUSHBUTTON_1_PIO, (IRQn_Type) PIN_PUSHBUTTON_1_ID, IRQ_PRIOR_PIO); pio_enable_interrupt(PIN_PUSHBUTTON_1_PIO, PIN_PUSHBUTTON_1_MASK); // [main_button1_configure] #ifndef BOARD_NO_PUSHBUTTON_2 // [main_button2_configure] /* Configure Pushbutton 2 */ pmc_enable_periph_clk(PIN_PUSHBUTTON_2_ID); pio_set_debounce_filter(PIN_PUSHBUTTON_2_PIO, PIN_PUSHBUTTON_2_MASK, 10); /* Interrupt on falling edge */ pio_handler_set(PIN_PUSHBUTTON_2_PIO, PIN_PUSHBUTTON_2_ID, PIN_PUSHBUTTON_2_MASK, PIN_PUSHBUTTON_2_ATTR, Button2_Handler); NVIC_EnableIRQ((IRQn_Type) PIN_PUSHBUTTON_2_ID); pio_handler_set_priority(PIN_PUSHBUTTON_2_PIO, (IRQn_Type) PIN_PUSHBUTTON_2_ID, IRQ_PRIOR_PIO); pio_enable_interrupt(PIN_PUSHBUTTON_2_PIO, PIN_PUSHBUTTON_2_MASK); // [main_button2_configure] #endif }
/** * \brief Configure the push buttons. * * Configure the PIOs as inputs and generate corresponding interrupt when * the push buttons are pressed or released. */ static void configure_buttons(void) { /* Enable the pmc clocks of the push buttons for all SAM3. */ pmc_enable_periph_clk(PIN_PUSHBUTTON_1_ID); pmc_enable_periph_clk(PIN_PUSHBUTTON_2_ID); /* Adjust PIO debouncing filter patameters, using 10 Hz filter. */ pio_set_debounce_filter(PIN_PUSHBUTTON_1_PIO, PIN_PUSHBUTTON_1_MASK, 10); pio_set_debounce_filter(PIN_PUSHBUTTON_2_PIO, PIN_PUSHBUTTON_2_MASK, 10); /* Initialize PIOs interrupt handlers (see PIO definition in board.h). */ pio_handler_set(PIN_PUSHBUTTON_1_PIO, PIN_PUSHBUTTON_1_ID, PIN_PUSHBUTTON_1_MASK, PIN_PUSHBUTTON_1_ATTR, Int1Handler); /* Interrupt on rising edge. */ pio_handler_set(PIN_PUSHBUTTON_2_PIO, PIN_PUSHBUTTON_2_ID, PIN_PUSHBUTTON_2_MASK, PIN_PUSHBUTTON_2_ATTR, Int2Handler); /* Interrupt on falling edge. */ /* Enable PIO controller IRQs. */ NVIC_EnableIRQ((IRQn_Type) PIN_PUSHBUTTON_1_ID); NVIC_EnableIRQ((IRQn_Type) PIN_PUSHBUTTON_2_ID); /* PIO configuration for Buttons. */ pio_handler_set_priority(PIN_PUSHBUTTON_1_PIO, (IRQn_Type) PIN_PUSHBUTTON_1_ID, 0); pio_handler_set_priority(PIN_PUSHBUTTON_2_PIO, (IRQn_Type) PIN_PUSHBUTTON_2_ID, 0); /* Enable PIO line interrupts. */ pio_enable_interrupt(PIN_PUSHBUTTON_1_PIO, PIN_PUSHBUTTON_1_MASK); pio_enable_interrupt(PIN_PUSHBUTTON_2_PIO, PIN_PUSHBUTTON_2_MASK); }
OSStatus gpio_irq_enable( gpio_port_t gpio_port, gpio_pin_number_t gpio_pin_number, gpio_irq_trigger_t trigger, gpio_irq_handler_t handler, void* arg ) { //gpio_irq_data_t *pSource; uint32_t ul_attr; if (gs_ul_nb_sources >= MAX_INTERRUPT_SOURCES) return 1; //Pio *p_pio = (Pio *)ioport_port_to_base(gpio_port); Pio *p_pio = arch_ioport_port_to_base(gpio_port); //ioport_pin_t pin = CREATE_IOPORT_PIN(gpio_port, gpio_pin_number); ioport_port_mask_t ul_mask = ioport_pin_to_mask(CREATE_IOPORT_PIN(gpio_port, gpio_pin_number)); if ( gpio_irq_management_initted == 0 ) { memset( (void*)gpio_irq_data, 0, sizeof( gpio_irq_data ) ); gpio_irq_management_initted = 1; } if (trigger == IRQ_TRIGGER_RISING_EDGE ) { ul_attr = PIO_IT_RISE_EDGE; } else if (trigger == IRQ_TRIGGER_FALLING_EDGE ) { ul_attr = PIO_IT_FALL_EDGE; } else if (trigger == IRQ_TRIGGER_BOTH_EDGES ) { ul_attr = PIO_IT_EDGE; } //pSource = &(gpio_irq_data[gs_ul_nb_sources]); gpio_irq_data[gs_ul_nb_sources].owner_port = gpio_port; if ( gpio_port == PORTA) { gpio_irq_data[gs_ul_nb_sources].id = ID_PIOA; // pmc_enable_periph_clk(ID_PIOA); } else if (gpio_port == PORTB) { gpio_irq_data[gs_ul_nb_sources].id = ID_PIOB; // pmc_enable_periph_clk(ID_PIOB); } gpio_irq_data[gs_ul_nb_sources].mask = ul_mask; gpio_irq_data[gs_ul_nb_sources].handler = handler; gpio_irq_data[gs_ul_nb_sources].arg = arg; gs_ul_nb_sources++; /* Configure interrupt mode */ pio_configure_interrupt(p_pio, ul_mask, ul_attr); if ( gpio_port == PORTA){ NVIC_EnableIRQ( PIOA_IRQn ); pio_handler_set_priority(PIOA, PIOA_IRQn, IRQ_PRIORITY_PIO); } else if (gpio_port == PORTB) { NVIC_EnableIRQ( PIOB_IRQn); pio_handler_set_priority(PIOB, PIOB_IRQn, IRQ_PRIORITY_PIO); } pio_enable_interrupt(p_pio, ul_mask); return kGeneralErr; }
/** * \brief Initialize SAM4E_XPRO board for low power test. */ void init_specific_board(void) { /* Disable all Extra functions in matrix except for SWD CLK/IO */ matrix_set_system_io(0x00001C30); /* Configure all PIOs as inputs to save power */ pio_set_input(PIOA, 0xFFFFFFFF, PIO_PULLUP); pio_set_input(PIOB, 0xFFFFFFFF, PIO_PULLUP); pio_set_input(PIOC, 0xFFFFFFFF, PIO_PULLUP); pio_set_input(PIOD, 0xFFFFFFFF, PIO_PULLUP); pio_set_input(PIOE, 0xFFFFFFFF, PIO_PULLUP); /* Disable USB Clock */ pmc_disable_udpck(); /* Disable pull-up on PHY */ pio_pull_up(PIOD, PIO_PD0 | PIO_PD4 | PIO_PD5 | PIO_PD6 | PIO_PD7, 0); /* Hold PHY in reset to avoid the clock output switching */ pio_set_output(PIOD, PIO_PD31, 0, 0, 0); /* Disable pull-up on VBUS */ pio_pull_up(PIOE, PIO_PE2, 0); /* Disable PIO pull-up for PB10(USB_DDM), PB11(USB_DDP) */ pio_pull_up(PIOB, PIO_PB10 | PIO_PB11, 0); /* Enable the PMC clocks of push button for wakeup */ pmc_enable_periph_clk(ID_PIOE); pio_handler_set_priority(PIOE, PIOE_IRQn, IRQ_PRIOR_PIO); }
/** * \brief Initialize SAM4C_EK board for low power test. */ void init_specific_board(void) { /* Configure all PIOs as inputs to save power */ pio_set_input(PIOA, 0xFFFFFFFF, PIO_PULLUP); pio_set_input(PIOB, 0xFFFFFFFF, PIO_PULLUP); /* Enable the PMC clocks of push button for wakeup */ pmc_enable_periph_clk(ID_PIOA); pio_handler_set_priority(PIOA, PIOA_IRQn, IRQ_PRIOR_PIO); }
static void irq_init(void) { pio_configure_pin(DW_IRQ_IDX, DW_IRQ_FLAGS); pio_pull_down(DW_IRQ_PIO, DW_IRQ_MASK, true); pio_handler_set(DW_IRQ_PIO, DW_IRQ_PIO_ID, DW_IRQ_MASK, DW_IRQ_ATTR, irq_handler); pio_enable_interrupt(DW_IRQ_PIO, DW_IRQ_MASK); pio_handler_set_priority(DW_IRQ_PIO, DW_IRQ_IRQ, 0); pmc_enable_periph_clk(DW_IRQ_PIO_ID); }
/** * \brief Configure the Pushbuttons. * * Configure the PIO as inputs and generate corresponding interrupt when * pressed or released. */ static void configure_buttons(void) { /* Configure Pushbutton 1. */ pmc_enable_periph_clk(PIN_PUSHBUTTON_1_ID); pio_set_debounce_filter(PIN_PUSHBUTTON_1_PIO, PIN_PUSHBUTTON_1_MASK, 10); pio_handler_set(PIN_PUSHBUTTON_1_PIO, PIN_PUSHBUTTON_1_ID, PIN_PUSHBUTTON_1_MASK, PIN_PUSHBUTTON_1_ATTR, Button1_Handler); NVIC_EnableIRQ((IRQn_Type) PIN_PUSHBUTTON_1_ID); pio_handler_set_priority(PIN_PUSHBUTTON_1_PIO, (IRQn_Type) PIN_PUSHBUTTON_1_ID, IRQ_PRIOR_PIO); pio_enable_interrupt(PIN_PUSHBUTTON_1_PIO, PIN_PUSHBUTTON_1_MASK); /* Configure Pushbutton 2. */ pmc_enable_periph_clk(PIN_PUSHBUTTON_2_ID); pio_set_debounce_filter(PIN_PUSHBUTTON_2_PIO, PIN_PUSHBUTTON_2_MASK, 10); pio_handler_set(PIN_PUSHBUTTON_2_PIO, PIN_PUSHBUTTON_2_ID, PIN_PUSHBUTTON_2_MASK, PIN_PUSHBUTTON_2_ATTR, Button2_Handler); NVIC_EnableIRQ((IRQn_Type) PIN_PUSHBUTTON_2_ID); pio_handler_set_priority(PIN_PUSHBUTTON_2_PIO, (IRQn_Type) PIN_PUSHBUTTON_2_ID, IRQ_PRIOR_PIO); pio_enable_interrupt(PIN_PUSHBUTTON_2_PIO, PIN_PUSHBUTTON_2_MASK); /* Configure Pushbutton 3. */ pmc_enable_periph_clk(PIN_PUSHBUTTON_3_ID); pio_set_debounce_filter(PIN_PUSHBUTTON_3_PIO, PIN_PUSHBUTTON_3_MASK, 10); pio_handler_set(PIN_PUSHBUTTON_3_PIO, PIN_PUSHBUTTON_3_ID, PIN_PUSHBUTTON_3_MASK, PIN_PUSHBUTTON_3_ATTR, Button3_Handler); NVIC_EnableIRQ((IRQn_Type) PIN_PUSHBUTTON_3_ID); pio_handler_set_priority(PIN_PUSHBUTTON_3_PIO, (IRQn_Type) PIN_PUSHBUTTON_3_ID, IRQ_PRIOR_PIO); pio_enable_interrupt(PIN_PUSHBUTTON_3_PIO, PIN_PUSHBUTTON_3_MASK); /* Configure SD card detection. */ pmc_enable_periph_clk(SD_MMC_0_CD_ID); pio_set_debounce_filter(SD_MMC_0_CD_PIO, SD_MMC_0_CD_MASK, 10); pio_handler_set(SD_MMC_0_CD_PIO, SD_MMC_0_CD_ID, SD_MMC_0_CD_MASK, SD_MMC_0_CD_ATTR, SD_Detect_Handler); NVIC_EnableIRQ((IRQn_Type) SD_MMC_0_CD_ID); pio_handler_set_priority(SD_MMC_0_CD_PIO, (IRQn_Type) SD_MMC_0_CD_ID, IRQ_PRIOR_PIO); pio_enable_interrupt(SD_MMC_0_CD_PIO, SD_MMC_0_CD_MASK); }
/* * @fn nm_bsp_register_isr * @brief Register interrupt service routine * @param[IN] pfIsr * Pointer to ISR handler */ void nm_bsp_register_isr(tpfNmBspIsr pfIsr) { gpfIsr = pfIsr; /* Configure PGIO pin for interrupt from SPI slave, used when slave has data to send. */ sysclk_enable_peripheral_clock(CONF_WINC_SPI_INT_PIO_ID); pio_configure_pin(CONF_WINC_SPI_INT_PIN, PIO_TYPE_PIO_INPUT); pio_pull_up(CONF_WINC_SPI_INT_PIO, CONF_WINC_SPI_INT_MASK, PIO_PULLUP); // pio_set_debounce_filter(CONF_WINC_SPI_INT_PIO, CONF_WINC_SPI_INT_MASK, 10); pio_handler_set_pin(CONF_WINC_SPI_INT_PIN, PIO_IT_LOW_LEVEL, chip_isr); pio_enable_interrupt(CONF_WINC_SPI_INT_PIO, CONF_WINC_SPI_INT_MASK); pio_handler_set_priority(CONF_WINC_SPI_INT_PIO, (IRQn_Type)CONF_WINC_SPI_INT_PIO_ID, CONF_WINC_SPI_INT_PRIORITY); }
/** * \brief Configure the push button 1. * * \note Configure the PIO as input and generate corresponding interrupt * when pressed or released. */ static void dsp_configure_button2(void) { /* Configure Pushbutton 1. */ pmc_enable_periph_clk(PIN_PUSHBUTTON_2_ID); pio_set_debounce_filter(PIN_PUSHBUTTON_2_PIO, PIN_PUSHBUTTON_2_MASK, 10); pio_handler_set(PIN_PUSHBUTTON_2_PIO, PIN_PUSHBUTTON_2_ID, PIN_PUSHBUTTON_2_MASK, PIN_PUSHBUTTON_2_ATTR, dsp_event_button2); /* Take care of NVIC_SetPriority. */ pio_handler_set_priority(PIN_PUSHBUTTON_2_PIO, (IRQn_Type) PIN_PUSHBUTTON_2_ID, INT_PRI_PUSHBUTTON); pio_enable_interrupt(PIN_PUSHBUTTON_2_PIO, PIN_PUSHBUTTON_2_MASK); }
/** * \brief Configures SAMG53 for low power demo. */ void init_specific_board(void) { /* For the lowest power consumption all pins should have defined state * e.g. no floating pins. * Set all pins as input with pull-up enabled with the exception: * - CDC UART RX (PA09) should not be pulled up because this pin is * driven by the EDBG in this application * - CDC UART TX (PA10) This is actively driven by the SAMG53 in this * application */ pio_set_input(PIOA, 0xFFFFF9FF, PIO_PULLUP); pio_set_input(PIOA, 0x00000600, PIO_DEFAULT); pio_set_input(PIOB, 0xFFFFFFFF, PIO_PULLUP); /* Enable the PMC clocks of push button for wakeup */ pmc_enable_periph_clk(ID_PIOA); pio_handler_set_priority(PIOA, PIOA_IRQn, IRQ_PRIORITY_PIO); }
/** * \brief Initialize SAM4E_EK board for low power test. */ void init_specific_board(void) { /* Configure all PIOs as inputs to save power */ pio_set_input(PIOA, 0xFFFFFFFF, PIO_PULLUP); pio_set_input(PIOB, 0xFFFFFFFF, PIO_PULLUP); pio_set_input(PIOC, 0xFFFFFFFF, PIO_PULLUP); /* Disable USB Clock */ pmc_disable_udpck(); /* Disable PIO pull-up for PB10(USB_DDM), PB11(USB_DDP) */ pio_pull_up(PIOB, (0x3 << 10), 0); /* Disable PIO pull-up for PC21(USB_CNX) */ pio_pull_up(PIOC, (0x1 << 21), 0); /* Enable the PMC clocks of push button for wakeup */ pmc_enable_periph_clk(ID_PIOA); pio_handler_set_priority(PIOA, PIOA_IRQn, IRQ_PRIOR_PIO); }
/** * \brief Initialize SAM3N_EK board for low power test. */ void init_specific_board(void) { /* Configure all PIOs as inputs to save power */ pio_set_input(PIOA, 0xFFFFFFFF, PIO_PULLUP); pio_set_input(PIOB, 0xFFFFFFFF, PIO_PULLUP); pio_set_input(PIOC, 0xFFFFFFFF, PIO_PULLUP); /* Disable USB Clock */ pmc_disable_upll_clock(); /* Disable PIO pull-up for PA0 (VBUS_USB) */ pio_pull_up(PIOA, (0x1 << 0), 0); /* Initialize ADC pin as ADC input mode to save power */ adc_enable_channel(ADC, ADC_CHANNEL_0); adc12b_enable_channel(ADC12B, ADC_CHANNEL_3); /* Enable the PMC clocks of push button for wakeup */ pmc_enable_periph_clk(ID_PIOA); pio_handler_set_priority(PIOA, PIOA_IRQn, IRQ_PRIOR_PIO); }
/** * \brief Initialize SAM3N_EK board for low power test. */ void init_specific_board(void) { /* Configure all PIOs as inputs to save power */ pio_set_input(PIOA, 0xFFFFFFFF, PIO_PULLUP); pio_set_input(PIOB, 0x0FFFFFFF, PIO_PULLUP); /* Exclude JTAG pins */ pio_set_input(PIOC, 0xFFFFFFFF, PIO_PULLUP); pio_set_input(PIOD, 0x7FFFFFFF, PIO_PULLUP); pio_set_input(PIOE, 0xFFFFFFFF, PIO_PULLUP); pio_set_input(PIOF, 0x3F, PIO_PULLUP); /* Disable USB Clock */ pmc_disable_udpck(); pmc_disable_upll_clock(); /* Disable PIO pull-up for PB4, PB5, PB6, PB7 */ pio_pull_up(PIOB, (0xF << 4), 0); /* Initialize ADC pin as ADC input mode to save power */ adc_enable_channel(ADC, ADC_CHANNEL_1); /* Enable the PMC clocks of push button for wakeup */ pmc_enable_periph_clk(ID_PIOB); pio_handler_set_priority(PIOB, PIOB_IRQn, IRQ_PRIOR_PIO); }
int main (void) { global_watt_hours_epoch = epoch; global_voltage_string = malloc(32); global_current_string = malloc(32); global_frequency_string = malloc(32); global_active_power_string = malloc(32); global_apparent_power_string = malloc(32); global_reactive_power_string = malloc(32); global_power_factor_string = malloc(32); global_phase_angle_string = malloc(32); sysclk_init(); board_init(); pmc_enable_periph_clk(PIN_ADE7753_ZX_ID); pio_handler_set(PIN_ADE7753_ZX_PIO, PIN_ADE7753_ZX_ID, PIN_ADE7753_ZX_MASK, PIN_ADE7753_ZX_ATTR, ZX_Handler); NVIC_EnableIRQ((IRQn_Type)PIN_ADE7753_ZX_ID); pio_handler_set_priority(PIN_ADE7753_ZX_PIO, (IRQn_Type)PIN_ADE7753_ZX_ID, IRQ_PRIOR_PIO); pio_enable_interrupt(PIN_ADE7753_ZX_PIO, PIN_ADE7753_ZX_MASK); pmc_enable_periph_clk(PIN_ADE7753_IRQ_ID); pio_handler_set(PIN_ADE7753_IRQ_PIO, PIN_ADE7753_IRQ_ID, PIN_ADE7753_IRQ_MASK, PIN_ADE7753_IRQ_ATTR, IRQ_Handler); NVIC_EnableIRQ((IRQn_Type)PIN_ADE7753_IRQ_ID); pio_handler_set_priority(PIN_ADE7753_IRQ_PIO, (IRQn_Type)PIN_ADE7753_IRQ_ID, IRQ_PRIOR_PIO); pio_enable_interrupt(PIN_ADE7753_IRQ_PIO, PIN_ADE7753_IRQ_MASK); pmc_enable_periph_clk(PIN_FP_BUTTON_LOAD_ID); pio_handler_set(PIN_FP_BUTTON_LOAD_PIO, PIN_FP_BUTTON_LOAD_ID, PIN_FP_BUTTON_LOAD_MASK, PIN_FP_BUTTON_LOAD_ATTR, FP_LOAD_Handler); NVIC_EnableIRQ((IRQn_Type)PIN_FP_BUTTON_LOAD_ID); pio_handler_set_priority(PIN_FP_BUTTON_LOAD_PIO, (IRQn_Type)PIN_FP_BUTTON_LOAD_ID, IRQ_PRIOR_PIO); pio_enable_interrupt(PIN_FP_BUTTON_LOAD_PIO, PIN_FP_BUTTON_LOAD_MASK); pmc_enable_periph_clk(PIN_FP_BUTTON_UP_ID); pio_handler_set(PIN_FP_BUTTON_UP_PIO, PIN_FP_BUTTON_UP_ID, PIN_FP_BUTTON_UP_MASK, PIN_FP_BUTTON_UP_ATTR, FP_UP_Handler); NVIC_EnableIRQ((IRQn_Type)PIN_FP_BUTTON_UP_ID); pio_handler_set_priority(PIN_FP_BUTTON_UP_PIO, (IRQn_Type)PIN_FP_BUTTON_UP_ID, IRQ_PRIOR_PIO); pio_enable_interrupt(PIN_FP_BUTTON_UP_PIO, PIN_FP_BUTTON_UP_MASK); pmc_enable_periph_clk(PIN_FP_BUTTON_DOWN_ID); pio_handler_set(PIN_FP_BUTTON_DOWN_PIO, PIN_FP_BUTTON_DOWN_ID, PIN_FP_BUTTON_DOWN_MASK, PIN_FP_BUTTON_DOWN_ATTR, FP_DOWN_Handler); NVIC_EnableIRQ((IRQn_Type)PIN_FP_BUTTON_DOWN_ID); pio_handler_set_priority(PIN_FP_BUTTON_DOWN_PIO, (IRQn_Type)PIN_FP_BUTTON_DOWN_ID, IRQ_PRIOR_PIO); pio_enable_interrupt(PIN_FP_BUTTON_DOWN_PIO, PIN_FP_BUTTON_DOWN_MASK); pmc_enable_periph_clk(PIN_FP_BUTTON_BACK_ID); pio_handler_set(PIN_FP_BUTTON_BACK_PIO, PIN_FP_BUTTON_BACK_ID, PIN_FP_BUTTON_BACK_MASK, PIN_FP_BUTTON_BACK_ATTR, FP_BACK_Handler); NVIC_EnableIRQ((IRQn_Type)PIN_FP_BUTTON_BACK_ID); pio_handler_set_priority(PIN_FP_BUTTON_BACK_PIO, (IRQn_Type)PIN_FP_BUTTON_BACK_ID, IRQ_PRIOR_PIO); pio_enable_interrupt(PIN_FP_BUTTON_BACK_PIO, PIN_FP_BUTTON_BACK_MASK); /* pmc_enable_periph_clk(PIN_FP_ENCODER_Q1_ID); pio_handler_set(PIN_FP_ENCODER_Q1_PIO, PIN_FP_ENCODER_Q1_ID, PIN_FP_ENCODER_Q1_MASK, PIN_FP_ENCODER_Q1_ATTR, FP_ENCODER_Handler); NVIC_EnableIRQ((IRQn_Type)PIN_FP_ENCODER_Q1_ID); pio_handler_set_priority(PIN_FP_ENCODER_Q1_PIO, (IRQn_Type)PIN_FP_ENCODER_Q1_ID, IRQ_PRIOR_PIO); pio_enable_interrupt(PIN_FP_ENCODER_Q1_PIO, PIN_FP_ENCODER_Q1_MASK); pmc_enable_periph_clk(PIN_FP_ENCODER_Q2_ID); pio_handler_set(PIN_FP_ENCODER_Q2_PIO, PIN_FP_ENCODER_Q2_ID, PIN_FP_ENCODER_Q2_MASK, PIN_FP_ENCODER_Q2_ATTR, FP_ENCODER_Handler); NVIC_EnableIRQ((IRQn_Type)PIN_FP_ENCODER_Q2_ID); pio_handler_set_priority(PIN_FP_ENCODER_Q2_PIO, (IRQn_Type)PIN_FP_ENCODER_Q2_ID, IRQ_PRIOR_PIO); pio_enable_interrupt(PIN_FP_ENCODER_Q2_PIO, PIN_FP_ENCODER_Q2_MASK); */ ioport_set_pin_level(LED1_GPIO, false); ioport_set_pin_level(LED2_GPIO, false); ioport_set_pin_level(LED3_GPIO, false); ioport_set_pin_level(FP_LED0_GPIO, false); ioport_set_pin_level(FP_LED1_GPIO, false); ioport_set_pin_level(FP_LED2_GPIO, false); ioport_set_pin_level(FP_LED3_GPIO, true); ioport_set_pin_level(RELAY_1_GPIO, false); ioport_set_pin_level(RELAY_2_GPIO, false); /* Initialize the console uart */ configure_console(); spi_master_initialize(); // We need to configure the ade7753... // ...to have a current gain of 2... uint8_t gain = ADE7753_GAIN_PGA1_2; ade7753_write(ADE7753_REGISTER_GAIN, &gain, ADE7753_REGISTER_GAIN_BYTES); // and a measurment of 2000 half line cycles uint32_t linecyc_int = 200; ade7753_write(ADE7753_REGISTER_LINECYC, &linecyc_int, ADE7753_REGISTER_LINECYC_BYTES); uint32_t mode_register = 0x0080; ade7753_write(ADE7753_REGISTER_MODE, &mode_register, ADE7753_REGISTER_MODE_BYTES); uint32_t irqen_register = 0x04; ade7753_write(ADE7753_REGISTER_IRQEN, &irqen_register, ADE7753_REGISTER_IRQEN_BYTES); uint8_t phase_offset = 14; ade7753_write(ADE7753_REGISTER_PHCAL, &phase_offset, ADE7753_REGISTER_PHCAL_BYTES); char input; vfd_init(); //vfd_cursor_on(); vfd_gui_splash(__DATE__, __TIME__); delay_s(5); vfd_clear(); menu_state = MENU_STATE_VAFAAR; for(;;) { if (menu_state == MENU_STATE_SPLASH) { if (changed == true) { vfd_clear(); vfd_home(); vfd_gui_splash(__DATE__, __TIME__); //vfd_gui_splash(); changed = false; } } else if (menu_state == MENU_STATE_VAFAAR) { if (changed == true) { vfd_clear(); changed = false; } vfd_home(); vfd_gui_vaf_aar(); } else if (menu_state == MENU_STATE_TRIG) { if (changed == true) { vfd_clear(); changed = false; } vfd_home(); vfd_gui_trig(); } else if (menu_state == MENU_STATE_COST) { if (changed == true) { vfd_clear(); changed = false; } vfd_home(); vfd_gui_cost(); } else if (menu_state == MENU_STATE_CONFIG) { if (changed == true) { vfd_clear(); changed = false; } vfd_home(); vfd_gui_config(); } else { vfd_clear(); } } }
/** * \brief Application entry point. * * \return Unused (ANSI-C compatibility). */ int main(void) { uint8_t uc_key; /* Initialize the SAM4 system */ sysclk_init(); board_init(); WDT->WDT_MR = WDT_MR_WDDIS; /* Enable the pmc clocks of the push buttons for all SAM4. */ pmc_enable_periph_clk(ID_PIOA); pmc_enable_periph_clk(ID_PIOB); pmc_enable_periph_clk(ID_PIOC); /* Initialize the console uart */ configure_console(); /* Output example information */ puts(STRING_HEADER); /* PIO configuration for LEDs and Buttons. */ pio_handler_set_priority(PIOA, PIOA_IRQn, 0); pio_handler_set_priority(PIOB, PIOB_IRQn, 0); pio_handler_set_priority(PIOC, PIOC_IRQn, 0); /* configure LED. */ led_config(); /* configure push buttons. */ configure_buttons(); /* Set default priorities for 2 buttons. */ puts("Set INT1's priority higher than INT2.\r"); set_interrupt_priority(INT_PRIOR_HIGH, INT_PRIOR_LOW); /* Display the main menu. */ display_menu(); // Flash the LED. while (1) { while (uart_read(CONSOLE_UART, &uc_key)); switch (uc_key) { case '1': set_interrupt_priority(INT_PRIOR_LOW, INT_PRIOR_HIGH); puts("Set INT2's priority higher than INT1.\n\r\r"); break; case '2': set_interrupt_priority(INT_PRIOR_HIGH, INT_PRIOR_LOW); puts("Set INT1's priority higher than INT2.\n\r\r"); break; case 'h': display_menu(); break; default: puts("Invalid input.\r"); break; } } }
/** * \brief Application entry point for spi_touchscreen example. * * \return Unused (ANSI-C compatibility). */ int main(void) { uint8_t uc_result; sysclk_init(); board_init(); configure_console(); /* Enable clocks for push buttons management */ pmc_enable_periph_clk(ID_PIOA); pmc_enable_periph_clk(ID_PIOB); pmc_enable_periph_clk(ID_PIOC); /* Output example information */ puts(STRING_HEADER); /* Configure systick for 1 ms. */ puts("Configure system tick to get 1ms tick period.\r"); if (SysTick_Config(sysclk_get_cpu_hz() / 1000)) { puts("Systick configuration error\r"); while (1); } /* Switch off backlight */ aat31xx_disable_backlight(); lcd_init(); lcd_set_foreground_color(UNI_COLOR_WHITE); lcd_draw_filled_rectangle(0, 0, LCD_WIDTH, LCD_HEIGHT); /* Turn on LCD */ lcd_display_on(); /* Switch on backlight */ aat31xx_set_backlight(AAT31XX_MAX_BACKLIGHT_LEVEL); /* Initializes the PIO interrupt management for touchscreen driver */ pio_handler_set_priority(PIOA, PIOA_IRQn, IRQ_PRIOR_PIO); pio_handler_set_priority(PIOB, PIOB_IRQn, IRQ_PRIOR_PIO); pio_handler_set_priority(PIOC, PIOC_IRQn, IRQ_PRIOR_PIO); /* Initialize touchscreen without calibration */ rtouch_init(LCD_WIDTH, LCD_HEIGHT); rtouch_enable(); rtouch_set_event_handler(event_handler); while (1) { uc_result = rtouch_calibrate(); if (uc_result == 0) { puts("Calibration successful !\r"); break; } else { puts("Calibration failed; error delta is too big ! Please retry calibration procedure...\r"); } } while (1) { } }
/** * \brief Application entry point for WDT example. * * \return Unused (ANSI-C compatibility). */ int main(void) { uint32_t wdt_mode, timeout_value; /* Initilize the system */ sysclk_init(); board_init(); /* Configure pins of console UART, LED and push button on board. */ configure_console(); configure_led(); configure_button(); /* Output example information. */ puts(STRING_HEADER); /* Systick configuration. */ puts("Configure systick to get 1ms tick period.\r"); if (SysTick_Config(sysclk_get_cpu_hz() / 1000)) { puts("-F- Systick configuration error\r"); } /* Get timeout value. */ timeout_value = wdt_get_timeout_value(WDT_PERIOD * 1000, BOARD_FREQ_SLCK_XTAL); if (timeout_value == WDT_INVALID_ARGUMENT) { while (1) { /* Invalid timeout value, error. */ } } /* Configure WDT to trigger an interrupt (or reset). */ wdt_mode = WDT_MR_WDFIEN | /* Enable WDT fault interrupt. */ WDT_MR_WDRPROC | /* WDT fault resets processor only. */ WDT_MR_WDDBGHLT | /* WDT stops in debug state. */ WDT_MR_WDIDLEHLT; /* WDT stops in idle state. */ /* Initialize WDT with the given parameters. */ wdt_init(WDT, wdt_mode, timeout_value, timeout_value); printf("Enable watchdog with %d microseconds period\n\r", (int)wdt_get_us_timeout_period(WDT, BOARD_FREQ_SLCK_XTAL)); /* Configure and enable WDT interrupt. */ NVIC_DisableIRQ(WDT_IRQn); NVIC_ClearPendingIRQ(WDT_IRQn); NVIC_SetPriority(WDT_IRQn, 0); NVIC_EnableIRQ(WDT_IRQn); /* Initialize and enable push button (PIO) interrupt. */ pio_handler_set_priority(PUSHBUTTON_PIO, PUSHBUTTON_IRQn, 0); pio_enable_interrupt(PUSHBUTTON_PIO, PUSHBUTTON_MASK); printf("Press %s to simulate a deadlock loop.\n\r", PUSHBUTTON_STRING); while (1) { if (g_b_systick_event == true) { g_b_systick_event = false; /* Toggle LED at the given period. */ if ((g_ul_ms_ticks % BLINK_PERIOD) == 0) { #if (SAM4E || SAM4N || SAM4C || SAMG) LED_Toggle(LED0); #else LED_Toggle(LED0_GPIO); #endif } /* Restart watchdog at the given period. */ if ((g_ul_ms_ticks % WDT_RESTART_PERIOD) == 0) { wdt_restart(WDT); } } /* Simulate deadlock when button is pressed. */ if (g_b_button_event == true) { puts("Program enters infinite loop for triggering watchdog interrupt.\r"); while (1) { } } } }
/* * 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); }
void rtos_button_init(uint16_t num_buttons, bool is_enable_tm_stick) { if(g_rtos_button_data.mutex == NULL) { g_rtos_button_data.mutex = xSemaphoreCreateMutex(); } if(g_rtos_button_data.rtos_internal_task_semaphore == NULL) { vSemaphoreCreateBinary(g_rtos_button_data.rtos_internal_task_semaphore); xSemaphoreTake(g_rtos_button_data.rtos_internal_task_semaphore, 0); // take it immediately so the waiting task doesn't fire on the first run. } if(g_rtos_button_data.rtos_task_semaphore == NULL) { vSemaphoreCreateBinary(g_rtos_button_data.rtos_task_semaphore); xSemaphoreTake(g_rtos_button_data.rtos_task_semaphore, 0); // take it immediately so the waiting task doesn't fire on the first run. } if(g_rtos_button_data.data == NULL) { g_rtos_button_data.num_button = num_buttons; size_t size = 0; if((num_buttons % 8) > 0) { size = 1; } size += num_buttons / 8; g_rtos_button_data.data = malloc(size); if(g_rtos_button_data.data != NULL) { for(size_t i = 0; i < size; i++) { g_rtos_button_data.data[i] = 0; } } } if(g_rtos_button_data.hat_data == NULL && g_rtos_button_data.num_hat > 0) { g_rtos_button_data.hat_data = malloc(g_rtos_button_data.num_hat); if(g_rtos_button_data.hat_data != NULL) { memset(g_rtos_button_data.hat_data, 0x0, g_rtos_button_data.num_hat); } } for(size_t i = 0; i <= MAX_PIO_PORT_IDX; i++) { uint32_t mask = 0; NVIC_DisableIRQ(PIOA_IRQn + i); g_rtos_button_data.ports[i].last_update_mask = 0; // init, clear out all. g_rtos_button_data.ports[i].last_update_data = 0; g_rtos_button_data.ports[i].flags = 0; for(size_t j = 0; j < 32; j++) { if((g_rtos_button_data.ports[i].button_conf[j].flags & RTOS_BUTTON_PIN_ENABLED_MASK) > 0) { // enabled. mask |= (1 << j); } } if(mask > 0) { uint32_t calculated_pio = (uint32_t)PIOA + PIO_DELTA * i; uint32_t calculated_pio_id = ID_PIOA + i; pio_handler_set((Pio*)calculated_pio, calculated_pio_id, mask, PIO_IT_EDGE, button_handler); pio_handler_set_priority((Pio*)calculated_pio, PIOA_IRQn + i, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY); // Make sure the interrupt priority is not higher than FreeRTOS mandates. Set proiroty uses 0 to 2* configPRIO_BITS, unshifted. It internally shift it to the left. So, don't use the shfited priority value. NVIC_EnableIRQ(PIOA_IRQn + i); pio_enable_interrupt((Pio*)calculated_pio, mask); } } // create the internal FreeRTOS button processing task waiting on the rtos_internal_task_semaphore xTaskHandle button_task_handle; xTaskCreate(button_task, (const signed char*)"Button Processing Task", configMINIMAL_STACK_SIZE * 2, NULL, ( ( unsigned portBASE_TYPE ) configTIMER_TASK_PRIORITY ) | portPRIVILEGE_BIT, &button_task_handle); #if (CONF_ENABLE_TM_STICK_IN_BUTTON == 1) if(is_enable_tm_stick) { xTaskHandle tm_stick_handle; xTaskCreate(tm_stick_task, (const signed char*)"TMStick Processing Task", configMINIMAL_STACK_SIZE * 2, NULL, ( ( unsigned portBASE_TYPE ) configTIMER_TASK_PRIORITY ) | portPRIVILEGE_BIT, &tm_stick_handle); } #endif }