Subversion Repositories EngineBay2

Rev

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