Subversion Repositories EngineBay2

Rev

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