summaryrefslogtreecommitdiffstats
path: root/app/tacho.c
diff options
context:
space:
mode:
Diffstat (limited to 'app/tacho.c')
-rw-r--r--app/tacho.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/app/tacho.c b/app/tacho.c
new file mode 100644
index 0000000..35331da
--- /dev/null
+++ b/app/tacho.c
@@ -0,0 +1,64 @@
+#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 exti3_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 able to express 60s in clock ticks, so divide everything by 2 */
+
+diff >>=1;
+
+
+
+raw_tacho =(US ((60*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);
+
+}
+
+
+