Subversion Repositories EngineBay2

Rev

Rev 36 | Rev 39 | 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
 
9 mjames 11
 
28 mjames 12
unsigned volatile long RPM_Time[RPM_SAMPLES];  // sampled on both  edges
13
unsigned volatile char RPM_Level[RPM_SAMPLES]; // active level when sampled
14
unsigned volatile long RPM_Count; // incremented every reading
3 mjames 15
 
29 mjames 16
// this is set if there is a timer timeout interrupt
31 mjames 17
unsigned char volatile periodPulse = 0;
29 mjames 18
 
31 mjames 19
static void
20
triggerTim3 (void)
29 mjames 21
{
31 mjames 22
  htim3.Instance->CNT = 0;
23
  htim3.Instance->CR1 |= TIM_CR1_CEN;
29 mjames 24
 
25
}
26
 
31 mjames 27
void
28
TIM2_IRQHandler (void)
8 mjames 29
{
31 mjames 30
  if (__HAL_TIM_GET_FLAG(&htim2, TIM_FLAG_CC1))
31
    {
32
      __HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_CC1);
33
      RPM_Time[RPM_Count] = __HAL_TIM_GET_COMPARE(&htim2, TIM_CHANNEL_1);
34
      int CB_level = HAL_GPIO_ReadPin (CB_Pulse_GPIO_Port,
35
                                       CB_Pulse_Pin);
36
      RPM_Level[RPM_Count] = CB_level;
37
      RPM_Count = (RPM_Count + 1) % RPM_SAMPLES;
38
 
39
      if ( CB_level == 0)
9 mjames 40
        {
31 mjames 41
          periodPulse = 0;
42
          triggerTim3 ();
9 mjames 43
        }
31 mjames 44
    }
8 mjames 45
}
46
 
47
 
37 mjames 48
// timer variable shared between TIM3 and TIM4 handler.
49
static char chtTimer = 0;
8 mjames 50
 
31 mjames 51
void
52
TIM3_IRQHandler (void)
53
{
54
  if (__HAL_TIM_GET_FLAG(&htim3, TIM_FLAG_UPDATE))
55
    {
56
      __HAL_TIM_CLEAR_FLAG(&htim3, TIM_FLAG_UPDATE);
6 mjames 57
 
31 mjames 58
      if (chtTimer >= 3)  // every 300mS
59
        {
60
          chtTimer = 0;
6 mjames 61
 
31 mjames 62
          for (int instance = 0; instance < 2; instance++)
63
            {
64
              uint8_t buffer[2];
65
              uint16_t Pin =
66
                  (instance == 0) ? SPI_NS_Temp_Pin : SPI_NS_Temp2_Pin;
6 mjames 67
 
31 mjames 68
              HAL_GPIO_WritePin (SPI_NS_Temp_GPIO_Port, Pin, GPIO_PIN_RESET);
6 mjames 69
 
31 mjames 70
              HAL_SPI_Receive (&hspi1, buffer, 2, 2);
6 mjames 71
 
31 mjames 72
              HAL_GPIO_WritePin (SPI_NS_Temp_GPIO_Port, Pin, GPIO_PIN_SET);
6 mjames 73
 
31 mjames 74
              uint16_t obs = (buffer[0] << 8) | buffer[1];
6 mjames 75
 
31 mjames 76
              // good observation if the status bit is clear, and the reading is less than 1023
6 mjames 77
 
31 mjames 78
              uint16_t temp_c = obs >> 5;
6 mjames 79
 
31 mjames 80
              uint8_t good = ((obs & 7) == 0) && (temp_c > 0) && (temp_c < 250);
6 mjames 81
 
31 mjames 82
              if (good)
83
                {
84
                  CHT_Observations[instance] = temp_c;
6 mjames 85
                }
31 mjames 86
            }
6 mjames 87
        }
31 mjames 88
    }
6 mjames 89
}
90
 
37 mjames 91
// 100mS periodic sampler handler
31 mjames 92
void
37 mjames 93
TIM4_IRQHandler (void)
94
{
95
  static char blink = 0;
96
  if (__HAL_TIM_GET_FLAG(&htim4, TIM_FLAG_UPDATE))
97
    {
98
      __HAL_TIM_CLEAR_FLAG(&htim4, TIM_FLAG_UPDATE);
99
 
100
      blink = !blink;
101
      HAL_GPIO_WritePin (LED_Blink_GPIO_Port, LED_Blink_Pin,
102
                         blink ? GPIO_PIN_SET : GPIO_PIN_RESET);
103
 
104
      TimerFlag = 1;
105
      if (NoSerialInCTR < 5)
106
        {
107
          NoSerialInCTR++;
108
          if (NoSerialInCTR == 5)
109
            {
110
              NoSerialIn = 1;
111
            }
112
        }
113
 
114
      if (periodPulse == 1)
115
        {
116
        triggerTim3 ();
117
 
118
        }
119
      periodPulse = 1;
120
 
121
 
122
      chtTimer++;
123
    }
124
 
125
}
126
 
127
 
128
void
31 mjames 129
resetSerialTimeout (void)
130
{
131
  __disable_irq ();
132
  NoSerialInCTR = 0;
133
  NoSerialIn = 0;
134
  __enable_irq ();
6 mjames 135
}
136