From e41764fceeabb1cdb6a7a299e00f2166a6f6ac32 Mon Sep 17 00:00:00 2001 From: fishsoupisgood Date: Thu, 18 Jun 2020 13:26:56 +0100 Subject: moved stm32 into directory added noddy pcb --- stm32/app/tacho.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 stm32/app/tacho.c (limited to 'stm32/app/tacho.c') diff --git a/stm32/app/tacho.c b/stm32/app/tacho.c new file mode 100644 index 0000000..bf0222c --- /dev/null +++ b/stm32/app/tacho.c @@ -0,0 +1,66 @@ +#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); + +} -- cgit v1.2.3