/** * \internal * \brief Test the callback API * * This test tests the callback API for the TC. The TC uses one-shot mode. * * \param test Current test case. */ static void run_callback_test(const struct test_case *test) { test_assert_true(test, tc_init_success == true, "TC initialization failed, skipping test"); test_assert_true(test, basic_functionality_test_passed == true, "Basic functionality test failed, skipping test"); /* Setup TC0 */ tc_reset(&tc_test0_module); tc_get_config_defaults(&tc_test0_config); tc_test0_config.wave_generation = TC_WAVE_GENERATION_MATCH_PWM; tc_test0_config.counter_16_bit.compare_capture_channel\ [TC_COMPARE_CAPTURE_CHANNEL_0] = 0x03FF; tc_test0_config.counter_16_bit.compare_capture_channel\ [TC_COMPARE_CAPTURE_CHANNEL_1] = 0x03FA; tc_init(&tc_test0_module, CONF_TEST_TC0, &tc_test0_config); /* Setup callbacks */ tc_register_callback(&tc_test0_module, tc_callback_function, TC_CALLBACK_CC_CHANNEL1); tc_enable_callback(&tc_test0_module, TC_CALLBACK_CC_CHANNEL1); tc_enable(&tc_test0_module); while ((tc_get_status(&tc_test0_module) & TC_STATUS_COUNT_OVERFLOW) == 0) { /* Wait for overflow of TC1*/ } tc_disable(&tc_test0_module); tc_clear_status(&tc_test0_module, TC_STATUS_COUNT_OVERFLOW); test_assert_true(test, callback_function_entered == 1, "The callback has failed callback_function_entered = %d", (int)callback_function_entered); /* Test disable callback function */ tc_disable_callback(&tc_test0_module, TC_CALLBACK_CC_CHANNEL1); tc_set_count_value(&tc_test0_module, 0x00000000); tc_enable(&tc_test0_module); while ((tc_get_status(&tc_test0_module) & TC_STATUS_COUNT_OVERFLOW) == 0) { /* Wait for overflow of TC1*/ } /* Test tc_disable() */ tc_disable(&tc_test0_module); test_assert_true(test, callback_function_entered == 1, "Disabling the callback has failed"); }
/** * \brief Initialize Timer/Counters used to simulate oven actuation signal * * TCC0 is set up to generate a dual variable frequency signal with dead-time * insertion using the AWEX module. This is similar to how heating elements are * actuated in real induction ovens. Its output is on pin C2 which is marked as * RXD on header J1. * * TCC1 is set up to capture a frequency signal via a pin change event using the * XMEGA Event System. Its input is pin C4 which is marked as SS on header J1. */ void main_init_tc(void) { /* Set up timer for PWM output, used to actuate cooking element */ tc_enable(&OVEN_FREQ_TC); /* Configures the waveform generator in Frequency generation mode */ tc_set_wgm(&OVEN_FREQ_TC, TC_WG_FRQ); /* Configures the CCA level. This controls frequency generation */ tc_write_cc(&OVEN_FREQ_TC, TC_CCA, FREQ_TIMER_PERIOD_INIT / 2); /* Enables and configures the deadtime of AWEX channel B outputs */ tc_awex_enable_ccb_deadtime(&AWEXC); tc_awex_set_dti_high(&AWEXC, FREQ_TIMER_PERIOD_INIT / 4); tc_awex_set_dti_low(&AWEXC, FREQ_TIMER_PERIOD_INIT / 4); /* Output of AWEX channel B is on pins C2 and C3 */ tc_awex_set_output_override(&AWEXC, 0x0C); /* Make sure that the output is initially turned off */ tc_write_clock_source(&OVEN_FREQ_TC, TC_CLKSEL_OFF_gc); /* Set up timer for input capture for the simulation to read "real" * power */ tc_enable(&OVEN_FREQ_CAPT_TC); /* Select Event Channel 1 as input to the timer, and perform frequency * capture. */ tc_set_input_capture(&OVEN_FREQ_CAPT_TC, TC_EVSEL_CH1_gc, TC_EVACT_FRQ_gc); /* Enable Capture Channel A */ tc_enable_cc_channels(&OVEN_FREQ_CAPT_TC, TC_CCAEN); /* Make sure pin C4 is configured for input and sensing on rise and fall * and pin C2 is configured for output. */ ioport_configure_pin(J1_PIN4, IOPORT_DIR_INPUT | IOPORT_BOTHEDGES); ioport_configure_pin(J1_PIN2, IOPORT_DIR_OUTPUT); /* Turn on power to the event system */ PR.PRGEN &= ~PR_EVSYS_bm; /* Use pin C4 as input to Event Channel 1 */ EVSYS.CH1MUX = EVSYS_CHMUX_PORTC_PIN4_gc; /* Turn on timer used for input capture */ tc_write_clock_source(&OVEN_FREQ_CAPT_TC, TC_CLKSEL_DIV256_gc); }
/** * \brief Run tests and turn on power to simulated oven plate * * This functions runs some class B tests, reinitializes Timer/Counters, ADC and * DAC used by the oven, then turns on and starts monitoring of periodic tests. */ static void ovenctl_turn_on_plate(void) { /* Store current DAC value since the analog IO test is destructive */ uint16_t dac_val = DACB.CH0DATA; /* Run tests -- classb_error is updated if any of them fail */ oven_classb_run_tests(); /* Initialize the ADC and DAC modules, as well as the Timer/Counters * we use to emulate real world application. */ main_init_adc_dac(); DACB.CH0DATA = dac_val; main_init_tc(); /* Enable and set up timer for periodic temperature checking */ tc_enable(&OVEN_PERIODIC_TEMPTEST_TC); /* Set timer to overflow every 600ms: 24MHz / (1024 * 14063) = 0.6s */ tc_write_clock_source(&OVEN_PERIODIC_TEMPTEST_TC, TC_CLKSEL_DIV1024_gc); tc_write_period(&OVEN_PERIODIC_TEMPTEST_TC, 14062); /* Set up temperature check as interrupt callback function, then enable * interrupts */ tc_set_overflow_interrupt_callback(&OVEN_PERIODIC_TEMPTEST_TC, ovenctl_periodic_temperature_sanity_check); tc_set_overflow_interrupt_level(&OVEN_PERIODIC_TEMPTEST_TC, TC_OVFINTLVL_LO_gc); /* Enable monitoring of the periodic temperature check */ classb_intmon_set_state(TEMP_SANITY_TEST, M_ENABLE); /* Enable and set up timer for periodic execution of Class B tests */ tc_enable(&OVEN_PERIODIC_CLASSB_TC); /* Set timer to overflow every second: 24MHz / (1024 * 23438) = 1s */ tc_write_clock_source(&OVEN_PERIODIC_CLASSB_TC, TC_CLKSEL_DIV1024_gc); tc_write_period(&OVEN_PERIODIC_CLASSB_TC, 23437); /* Set up periodic class B test as interrupt callback function, then * enable interrupts */ tc_set_overflow_interrupt_callback(&OVEN_PERIODIC_CLASSB_TC, ovenctl_periodic_classb_tests); tc_set_overflow_interrupt_level(&OVEN_PERIODIC_CLASSB_TC, TC_OVFINTLVL_LO_gc); /* Enable monitoring of the periodic temperature check */ classb_intmon_set_state(PER_CLASSB_TESTS, M_ENABLE); }
void Adafruit_ZeroTimer::enable(boolean en) { if (en) { tc_enable(&tc_instance); } else { tc_disable(&tc_instance); } }
static void configure_tc(struct tc_module *tc_instance) { //! [setup_6] struct tc_config config_tc; struct tc_events config_events; //! [setup_6] //! [setup_7] tc_get_config_defaults(&config_tc); //! [setup_7] //! [setup_8] config_tc.counter_size = TC_COUNTER_SIZE_8BIT; config_tc.wave_generation = TC_WAVE_GENERATION_NORMAL_FREQ; config_tc.clock_source = GCLK_GENERATOR_1; config_tc.clock_prescaler = TC_CLOCK_PRESCALER_DIV64; //! [setup_8] //! [setup_9] tc_init(tc_instance, CONF_TC_MODULE, &config_tc); //! [setup_9] //! [setup_10] config_events.generate_event_on_overflow = true; tc_enable_events(tc_instance, &config_events); //! [setup_10] //! [setup_11] tc_enable(tc_instance); //! [setup_11] }
void printregs() { printf("[d0]%08x [d1]%08x [d2]%08x [d3]%08x\n", shoe.d[0], shoe.d[1], shoe.d[2], shoe.d[3]); printf("[d4]%08x [d5]%08x [d6]%08x [d7]%08x\n", shoe.d[4], shoe.d[5], shoe.d[6], shoe.d[7]); printf("[a0]%08x [a1]%08x [a2]%08x [a3]%08x\n", shoe.a[0], shoe.a[1], shoe.a[2], shoe.a[3]); printf("[a4]%08x [a5]%08x [a6]%08x [a7]%08x\n", shoe.a[4], shoe.a[5], shoe.a[6], shoe.a[7]); printf("[pc]%08x [sr]%c%c%c%c%c%c%c [tc]%08x\n", shoe.pc, sr_s()?'S':'s', sr_m()?'M':'m', sr_x()?'X':'x', sr_n()?'N':'n', sr_z()?'Z':'z', sr_v()?'V':'v', sr_c()?'C':'c', shoe.tc ); printf("[vbr]%08x\n", shoe.vbr); printf("srp: "); print_mmu_rp(shoe.srp); printf("crp: "); print_mmu_rp(shoe.crp); printf("tc: e=%u sre=%u fcl=%u ps=%u is=%u (tia=%u tib=%u tic=%u tid=%u)\n", tc_enable(), tc_sre(), tc_fcl(), tc_ps(), tc_is(), tc_tia(), tc_tib(), tc_tic(), tc_tid()); printf("\n"); }
void *platform_configure_timer(platform_hw_timer_callback_t bus_tc_cb_ptr) { struct tc_config timer_config; system_interrupt_enter_critical_section(); if (hw_timers[0].timer_usage == 0) { hw_timers[0].timer_usage = 1; platform_cc1_cb = bus_tc_cb_ptr; tc_get_config_defaults(&timer_config); timer_config.clock_prescaler = TC_CLOCK_PRESCALER_DIV1; timer_config.oneshot = true; timer_config.counter_size = TC_COUNTER_SIZE_32BIT; timer_config.count_direction = TC_COUNT_DIRECTION_UP; tc_init(&bus_tc_instance, CONF_BUS_TC_MODULE, &timer_config); timer_count_per_ms = ((system_gclk_gen_get_hz(timer_config.clock_source)) /1000); tc_set_count_value(&bus_tc_instance, 0); tc_enable(&bus_tc_instance); tc_stop_counter(&bus_tc_instance); tc_register_callback(&bus_tc_instance, tc_cc1_cb, TC_CALLBACK_OVERFLOW); tc_enable_callback(&bus_tc_instance, TC_CALLBACK_OVERFLOW); hw_timers[0].timer_frequency = (system_gclk_gen_get_hz(timer_config.clock_source)); hw_timers[0].timer_instance = bus_tc_instance; system_interrupt_leave_critical_section(); return (&hw_timers[0]); } system_interrupt_leave_critical_section(); return NULL; }
/*! \brief to initialiaze hw timer */ uint8_t tmr_init(void) { uint8_t timer_multiplier; tc_enable(TIMER); tc_set_overflow_interrupt_callback(TIMER, (tc_callback_t)tc_ovf_callback); /*initialize timer in waveform generator - Normal mode */ tc_set_wgm(TIMER, TC_WG_NORMAL); tc_write_period(TIMER, TIMER_PERIOD); /* select clock division as 1 */ tc_write_clock_source(TIMER, TC_CLKSEL_DIV1_gc); tc_set_overflow_interrupt_level(TIMER, TC_INT_LVL_HI); tc_set_cca_interrupt_callback(TIMER, (tc_callback_t)tc_cca_callback); tc_enable_cc_channels(TIMER, TC_CCAEN); tc_set_cca_interrupt_level(TIMER, TC_INT_LVL_OFF); /* calculate how faster the timer with current clk freq compared to * timer with 1Mhz */ timer_multiplier = sysclk_get_peripheral_bus_hz(TIMER) / DEF_1MHZ; return timer_multiplier; }
//! [setup] void configure_tc(void) { //! [setup_config] struct tc_config config_tc; //! [setup_config] //! [setup_config_defaults] tc_get_config_defaults(&config_tc); //! [setup_config_defaults] //! [setup_change_config] config_tc.counter_size = TC_COUNTER_SIZE_16BIT; config_tc.wave_generation = TC_WAVE_GENERATION_NORMAL_PWM; config_tc.counter_16_bit.compare_capture_channel[0] = (0xFFFF / 4); //! [setup_change_config] //! [setup_change_config_pwm] config_tc.pwm_channel[0].enabled = true; config_tc.pwm_channel[0].pin_out = PWM_OUT_PIN; config_tc.pwm_channel[0].pin_mux = PWM_OUT_MUX; //! [setup_change_config_pwm] //! [setup_set_config] tc_init(&tc_instance, PWM_MODULE, &config_tc); //! [setup_set_config] //! [setup_enable] tc_enable(&tc_instance); //! [setup_enable] }
/** * \internal * \brief Initializes the timer counter used to generate a delay */ static void main_delay_init(void) { tc_enable(CONF_DELAY_TC); tc_set_wgm(CONF_DELAY_TC, TC_WG_NORMAL); tc_set_resolution(CONF_DELAY_TC, 1); tc_write_period(CONF_DELAY_TC, 0xFFFF); }
void generator_qenc_enable(PORT_t *port, uint8_t pins_base, volatile void *timer, uint8_t revolution, uint32_t freq, bool dir ) { #if XMEGA_E Assert((TC4_t *)timer == &TCC4); #endif /* Store parameter in static global variable */ generator_qenc_port = port; generator_qenc_pins_base = pins_base; generator_qenc_revolution = revolution; generator_qenc_timer = timer; /* Clear all pins on test port */ port->DIRSET = QENC_PH0_PH90_INDEX_PINS << generator_qenc_pins_base; port->OUTCLR = QENC_PH0_PH90_INDEX_PINS << generator_qenc_pins_base; tc_enable(timer); tc_set_wgm(timer, TC_WG_NORMAL); generator_qenc_set_freq(freq); generator_qenc_set_direction(dir); /* Enable low level interrupt on CCA */ tc_set_overflow_interrupt_level(timer, TC_INT_LVL_LO); /* Set interrupt callback function for CCA */ tc_set_overflow_interrupt_callback(timer, generator_qenc_timer_ccaint_handler); }
/** * \b avrInitSystemTickTimer * * Initialise the system tick timer. Uses the AVR's timer1 facility. * * @return None */ void avrInitSystemTickTimer ( void ) { /* * Unmask clock for TCC1 */ tc_enable(&TCC1); /* * Configure interrupts callback functions for CCA interrupt */ tc_set_cca_interrupt_callback(&TCC1, cca_interrupt_callback); /* * Configure TC in normal mode, configure period, CCA * Enable CCA channel */ tc_set_wgm(&TCC1, TC_WG_NORMAL); tc_write_period(&TCC1, AVR_CPU_HZ / 256 / SYSTEM_TICKS_PER_SEC); tc_write_cc(&TCC1, TC_CCA, AVR_CPU_HZ / 256 / SYSTEM_TICKS_PER_SEC / 2); tc_enable_cc_channels(&TCC1,(enum tc_cc_channel_mask_enable_t)(TC_CCAEN)); /* * Enable TC interrupts (overflow, CCA and CCB) */ tc_set_cca_interrupt_level(&TCC1, TC_CCAINTLVL_LO_gc); /* * Run TCC1 at AVR_CPU_HZ / 256 */ tc_write_clock_source(&TCC1, TC_CLKSEL_DIV256_gc); }
/*---------------------------------------------------------------------------*/ void clock_init(void) { #define TIMER_PERIOD UINT16_MAX #define TIMER TC3 struct tc_config cfg; tc_get_config_defaults(&cfg); cfg.clock_source = GCLK_GENERATOR_5; cfg.clock_prescaler = TC_CLOCK_PRESCALER_DIV1; cfg.run_in_standby = false; cfg.counter_16_bit.compare_capture_channel[0] = TIMER_PERIOD; tc_init(&tc_instance, TIMER, &cfg); /* tc_register_callback(&tc_instance, clock_irq_callback, TC_CALLBACK_OVERFLOW);*/ tc_register_callback(&tc_instance, clock_irq_callback, TC_CALLBACK_CC_CHANNEL0); /* tc_register_callback(&tc_instance, clock_irq_callback, TC_CALLBACK_CC_CHANNEL1); tc_register_callback(&tc_instance, clock_irq_callback, TC_CALLBACK_ERROR);*/ /* tc_enable_callback(&tc_instance, TC_CALLBACK_OVERFLOW);*/ tc_enable_callback(&tc_instance, TC_CALLBACK_CC_CHANNEL0); /* tc_enable_callback(&tc_instance, TC_CALLBACK_CC_CHANNEL1); tc_enable_callback(&tc_instance, TC_CALLBACK_ERROR);*/ tc_enable(&tc_instance); }
/** * \brief Initialize Class B tests * * Set up timers, timer callbacks, initialize interrupt monitor, * add interrupts to be monitored. */ void oven_classb_init_tests(void) { /* == Frequency consistency test == */ /* Use Timer D1 for frequency consistency test */ tc_enable(&CLASSB_FREQTEST_TC); tc_set_overflow_interrupt_callback(&CLASSB_FREQTEST_TC, classb_freq_tc_callback); classb_freq_setup_timer(); /* == Frequency consistency test and interrupt monitor timer == */ /* Use relative RTC interrupt from ASF to execute periodic check for * frequency consistency and interrupt monitor. */ rtc_set_alarm_relative(CLASSB_RTC_INT_PERIOD); rtc_set_callback(classb_rtc_callback); /* TODO: Workaround: RTC32_COMPINTLVL is reset to OFF for some reason */ /* after set_alarm_relative is called the the first time. Set to LO. */ RTC32.INTCTRL = RTC32_COMPINTLVL_LO_gc; /* == Interrupt monitor test == */ /* Register the periodic temperature measurement sanity test in the * monitor. * It's run every 600ms => 3.41 times per RTC compare period of 2 secs. * 25% tolerance. */ classb_intmon_reg_int(TEMP_SANITY_TEST, 4, 25); /* Register the periodic classb test execution in the monitor. * It's run every 1000ms => 2 times per RTC compare period of 2 secs. * 50% tolerance. */ classb_intmon_reg_int(PER_CLASSB_TESTS, 2, 50); }
/*! \brief to initialize hw timer */ uint8_t tmr_init(void) { uint8_t timer_multiplier; tc_get_config_defaults(&timer_config); #ifdef ENABLE_SLEEP if(sys_sleep == true) { timer_config.clock_source = GCLK_GENERATOR_1; timer_config.clock_prescaler = TC_CLOCK_PRESCALER_DIV2; timer_config.run_in_standby=true; } #endif timer_config.counter_16_bit.compare_capture_channel[0] = TIMER_PERIOD; tc_init(&module_inst, TIMER, &timer_config); tc_register_callback(&module_inst, tc_ovf_callback, TC_CALLBACK_OVERFLOW); tc_register_callback(&module_inst, tc_cca_callback, TC_CALLBACK_CC_CHANNEL0); tc_enable_callback(&module_inst, TC_CALLBACK_OVERFLOW); tc_enable_callback(&module_inst, TC_CALLBACK_CC_CHANNEL0); tc_enable(&module_inst); /* calculate how faster the timer with current clk freq compared to timer with 1Mhz */ #ifdef ENABLE_SLEEP if(sys_sleep ==true) { timer_multiplier = system_gclk_gen_get_hz(1) / 2000000; } else { timer_multiplier = system_gclk_gen_get_hz(0) / DEF_1MHZ; } #else timer_multiplier = system_gclk_gen_get_hz(0) / DEF_1MHZ; #endif return timer_multiplier; }
int main(void) { pmic_init(); board_init(); sysclk_init(); sleepmgr_init(); cpu_irq_enable(); #if (BOARD == XMEGA_A3BU_XPLAINED) /* The status LED must be used as LED2, so we turn off * the green led which is in the same packaging. */ ioport_set_pin_high(LED3_GPIO); #endif /* * Unmask clock for TIMER_EXAMPLE */ tc_enable(&TIMER_EXAMPLE); /* * Configure interrupts callback functions for TIMER_EXAMPLE * overflow interrupt, CCA interrupt and CCB interrupt */ tc_set_overflow_interrupt_callback(&TIMER_EXAMPLE, example_ovf_interrupt_callback); tc_set_cca_interrupt_callback(&TIMER_EXAMPLE, example_cca_interrupt_callback); tc_set_ccb_interrupt_callback(&TIMER_EXAMPLE, example_ccb_interrupt_callback); /* * Configure TC in normal mode, configure period, CCA and CCB * Enable both CCA and CCB channels */ tc_set_wgm(&TIMER_EXAMPLE, TC_WG_NORMAL); tc_write_period(&TIMER_EXAMPLE, TIMER_EXAMPLE_PERIOD); tc_write_cc(&TIMER_EXAMPLE, TC_CCA, TIMER_EXAMPLE_PERIOD / 2); tc_write_cc(&TIMER_EXAMPLE, TC_CCB, TIMER_EXAMPLE_PERIOD / 4); tc_enable_cc_channels(&TIMER_EXAMPLE,(enum tc_cc_channel_mask_enable_t)(TC_CCAEN | TC_CCBEN)); /* * Enable TC interrupts (overflow, CCA and CCB) */ tc_set_overflow_interrupt_level(&TIMER_EXAMPLE, TC_INT_LVL_LO); tc_set_cca_interrupt_level(&TIMER_EXAMPLE, TC_INT_LVL_LO); tc_set_ccb_interrupt_level(&TIMER_EXAMPLE, TC_INT_LVL_LO); /* * Run TIMER_EXAMPLE at TIMER_EXAMPLE_PERIOD(31250Hz) resolution */ tc_set_resolution(&TIMER_EXAMPLE, TIMER_EXAMPLE_PERIOD); do { /* Go to sleep, everything is handled by interrupts. */ sleepmgr_enter_sleep(); } while (1); }
void init_timer_isr( void ) { tc_enable (&TCC0); tc_write_period (&TCC0, (TICKS_PER_MS * qt_measurement_period_msec)); tc_write_clock_source (&TCC0, TC_CLKSEL_DIV8_gc); tc_set_cca_interrupt_level (&TCC0, PMIC_LVL_LOW); tc_set_cca_interrupt_callback(&TCC0, example_cca_interrupt_callback); }
static void init_timer_isr(void) { tc_enable(&TCC0); tc_write_period(&TCC0, TIMER_PERIOD); tc_write_clock_source(&TCC0, TC_CLKSEL_DIV8_gc); tc_set_cca_interrupt_level(&TCC0, PMIC_LVL_LOW); tc_set_cca_interrupt_callback(&TCC0, example_cca_interrupt_callback); }
/** * Initialize trace */ void hf_trace_init() { tc_enable(&TCC0); tc_set_overflow_interrupt_callback(&TCC0, tc_wrap); tc_set_wgm(&TCC0, TC_WG_NORMAL); tc_write_period(&TCC0, 65535); tc_set_overflow_interrupt_level(&TCC0, TC_INT_LVL_LO); tc_write_clock_source(&TCC0, TC_CLKSEL_DIV8_gc); }
/** * \brief Configure timer interrupts for touch measurements */ static void touch_init_timer_isr(void) { tc_enable(&TOUCH_TC); tc_set_overflow_interrupt_callback(&TOUCH_TC, &touch_timer_period_handler); tc_set_resolution(&TOUCH_TC, (uint32_t)1000000); tc_write_period(&TOUCH_TC, (tc_get_resolution(&TOUCH_TC) * TOUCH_TC_PERIOD_MS) / 1000); tc_set_overflow_interrupt_level(&TOUCH_TC, TC_INT_LVL_LO); }
void XBee_Init() { tc_enable(&(XBEE_TIMER)); // timer used for callback functionality tc_set_overflow_interrupt_callback(&(XBEE_TIMER), SendTelemetry); // sets up a function to callback when it's time to run tc_set_wgm(&(XBEE_TIMER), TC_WG_NORMAL); // sets the waveform generation tc_write_period(&(XBEE_TIMER), XBEE_HZ); // this should run at 2 HZ tc_set_overflow_interrupt_level(&(XBEE_TIMER), TC_INT_LVL_MED); // low priority 0x01 tc_write_clock_source(&(XBEE_TIMER), TC_CLKSEL_DIV1024_gc); // set clock prescaler to 1024 }
void app_touch_init(void) { #ifdef QTOUCH_STUDIO_MASKS SNS_array[0][0] = 0x50; SNS_array[0][1] = 0x00; SNS_array[1][0] = 0x00; SNS_array[1][1] = 0x00; SNSK_array[0][0] = 0xA0; SNSK_array[0][1] = 0x00; SNSK_array[1][0] = 0x00; SNSK_array[1][1] = 0x00; #endif /* Configures the sensors as keys and assigns the channel numbers. * The sensor is wired up with SNS=PF6 and SNSK=PF7 * When using "pin reconfigurability" this will result in channel 0 * because it is the first and only channel that is used. * For the standard qtouch library setup we would need to use * channel 3 since we are using the last two pins on the port. */ qt_enable_key(CHANNEL_0, NO_AKS_GROUP, 10u, HYST_6_25); qt_enable_key(CHANNEL_1, NO_AKS_GROUP, 10u, HYST_6_25); qt_init_sensing(); /* This will fill the default threshold values in the configuration * data structure. But User can change the values of these parameters. */ qt_config_data.qt_di = DEF_QT_DI; qt_config_data.qt_neg_drift_rate = DEF_QT_NEG_DRIFT_RATE; qt_config_data.qt_pos_drift_rate = DEF_QT_POS_DRIFT_RATE; qt_config_data.qt_max_on_duration = DEF_QT_MAX_ON_DURATION; qt_config_data.qt_drift_hold_time = DEF_QT_DRIFT_HOLD_TIME; qt_config_data.qt_recal_threshold = DEF_QT_RECAL_THRESHOLD; qt_config_data.qt_pos_recal_delay = DEF_QT_POS_RECAL_DELAY; /* Initialize the timer counter */ tc_enable(&TCC0); tc_write_period(&TCC0, TIMER_PERIOD); tc_write_clock_source(&TCC0, TC_CLKSEL_DIV8_gc); tc_set_cca_interrupt_level(&TCC0, PMIC_LVL_LOW); tc_set_cca_interrupt_callback(&TCC0, app_touch_tc_interrupt_callback); /* * Set up callback function. This function is called after the library * has made capacitive measurements, but before it has processed them. * The user can use this hook to apply filter functions to the measured * signal values.(Possibly to fix sensor layout faults) */ qt_filter_callback = NULL; #ifdef _DEBUG_INTERFACE_ QDebug_Init(); #endif sleepmgr_lock_mode(SLEEPMGR_IDLE); }
void TimerD0_init(void) { tc_write_clock_source(&TCD0,TC_CLKSEL_DIV256_gc); tc_set_wgm(&TCD0,TC_WG_NORMAL); tc_set_overflow_interrupt_level(&TCD0,TC_INT_LVL_MED); tc_write_period(&TCD0,TIMERD0_PER); tc_set_direction(&TCD0,TC_UP); tc_enable(&TCD0); };
void init_MS5611_callback() { tc_enable(&(MS5611_TIMER)); // timer used for callback functionality tc_set_overflow_interrupt_callback(&(MS5611_TIMER), ms5611_altitude); // sets up a function to callback when it's time to run tc_set_wgm(&(MS5611_TIMER), TC_WG_NORMAL); // sets the waveform generation tc_write_period(&(MS5611_TIMER), MS5611_HZ); tc_set_overflow_interrupt_level(&(MS5611_TIMER), TC_INT_LVL_LO); // medium priority 0x02 tc_write_clock_source(&(MS5611_TIMER), TC_CLKSEL_DIV1024_gc); // set clock prescaler to 1024 }
void TimerE1_init(void) { tc_write_clock_source(&TCE1,TC_CLKSEL_DIV256_gc); tc_set_wgm(&TCE1,TC_WG_SS); tc_write_period(&TCE1,0x00FF); tc_set_direction(&TCE1,TC_UP); tc_enable_cc_channels(&TCE1,TC_CCAEN); tc_enable(&TCE1); };
int main(void) { pmic_init(); board_init(); sysclk_init(); sleepmgr_init(); cpu_irq_enable(); /* Enables the Timer defined in conf_example.h : TCE0 in this example */ tc_enable(&TIMER_EXAMPLE); /* Configures the interrupt level of CCA and CCB modules : low */ tc_set_cca_interrupt_level(&TIMER_EXAMPLE, TC_INT_LVL_LO); tc_set_ccb_interrupt_level(&TIMER_EXAMPLE, TC_INT_LVL_LO); /* Configures the waveform generator of this Timer mode in NORMAL mode */ tc_set_wgm(&TIMER_EXAMPLE, TC_WG_NORMAL); /* Declares the interrupt functions which will be called when CCA and CCB interrupts will occur */ tc_set_cca_interrupt_callback(&TIMER_EXAMPLE, example_cca_interrupt_callback); tc_set_ccb_interrupt_callback(&TIMER_EXAMPLE, example_ccb_interrupt_callback); /* Configures the Timer period*/ tc_write_period(&TIMER_EXAMPLE, TIMER_EXAMPLE_PERIOD); /* Configures the CCA and CCB levels*/ tc_write_cc(&TIMER_EXAMPLE, TC_CCA, TIMER_EXAMPLE_PERIOD/2); tc_write_cc(&TIMER_EXAMPLE, TC_CCB, TIMER_EXAMPLE_PERIOD/2); /* Enables the CCA and CCB channels*/ tc_enable_cc_channels(&TIMER_EXAMPLE,TC_CCAEN); tc_enable_cc_channels(&TIMER_EXAMPLE,TC_CCAEN); /* Configures the waveform genertaor in Dual Slope mode and Top*/ tc_set_wgm(&TIMER_EXAMPLE,TC_WG_DS_T); /* Enables and configures the deadtime of CCA and CCB outputs*/ tc_awex_enable_cca_deadtime(&AWEXE); tc_awex_enable_ccb_deadtime(&AWEXE); tc_awex_set_dti_high(&AWEXE, TIMER_EXAMPLE_PERIOD/6); tc_awex_set_dti_low(&AWEXE, TIMER_EXAMPLE_PERIOD/6); /* Outputs CCA and CCB on Port E0 and E1*/ tc_awex_set_output_override(&AWEXE, 0x03); tc_set_resolution(&TIMER_EXAMPLE, 10000); do { /* Go to sleep, everything is handled by interrupts. */ sleepmgr_enter_sleep(); } while (1); }
/** Configures TC function with the driver. */ static void configure_tc(void) { struct tc_config config_tc; tc_get_config_defaults(&config_tc); config_tc.counter_size = TC_COUNTER_SIZE_16BIT; config_tc.counter_16_bit.value = TC_COUNT_VALUE; tc_init(&tc_instance, CONF_TC_INSTANCE, &config_tc); tc_enable(&tc_instance); }
void TimerC0_init(void) { tc_write_clock_source(&TCC0,TC_CLKSEL_DIV64_gc);//1 tc_set_wgm(&TCC0,TC_WG_SS); tc_write_period(&TCC0,0x77);//0x01DFF tc_set_direction(&TCC0,TC_UP); tc_enable_cc_channels(&TCC0,TC_CCAEN); tc_enable_cc_channels(&TCC0,TC_CCBEN); tc_enable(&TCC0); tc_write_cc(&TCC0,TC_CCA,0x5D); };
/** * @brief configure timer ISR to fire regularly * @ingroup QTouch * */ static void init_timer_isr( void ) { tc_enable(&TCCR0A); /* set timer compare value (how often timer ISR will fire) */ OCR0A = (TICKS_PER_MS * qt_measurement_period_msec); /* enable timer ISR on compare A */ TIMSK0 = 0x02u; /* Caution */ /* timer prescaler = system clock / 1024 */ TCCR0B = 0x05u; /* Caution */ /* timer mode = CTC (count up to compare value, then reset) */ TCCR0A = 0x02u; /* Caution */ }
void us_ticker_init(void) { uint32_t cycles_per_us; uint32_t prescaler = 0; struct tc_config config_tc; enum status_code ret_status; if (us_ticker_inited) return; us_ticker_inited = 1; if (g_sys_init == 0) { system_init(); g_sys_init = 1; } tc_get_config_defaults(&config_tc); cycles_per_us = system_gclk_gen_get_hz(config_tc.clock_source) / 1000000; MBED_ASSERT(cycles_per_us > 0); /*while((cycles_per_us & 1) == 0 && prescaler <= 10) { cycles_per_us = cycles_per_us >> 1; prescaler++; }*/ while((cycles_per_us > 1) && (prescaler <= 10)) { cycles_per_us = cycles_per_us >> 1; prescaler++; } if (prescaler >= 9) { prescaler = 7; } else if (prescaler >= 7) { prescaler = 6; } else if (prescaler >= 5) { prescaler = 5; } config_tc.clock_prescaler = TC_CTRLA_PRESCALER(prescaler); config_tc.counter_size = TC_COUNTER_SIZE_32BIT; config_tc.run_in_standby = true; config_tc.counter_32_bit.value = 0; config_tc.counter_32_bit.compare_capture_channel[TC_COMPARE_CAPTURE_CHANNEL_0] = 0xFFFFFFFF; config_tc.counter_32_bit.compare_capture_channel[TC_COMPARE_CAPTURE_CHANNEL_1] = 0xFFFFFFFF; /* Initialize the timer */ ret_status = tc_init(&us_ticker_module, TICKER_COUNTER_uS, &config_tc); MBED_ASSERT(ret_status == STATUS_OK); /* Register callback function */ tc_register_callback(&us_ticker_module, (tc_callback_t)us_ticker_irq_handler_internal, TC_CALLBACK_CC_CHANNEL0); /* Enable the timer module */ tc_enable(&us_ticker_module); }