static int GetPulseTimeFiltered()
{
   int pulses = 0;

   if (timer_get_flag(REV_CNT_TIMER, TIM_SR_CC3IF))
   {
      ignore = 0;
      if (timer_get_flag(REV_CNT_TIMER, TIM_SR_UIF))
      {
         timer_clear_flag(REV_CNT_TIMER, TIM_SR_CC3IF);
         timer_clear_flag(REV_CNT_TIMER, TIM_SR_UIF);
         ignore = 1;
      }

      if (timer_get_flag(REV_CNT_TIMER, TIM_SR_CC3OF))
      {
         last_pulse_timespan = IIRFILTER(last_pulse_timespan, REV_CNT_CCR, filter);
         timer_clear_flag(REV_CNT_TIMER, TIM_SR_CC3OF);
         pulses = 2;
      }
      else
      {
         last_pulse_timespan = IIRFILTER(last_pulse_timespan, REV_CNT_CCR, filter);
         pulses = 1;
      }
   }

   return pulses;
}
Exemplo n.º 2
0
static int command(char *command, size_t length)
{
    size_t i;

    calculate_crc_and_ack(command, length);

    for (i = 0; i < length; i++)
        usart_send_blocking(USART2, command[i]);

    timer_set_counter(TIM5, 0);
    timer_clear_flag(TIM5, TIM_SR_UIF);
    timer_enable_counter(TIM5);

    for (i = 0; i < sizeof(expect_ack); )
    {
        while (!timer_get_flag(TIM5, TIM_SR_UIF) &&
               !usart_get_flag(USART2, USART_SR_RXNE));

        if (timer_get_flag(TIM5, TIM_SR_UIF))
            break;

        if (expect_ack[i] != usart_recv(USART2))
            break;
    }

    timer_disable_counter(TIM5);

    return (i == sizeof(expect_ack));
}
void tim2_isr(void)
{
	gpio_toggle(GPIOB, GPIO8);              /* LED1 on/off. */
	if (timer_get_flag(TIM2, TIM_SR_UIF))
        timer_clear_flag(TIM2, TIM_SR_UIF); /* Clear interrrupt flag. */
	timer_get_flag(TIM2, TIM_SR_UIF);	/* Reread to force the previous write */
}
void tim2_isr(void)
{
	gpio_toggle(GPIOB, GPIO8);              /* LED2 on/off. */
	if (timer_get_flag(TIM2, TIM_SR_UIF))
        timer_clear_flag(TIM2, TIM_SR_UIF); /* Clear interrrupt flag. */
	timer_get_flag(TIM2, TIM_SR_UIF);	/* Reread to force the previous write */
	timer_disable_irq(TIM2, TIM_DIER_UIE);
	timer_disable_counter(TIM2);
}
Exemplo n.º 5
0
/******************************************************************************
Interruptserviceroutine Timer 3 Compare 1 for the CAN timer
Pull in the timer counter value to represent the last time an alarm was set.
******************************************************************************/
void tim3_isr(void)
{
    /* Clear interrrupt flag. */
    if (timer_get_flag(TIM3, TIM_SR_CC1IF))
    {
        timer_clear_flag(TIM3, TIM_SR_CC1IF);
        /* Reread to force the previous write before leaving (a side-effect of hardware pipelining)*/
        timer_get_flag(TIM3, TIM_SR_CC1IF);
        last_time_set = timer_get_counter(TIM3);
        /* Call the time handler of the stack to adapt the elapsed time */
        TimeDispatch();
    }
}
Exemplo n.º 6
0
void tim4_isr(){
	if(timer_get_flag(TIM4, TIM_SR_UIF)){
		// Clear compare interrupt flag
		timer_clear_flag(TIM4, TIM_SR_UIF);
		timer_flag[1] = 1;
	}
}
Exemplo n.º 7
0
void tim2_isr(void)
{
count++;
	if (timer_interrupt_source(TIM2, TIM_SR_UIF)) timer_clear_flag(TIM2, TIM_SR_UIF); /* Clear interrrupt flag. */
	timer_get_flag(TIM2, TIM_SR_UIF);	/* Reread to force the previous (buffered) write before leaving */
    pxMBPortCBTimerExpired();
}
void tim2_isr(void)
{
	if (timer_get_flag(TIM2, TIM_SR_CC1IF)) {

		/* Clear compare interrupt flag. */
		timer_clear_flag(TIM2, TIM_SR_CC1IF);

		/*
		 * Get current timer value to calculate next
		 * compare register value.
		 */
		compare_time = timer_get_counter(TIM2);

		/* Calculate and set the next compare value. */
		frequency = frequency_sequence[frequency_sel++];
		new_time = compare_time + frequency;

		timer_set_oc_value(TIM2, TIM_OC1, new_time);
		if (frequency_sel == 18)
			frequency_sel = 0;

		/* Toggle LED to indicate compare event. */
		gpio_toggle(GPIOC, GPIO12);
	}
}
Exemplo n.º 9
0
void tim1_up_tim10_isr(void)
{
    if(timer_get_flag(TIM1, TIM_SR_UIF)) {
        timer_clear_flag(TIM1, TIM_SR_UIF);
        if(n_overflow <= 8) n_overflow++;
    }
}
Exemplo n.º 10
0
void tim2_isr() {
  if (timer_get_flag(TIM2, TIM_SR_CC1IF)) {
    timer_clear_flag(TIM2, TIM_SR_CC1IF);
    scheduler_execute();
  }
  if (timer_get_flag(TIM2, TIM_SR_CC2IF)) {
    timer_clear_flag(TIM2, TIM_SR_CC2IF);
    config.decoder.last_t0 = TIM2_CCR2;
    config.decoder.needs_decoding_t0 = 1;
  }
  if (timer_get_flag(TIM2, TIM_SR_CC3IF)) {
    timer_clear_flag(TIM2, TIM_SR_CC3IF);
    config.decoder.last_t1 = TIM2_CCR3;
    config.decoder.needs_decoding_t1 = 1;
  }
}
Exemplo n.º 11
0
static int silence_nmea()
{
    int attempt;
    size_t i;

    /* Configure the GPS */
    for (attempt = 0; attempt < 4; attempt++)
    {
        calculate_crc_and_ack(set_port, sizeof(silence_nmea));

        for (i = 0; i < sizeof(set_port); i++)
            usart_send_blocking(USART2, set_port[i]);

        timer_clear_flag(TIM5, TIM_SR_UIF);
        timer_set_counter(TIM5, 0);
        timer_enable_counter(TIM5);

        for (i = 0; i < sizeof(expect_ack); )
        {
            while (!timer_get_flag(TIM5, TIM_SR_UIF) &&
                   !usart_get_flag(USART2, USART_SR_RXNE));

            if (timer_get_flag(TIM5, TIM_SR_UIF))
                break;

            if (expect_ack[i] == usart_recv(USART2))
                i++;
            else
                i = 0;
        }

        timer_disable_counter(TIM5);

        if (i < sizeof(expect_ack))
            continue;
        else
            break;
    }

    while (usart_get_flag(USART2, USART_SR_RXNE))
        usart_recv(USART2);

    return attempt < 4;
}
Exemplo n.º 12
0
void tim1_cc_isr(void) {
    static int last_value = 0;
    int value;
    if (timer_get_flag(TIM1, TIM_SR_CC1IF) != 0) {
	// Timer channel 1 interrupt -> First edge (outcoming signal).
	timer_clear_flag(TIM1, TIM_SR_CC1IF);
	last_value = timer_get_counter(TIM1);
    }
    if (timer_get_flag(TIM1, TIM_SR_CC2IF)) {
	// Timer channel 2 interrupt -> response (incoming signal).
	timer_clear_flag(TIM1, TIM_SR_CC2IF);
	value = timer_get_counter(TIM1) - last_value;
	if (value < 0)
	    value = value + 0xffff;
	latency = value;
	if (latency > latency_max)
	    latency_max = latency;
    }
}
Exemplo n.º 13
0
void PLC_WAIT_TMR_ISR(void)
{
    if (timer_get_flag(PLC_WAIT_TMR, TIM_SR_UIF))
    {

        /* Clear compare interrupt flag. */
        timer_clear_flag(PLC_WAIT_TMR, TIM_SR_UIF);
        plc_sys_timer++;
        plc_iom_tick();
    }
}
Exemplo n.º 14
0
void tim2_isr(void){
    if(timer_get_flag(TIM2, TIM_SR_UIF)){
        if(last_usb_request_time < (system_millis-10)){
            gpio_set(GPIOD, GPIO13);
            if(usb_ready == 3 && system_millis>5000) msleep(10);
        }else{
            if(usb_ready >= 1) usb_ready = 3;
            gpio_clear(GPIOD, GPIO13);
        }
        timer_clear_flag(TIM2, TIM_SR_UIF);
    }
}
Exemplo n.º 15
0
void tim2_isr(void)
{
  /* reset interrupt flag */
  if (timer_get_flag(TIM2, TIM_DIER_UIE)) {
    timer_clear_flag(TIM2, TIM_DIER_UIE);
  }

  /* call user callback */
  uint16_t next_value;
  pwm_update_callback(&next_value);
  pwm_set(next_value);
}
Exemplo n.º 16
0
/**
 * Timer 2 interrupt handler
 */
void tim2_isr(void)
{

	if (timer_get_flag(TIM2, TIM_SR_CC1IF)) {
		timer_clear_flag(TIM2, TIM_SR_CC1IF);

		/* prepare for next comm */
		comm_tim_data.last_capture_time = timer_get_counter(TIM2);
		//comm_tim_data.last_capture_time = timer_get_ic_value(TIM2, TIM_OC1);
		//comm_tim_data.last_capture_time = TIM2_CCR1;

		/* triggering commutation event */
		if (comm_tim_trigger_comm || comm_tim_trigger_comm_once) {
			timer_generate_event(TIM1, TIM_EGR_COMG);
			//timer_generate_event(TIM1, TIM_EGR_COMG | TIM_EGR_UG);
		}

		/* (re)setting "semaphors" */
		comm_tim_trigger_comm_once = false;
		comm_tim_trigger = true;

		/* Set next comm time */
		timer_set_oc_value(TIM2, TIM_OC1,
				comm_tim_data.last_capture_time +
				(comm_tim_data.freq * 2));
		//timer_set_oc_value(TIM2, TIM_OC1,
		//		comm_tim_data.last_capture_time +
		//		65535);

		TOGGLE(DP_EXT_SCL);
	}

	if (timer_get_flag(TIM2, TIM_SR_UIF)) {
		timer_clear_flag(TIM2, TIM_SR_UIF);

		comm_tim_state.update_count++;
	}
}
Exemplo n.º 17
0
void tim1_cc_isr(void) {
	if(timer_get_flag(TIM1, TIM_SR_CC2IF)) {
		if (timer_get_flag(TIM1, TIM_SR_CC2OF)) 
			overcapture++;
		uint32_t current_counter = timer_get_counter(TIM1);
		uint32_t current_overflow = overflow_counter;
		timer_clear_flag(TIM1, TIM_SR_CC2IF);
		//gpio_toggle(GPIOE, GPIO13); /* toggle pin for 'scope debugging */
		if (m_state == PENDING) { // start measurement
			edges=0;
			start_counter = current_counter;
			overflow_counter = 0;
			m_state = RUNNING;
		}
		if (edges == NUM_EDGES) { // end measurement
			cycles_during_measurement = 
				(current_overflow*TIMER1_PERIOD) + current_counter - start_counter;
			m_state = IDLE;
		} else {
			edges += 1;
		}
	}
}
Exemplo n.º 18
0
/* TIM2 interrupt service routine */
void tim2_isr(void)
{
	/* manage update interrupt */
	if (timer_get_flag(TIM2, TIM_SR_UIF)) {

		/* call TICK timer callback */
		rtos_tick_timer_callback();

		/* Clear compare interrupt flag. */
		timer_clear_flag(TIM2, TIM_SR_UIF);
	} else {
		/* do nothing. ATTENTION: it could be better to clear all interrupts flags */
	}
}
Exemplo n.º 19
0
void tim1_cc_isr(void)
{
    if(timer_get_flag(TIM1, TIM_SR_CC2IF)) {
        int ccr = TIM_CCR2(TIM1);
        int save_n_overflow = n_overflow;
        n_overflow = 0;
        
        int delta = (ccr + (save_n_overflow<<16)) - last_ccr;
        
        if(dma_enabled)
            start_dma();
        
        timer_clear_flag(TIM1, TIM_SR_CC2IF);
        
        //gpio_set(DEBUG0_OUT_PORT, DEBUG0_OUT_PIN);
        //gpio_clear(DEBUG0_OUT_PORT, DEBUG0_OUT_PIN);
        
        last_ccr = ccr;
        
        int speed_delta = motor_ctrl_step(delta);
        // Wait for motor speed to get stable.
        if(!dma_enabled && speed_delta > -10 && speed_delta < 10) {
            if(motor_ok == 0) {
                set_status(LED_GREEN, 1);
                set_status(LED_RED, 0);
                dma_enabled = 1;
            } else {
                motor_ok--;
            }
        }
        
        if(dma_enabled && (speed_delta < -100 || speed_delta > 100)) {
            set_status(LED_GREEN, 0);
            set_status(LED_RED, 1);
            dma_enabled = 0;
            motor_ok = 16;
        }
        
        if(delta < 100) {
            gpio_set(GPIOB, GPIO0);
            __asm("nop");
            __asm("nop");
            __asm("nop");
            __asm("nop");
            gpio_clear(GPIOB, GPIO0);
        }
    }
}
/*--------------------------------------------------------------------*/
void tim2_isr(void)
{
	if (timer_get_flag(TIM2, TIM_SR_CC1IF))
	{
/* Clear compare interrupt flag. */
		timer_clear_flag(TIM2, TIM_SR_CC1IF);

/* Set the next DAC output - saw-tooth.*/
		if (++cntr > 255)
		{
			cntr=0;
/* Toggle PC0 just to keep aware of activity and frequency. */
			gpio_toggle(GPIOC, GPIO0);
		}
/* Trigger the DAC then load the next value */
		dac_software_trigger(CHANNEL_D);
		dac_load_data_buffer_single(v[cntr], RIGHT8, CHANNEL_D);
	}

}
Exemplo n.º 21
0
void tim3_isr(void) {
    if (timer_get_flag(TIM3, TIM_SR_CC1IF)) {
        /* Clear compare interrupt flag. */
        timer_clear_flag(TIM3, TIM_SR_CC1IF);
        
        leds = leds+0x0100;
        gpio_port_write(GPIOE, leds);
        
        //TIM3_SMCR &= ~TIM_SMCR_SMS_MASK;
        //gpio_port_write(GPIOE, 0xFF00);
        //state = 0;
        //usb transfer stuff
        
        /*if ( usb_ready_to_send == 1 ) {
            usb_ready_to_send = 0;
            char hi[4] = {'h', 'i', '\r', '\n'};
            usbd_ep_write_packet(usb_device, 0x82, hi, 4);
        }
         */
        
    }
}
Exemplo n.º 22
0
void tim1_up_tim10_isr(void) {
	if(timer_get_flag(TIM1, TIM_SR_UIF)) {
		timer_clear_flag(TIM1, TIM_SR_UIF);
		overflow_counter++;
	}
}
Exemplo n.º 23
0
static void handle_data(void){
    #ifdef USE_STTERM
    unsigned char tmp_c;
    #endif

    uint32_t tmp=0;
    char crc=0;

    #ifdef USE_STTERM
    if(stlinky_rx_ready()){
        scanf("%c", &tmp_c);
        INPUT[k] = tmp_c;
        gpio_toggle(GPIOD, GPIO15);
    #else
    if(usart_has_data()){
        INPUT[k] = usart_read();
    #endif
    k++;
    if(k==1 && INPUT[0]!=0x53){ // 'S'
        #ifdef DEBUG
        printf("ERROR AT START-BYTE %d\r\n",INPUT[0]);
        #endif
        k=0;
    }
    if(k==10 && INPUT[9]!=0x45){ // 'E'
        #ifdef DEBUG
        printf("ERROR AT END-BYTE %d\r\n",INPUT[8]);
        #endif
        k=0;
    }
    if(k==11){
        #ifdef DEBUG
        printf("Package: ");
        for(tmp=0;tmp<10;tmp++) printf("%d ",INPUT[tmp]);
        printf("\n");
        #endif
        for(tmp=0;tmp<8;tmp++) KEYS[tmp] = INPUT[tmp+1];
        crc = getCRC(KEYS,8);
        if(crc==INPUT[10]){
            #ifdef DEBUG
            printf("INPUT OK!\r\n");
                #endif
                if(usb_ready == 3) usb_send_packet(KEYS, 8);
            }else{
                #ifdef DEBUG
                printf("ERROR AT CRC-BYTE %d vs %d\r\n",INPUT[10],crc);
                #endif
            }
            k=0;
        }
    }
}
void tim3_isr(void){
    if(timer_get_flag(TIM3, TIM_SR_UIF)){
        handle_data();
        timer_clear_flag(TIM3, TIM_SR_UIF);
    }
}

void tim4_isr(void){
    if(timer_get_flag(TIM4, TIM_SR_UIF)){
        if(usb_ready==3){
            gpio_toggle(GPIOD,GPIO14);
            KEYS[0]=0;
            KEYS[1]=0;
            KEYS[3]=0;
            KEYS[4]=0;
            KEYS[5]=0;
            KEYS[6]=0;
            KEYS[7]=0;

            KEYS[2]=0x04;
            usb_send_packet(KEYS, 8);
            KEYS[2]=0x00;
            usb_send_packet(KEYS, 8);
            KEYS[2]=0x05;
            usb_send_packet(KEYS, 8);
            KEYS[2]=0x00;
            usb_send_packet(KEYS, 8);
        }
        timer_clear_flag(TIM4, TIM_SR_UIF);
    }
}

void tim5_isr(void){
    if(timer_get_flag(TIM5, TIM_SR_UIF)){
        gpio_toggle(GPIOD, GPIO15);
        timer_clear_flag(TIM5, TIM_SR_UIF);
    }
}

int main(void) {
    iwdg_reset();
    iwdg_set_period_ms(5);
    iwdg_start();

    rcc_clock_setup_hse_3v3(&hse_8mhz_3v3[CLOCK_3V3_168MHZ]);
    rcc_periph_clock_enable(RCC_GPIOD);
    systick_setup();

    gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO12 | GPIO13 | GPIO14 | GPIO15);
    gpio_clear(GPIOD, GPIO12 | GPIO13 | GPIO14 | GPIO15);

    iwdg_set_period_ms(500);
    iwdg_reset();
    msleep(400);
    iwdg_set_period_ms(5);
    iwdg_reset();

    gpio_set(GPIOD, GPIO15);

//    msleep(100000); /* SLEEEEEEEEEEEEEEEEEEEEEEP */

    #ifdef USE_STTERM
    stlinky_init();
    #else
    usart_setup();
    #endif

    timer2_setup(100);

    usb_setup();

    //timer4_setup(1);
    timer3_setup(100);
    timer5_setup(2);

//    gpio_set(GPIOD, GPIO12 | GPIO13 | GPIO14 | GPIO15);

    while(1) {
        iwdg_reset();
    }

    return 0;
}