FTVOID ft9xx_int_timer (FTVOID) { #if defined(DEF_TIMER) /* The Timer Prescaler will divide the 100MHz Master clock down to 2kHz */ #define TIMER_PRESCALER (50000) #define TIMER_M_SEC (100000/TIMER_PRESCALER) #define TIMER_SEC (1000*TIMER_M_SEC) #define TIMER_PRI (17) //why this number? I don't know, copy from sample. /* Enable Timers... */ sys_enable(sys_device_timer_wdt); /* Set up the Timer tick to be 0.5 ms... */ timer_prescaler(TIMER_PRESCALER); /* Set up Timer A to be triggered 1 second */ timer_init(timer_select_a, /* Device */ TIMER_SEC, /* Initial Value */ timer_direction_down, /* Count Direction */ timer_prescaler_select_on, /* Prescaler Select */ timer_mode_continuous); /* Timer Mode */ /* Enable the timers... */ timer_enable_interrupt(timer_select_a); /* Register the interrupt... */ interrupt_attach(interrupt_timers, TIMER_PRI, timerISR); /* Start all the timers at the same time... */ timer_start(timer_select_a); #endif }
/** * @brief timer hardware routine */ void timer_hardware_init(uint8_t interval, uint8_t scale){ HAS_CRITICAL_SECTION; ENTER_CRITICAL_SECTION(); scale &= 0x7; scale |= (1<<WGM1); // reset on match TIMSK &= ((unsigned char)~(1 << (TOIE0))); TIMSK &= ((unsigned char)~(1 << (OCIE0))); //!< Disable TC0 interrupt /** * set Timer/Counter0 to be asynchronous * from the CPU clock with a second external * clock(32,768kHz)driving it */ ASSR |= (1 << (AS0)); //!< us external oscillator TCCR0 = scale; TCNT0 = 0; OCR0 = interval; //TIMSK |= (1 << (OCIE0)); replaced by the line below timer_enable_interrupt(); LEAVE_CRITICAL_SECTION(); timer_init(); }
int main(void) { int test_mode; int vm_present; int i; unsigned int seed; // Needs to be called ASAP as rf need a looooooong time to wake up. // This function is just sending a pulse over the SCL line. rf_wakeup(); clock_set_speed(16000000UL,16); setup_pps(); setup_io(); leds_init(); CHARGE_500MA = 0; // Switch back to 100mA charge. // Switch on one led to say we are powered on leds_set(LED_BATTERY_0, 32); // Enable the poweroff softirq. _INT3IF = 0; _INT3IP = 1; _INT3IE = 1; // Sound must be enabled before analog, as // The analog interrupt callback into sound processing ... // But must be initialised _after_ leds as it use one led IO for enabling amp. sound_init(); tone_init(); // Init tone generator pwm_motor_init(); pid_motor_init(); // We need the settings for the horizontal prox. load_settings_from_flash(); for (i = 0; i < 2; i++) { // Settings is definitely wrong.... if(settings.mot256[i] <= 0) settings.mot256[i] = 256; // 1024 (AD resolution is 10 bits) * 256 / 9 fits in signed 16 bits. if (settings.mot256[i] < 9) settings.mot256[i] = 9; } // This is the horizontal prox. Vertical one are handled by the ADC // but ADC sync the motor mesurment with the prox, so we don't pullute it with noise ... timer_init(TIMER_IR_COMM, 0,-1); // The period will be changed later. prox_init(PRIO_SENSORS); // Same priority as analog (maybe should be at 7 ...) // Warning: We cannot use the SD before the analog init as some pin are on the analog port. analog_init(TIMER_ANALOG, PRIO_SENSORS); wait_valid_vbat(); log_init(); // We will need to read vbat to be sure we can flash. ntc_init(ntc_callback, PRIO_1KHZ); // i2c_init(I2C_3); i2c_init_master(I2C_3, 400000, PRIO_I2C); I2C3CON = 0x9000; mma7660_init(I2C_3, MMA7660_DEFAULT_ADDRESS, acc_cb, 0); mma7660_set_mode(MMA7660_120HZ, 1); rc5_init(TIMER_RC5, rc5_callback, PRIO_RC5); sd_init(); timer_init(TIMER_1KHZ, 1000, 6); timer_enable_interrupt(TIMER_1KHZ, timer_1khz, PRIO_1KHZ); rf_init(I2C_3); timer_enable(TIMER_1KHZ); sd_log_file(); vm_present = init_aseba_and_fifo(); if(vm_present) log_analyse_bytecode(); vmVariables.fwversion[0] = FW_VERSION; vmVariables.fwversion[1] = FW_VARIANT; // SD file is more important than internal flash if(!sd_load_aseba_code()) { log_set_flag(LOG_FLAG_VMCODESD); vm_present = 1; log_analyse_bytecode(); } // Behavior is on INT4 (softirq trigged by 1khz timer). behavior_init(PRIO_BEHAVIOR); test_mode = sd_test_file_present(); if(!test_mode) mode_init(vm_present); // Enable the LVD interrupt _LVDIE = 1; play_sound(SOUND_POWERON); if(test_mode) { test_mode_start(); while(1) idle_without_aseba(); } while(behavior_enabled(B_MODE)) idle_without_aseba(); // If usb did not put us out of behavior mode, then start the rf link if(!usb_uart_serial_port_open() && (rf_get_status() & RF_PRESENT)) { rf_set_link(RF_UP); } // get the random seed seed = 0; for(i = 0; i < 5; i++) { seed += vmVariables.buttons_mean[i]; seed += vmVariables.buttons_noise[i]; } seed += vmVariables.vbat[0]; seed += vmVariables.vbat[1]; for(i = 0; i < 3; i++) seed += vmVariables.acc[i]; AsebaSetRandomSeed(seed); for(i = 0; i < 3; i++) AsebaGetRandom(); // Give full control to aseba. No way out (except reset). run_aseba_main_loop(); }
void hwtimer_arch_enable_interrupt(void) { for (int i = 0; i < HWTIMER_MAXTIMERS; i++) { timer_enable_interrupt(i); } }
static void timer_set(uint32_t value, short timer) { DEBUG("Setting timer %u to %lu\n", timer, value); timer_set_nostart(value, timer); timer_enable_interrupt(timer); }