Subversion Repositories EngineBay2

Rev

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

Rev 44 Rev 45
Line 8... Line 8...
8
#include "misc.h"
8
#include "misc.h"
9
#include "main.h"
9
#include "main.h"
10
 
10
 
11
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
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
}
25
}
26
 
26
 
27
void
-
 
28
TIM2_IRQHandler (void)
27
void TIM2_IRQHandler(void)
29
{
28
{
30
// rising edge trigger CB pulse
29
  // rising edge trigger CB pulse
31
  if (__HAL_TIM_GET_FLAG(&htim2, TIM_FLAG_CC1))
30
  if (__HAL_TIM_GET_FLAG(&htim2, TIM_FLAG_CC1))
32
    {
31
  {
33
      __HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_CC1);
32
    __HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_CC1);
34
      RPM_Time[RPM_Count] = __HAL_TIM_GET_COMPARE(&htim2, TIM_CHANNEL_1);
33
    RPM_Time[RPM_Count] = __HAL_TIM_GET_COMPARE(&htim2, TIM_CHANNEL_1);
35
      RPM_Level[RPM_Count] = 1; // rising so it is high now.
34
    RPM_Level[RPM_Count] = 1; // rising so it is high now.
36
      RPM_Count = (RPM_Count + 1) % RPM_SAMPLES;
35
    RPM_Count = (RPM_Count + 1) % RPM_SAMPLES;
37
      // trigger timer some time after falling edge
36
    // trigger timer some time after falling edge
38
      if(tim3triggered ==0 )
37
    if (tim3triggered == 0)
39
 
-
 
40
        {
-
 
41
          tim3triggered = 1;
-
 
42
          triggerTim3 ();
-
 
43
        }
-
 
44
 
38
 
45
    }
-
 
46
// falling edge trigger CB pulse
-
 
47
  if (__HAL_TIM_GET_FLAG(&htim2, TIM_FLAG_CC2))
-
 
48
    {
39
    {
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;
40
      tim3triggered = 1;
53
      // indicate that CB pulse is owning the timer 3 timing
-
 
54
      periodPulse = 0;
41
      triggerTim3();
55
    }
42
    }
-
 
43
  }
-
 
44
  // falling edge trigger CB pulse
-
 
45
  if (__HAL_TIM_GET_FLAG(&htim2, TIM_FLAG_CC2))
56
 
46
  {
-
 
47
    __HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_CC2);
-
 
48
    RPM_Time[RPM_Count] = __HAL_TIM_GET_COMPARE(&htim2, TIM_CHANNEL_2);
-
 
49
    RPM_Level[RPM_Count] = 0; // falling so it is low now.
-
 
50
    RPM_Count = (RPM_Count + 1) % RPM_SAMPLES;
-
 
51
    // indicate that CB pulse is owning the timer 3 timing
-
 
52
    periodPulse = 0;
-
 
53
  }
57
}
54
}
58
 
55
 
59
// timer variable shared between TIM3 and TIM4 handler.
56
// timer variable shared between TIM3 and TIM4 handler.
60
static char chtTimer = 0;
57
static char chtTimer = 0;
61
 
58
 
62
void
-
 
63
TIM3_IRQHandler (void)
59
void TIM3_IRQHandler(void)
64
{
60
{
65
  if (__HAL_TIM_GET_FLAG(&htim3, TIM_FLAG_UPDATE))
61
  if (__HAL_TIM_GET_FLAG(&htim3, TIM_FLAG_UPDATE))
66
    {
62
  {
67
      __HAL_TIM_CLEAR_FLAG(&htim3, TIM_FLAG_UPDATE);
63
    __HAL_TIM_CLEAR_FLAG(&htim3, TIM_FLAG_UPDATE);
68
 
-
 
69
 
-
 
70
      tim3triggered = 0;
-
 
71
      if (chtTimer >= 3)  // every 300mS
-
 
72
        {
-
 
73
          chtTimer = 0;
-
 
74
 
64
 
75
          resetTempCS();
65
    tim3triggered = 0;
76
          for (int instance = 0; instance < NUM_SPI_TEMP_SENS; instance++)
66
    if (chtTimer >= 3) // every 300mS
77
            {
67
    {
78
              uint8_t buffer[2];
68
      chtTimer = 0;
79
 
69
 
80
              nextTempCS();
70
      resetTempCS();
-
 
71
      for (int instance = 0; instance < NUM_SPI_TEMP_SENS; instance++)
-
 
72
      {
81
              HAL_SPI_Receive (&hspi1, buffer, 2, 2);
73
        uint8_t buffer[2];
82
 
74
 
-
 
75
        nextTempCS();
-
 
76
        HAL_SPI_Receive(&hspi1, buffer, 2, 2);
83
 
77
 
84
              uint16_t obs = (buffer[0] << 8) | buffer[1];
78
        uint16_t obs = (buffer[0] << 8) | buffer[1];
85
 
79
 
86
              // good observation if the status bit is clear, and the reading is less than 1023
80
        // good observation if the status bit is clear, and the reading is less than 1023
87
 
81
 
88
              uint16_t temp_c = obs >> 5;
82
        uint16_t temp_c = obs >> 5;
89
 
83
 
90
              uint8_t good = ((obs & 7) == 0) && (temp_c > 0) && (temp_c < 250);
84
        uint8_t good = ((obs & 7) == 0) && (temp_c > 0) && (temp_c < 250);
91
 
85
 
92
              if (good)
86
        if (good)
93
                {
87
        {
94
                  Temp_Observations[instance] = temp_c;
88
          Temp_Observations[instance] = temp_c;
95
                }
89
        }
96
            }
90
      }
97
          nextTempCS(); // clock CS one more time to deselect all chips
91
      nextTempCS(); // clock CS one more time to deselect all chips
98
        }
-
 
99
    }
92
    }
-
 
93
  }
100
}
94
}
101
 
95
 
102
// 100mS periodic sampler handler
96
// 100mS periodic sampler handler
103
void
-
 
104
TIM4_IRQHandler (void)
97
void TIM4_IRQHandler(void)
105
{
98
{
106
  static char blink = 0;
99
  static char blink = 0;
107
  if (__HAL_TIM_GET_FLAG(&htim4, TIM_FLAG_UPDATE))
100
  if (__HAL_TIM_GET_FLAG(&htim4, TIM_FLAG_UPDATE))
108
    {
101
  {
109
      __HAL_TIM_CLEAR_FLAG(&htim4, TIM_FLAG_UPDATE);
102
    __HAL_TIM_CLEAR_FLAG(&htim4, TIM_FLAG_UPDATE);
110
 
103
 
111
      blink = !blink;
104
    blink = !blink;
112
      HAL_GPIO_WritePin (LED_Blink_GPIO_Port, LED_Blink_Pin,
105
    HAL_GPIO_WritePin(LED_Blink_GPIO_Port, LED_Blink_Pin,
113
                         blink ? GPIO_PIN_SET : GPIO_PIN_RESET);
106
                      blink ? GPIO_PIN_SET : GPIO_PIN_RESET);
114
 
-
 
115
      TimerFlag = 1;
-
 
116
      if (NoSerialInCTR < 5)
-
 
117
        {
-
 
118
          NoSerialInCTR++;
-
 
119
          if (NoSerialInCTR == 5)
-
 
120
            {
-
 
121
              NoSerialIn = 1;
-
 
122
            }
-
 
123
        }
-
 
124
 
-
 
125
      if (periodPulse == 1)
-
 
126
        {
-
 
127
          triggerTim3 ();
-
 
128
 
-
 
129
        }
-
 
130
      // indicate that timer 4 firing is owning the timer 3 trigger
-
 
131
      periodPulse = 1;
-
 
132
 
107
 
-
 
108
    TimerFlag = 1;
-
 
109
    if (NoSerialInCTR < 5)
-
 
110
    {
133
      chtTimer++;
111
      NoSerialInCTR++;
-
 
112
      if (NoSerialInCTR == 5)
-
 
113
      {
-
 
114
        NoSerialIn = 1;
-
 
115
      }
134
    }
116
    }
135
 
117
 
-
 
118
    if (periodPulse == 1)
-
 
119
    {
-
 
120
      triggerTim3();
-
 
121
    }
-
 
122
    // indicate that timer 4 firing is owning the timer 3 trigger
-
 
123
    periodPulse = 1;
-
 
124
 
-
 
125
    chtTimer++;
-
 
126
  }
136
}
127
}
137
 
128
 
138
void
-
 
139
resetSerialTimeout (void)
129
void resetSerialTimeout(void)
140
{
130
{
141
  __disable_irq ();
131
  __disable_irq();
142
  NoSerialInCTR = 0;
132
  NoSerialInCTR = 0;
143
  NoSerialIn = 0;
133
  NoSerialIn = 0;
144
  __enable_irq ();
134
  __enable_irq();
145
}
135
}
146
 
-