void BUTTON_DISCO_USER_isr(void) { exti_reset_request(BUTTON_DISCO_USER_EXTI); state.pressed = true; if (state.falling) { state.falling = false; exti_set_trigger(BUTTON_DISCO_USER_EXTI, EXTI_TRIGGER_RISING); state.hold_time = TIM_CNT(TIMER_BUTTON_PRESS); } else { state.falling = true; exti_set_trigger(BUTTON_DISCO_USER_EXTI, EXTI_TRIGGER_FALLING); state.hold_time = TIM_CNT(TIMER_BUTTON_PRESS) = 0; } }
static void timer2_setup ( void ) { //timer_reset ( TIM2 ); //timer_set_mode //timer_continuous_mode ( TIM2 ); /* Set timer start value. */ TIM_CNT(TIM2) = 1; /* Set timer prescaler. 72MHz/1440 => 50000 counts per second. */ TIM_PSC(TIM2) = 300; // 280K/s or 0.000 003 571 /* End timer value. If this is reached an interrupt is generated. */ TIM_ARR(TIM2) = 8; // // o-scope reports: // prescale 2000, 1->600 should be 100/sec; in fact, we're exactly 20ms between which is exactly 50 .. so callback is 60MHz, not 120MHz // manual says: // The reference manual (see page 133) states that the GPIO is capable of: // Fast toggle capable of changing every two clock cycles // --> okay so at 120MHz, the best we can do is 60MHz of GPIO. But thats different than here, where the timer seems halved.. /* Update interrupt enable. */ TIM_DIER(TIM2) |= TIM_DIER_UIE; //timer_set_repetition_counter ( TIM2, 100 ); /* Start timer. */ TIM_CR1(TIM2) |= TIM_CR1_CEN; return; }
void torture_setup ( void ) { rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_TIM3EN); timer_reset(TIM3); // 24Mhz / 10khz -1. timer_set_prescaler(TIM3, 20000); // 24Mhz/10000hz - 1 // 10khz for 10 ticks = 1 khz overflow = 1ms overflow interrupts timer_set_period(TIM3, 1); /* Set timer start value. */ TIM_CNT(TIM3) = 1; /* Set timer prescaler. */ TIM_PSC(TIM3) = 100; // 100 .. so about 840000 ticks per second /* End timer value. If this is reached an interrupt is generated. */ TIM_ARR(TIM3) = 60; // 840000/14 -> 60,000/sec nvic_enable_irq(NVIC_TIM3_IRQ); timer_enable_update_event(TIM3); // default at reset! timer_enable_irq(TIM3, TIM_DIER_UIE); timer_enable_counter(TIM3); }
int main(void) { rcc_clock_setup_in_hse_16mhz_out_72mhz(); gpio_setup(); nvic_setup(); gpio_clear(GPIOB, GPIO7); /* LED1 on */ gpio_set(GPIOB, GPIO6); /* LED2 off */ rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_TIM2EN); /* the goal is to let the LED2 glow for a second and then be off for a second */ /* Set timer start value */ TIM_CNT(TIM2) = 1; /* Set timer prescaler. 72MHz/1440 => 50000 counts per second */ TIM_PSC(TIM2) = 1440; /* End timer value. If this value is reached an interrupt is generated */ TIM_ARR(TIM2) = 50000; /* Update interrupt enable */ TIM_DIER(TIM2) |= TIM_DIER_UIE; /* Start timer */ TIM_CR1(TIM2) |= TIM_CR1_CEN; while(1); /* Halt. */ return 0; }
void BUTTON_DISCO_USER_isr(void) { exti_reset_request(BUTTON_DISCO_USER_EXTI); if (state.falling) { gpio_clear(LED_DISCO_BLUE_PORT, LED_DISCO_BLUE_PIN); state.falling = false; exti_set_trigger(BUTTON_DISCO_USER_EXTI, EXTI_TRIGGER_RISING); unsigned int x = TIM_CNT(TIM7); printf("held: %u ms\n", x); } else { gpio_set(LED_DISCO_BLUE_PORT, LED_DISCO_BLUE_PIN); printf("Pushed down!\n"); TIM_CNT(TIM7) = 0; state.falling = true; exti_set_trigger(BUTTON_DISCO_USER_EXTI, EXTI_TRIGGER_FALLING); } }
/** * @brief Trigger function to start a 2 wire SSI read of arbitrary length * @param u7 nbits The number of bits to read */ void ssi_start_read(u8 nbits) { if(ext_buffer_flag == true) { ext_buffer->ready_flag = 0; } ssi_counter = nbits; ssi_state = SSI_START; TIM_CNT(SSI_TIMER) = 0; TIM_CR1(SSI_TIMER) |= TIM_CR1_CEN;// Start counter }
void ssi_bit_handler(u8 bit) { ssi_counter--; // Decrement bit Counter ssi_buffer = ssi_buffer << 1; // Shift buffer if(bit == 1) { ssi_buffer |= 1; // Add 1 to LSB } if((ssi_counter == 0) && (ext_buffer_flag == true)) { ext_buffer->value = ssi_buffer; ext_buffer->ready_flag = true; } // Set Timer TIM_CNT(SSI_TIMER) = 0; TIM_CR1(SSI_TIMER) |= TIM_CR1_CEN; }
void timer1_setup() { // Timer 1 a 2MHz /* Set timer start value */ TIM_CNT(TIM1) = 1; /* Set timer prescaler. 72MHz/36 => 2MHz counts per second */ TIM_PSC(TIM1) = 36; // 2MHz /* End timer value. If this value is reached an interrupt is generated */ TIM_ARR(TIM1) = 60000; // 1kHz /* Update interrupt enable */ // TIM_DIER(TIM1) |= TIM_DIER_UIE; /* Start timer */ TIM_CR1(TIM1) |= TIM_CR1_CEN; }
void setLEDColor(uint8_t red, uint8_t green, uint8_t blue) { if (false == g_isRedBlinking) { int cnt = TIM_CNT(RGB_TIMER); TIM_CCR1(RGB_TIMER) = TIM_ARR(RGB_TIMER) - green; TIM_CCR2(RGB_TIMER) = TIM_ARR(RGB_TIMER) - red; int tim_ccr1 = TIM_CCR1(RGB_TIMER); int tim_ccr2 = TIM_CCR2(RGB_TIMER); UNUSED(tim_ccr1); UNUSED(tim_ccr2); UNUSED(cnt); if (0 == blue) { gpio_set(LED_PORT, LED_B_PIN); } else { gpio_clear(LED_PORT, LED_B_PIN); } } }
u32 timer_get_counter(u32 timer_peripheral) { return TIM_CNT(timer_peripheral); }