summaryrefslogtreecommitdiffstats
path: root/app/tacho.c
blob: bf0222c6af8af16b14f02233a37fa01b6241a010 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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);

}