#include "project.h" #define SPURIOUS MS(10) /*Anything faster than 6000rpm is a false trigger */ uint32_t raw_tacho; static uint32_t cycle_diff (uint32_t a, uint32_t b) { return b - a; } void TACHO_ISR (void) { uint32_t now, diff; static uint32_t last_edge; if (! (EXTI_PR & TACHO)) return; EXTI_PR = TACHO; now = dwt_read_cycle_counter(); diff = cycle_diff (last_edge, now); if (diff < SPURIOUS) return; last_edge = now; /* Want RPM, diff is in units of 1/72 us */ if (!diff) return; /* uint32_t is un-able to express 60s in clock ticks, so divide everything by 2 */ diff >>= 1; raw_tacho = (US ((60U * 1000000) / 2)) / diff; } void tacho_init (void) { MAP_INPUT_PU (TACHO); exti_select_source (TACHO, TACHO_PORT); exti_set_trigger (TACHO, EXTI_TRIGGER_FALLING); exti_enable_request (TACHO); exti_reset_request (TACHO); nvic_enable_irq (TACHO_IRQ); }