Subversion Repositories EngineBay2

Rev

Rev 42 | Rev 45 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 mjames 1
/*
2
 * misc.c
3
 *
4
 *  Created on: 21 Sep 2016
5
 *      Author: Mike
6
 */
37 mjames 7
#include "stm32f1xx_hal.h"
9 mjames 8
#include "misc.h"
9
#include "main.h"
3 mjames 10
 
28 mjames 11
unsigned volatile long RPM_Time[RPM_SAMPLES];  // sampled on both  edges
41 mjames 12
unsigned volatile char RPM_Level[RPM_SAMPLES]; // active level when sampled
28 mjames 13
unsigned volatile long RPM_Count; // incremented every reading
3 mjames 14
 
29 mjames 15
// this is set if there is a timer timeout interrupt
31 mjames 16
unsigned char volatile periodPulse = 0;
29 mjames 17
 
39 mjames 18
unsigned char volatile tim3triggered = 0;
19
 
31 mjames 20
static void
21
triggerTim3 (void)
29 mjames 22
{
41 mjames 23
      htim3.Instance->CNT = 0;
24
      htim3.Instance->CR1 |= TIM_CR1_CEN;
29 mjames 25
}
26
 
31 mjames 27
void
28
TIM2_IRQHandler (void)
8 mjames 29
{
41 mjames 30
// rising edge trigger CB pulse
31 mjames 31
  if (__HAL_TIM_GET_FLAG(&htim2, TIM_FLAG_CC1))
32
    {
33
      __HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_CC1);
34
      RPM_Time[RPM_Count] = __HAL_TIM_GET_COMPARE(&htim2, TIM_CHANNEL_1);
41 mjames 35
      RPM_Level[RPM_Count] = 1; // rising so it is high now.
31 mjames 36
      RPM_Count = (RPM_Count + 1) % RPM_SAMPLES;
41 mjames 37
      // trigger timer some time after falling edge
38
      if(tim3triggered ==0 )
31 mjames 39
 
9 mjames 40
        {
41 mjames 41
          tim3triggered = 1;
42
          triggerTim3 ();
9 mjames 43
        }
41 mjames 44
 
31 mjames 45
    }
41 mjames 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;
55
    }
56
 
8 mjames 57
}
58
 
37 mjames 59
// timer variable shared between TIM3 and TIM4 handler.
60
static char chtTimer = 0;
8 mjames 61
 
31 mjames 62
void
63
TIM3_IRQHandler (void)
64
{
65
  if (__HAL_TIM_GET_FLAG(&htim3, TIM_FLAG_UPDATE))
66
    {
67
      __HAL_TIM_CLEAR_FLAG(&htim3, TIM_FLAG_UPDATE);
6 mjames 68
 
41 mjames 69
 
39 mjames 70
      tim3triggered = 0;
31 mjames 71
      if (chtTimer >= 3)  // every 300mS
72
        {
73
          chtTimer = 0;
6 mjames 74
 
42 mjames 75
          resetTempCS();
76
          for (int instance = 0; instance < NUM_SPI_TEMP_SENS; instance++)
31 mjames 77
            {
78
              uint8_t buffer[2];
6 mjames 79
 
44 mjames 80
              nextTempCS();
31 mjames 81
              HAL_SPI_Receive (&hspi1, buffer, 2, 2);
6 mjames 82
 
83
 
31 mjames 84
              uint16_t obs = (buffer[0] << 8) | buffer[1];
6 mjames 85
 
31 mjames 86
              // good observation if the status bit is clear, and the reading is less than 1023
6 mjames 87
 
31 mjames 88
              uint16_t temp_c = obs >> 5;
6 mjames 89
 
31 mjames 90
              uint8_t good = ((obs & 7) == 0) && (temp_c > 0) && (temp_c < 250);
6 mjames 91
 
31 mjames 92
              if (good)
93
                {
42 mjames 94
                  Temp_Observations[instance] = temp_c;
6 mjames 95
                }
31 mjames 96
            }
42 mjames 97
          nextTempCS(); // clock CS one more time to deselect all chips
6 mjames 98
        }
31 mjames 99
    }
6 mjames 100
}
101
 
37 mjames 102
// 100mS periodic sampler handler
31 mjames 103
void
37 mjames 104
TIM4_IRQHandler (void)
105
{
106
  static char blink = 0;
107
  if (__HAL_TIM_GET_FLAG(&htim4, TIM_FLAG_UPDATE))
108
    {
109
      __HAL_TIM_CLEAR_FLAG(&htim4, TIM_FLAG_UPDATE);
110
 
111
      blink = !blink;
112
      HAL_GPIO_WritePin (LED_Blink_GPIO_Port, LED_Blink_Pin,
113
                         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
        {
41 mjames 127
          triggerTim3 ();
37 mjames 128
 
129
        }
41 mjames 130
      // indicate that timer 4 firing is owning the timer 3 trigger
37 mjames 131
      periodPulse = 1;
132
 
133
      chtTimer++;
134
    }
135
 
136
}
137
 
138
void
31 mjames 139
resetSerialTimeout (void)
140
{
141
  __disable_irq ();
142
  NoSerialInCTR = 0;
143
  NoSerialIn = 0;
144
  __enable_irq ();
6 mjames 145
}
146