Subversion Repositories EngineBay2

Rev

Rev 39 | Rev 42 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 39 Rev 41
Line 6... Line 6...
6
 */
6
 */
7
#include "stm32f1xx_hal.h"
7
#include "stm32f1xx_hal.h"
8
#include "misc.h"
8
#include "misc.h"
9
#include "main.h"
9
#include "main.h"
10
 
10
 
11
 
-
 
12
unsigned volatile long RPM_Time[RPM_SAMPLES];  // sampled on both  edges
11
unsigned volatile long RPM_Time[RPM_SAMPLES];  // sampled on both  edges
-
 
12
unsigned volatile char RPM_Level[RPM_SAMPLES]; // active level when sampled
13
unsigned volatile long RPM_Count; // incremented every reading
13
unsigned volatile long RPM_Count; // incremented every reading
14
 
14
 
15
// this is set if there is a timer timeout interrupt
15
// this is set if there is a timer timeout interrupt
16
unsigned char volatile periodPulse = 0;
16
unsigned char volatile periodPulse = 0;
17
 
17
 
18
unsigned char volatile tim3triggered = 0;
18
unsigned char volatile tim3triggered = 0;
19
 
19
 
20
static void
20
static void
21
triggerTim3 (void)
21
triggerTim3 (void)
22
{
22
{
23
  htim3.Instance->CNT = 0;
23
      htim3.Instance->CNT = 0;
24
  htim3.Instance->CR1 |= TIM_CR1_CEN;
24
      htim3.Instance->CR1 |= TIM_CR1_CEN;
25
 
-
 
26
}
25
}
27
 
26
 
28
void
27
void
29
TIM2_IRQHandler (void)
28
TIM2_IRQHandler (void)
30
{
29
{
-
 
30
// rising edge trigger CB pulse
31
  if (__HAL_TIM_GET_FLAG(&htim2, TIM_FLAG_CC1))
31
  if (__HAL_TIM_GET_FLAG(&htim2, TIM_FLAG_CC1))
32
    {
32
    {
33
      __HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_CC1);
33
      __HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_CC1);
34
      RPM_Time[RPM_Count] = __HAL_TIM_GET_COMPARE(&htim2, TIM_CHANNEL_1);
34
      RPM_Time[RPM_Count] = __HAL_TIM_GET_COMPARE(&htim2, TIM_CHANNEL_1);
-
 
35
      RPM_Level[RPM_Count] = 1; // rising so it is high now.
35
      RPM_Count = (RPM_Count + 1) % RPM_SAMPLES;
36
      RPM_Count = (RPM_Count + 1) % RPM_SAMPLES;
-
 
37
      // trigger timer some time after falling edge
-
 
38
      if(tim3triggered ==0 )
36
 
39
 
37
      if ( tim3triggered == 0)
-
 
38
        {
40
        {
39
          tim3triggered = 1; // make it a monostable
-
 
40
          periodPulse = 0;
41
          tim3triggered = 1;
41
          triggerTim3 ();
42
          triggerTim3 ();
42
        }
43
        }
-
 
44
 
-
 
45
    }
-
 
46
// falling edge trigger CB pulse
-
 
47
  if (__HAL_TIM_GET_FLAG(&htim2, TIM_FLAG_CC2))
-
 
48
    {
-
 
49
      __HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_CC2);
-
 
50
      RPM_Time[RPM_Count] = __HAL_TIM_GET_COMPARE(&htim2, TIM_CHANNEL_2);
-
 
51
      RPM_Level[RPM_Count] = 0; // falling so it is low now.
-
 
52
      RPM_Count = (RPM_Count + 1) % RPM_SAMPLES;
-
 
53
      // indicate that CB pulse is owning the timer 3 timing
-
 
54
      periodPulse = 0;
43
    }
55
    }
44
}
-
 
45
 
56
 
-
 
57
}
46
 
58
 
47
// timer variable shared between TIM3 and TIM4 handler.
59
// timer variable shared between TIM3 and TIM4 handler.
48
static char chtTimer = 0;
60
static char chtTimer = 0;
49
 
61
 
50
void
62
void
Line 52... Line 64...
52
{
64
{
53
  if (__HAL_TIM_GET_FLAG(&htim3, TIM_FLAG_UPDATE))
65
  if (__HAL_TIM_GET_FLAG(&htim3, TIM_FLAG_UPDATE))
54
    {
66
    {
55
      __HAL_TIM_CLEAR_FLAG(&htim3, TIM_FLAG_UPDATE);
67
      __HAL_TIM_CLEAR_FLAG(&htim3, TIM_FLAG_UPDATE);
56
 
68
 
-
 
69
 
57
      tim3triggered = 0;
70
      tim3triggered = 0;
58
      if (chtTimer >= 3)  // every 300mS
71
      if (chtTimer >= 3)  // every 300mS
59
        {
72
        {
60
          chtTimer = 0;
73
          chtTimer = 0;
61
 
74
 
Line 111... Line 124...
111
            }
124
            }
112
        }
125
        }
113
 
126
 
114
      if (periodPulse == 1)
127
      if (periodPulse == 1)
115
        {
128
        {
116
        triggerTim3 ();
129
          triggerTim3 ();
117
 
130
 
118
        }
131
        }
-
 
132
      // indicate that timer 4 firing is owning the timer 3 trigger
119
      periodPulse = 1;
133
      periodPulse = 1;
120
 
134
 
121
 
-
 
122
      chtTimer++;
135
      chtTimer++;
123
    }
136
    }
124
 
137
 
125
}
138
}
126
 
139
 
127
 
-
 
128
void
140
void
129
resetSerialTimeout (void)
141
resetSerialTimeout (void)
130
{
142
{
131
  __disable_irq ();
143
  __disable_irq ();
132
  NoSerialInCTR = 0;
144
  NoSerialInCTR = 0;