static void tc_irq(void) { // La lecture du registre SR efface le fanion de l'interruption. tc_read_sr(EXAMPLE_TC, TC_CHANNEL); // Toggle le premier et le second LED. gpio_tgl_gpio_pin(LED0_GPIO); gpio_tgl_gpio_pin(LED1_GPIO); }
/* \brief This is an example that shows how to do the following: * - configure and start the OSC32K 32kHz oscillator, * - set-up a generic clock at 32kHz with the OSC32K osc as a source, * - output the generic clock to a pin, * - toggle all four leds 10 times, * - then switch the device into the static sleep mode (while still maintaining * the OSC32 32kHz oscillator). * */ int main(void) { //! OSC32K config. scif_osc32_opt_t osc32Conf = { SCIF_OSC_MODE_2PIN_CRYSTAL_HICUR, // 2-pin Crystal and high current mode. Crystal is connected to XIN32/XOUT32. OSC32_STARTUP, // oscillator startup time true, // select the alternate xin32_2 and xout32_2 for the 32kHz crystal oscillator false, // disable the 1kHz output true // enable the 32kHz output }; volatile int i,j; // - configure and start the OSC32K 32kHz oscillator, if(PASS != scif_start_osc32(&osc32Conf, true)) { // Error while(1) { gpio_tgl_gpio_pin(LED3_GPIO); for(i=1000; i; i--); } } // - set-up a generic clock at 32kHz with the OSC32K osc as a source, // - output the generic clock to a pin. local_start_gc(); // - toggle all four leds 10 times, for(i=25;i;i--) { gpio_tgl_gpio_pin(LED0_GPIO); gpio_tgl_gpio_pin(LED1_GPIO); gpio_tgl_gpio_pin(LED2_GPIO); gpio_tgl_gpio_pin(LED3_GPIO); for(j=1000;j;j--); } // - then switch the device into the static sleep mode (while still maintaining // the OSC32 32kHz oscillator). // If there is a chance that any PB write operations are incomplete, the CPU // should perform a read operation from any register on the PB bus before // executing the sleep instruction. AVR32_INTC.ipr[0]; // Dummy read SLEEP(AVR32_PM_SMODE_STATIC); // Note: in static sleep mode, the GCLK output is not maintained but the OSC32K // oscillator is still active (check the XIN32_2 pin (500mv peek-to-peek amplitude)). while(1); }
static void gpio_pin_change_interrupt_handler(void) { // Clear the pin change interrupt flag gpio_clear_pin_interrupt_flag(GPIO_PIN_EXAMPLE_2); // Toggle GPIO_PIN_EXAMPLE_3 gpio_tgl_gpio_pin(GPIO_PIN_EXAMPLE_3); }
/*! \brief CAN Call Back when message is transmitted * */ void can_out_callback_channel1(U8 handle, U8 event) { gpio_tgl_gpio_pin(LED2_GPIO); // Transmission Only can_mob_free(1,handle); message_transmitted_on_channel1 = true; }
void Actividad3(void){ pm_switch_to_osc0(&AVR32_PM,FOSC0,OSC0_STARTUP); Disable_global_interrupt(); //! Structure holding the configuration parameters of the EIC module. eic_options_t eic_options[2]; // Enable edge-triggered interrupt. eic_options[0].eic_mode = EIC_MODE_EDGE_TRIGGERED; // Interrupt will trigger on falling edge (this is a must-do for the keypad scan // feature if the chosen mode is edge-triggered). eic_options[0].eic_edge = EIC_EDGE_RISING_EDGE; // Initialize in synchronous mode : interrupt is synchronized to the clock eic_options[0].eic_async = EIC_SYNCH_MODE; // Set the interrupt line number. eic_options[0].eic_line = QT1081_EIC_EXTINT_INT; INTC_init_interrupts(); /* Register the EXTINT1 interrupt handler to the interrupt controller */ INTC_register_interrupt(&touch_button_isr, QT1081_EIC_EXTINT_IRQ, AVR32_INTC_INT0); // Init the EIC controller with the options eic_init(&AVR32_EIC, eic_options, 1); // Enable the EIC lines. eic_enable_lines(&AVR32_EIC, (1<<eic_options[0].eic_line)); // Enable the interrupt for each EIC line. eic_enable_interrupt_lines(&AVR32_EIC, (1<<eic_options[0].eic_line)); gpio_enable_module_pin( QT1081_EIC_EXTINT_PIN, QT1081_EIC_EXTINT_FUNCTION); Enable_global_interrupt(); while (1){ gpio_tgl_gpio_pin(LED0_GPIO); gpio_tgl_gpio_pin(LED1_GPIO); gpio_tgl_gpio_pin(LED2_GPIO); gpio_tgl_gpio_pin(LED3_GPIO); delay_s(100); } }
__interrupt #endif static void tc_irq_handler(void) { // Clear the interrupt flag. This is a side effect of reading the TC SR. chan_status = tc_read_sr(EXAMPLE_TC, EXAMPLE_TC_CHANNEL); // Toggle LED0. gpio_tgl_gpio_pin(LED0_GPIO); }
/*! \brief CAN Call Back when message is received */ void can_out_callback_channel0(U8 handle, U8 event) { gpio_tgl_gpio_pin(LED3_GPIO); // Reception Only pCANMOB_message2[0].can_msg->data.u64 = can_get_mob_data(0,handle).u64; pCANMOB_message2[0].can_msg->id = can_get_mob_id(0,handle); pCANMOB_message2[0].dlc = can_get_mob_dlc(0,handle); pCANMOB_message2[0].status = event; can_mob_free(0,handle); message_received_on_channel0 = true; }
void touch_button_isr(void){ if(is_touch_up()){ gpio_tgl_gpio_pin(LED0_GPIO); } if(is_touch_down()){ gpio_tgl_gpio_pin(LED1_GPIO); } if(is_touch_right()){ gpio_tgl_gpio_pin(LED1_GPIO); } if(is_touch_left()){ gpio_tgl_gpio_pin(LED2_GPIO); } if(is_touch_center()){ gpio_tgl_gpio_pin(LED2_GPIO); } eic_clear_interrupt_line(&AVR32_EIC, QT1081_EIC_EXTINT_INT); }
/** \brief Toggle LED 0 for a short while. * */ static void toggle_led(void) { volatile uint32_t i; volatile uint32_t j = 30; do { j--; i = 1000; while (i--); gpio_tgl_gpio_pin(LED0_GPIO); // Toggle the LED0. } while (j); }
void Blinker_Task_Function (void *Parameters) { char Buffer [3]; for (;;) { vTaskDelay (1000); gpio_tgl_gpio_pin (LED0_GPIO); usart_write_line (&AVR32_USART0, "Last "); snprintf (Buffer, 3, "%02i", Item_Buffer_Last); usart_write_line (&AVR32_USART0, Buffer); usart_write_line (&AVR32_USART0, "\n"); } }
/* \brief This is an example that shows how to do the following: * - start the RC120M RC Osc * - switch the main clock to RC120M/4 * - set-up a generic clock with the RC120M RC Osc as a source * - output a generic clock to : * - GCLK_1_0(STK600_RCUC3L0 & AT32UC3L-EK) * - GCLK_0 (STK600_RCUC3D) */ int main(void) { // Start RC120M, switch main clock to RC120M/4. local_set_main_clock_to_rc120m(); /* Set-up a generic clock from a high frequency clock and output it to a gpio pin. */ local_start_gc(); /* Now toggle LED0 using a GPIO */ while(1) { gpio_tgl_gpio_pin(LED0_GPIO); software_delay(); } }
__interrupt #endif #if(INT_MODE == INT_MODE_GPIO) void touch_button_isr(void){ // UP if(gpio_get_pin_interrupt_flag(QT1081_TOUCH_SENSOR_0)) { gpio_clear_pin_interrupt_flag(QT1081_TOUCH_SENSOR_0); gpio_tgl_gpio_pin(LED0_GPIO); } // DOWN if(gpio_get_pin_interrupt_flag(QT1081_TOUCH_SENSOR_1)) { gpio_clear_pin_interrupt_flag(QT1081_TOUCH_SENSOR_1); gpio_tgl_gpio_pin(LED1_GPIO); } // Right if(gpio_get_pin_interrupt_flag(QT1081_TOUCH_SENSOR_2)) { gpio_clear_pin_interrupt_flag(QT1081_TOUCH_SENSOR_2); gpio_tgl_gpio_pin(LED2_GPIO); } // Left if(gpio_get_pin_interrupt_flag(QT1081_TOUCH_SENSOR_3)) { gpio_clear_pin_interrupt_flag(QT1081_TOUCH_SENSOR_3); gpio_tgl_gpio_pin(LED3_GPIO); } // Push if(gpio_get_pin_interrupt_flag(QT1081_TOUCH_SENSOR_4)) { gpio_clear_pin_interrupt_flag(QT1081_TOUCH_SENSOR_4); gpio_tgl_gpio_pin(LED0_GPIO); } }
/* \brief This is an example that shows how to do the following: * - start a high frequency clock * - switch the main clock to that high frequency clock * - set-up a generic clock with a high frequency clock as a source * - output a generic clock to GCLK_0_1(EVK1100) / GCLK_2_1 / GCLK_2(EVK1101) / * GCLK_1_0(EVK1104) / GCLK_0_2(UC3C_EK) / GCLK_1_0(STK600_RCUC3L0 & AT32UC3L-EK) * */ int main(void) { /* Start a high frequency clock and switch the main clock to that high frequency clock */ local_start_highfreq_clock(); /* Set-up a generic clock from a high frequency clock and output it to a gpio pin. */ local_start_gc(); /* Now toggle LED0 using a GPIO */ while(1) { gpio_tgl_gpio_pin(LED0_GPIO); software_delay(); } }
__interrupt #endif void touch_button_isr(void) { manage_button_isr(QT1081_TOUCH_SENSOR_UP, JOYSTICK_STATUS_UP, JOYSTICK_STATUS_RELEASED_UP); manage_button_isr(QT1081_TOUCH_SENSOR_DOWN, JOYSTICK_STATUS_DOWN, JOYSTICK_STATUS_RELEASED_DOWN); manage_button_isr(QT1081_TOUCH_SENSOR_RIGHT, JOYSTICK_STATUS_RIGHT, JOYSTICK_STATUS_RELEASED_RIGHT); manage_button_isr(QT1081_TOUCH_SENSOR_LEFT, JOYSTICK_STATUS_LEFT, JOYSTICK_STATUS_RELEASED_LEFT); manage_button_isr(QT1081_TOUCH_SENSOR_ENTER, JOYSTICK_STATUS_PRESSED, JOYSTICK_STATUS_RELEASED_PRESSED); // Handle left slide if (IS_JOYSTICK_RELEASED_KEY_PRESSED() && IS_JOYSTICK_KEY_LEFT()) { CLEAR_JOYSTICK_RELEASED_KEY_PRESSED(); CLEAR_JOYSTICK_KEY_LEFT(); joystick_status |= JOYSTICK_STATUS_SLIDING_LEFT; } if (IS_JOYSTICK_KEY_SLIDING_LEFT() && IS_JOYSTICK_RELEASED_KEY_LEFT()) { CLEAR_JOYSTICK_KEY_SLIDING_LEFT(); CLEAR_JOYSTICK_RELEASED_KEY_LEFT(); joystick_status |= JOYSTICK_STATUS_SLIDE_LEFT; } // Handle right slide if (IS_JOYSTICK_RELEASED_KEY_PRESSED() && IS_JOYSTICK_KEY_RIGHT()) { CLEAR_JOYSTICK_RELEASED_KEY_PRESSED(); CLEAR_JOYSTICK_KEY_RIGHT(); joystick_status |= JOYSTICK_STATUS_SLIDING_RIGHT; } if (IS_JOYSTICK_KEY_SLIDING_RIGHT() && IS_JOYSTICK_RELEASED_KEY_RIGHT()) { CLEAR_JOYSTICK_KEY_SLIDING_RIGHT(); CLEAR_JOYSTICK_RELEASED_KEY_RIGHT(); joystick_status |= JOYSTICK_STATUS_SLIDE_RIGHT; } if (fast_mode && cpu_is_timeout(&joystick_key_sensibility_timer)) { CLEAR_JOYSTICK_RELEASED_KEY_RIGHT(); CLEAR_JOYSTICK_RELEASED_KEY_LEFT(); } if (!IS_JOYSTICK_KEY_LEFT() && !IS_JOYSTICK_KEY_RIGHT()) fast_mode = false; gpio_tgl_gpio_pin(LED0_GPIO); }
static void tc_irq(void) { // Increment the ms seconds counter tc_tick++; /* * TODO: Place a breakpoint here and watch the update of tc_tick variable * in the Watch Window. */ // Clear the interrupt flag. This is a side effect of reading the TC SR. tc_read_sr(EXAMPLE_TC, EXAMPLE_TC_CHANNEL); // specify that an interrupt has been raised update_timer = true; // Toggle the GPIO line gpio_tgl_gpio_pin(EXAMPLE_TOGGLE_PIN); }
int main(void) { // Switch main clock from internal RC to external Oscillator 0 pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP); init_dbg_rs232(FOSC0); gpio_enable_gpio_pin(AVR32_PIN_PB00); print_dbg("\r\n\nstart"); while (true) { delay_ms(250); print_dbg("."); gpio_tgl_gpio_pin(AVR32_PIN_PB00); } }
/*! \brief Set-up a generic clock at 32kz with the OSC32 as a source, output the * generic clock to a pin. * */ static void local_start_gc(void) { scif_gclk_opt_t gclkOpt = {SCIF_GCCTRL_OSC32K, 0, 0}; volatile int i; if(scif_start_gclk(EXAMPLE_GCLK_ID, &gclkOpt)) { // Error while(1) { gpio_tgl_gpio_pin(LED3_GPIO); for(i=1000; i; i--); } } // Assign a GPIO to generic clock output gpio_enable_module_pin(EXAMPLE_GCLK_PIN, EXAMPLE_GCLK_FUNCTION); // Note that gclk1 is GPIO pin 6 pa06 on AT32UC3L064 pin 10 on QFP48. }
void Consumer_Task_Function (void *Parameters) { Item X; for (;;) { gpio_tgl_gpio_pin (LED6_GPIO); while (xSemaphoreTake (Item_Available_Count, 1000) != pdPASS); /* if (xSemaphoreTake (Item_Available_Count, 0) != pdPASS) { vTaskSuspend (NULL); } */ X = Item_Remove (); xSemaphoreGive (Space_Available_Count); //vTaskResume (Producer_Task_Handle); Item_Consume (X); } vTaskDelete (NULL); }
void Producer_Task_Function (void * Parameters) { Item X; for (;;) { gpio_tgl_gpio_pin (LED4_GPIO); X = Item_Produce (); while (xSemaphoreTake (Space_Available_Count, 1000) != pdPASS); /* if (xSemaphoreTake (Space_Available_Count, 0) != pdPASS) { vTaskSuspend (NULL); } */ Item_Store (X); if( xSemaphoreGive( Item_Available_Count ) != pdTRUE ) { usart_write_line (&AVR32_USART0, "xSemaphoreGive Item_Available_Count failed\n"); } //vTaskResume (Consumer_Task_Handle); } vTaskDelete (NULL); }
/*============================================================================ Name : main ------------------------------------------------------------------------------ Purpose : main code entry point. Input : n/a Output : n/a Notes : ============================================================================*/ int main( void ) { /* BEFORE USING THE EXAMPLE PROJECTS. 1. The Example application uses a CPU, PBA and PBB clock of 48MHz. When using a different frequency setting, the following parameters must be changed accordingly to ensure proper QMatrix operation. a. QM_GCLK_CAT_DIV. b. QM_CAT_CLK_DIV. c. TOUCH_SPREAD_SPECTRUM_MAX_DEV, when Spread spectrum is enabled. d. PBA_HZ, when using QDebug/SPI_Master.c e. TARGET_PBA_FREQ_HZ and TARGET_CPU_FREQ_HZ, when using QDebug/SERIAL.c 2. In the UC3L-Evaluation kit (Rev2), the R42 and R54 (both 470KOhm) resistors MUST be replaced to 910KOhms. 3. The QTouch library uses PDCA channels 0 and 1. (QM_DMA_CHANNEL_0, QM_DMA_CHANNEL_1). Similarly, the QDebug/SERIAL.c uses PDCA channels 2 and 3. (PDCA_CHANNEL_RX_USART, PDCA_CHANNEL_TX_USART) 4. For QMatrix operation, the Analog comparators channels are used (using the ACIFB interface) depending on the Y Lines enabled. For example, when Y lines Y2 and Y7 are enabled the Analog comparator channels 2 and 7 are used by the CAT module for QMatrix operation. The user can uses the rest of the Analog comparator channels in the main application. The QTouch Library enables the ACIFB using the Control register (if not already enabled by the main application) when the touch_qm_sensors_init API is called. 5. When two or more acquisition methods are used, care must be taken such that a given port pin is not used by more than one method at the same time. The following pin configuration options available in touch_config_at32uc3l.h must be carefully chosen to avoid any overlapping. a. QMatrix Pin Configuration Options. b. Autonomous QTouch Pin Configuration Options. c. QTouch Group A Pin Configuration Options. d. QTouch Group B Pin Configuration Options. e. Touch Sync Pin option. */ touch_ret_t touch_ret = TOUCH_SUCCESS; touch_qm_dma_t qm_dma; /* Initialize host clock, pins, watchdog, etc. */ init_system(); /* Disable interrupts. */ Disable_global_interrupt(); /* The INTC driver has to be used only for GNU GCC for AVR32. */ #if (defined __GNUC__) /* initialize interrupt vectors. */ INTC_init_interrupts(); /* Register the Timer interrupt handler to the interrupt controller. */ INTC_register_interrupt(&tc_irq, EXAMPLE_TC_IRQ, AVR32_INTC_INT1); /* Register the Touch Library CAT interrupt handler to the interrupt controller. Note: This interrupt registration is a MUST before using the Touch Library with the GCC compiler. For the case of IAR the registration of interrupt is automatically taken care by the compiler. The Touch Library CAT interrupt level for the case of IAR is fixed to Interrupt level 3. */ INTC_register_interrupt(&touch_acq_done_irq, AVR32_CAT_IRQ, AVR32_INTC_INT3); #endif /* Enable interrupts. */ Enable_global_interrupt(); /* Configure timer to fire ISR regularly. */ init_timer(); /* Initialize touch library and uc3l cat module for QMatrix operation. Note: Set up the GCLK_CAT for proper QMatrix operation. Refer init_system(). */ touch_ret = touch_qm_sensors_init( &touch_config ); if(touch_ret != TOUCH_SUCCESS) { while(1u); /* Check API Error return code. */ } #if DEF_TOUCH_QDEBUG_ENABLE == 1 /* Initialize the debug interface. */ QDebug_Init(); #endif /* configure the touch library sensors. */ touch_ret = config_uc3lek_touch_sensors(); if(touch_ret != TOUCH_SUCCESS) { while(1u); /* Check API Error return code. */ } /* Initialize touch sensing. */ touch_ret = touch_qm_sensors_calibrate(); if(touch_ret != TOUCH_SUCCESS) { while(1u); /* Check API Error return code. */ } /* Provide the dma channels to be used by the CAT module. For each acquisition cycle, any different combination of dma channels from 0 to 11 can be provided. The touch library can also handle a different combination of dma channels for each call of the touch_qm_sensors_start_acquisition API. */ qm_dma.dma_ch1 = QM_DMA_CHANNEL_0; qm_dma.dma_ch2 = QM_DMA_CHANNEL_1; // Initialize the PWMA module demo_init_pwma(); // At the start of the demo, automatically change several times the PWMA duty // cycle (i.e. the intensity) of all LEDs. demo_automatic_ledshow_play(DEMO_INIT_NB_AUTOMATIC_CHANGES); /* Loop forever */ for( ; ; ) { /* Process touch library events. The touch_event_dispatcher API needs to be called as frequently as possible in order to have a good touch response. */ touch_event_dispatcher(); if( time_to_measure_touch == 1u ) { /* Clear flag: it's time to measure touch */ time_to_measure_touch = 0u; /* Start a touch sensors measurement process. */ touch_ret = touch_qm_sensors_start_acquisition( current_time_ms_touch, &qm_dma, NORMAL_ACQ_MODE, touch_qm_measure_complete_callback); if( (touch_ret != TOUCH_SUCCESS) && (touch_ret != TOUCH_ACQ_INCOMPLETE) ) { gpio_clr_gpio_pin(LED0_GPIO); // LED0 gpio_clr_gpio_pin(LED1_GPIO); // LED1 gpio_clr_gpio_pin(LED2_GPIO); // LED2 gpio_clr_gpio_pin(LED3_GPIO); // LED3 do{ delay_ms(50); gpio_tgl_gpio_pin(LED0_GPIO); gpio_tgl_gpio_pin(LED1_GPIO); gpio_tgl_gpio_pin(LED2_GPIO); gpio_tgl_gpio_pin(LED3_GPIO); }while(1); /* Reaching this point can be due to - 1. The api has returned an error due to a invalid input parameter. 2. The api has been called during a invalid Touch Library state. */ } } /* Host application code goes here */ /* Led demo application. */ if(qm_measurement_done_touch == 1u) { #if DEF_TOUCH_QDEBUG_ENABLE == 1 /* UC3L_EK two-way QDebug communication application Example. */ /* Process any commands received from QTouch Studio. */ QDebug_ProcessCommands(); /* Send out the Touch debug information data each time when Touch */ /* measurement process is completed . */ QDebug_SendData(p_qm_measure_data->acq_status); #endif // New touch data measurement are available. process_qtouchlib_data(); /* Clear flag: QMatrix measurement complete. */ qm_measurement_done_touch = 0u; // Once the latest touch data measurements have been processed, clear them. // Here we clear only the measurements that are used by the application. p_qm_measure_data->p_sensor_states[0] = 0; p_qm_measure_data->acq_status = TOUCH_NO_ACTIVITY; p_qm_measure_data->p_rotor_slider_values[0] = 0; } else process_qtouchlib_data(); // Note: we cannot go deeper than the IDLE sleep mode because the QMatrix lib // uses the PDMA. SLEEP(AVR32_PM_SMODE_IDLE); } }
/*! \brief Main function. Execution starts here. */ int main(void) { // Options for QDEC timer Mode static const qdec_timer_opt_t QUADRATURE_TIMER_OPT = { .upd = false, // Up/Down Mode Timer Disabled. .tsdir = QDEC_TSIR_DOWN, // Count Down Timer. .filten = false, // Disable filtering. .evtrge = false, // Disable event triggering. .rcce = true, // Enable Position Compare. .pcce = true, // Enable Revolution Compare. }; // Options for QDEC Interrupt Management static const qdec_interrupt_t QDEC_INTERRUPT = { .cmp = 1, // Enable Compare Interrupt. .cap = 0, // Disable Capture Interrupt. .pcro = 0, // Disable Position Roll-over Interrupt. .rcro = 0 // Disable Counter Roll-over Interrupt. }; // Configure system clocks. if (pcl_configure_clocks(&pcl_freq_param) != PASS) { while(1); } // Setup the generic clock for QDEC scif_gc_setup(AVR32_SCIF_GCLK_QDEC0, SCIF_GCCTRL_OSC0, AVR32_SCIF_GC_NO_DIV_CLOCK, 0); // Now enable the generic clock scif_gc_enable(AVR32_SCIF_GCLK_QDEC0); Disable_global_interrupt(); // Initialize interrupt vectors. INTC_init_interrupts(); // Register the QDEC interrupt handler to the interrupt controller. INTC_register_interrupt(&qdec_int_handler, AVR32_QDEC0_IRQ, AVR32_INTC_INT0); Enable_global_interrupt(); // Initialization of counter value as a 32-bit counter to 0x0000FFFF (Rc=0x0000/Pc=0xFFFF) qdec_write_rc_cnt(qdec,0x0000); qdec_write_pc_cnt(qdec,0xffff); // Initialization of compare value to 0x0 as the interrupt will be generated when the counter value will be equal to 0 qdec_write_rc_cmp(qdec,0); qdec_write_pc_cmp(qdec,0); // Initialize the QDEC in quadrature decoder mode. qdec_init_timer_mode(qdec,&QUADRATURE_TIMER_OPT); // Configure the QDEC interrupts. qdec_configure_interrupts(qdec,&QDEC_INTERRUPT); // Start the QDEC. qdec_software_trigger(qdec); unsigned int pc_cmp = 0; while(1) { // Compare Interrupt Flag if(flag_qdec == 1) { gpio_tgl_gpio_pin(LED0_GPIO); // Check signal on oscilloscope. if (pc_cmp < 0xffff ) pc_cmp += 0x100; // Increase PC CMP else pc_cmp = 0; // Start the signal pattern over. qdec_write_pc_cmp(qdec,pc_cmp); // Reload Compare Flag flag_qdec = 0; // Reset Interrupt Flag } } }
/** * \brief Main Application Routine * -Initialize the system clocks \n * -Configure and Enable the PWMA Generic clock \n * -Configure and Enable the PWMA Module \n * -Load Duty cycle value using Interlinked multi-value mode \n * -Register and Enable the Interrupt \n * -Enter into sleep mode \n */ int main (void) { uint32_t div; bool config_status = FAIL; bool set_value_status = FAIL; /* * Duty cycle value to be loaded. As Two channels are used * in this example, two values has been assigned. Initialize the unused * channel duty cycle value to 0, as it may take some garbage values * and will return FAIL while updating duty cycle as the value might * exceed the limit NOTE : Maximum four values can be loaded at a time, * as the channel limitation for Interlinked multi-value mode is four. */ uint16_t duty_cycle[] = {11,5,0,0}; /* PWMA Pin and Function Map */ static const gpio_map_t PWMA_GPIO_MAP = { {EXAMPLE_PWMA_PIN1, EXAMPLE_PWMA_FUNCTION1}, {EXAMPLE_PWMA_PIN2, EXAMPLE_PWMA_FUNCTION2}, }; /* Enable the PWMA Pins */ gpio_enable_module(PWMA_GPIO_MAP, sizeof(PWMA_GPIO_MAP) / sizeof(PWMA_GPIO_MAP[0])); /* * Calculate the division factor value to be loaded and * Enable the Generic clock */ div = div_ceil((sysclk_get_pba_hz()) , EXAMPLE_PWMA_GCLK_FREQUENCY); genclk_enable_config(EXAMPLE_PWMA_GCLK_ID,EXAMPLE_PWMA_GCLK_SOURCE,div); /* * Initialize the System clock * Note: Clock should be configured in conf_clock.h */ sysclk_init(); /* Initialize the delay routines */ delay_init(sysclk_get_cpu_hz()); /* * Configure and Enable the PWMA Module. The LED will turned if * configuration fails because of invalid argument. */ config_status = pwma_config_enable(pwma, EXAMPLE_PWMA_OUTPUT_FREQUENCY, EXAMPLE_PWMA_GCLK_FREQUENCY, PWMA_SPREAD); /* Error in configuring the PWMA module */ if (config_status == FAIL){ while (1) { delay_ms(10); gpio_tgl_gpio_pin(ERROR_LED); } } /* Load the duty cycle values using Interlinked multi-value mode */ set_value_status = pwma_set_multiple_values(pwma, ((EXAMPLE_PWMA_CHANNEL_ID1<<0) | (EXAMPLE_PWMA_CHANNEL_ID2<<8)), (uint16_t*)&duty_cycle); /* Error in loading the duty cycle value */ if (set_value_status == FAIL){ while (1) { delay_ms(10); gpio_tgl_gpio_pin(ERROR_LED); } } /* Disable global interrupts */ cpu_irq_disable(); /* * Initialize the interrupt vectors * Note: This function adds nothing for IAR as the interrupts are * handled by the IAR compiler itself. It provides an abstraction * between GCC & IAR compiler to use interrupts. * Refer function implementation in interrupt_avr32.h */ irq_initialize_vectors(); /* * Register the ACIFB interrupt handler * Note: This function adds nothing for IAR as the interrupts are * handled by the IAR compiler itself. It provides an abstraction * between GCC & IAR compiler to use interrupts. * Refer function implementation in interrupt_avr32.h */ irq_register_handler(&tofl_irq, AVR32_PWMA_IRQ, PWMA_INTERRUPT_PRIORITY); /* Enable the Timebase overflow interrupt */ pwma->ier = AVR32_PWMA_IER_TOFL_MASK; /* Enable global interrupt */ cpu_irq_enable(); /* Go to sleep mode */ while(1){ /* Enter into sleep mode */ pm_sleep(AVR32_PM_SMODE_FROZEN); } } /* End of main() */
void Actividad1(void){ pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP); //osc0 a 12Mhz while (1) { if (debounce2(QT1081_TOUCH_SENSOR_LEFT)) { pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP); while(gpio_get_pin_value(QT1081_TOUCH_SENSOR_ENTER)==0) { gpio_tgl_gpio_pin(LED3_GPIO); delay_s(50); } display(0); } if (debounce2(QT1081_TOUCH_SENSOR_RIGHT)) { pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP); while (gpio_get_pin_value(QT1081_TOUCH_SENSOR_ENTER)==0) { displayPrimo(); delay_s(50); } } if (debounce2(QT1081_TOUCH_SENSOR_UP)) { pm_switch_to_clock(&AVR32_PM,0); //cambia al RC pm_pll_disable(&AVR32_PM,0); // deshabilita el el PLL 0 pm_pll_setup(&AVR32_PM,0,2,1,0,16); // lockcount in main clock for the PLL wait lock //_______________________________________________________________________________ // Establece la frecuencia de salida del PLL pm_pll_set_option(&AVR32_PM,0,1,0,0);//1 Star-up faster, Start-up normal //_______________________________________________________________________________ //Habilita el PLL 0 pm_pll_enable(&AVR32_PM,0); //_______________________________________________________________________________ //Espera a que se establesca el PLL pm_wait_for_pll0_locked(&AVR32_PM) ; //_______________________________________________________________________________ // Set one wait-state (WS) for flash controller flashc_set_wait_state(1); //habilita la salida del PLL0 con 2 y el OSC0 con 1 pm_switch_to_clock(&AVR32_PM, 2); while (gpio_get_pin_value(QT1081_TOUCH_SENSOR_ENTER)==0) { displayA1(); delay_s(50); } display(0); } if (debounce2(QT1081_TOUCH_SENSOR_DOWN)) { pm_switch_to_clock(&AVR32_PM,0); //cambia al RC pm_pll_disable(&AVR32_PM,0); // deshabilita el el PLL 0 pm_pll_setup(&AVR32_PM,0,6,1,0,16); // lockcount in main clock for the PLL wait lock //_______________________________________________________________________________ // Establece la frecuencia de salida del PLL pm_pll_set_option(&AVR32_PM,0,1,0,0);//1 Star-up faster, Start-up normal //_______________________________________________________________________________ //Habilita el PLL 0 pm_pll_enable(&AVR32_PM,0); //_______________________________________________________________________________ //Espera a que se establesca el PLL pm_wait_for_pll0_locked(&AVR32_PM) ; //_______________________________________________________________________________ // Set one wait-state (WS) for flash controller flashc_set_wait_state(1); //habilita la salida del PLL0 con 2 y el OSC0 con 1 pm_switch_to_clock(&AVR32_PM, 2); while (gpio_get_pin_value(QT1081_TOUCH_SENSOR_ENTER)==0) { displayOnOff(); } display(0); } } }
/*! \brief Initializes QT60168 resources: GPIO and SPI */ static void qt60168_resources_init(void) { static const gpio_map_t QT60168_SPI_GPIO_MAP = { {QT60168_SPI_SCK_PIN, QT60168_SPI_SCK_FUNCTION }, // SPI Clock. {QT60168_SPI_MISO_PIN, QT60168_SPI_MISO_FUNCTION }, // MISO. {QT60168_SPI_MOSI_PIN, QT60168_SPI_MOSI_FUNCTION }, // MOSI. {QT60168_SPI_NPCS0_PIN, QT60168_SPI_NPCS0_FUNCTION} // Chip Select NPCS. }; // SPI options. spi_options_t spiOptions = { .reg = QT60168_SPI_NCPS, .baudrate = QT60168_SPI_MASTER_SPEED, // Defined in conf_qt60168.h. .bits = QT60168_SPI_BITS, // Defined in conf_qt60168.h. .spck_delay = 0, .trans_delay = 0, .stay_act = 0, .spi_mode = 3, .modfdis = 1 }; // Assign I/Os to SPI. gpio_enable_module(QT60168_SPI_GPIO_MAP, sizeof(QT60168_SPI_GPIO_MAP) / sizeof(QT60168_SPI_GPIO_MAP[0])); // Initialize as master. spi_initMaster(QT60168_SPI, &spiOptions); // Set selection mode: variable_ps, pcs_decode, delay. spi_selectionMode(QT60168_SPI, 0, 0, 0); // Enable SPI. spi_enable(QT60168_SPI); // Initialize QT60168 with SPI clock Osc0. spi_setupChipReg(QT60168_SPI, &spiOptions, FOSC0); } typedef enum { DEMO_COLOR_ALL=0 , DEMO_COLOR_BLUE , DEMO_COLOR_RED , DEMO_COLOR_GREEN , DEMO_COLOR_MAX } demo_color_t; typedef enum { DEMO_DISPLAY_BOXES=0 , DEMO_DISPLAY_WHEEL , DEMO_DISPLAY_MAX } demo_display_t; /*! \brief Main function */ int main(void) { int i; bool idle=false; // Detect key transition (PRESSED -> RELEASED) U32 x_start; U32 y_start; U32 x_size; U32 y_size; U16 color; const U16 icon[QT60168_TOUCH_NUMBER_OF_SENSORS] = {0, 1*16, 2*16, 3*16, 4*16, 5*16, -1, -1, 6*16, 7*16, 8*16, 9*16, 10*16, 11*16, -1, -1}; demo_color_t demo_color=DEMO_COLOR_ALL; demo_display_t demo_display=DEMO_DISPLAY_WHEEL; bool touch_states[QT60168_TOUCH_NUMBER_OF_SENSORS]; // Switch the main clock to the external oscillator 0 pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP); // Initialize RS232 debug text output. init_dbg_rs232(FOSC0); // Initialize QT60168 resources: GPIO, SPI and QT60168. qt60168_resources_init(); // Initialize QT60168 component. qt60168_init(FOSC0); // Initialize the LCD. et024006_Init( FOSC0/*CPU*/, FOSC0/*HSB*/); // Clear the display i.e. make it black et024006_DrawFilledRect(0, 0, ET024006_WIDTH, ET024006_HEIGHT, BLACK ); // Set the backlight. gpio_set_gpio_pin(ET024006DHU_BL_PIN); // Display welcome string. et024006_PrintString("QT60168 EXAMPLE", (const unsigned char *)&FONT8x8, 110, 5, WHITE, -1); et024006_PrintString("Press the QTouch sensors.", (const unsigned char *)&FONT6x8, 95, 20, WHITE, -1); et024006_PrintString("Color: All", (const unsigned char *)&FONT6x8, 10, 200, WHITE, -1); et024006_PrintString("Display sensors", (const unsigned char *)&FONT6x8, 120, 200, WHITE, -1); et024006_DrawLine(DEMO_START_X, DEMO_START_Y-1, DEMO_START_X+DEMO_SIZE_X, DEMO_START_Y-1, WHITE ); et024006_DrawLine(DEMO_START_X, DEMO_START_Y+DEMO_SIZE_Y+1, DEMO_START_X+DEMO_SIZE_X, DEMO_START_Y+DEMO_SIZE_Y+1, WHITE ); // Memorize the status for each key. for( i=0 ; i<QT60168_TOUCH_NUMBER_OF_SENSORS ; i++ ) touch_states[i] = qt60168_is_key_pressed(i); // Set LED state in a known state. gpio_set_gpio_pin(LED0_GPIO); gpio_set_gpio_pin(LED1_GPIO); gpio_set_gpio_pin(LED2_GPIO); gpio_set_gpio_pin(LED3_GPIO); while(1) { for( i=0 ; i<QT60168_TOUCH_NUMBER_OF_SENSORS ; i++) { // Test Press event on sensors // if( !touch_states[i] && qt60168_is_key_pressed(i) ) { touch_states[i] = true; if( i==QT60168_TOUCH_SENSOR_BUTTON_0 ) { gpio_tgl_gpio_pin(LED0_GPIO); et024006_PrintString("B0", (const unsigned char *)&FONT6x8, 10, 215, WHITE, -1); demo_color=(demo_color+1) % DEMO_COLOR_MAX; // Erase previous line et024006_DrawFilledRect(10, 200, 80, 10, BLACK ); switch( demo_color ) { case DEMO_COLOR_BLUE: et024006_PrintString("Color: Blue", (const unsigned char *)&FONT6x8, 10, 200, WHITE, -1); break; case DEMO_COLOR_RED: et024006_PrintString("Color: Red", (const unsigned char *)&FONT6x8, 10, 200, WHITE, -1); break; case DEMO_COLOR_GREEN: et024006_PrintString("Color: Green", (const unsigned char *)&FONT6x8, 10, 200, WHITE, -1); break; default: et024006_PrintString("Color: All", (const unsigned char *)&FONT6x8, 10, 200, WHITE, -1); break; } } else if( i==QT60168_TOUCH_SENSOR_BUTTON_1 ) { gpio_tgl_gpio_pin(LED1_GPIO); et024006_PrintString("B1", (const unsigned char *)&FONT6x8, 30, 215, WHITE, -1); demo_display=(demo_display+1) % DEMO_DISPLAY_MAX; // Erase previous line et024006_DrawFilledRect(120, 200, 160, 10, BLACK ); switch( demo_display ) { case DEMO_DISPLAY_WHEEL: et024006_PrintString("Display sensors", (const unsigned char *)&FONT6x8, 120, 200, WHITE, -1); break; case DEMO_DISPLAY_BOXES: default: et024006_PrintString("Display random boxes", (const unsigned char *)&FONT6x8, 120, 200, WHITE, -1); break; } // Erase display et024006_DrawFilledRect(DEMO_START_X, DEMO_START_Y, DEMO_SIZE_X, DEMO_SIZE_Y, BLACK ); } else if( i==QT60168_TOUCH_SENSOR_BUTTON_2 ) { gpio_tgl_gpio_pin(LED2_GPIO); et024006_PrintString("B2", (const unsigned char *)&FONT6x8, 50, 215, WHITE, -1); } else if( i==QT60168_TOUCH_SENSOR_BUTTON_3 ) { gpio_tgl_gpio_pin(LED3_GPIO); et024006_PrintString("B3", (const unsigned char *)&FONT6x8, 70, 215, WHITE, -1); } else { // Press transition detected for the wheel idle = false; // Draw Wheel[i] et024006_DrawFilledRect(100 + icon[i], 215-2, 10, 10, WHITE ); } } // Test Release event on sensors // if(touch_states[i] && !qt60168_is_key_pressed(i)) { touch_states[i] = false; if( i==QT60168_TOUCH_SENSOR_BUTTON_0 ) { // Erase "B0" et024006_DrawFilledRect(10, 215-2, 12, 12, BLACK ); } else if( i==QT60168_TOUCH_SENSOR_BUTTON_1 ) { // Erase "B1" et024006_DrawFilledRect(30, 215-2, 12, 12, BLACK ); } else if( i==QT60168_TOUCH_SENSOR_BUTTON_2 ) { // Erase "B2" et024006_DrawFilledRect(50, 215-2, 12, 12, BLACK ); } else if( i==QT60168_TOUCH_SENSOR_BUTTON_3 ) { // Erase "B3" et024006_DrawFilledRect(70, 215-2, 12, 12, BLACK ); } else { // Erase Wheel[i] et024006_DrawFilledRect(100 + icon[i], 215-2, 10, 10, BLACK ); } } } // for... if( demo_display==DEMO_DISPLAY_WHEEL ) { if( touch_states[QT60168_TOUCH_SENSOR_BUTTON_0] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(30, 50, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_BUTTON_1] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(30, 80, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_BUTTON_2] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(30, 110, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_BUTTON_3] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(30, 140, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_0] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X + DEMO_WHEEL_RADIUS*SIN0, DEMO_WHEEL_START_Y - DEMO_WHEEL_RADIUS*COS0, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_1] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X + DEMO_WHEEL_RADIUS*SIN30, DEMO_WHEEL_START_Y - DEMO_WHEEL_RADIUS*COS30, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_2] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X + DEMO_WHEEL_RADIUS*SIN60, DEMO_WHEEL_START_Y - DEMO_WHEEL_RADIUS*COS60, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_3] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X + DEMO_WHEEL_RADIUS*SIN90, DEMO_WHEEL_START_Y - DEMO_WHEEL_RADIUS*COS90, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_4] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X + DEMO_WHEEL_RADIUS*SIN60, DEMO_WHEEL_START_Y + DEMO_WHEEL_RADIUS*COS60, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_5] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X + DEMO_WHEEL_RADIUS*SIN30, DEMO_WHEEL_START_Y + DEMO_WHEEL_RADIUS*COS30, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_6] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X - DEMO_WHEEL_RADIUS*SIN0, DEMO_WHEEL_START_Y + DEMO_WHEEL_RADIUS*COS0, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_7] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X - DEMO_WHEEL_RADIUS*SIN30, DEMO_WHEEL_START_Y + DEMO_WHEEL_RADIUS*COS30, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_8] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X - DEMO_WHEEL_RADIUS*SIN60, DEMO_WHEEL_START_Y + DEMO_WHEEL_RADIUS*COS60, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_9] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X - DEMO_WHEEL_RADIUS*SIN90, DEMO_WHEEL_START_Y + DEMO_WHEEL_RADIUS*COS90, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_10] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X - DEMO_WHEEL_RADIUS*SIN60, DEMO_WHEEL_START_Y - DEMO_WHEEL_RADIUS*COS60, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_11] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X - DEMO_WHEEL_RADIUS*SIN30, DEMO_WHEEL_START_Y - DEMO_WHEEL_RADIUS*COS30, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); } else if( !idle && ( demo_display==DEMO_DISPLAY_BOXES ) ) { // Display a box randomly on the screen. idle = true; x_start = DEMO_START_X + rand()%DEMO_SIZE_X; y_start = DEMO_START_Y + rand()%DEMO_SIZE_Y; x_size = rand()%(DEMO_START_X+DEMO_SIZE_X-x_start); y_size = rand()%(DEMO_START_Y+DEMO_SIZE_Y-y_start); color = rand()%0x10000; switch( demo_color ) { case DEMO_COLOR_BLUE: color = color & BLUE; break; case DEMO_COLOR_RED: color = color & RED; break; case DEMO_COLOR_GREEN: color = color & GREEN; break; default: break; } et024006_DrawFilledRect( x_start , y_start , x_size , y_size , color ); } } // while(1)... }
/*! * \brief main function : do init and loop (poll if configured so) */ int main(void) { char temp[20]; char *ptemp; uint32_t ast_alarm; static const gpio_map_t USART_GPIO_MAP = { {EXAMPLE_USART_RX_PIN, EXAMPLE_USART_RX_FUNCTION}, {EXAMPLE_USART_TX_PIN, EXAMPLE_USART_TX_FUNCTION} }; /* USART options */ static const usart_options_t USART_OPTIONS = { .baudrate = 57600, .charlength = 8, .paritytype = USART_NO_PARITY, .stopbits = USART_1_STOPBIT, .channelmode = 0 }; #if BOARD == UC3L_EK scif_osc32_opt_t opt = { /* 2-pin Crystal connected to XIN32/XOUT32 and high current * mode. */ SCIF_OSC_MODE_2PIN_CRYSTAL_HICUR, /* oscillator startup time */ AVR32_SCIF_OSCCTRL32_STARTUP_0_RCOSC, /* select alternate xin32_2 and xout32_2 for 32kHz crystal * oscillator */ true, /* disable the 1kHz output */ false, /* enable the 32kHz output */ true }; #else scif_osc32_opt_t opt; opt.mode = SCIF_OSC_MODE_2PIN_CRYSTAL; opt.startup = AVR32_SCIF_OSCCTRL32_STARTUP_0_RCOSC; #endif #if BOARD == UC3L_EK /* * Note: on the AT32UC3L-EK board, there is no crystal/external clock * connected to the OSC0 pinout XIN0/XOUT0. We shall then program the * DFLL and switch the main clock source to the DFLL. */ pcl_configure_clocks(&pcl_dfll_freq_param); /* * Note: since it is dynamically computing the appropriate field values * of the configuration registers from the parameters structure, this * function is not optimal in terms of code size. For a code size * optimal solution, it is better to create a new function from * pcl_configure_clocks_dfll0() and modify it to use preprocessor * computation from pre-defined target frequencies. */ #else pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP); #endif /* Start OSC_32KHZ */ scif_start_osc32(&opt, true); /* Assign GPIO pins to USART0. */ gpio_enable_module(USART_GPIO_MAP, sizeof(USART_GPIO_MAP) / sizeof(USART_GPIO_MAP[0])); /* Initialize USART in RS232 mode */ usart_init_rs232(EXAMPLE_USART, &USART_OPTIONS, FPBA); /* Welcome sentence // 2-pin Crystal and high current mode. */ /* Crystal is connected to XIN32/XOUT32. */ usart_write_line(EXAMPLE_USART, "\x1B[2J\x1B[H\r\nATMEL\r\n"); usart_write_line(EXAMPLE_USART, "AVR32 UC3 - AST example 2\r\n"); usart_write_line(EXAMPLE_USART, "AST 32 KHz oscillator counter example.\r\n"); usart_write_line(EXAMPLE_USART, "Alarm0 wakeup from static sleep mode every second.\r\n"); /* Using counter mode and set it to 0 */ unsigned long ast_counter = 0; /* Initialize the AST */ if (!ast_init_counter(&AVR32_AST, AST_OSC_32KHZ, AST_PSEL_32KHZ_1HZ, ast_counter)) { usart_write_line(EXAMPLE_USART, "Error initializing the AST\r\n"); while (1) { } } /* Alarm 0 sends a wakeup signal to the Power manager */ ast_enable_alarm_async_wakeup(&AVR32_AST, 0); /* Enable the AST */ ast_enable(&AVR32_AST); while (1) { /* disable alarm 0 */ ast_disable_alarm0(&AVR32_AST); /* ast_init_counter Set Alarm to current time+30 seconds */ ast_alarm = ast_counter + 1; ast_set_alarm0_value(&AVR32_AST, ast_alarm); /* Enable alarm 0 */ ast_enable_alarm0(&AVR32_AST); /* * Precautions when entering a sleep mode * Modules communicating with external circuits should normally * be disabled before entering a sleep mode that will stop the * module operation. * Make sure the USART dumps the last message completely before * turning it off. */ while (!usart_tx_empty(EXAMPLE_USART)) { } pcl_disable_module(EXAMPLE_USART_CLOCK_MASK); /* * Since we're going into a sleep mode deeper than IDLE, all HSB * masters must be stopped before entering the sleep mode. * Note: since we're not using the PDCA, we don't have to stop *it. */ /* * If there is a chance that any PB write operations are *incomplete, * the CPU should perform a read operation from any register on *the * PB bus before executing the sleep instruction. */ AVR32_INTC.ipr[0]; /* Dummy read */ /* Go into static sleep mode */ SLEEP(AVR32_PM_SMODE_STATIC); /* We're out of the static sleep mode now => re-enable the USART * module */ pcl_enable_module(EXAMPLE_USART_CLOCK_MASK); /* After wake up, clear the Alarm0 */ ast_clear_alarm_status_flag(&AVR32_AST, 0); /* Toggle Led0 */ gpio_tgl_gpio_pin(LED0_GPIO); /* Set cursor to the position (1; 6) */ usart_write_line(EXAMPLE_USART, "\x1B[6;1H"); ast_counter = ast_get_counter_value(&AVR32_AST); usart_write_line(EXAMPLE_USART, "Timer: "); ptemp = print_i(temp, ast_counter); usart_write_line(EXAMPLE_USART, ptemp); usart_write_line(EXAMPLE_USART, " sec "); } }
/*! * \brief main function : do init and loop (poll if configured so) */ int main(void) { char temp[20]; char *ptemp; static const gpio_map_t USART_GPIO_MAP = { {EXAMPLE_USART_RX_PIN, EXAMPLE_USART_RX_FUNCTION}, {EXAMPLE_USART_TX_PIN, EXAMPLE_USART_TX_FUNCTION} }; /* USART options */ static const usart_options_t USART_OPTIONS = { .baudrate = 57600, .charlength = 8, .paritytype = USART_NO_PARITY, .stopbits = USART_1_STOPBIT, .channelmode = 0 }; #if BOARD == UC3L_EK scif_osc32_opt_t opt = { /* * 2-pin Crystal connected to XIN32/XOUT32 and high current * mode. */ SCIF_OSC_MODE_2PIN_CRYSTAL_HICUR, /* oscillator startup time */ AVR32_SCIF_OSCCTRL32_STARTUP_0_RCOSC, /* * select alternate xin32_2 and xout32_2 for 32kHz crystal * oscillator */ true, /* disable the 1kHz output */ false, /* enable the 32kHz output */ true }; #else scif_osc32_opt_t opt; opt.mode = SCIF_OSC_MODE_2PIN_CRYSTAL; opt.startup = AVR32_SCIF_OSCCTRL32_STARTUP_0_RCOSC; #endif #if BOARD == UC3L_EK /* * Note: on the AT32UC3L-EK board, there is no crystal/external clock * connected to the OSC0 pinout XIN0/XOUT0. We shall then program the * DFLL and switch the main clock source to the DFLL. */ pcl_configure_clocks(&pcl_dfll_freq_param); /* * Note: since it is dynamically computing the appropriate field values * of the configuration registers from the parameters structure, this * function is not optimal in terms of code size. For a code size * optimal solution, it is better to create a new function from * pcl_configure_clocks_dfll0() and modify it to use preprocessor * computation from pre-defined target frequencies. */ #else pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP); #endif /* Start OSC_32KHZ */ scif_start_osc32(&opt, true); /* Assign GPIO pins to USART0. */ gpio_enable_module(USART_GPIO_MAP, sizeof(USART_GPIO_MAP) / sizeof(USART_GPIO_MAP[0])); /* Initialize USART in RS232 mode */ usart_init_rs232(EXAMPLE_USART, &USART_OPTIONS, FPBA); /* Welcome message */ usart_write_line(EXAMPLE_USART, "\x1B[2J\x1B[H\r\nATMEL\r\n"); usart_write_line(EXAMPLE_USART, "AVR32 UC3 - AST example\r\n"); usart_write_line(EXAMPLE_USART, "AST 32 KHz oscillator program test.\r\n"); ast_calendar_t ast_calendar; ast_calendar.FIELD.sec = 0; ast_calendar.FIELD.min = 15; ast_calendar.FIELD.hour = 12; ast_calendar.FIELD.day = 5; ast_calendar.FIELD.month = 6; ast_calendar.FIELD.year = 9; /* Initialize the AST */ if (!ast_init_calendar(&AVR32_AST, AST_OSC_32KHZ, AST_PSEL_32KHZ_1HZ, ast_calendar)) { usart_write_line(EXAMPLE_USART, "Error initializing the AST\r\n"); while (1) { } } /* Enable the AST */ ast_enable(&AVR32_AST); volatile int i; while (1) { /* slow down operations */ for (i = 0; i < 10000; i++) { } gpio_tgl_gpio_pin(LED0_GPIO); /* Set cursor to the position (1; 5) */ usart_write_line(EXAMPLE_USART, "\x1B[5;1H"); ast_calendar = ast_get_calendar_value(&AVR32_AST); usart_write_line(EXAMPLE_USART, "Timer: "); ptemp = print_i(temp, ast_calendar.FIELD.sec); usart_write_line(EXAMPLE_USART, ptemp); usart_write_line(EXAMPLE_USART, " sec "); } }
/** \brief Main function - init and loop to display ADC values */ int main(void) { /* GPIO pin/ADC-function map. */ const gpio_map_t ADCIFB_GPIO_MAP = { {EXAMPLE_ADCIFB_PIN, EXAMPLE_ADCIFB_FUNCTION} }; /* ADCIFB Configuration */ adcifb_opt_t adcifb_opt = { /* Resolution mode */ .resolution = AVR32_ADCIFB_ACR_RES_12BIT, /* Channels Sample & Hold Time in [0,15] */ .shtim = 15, .ratio_clkadcifb_clkadc = (sysclk_get_pba_hz() / EXAMPLE_TARGET_CLK_ADC_FREQ_HZ), /* * Startup time in [0,127], where * Tstartup = startup * 8 * Tclk_adc (assuming Tstartup ~ 15us max) */ .startup = 3, /* ADCIFB Sleep Mode disabled */ .sleep_mode_enable = false }; uint32_t adc_data; /* Initialize the system clocks */ sysclk_init(); /* Init debug serial line */ init_dbg_rs232(sysclk_get_pba_hz()); /* Assign and enable GPIO pins to the ADC function. */ gpio_enable_module(ADCIFB_GPIO_MAP, sizeof(ADCIFB_GPIO_MAP) / sizeof(ADCIFB_GPIO_MAP[0])); /* Enable and configure the ADCIFB module */ if (adcifb_configure(&AVR32_ADCIFB, &adcifb_opt) != PASS) { /* Config error. */ while (true) { gpio_tgl_gpio_pin(LED0_GPIO); delay_ms(100); } } /* Configure the trigger mode */ /* "No trigger, only software trigger can start conversions". */ if (adcifb_configure_trigger(&AVR32_ADCIFB, AVR32_ADCIFB_TRGMOD_NT, 0) != PASS) { /* Config error. */ while (true) { gpio_tgl_gpio_pin(LED1_GPIO); delay_ms(100); } } /* Enable the ADCIFB channel the battery is connected to. */ adcifb_channels_enable(&AVR32_ADCIFB, EXAMPLE_ADCIFB_CHANNEL_MASK); while (true) { while (adcifb_is_ready(&AVR32_ADCIFB) != true) { /* Wait until the ADC is ready to perform a conversion. */ } /* Start an ADCIFB conversion sequence. */ adcifb_start_conversion_sequence(&AVR32_ADCIFB); while (adcifb_is_drdy(&AVR32_ADCIFB) != true) { /* Wait until the converted data is available. */ } /* Get the last converted data. */ adc_data = adcifb_get_last_data(&AVR32_ADCIFB); /* Display the current voltage of the battery. */ print_dbg("\x1B[2J\x1B[H\r\nADCIFB Example\r\nHEX Value for " EXAMPLE_ADCIFB_CHANNEL_NAME " : 0x"); print_dbg_hex(adc_data & AVR32_ADCIFB_LCDR_LDATA_MASK); print_dbg("\r\n"); delay_ms(500); /* * Note1: there is a resistor bridge between the battery and the * ADC pad on the AT32UC3L-EK. The data converted is thus half * of * the battery voltage. */ /* * Note2: if the battery is not in place, the conversion is out * of * spec because the ADC input is then higher than ADVREF. */ } }
__attribute__ ((__interrupt__)) void tecla_lrc_isr(void){//handler teclas left, right o center if (gpio_get_pin_interrupt_flag(QT1081_TOUCH_SENSOR_ENTER)) { gpio_clear_pin_interrupt_flag(QT1081_TOUCH_SENSOR_ENTER); } if (gpio_get_pin_interrupt_flag (QT1081_TOUCH_SENSOR_LEFT)) { gpio_clear_pin_interrupt_flag(QT1081_TOUCH_SENSOR_LEFT); pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP); while(gpio_get_pin_interrupt_flag(QT1081_TOUCH_SENSOR_ENTER)==0){ gpio_tgl_gpio_pin(LED3_GPIO); delay_s(50); } display(0); } if (gpio_get_pin_interrupt_flag (QT1081_TOUCH_SENSOR_RIGHT)) { gpio_clear_pin_interrupt_flag(QT1081_TOUCH_SENSOR_RIGHT); pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP); while(gpio_get_pin_interrupt_flag(QT1081_TOUCH_SENSOR_ENTER)==0){ displayPrimo(); delay_s(50); } } if (gpio_get_pin_interrupt_flag(QT1081_TOUCH_SENSOR_UP)) { gpio_clear_pin_interrupt_flag(QT1081_TOUCH_SENSOR_UP); pm_switch_to_clock(&AVR32_PM,0); //cambia al RC pm_pll_disable(&AVR32_PM,0); // deshabilita el el PLL 0 pm_pll_setup(&AVR32_PM,0,2,1,0,16); // lockcount in main clock for the PLL wait lock //_______________________________________________________________________________ // Establece la frecuencia de salida del PLL pm_pll_set_option(&AVR32_PM,0,1,0,0);//1 Star-up faster, Start-up normal //_______________________________________________________________________________ //Habilita el PLL 0 pm_pll_enable(&AVR32_PM,0); //_______________________________________________________________________________ //Espera a que se establesca el PLL pm_wait_for_pll0_locked(&AVR32_PM) ; //_______________________________________________________________________________ // Set one wait-state (WS) for flash controller flashc_set_wait_state(1); //habilita la salida del PLL0 con 2 y el OSC0 con 1 pm_switch_to_clock(&AVR32_PM, 2); while (gpio_get_pin_interrupt_flag(QT1081_TOUCH_SENSOR_ENTER)==0) { displayA1(); /*for (uint32_t j =0; j<18000; j++)//300ms 12Mhz { delay_ms(10); }*/ delay_s(50); } display(0); } if (gpio_get_pin_interrupt_flag(QT1081_TOUCH_SENSOR_DOWN)) { gpio_clear_pin_interrupt_flag(QT1081_TOUCH_SENSOR_DOWN); pm_switch_to_clock(&AVR32_PM,0); //cambia al RC pm_pll_disable(&AVR32_PM,0); // deshabilita el el PLL 0 pm_pll_setup(&AVR32_PM,0,6,1,0,16); // lockcount in main clock for the PLL wait lock //_______________________________________________________________________________ // Establece la frecuencia de salida del PLL pm_pll_set_option(&AVR32_PM,0,1,0,0);//1 Star-up faster, Start-up normal //_______________________________________________________________________________ //Habilita el PLL 0 pm_pll_enable(&AVR32_PM,0); //_______________________________________________________________________________ //Espera a que se establesca el PLL pm_wait_for_pll0_locked(&AVR32_PM) ; //_______________________________________________________________________________ // Set one wait-state (WS) for flash controller flashc_set_wait_state(1); //habilita la salida del PLL0 con 2 y el OSC0 con 1 pm_switch_to_clock(&AVR32_PM, 2); while (gpio_get_pin_interrupt_flag(QT1081_TOUCH_SENSOR_ENTER)==0) { displayOnOff(); } display(0); } }
/** ** PDCA Init. **/ void init_pdca(void) { // PDCA channel 0/1 options static const pdca_channel_options_t PDCA_CH_OPTIONS = { .addr = (void *)aDataTransfered, // memory address .pid = AVR32_PDCA_PID_USART2_TX, // select peripheral - data are transmit on USART TX line. .size = 0, // transfer counter .r_addr = (void *)aDataTransfered, // next memory address .r_size = sizeof(aDataTransfered), // next transfer counter .transfer_size = PDCA_TRANSFER_SIZE_BYTE, // select size of one data packet .etrig = true // Trigger transfer on event. }; Disable_global_interrupt(); // Initialize interrupt vectors. INTC_init_interrupts(); // Register the PDCA interrupt handler to the interrupt controller. INTC_register_interrupt(&pdca_int_handler, PDCA_CHANNEL_IRQ, AVR32_INTC_INT0); Enable_global_interrupt(); // Init PDCA channel with the pdca_options. pdca_init_channel(PDCA_CHANNEL_USART, &PDCA_CH_OPTIONS); pdca_channel = pdca_get_handler(PDCA_CHANNEL_USART); // For use in the pdca interrupt handler. // Enable pdca transfer error interrupt & transfer complete interrupt. pdca_enable_interrupt_transfer_error(PDCA_CHANNEL_USART); pdca_enable_interrupt_transfer_complete(PDCA_CHANNEL_USART); // Enable the PEVC channel "PDCA CHANNEL 0/1 ONE-ITEM-TRANSFER" PEVC_CHANNELS_ENABLE(ppevc, 1<<PEVC_PDCA_SOT_USER); // Enable the PDCA. pdca_enable(PDCA_CHANNEL_USART); } /** ** AST Init. **/ void init_ast(void) { avr32_ast_pir0_t pir = { .insel = 14 // Set a event every second }; ast_calendar_t ast_calendar; ast_calendar.FIELD.sec = 30; ast_calendar.FIELD.min = 45; ast_calendar.FIELD.hour = 12; ast_calendar.FIELD.day = 7; ast_calendar.FIELD.month= 10; ast_calendar.FIELD.year = 9; scif_osc32_opt_t opt; opt.mode = SCIF_OSC_MODE_2PIN_CRYSTAL; opt.startup = AVR32_SCIF_OSCCTRL32_STARTUP_0_RCOSC; // Start OSC_32KHZ scif_start_osc32(&opt,true); // Initialize the AST if (!ast_init_calendar(&AVR32_AST, AST_OSC_32KHZ, AST_PSEL_32KHZ_1HZ, ast_calendar)) { print_dbg("Error initializing the AST\r\n"); while(1); } ast_set_periodic0_value(&AVR32_AST,pir); ast_enable_periodic0(&AVR32_AST); // Clear All Interrupt AVR32_AST.scr=0xFFFFFFFF; // Enable the AST ast_enable(&AVR32_AST); } /*! \brief Initializes the MCU system clocks. */ static void init_sys_clocks(void) { /*! \name System Clock Frequencies */ //! @{ static pcl_freq_param_t pcl_freq_param = { .cpu_f = FCPU_HZ, .pba_f = FPBA_HZ, .osc0_f = FOSC0, .osc0_startup = OSC0_STARTUP }; //! @} // Configure system clocks. if (pcl_configure_clocks(&pcl_freq_param) != PASS) { while(1); } } /*! \brief This example show a DMA transfer to USART controlled by the AST periodic alarm using the PEVC. */ int main(void) { int i; // Init the string with a simple recognizable pattern. for(i=0;i<sizeof(aDataTransfered);i++) aDataTransfered[i] = '0' + (i%36); init_sys_clocks(); init_usart(); gpio_clr_gpio_pin(LED0_GPIO); init_pevc(); init_ast(); init_pdca(); while(1) { gpio_tgl_gpio_pin(LED1_GPIO); delay_ms(500); //Wait 500ms } }
/** * \brief Main Application Routine * - Initialize the system clocks * - Initialize the sleep manager * - Initialize the power save measures * - Initialize the ACIFB module * - Initialize the AST to trigger ACIFB at regular intervals * - Go to STATIC sleep mode and wake up on a touch status change */ int main(void) { /* Switch on the STATUS LED */ gpio_clr_gpio_pin(STATUS_LED); /* Switch off the error LED. */ gpio_set_gpio_pin(ERROR_LED); /* * Initialize the system clock. * Note: Clock settings are specified in conf_clock.h */ sysclk_init(); /* * Initialize the sleep manager. * Note: CONFIG_SLEEPMGR_ENABLE should have been defined in conf_sleepmgr.h */ sleepmgr_init(); /* Lock required sleep mode. */ sleepmgr_lock_mode(SLEEPMGR_STATIC); /* Initialize the delay routines */ delay_init(sysclk_get_cpu_hz()); /* Initialize the power saving features */ power_save_measures_init(); /* Switch off the error LED. */ gpio_set_gpio_pin(ERROR_LED); #if DEBUG_MESSAGES /* Enable the clock to USART interface */ sysclk_enable_peripheral_clock(DBG_USART); /* Initialize the USART interface to print trace messages */ init_dbg_rs232(sysclk_get_pba_hz()); print_dbg("\r Sleepwalking with ACIFB Module in UC3L \n"); print_dbg("\r Initializing ACIFB Module..... \n"); #endif /* Initialize the Analog Comparator peripheral */ if (ac_init() != STATUS_OK) { #if DEBUG_MESSAGES /* Error initializing the ACIFB peripheral */ print_dbg("\r Error initializing Analog Comparator module \n"); #endif while (1) { delay_ms(LED_DELAY); gpio_tgl_gpio_pin(ERROR_LED); } } #if DEBUG_MESSAGES print_dbg("\r ACIFB Module initialized. \n"); print_dbg("\r Initializing AST Module..... \n"); #endif /* Initialize the AST peripheral */ if (ast_init() != STATUS_OK) { #if DEBUG_MESSAGES print_dbg("\r Error initializing AST module \n"); #endif /* Error initializing the AST peripheral */ while (1) { delay_ms(LED_DELAY); gpio_tgl_gpio_pin(ERROR_LED); } } #if DEBUG_MESSAGES print_dbg("\r AST Module initialized. \n"); #endif /* Application routine */ while (1) { /* Enable Asynchronous wake-up for ACIFB */ pm_asyn_wake_up_enable(AVR32_PM_AWEN_ACIFBWEN_MASK); #if DEBUG_MESSAGES print_dbg("\r Going to STATIC sleep mode \n"); print_dbg( "\r Wake up only when input is higher than threshold \n"); #endif /* Switch off the status LED */ gpio_set_gpio_pin(STATUS_LED); /* Disable GPIO clock before entering sleep mode */ sysclk_disable_pba_module(SYSCLK_GPIO); AVR32_INTC.ipr[0]; /* Enter STATIC sleep mode */ sleepmgr_enter_sleep(); /* Enable GPIO clock after waking from sleep mode */ sysclk_enable_pba_module(SYSCLK_GPIO); /* Switch on the status LED */ gpio_clr_gpio_pin(STATUS_LED); #if DEBUG_MESSAGES print_dbg("\r Output higher than threshold \n"); print_dbg("\n"); #else /* LED on for few ms */ delay_ms(LED_DELAY); #endif /* Clear the wake up & enable it to enter sleep mode again */ pm_asyn_wake_up_disable(AVR32_PM_AWEN_ACIFBWEN_MASK); /* Clear All AST Interrupt request and clear SR */ ast_clear_all_status_flags(&AVR32_AST); } return 0; } /* End of main() */