Subversion Repositories EngineBay2

Rev

Rev 52 | 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
 
29 mjames 11
// this is set if there is a timer timeout interrupt
31 mjames 12
unsigned char volatile periodPulse = 0;
29 mjames 13
 
55 mjames 14
// this is set when timer 3 was triggerd
39 mjames 15
unsigned char volatile tim3triggered = 0;
16
 
55 mjames 17
// this is exported
18
void triggerTim3(void)
29 mjames 19
{
45 mjames 20
  htim3.Instance->CNT = 0;
21
  htim3.Instance->CR1 |= TIM_CR1_CEN;
29 mjames 22
}
23
 
37 mjames 24
// timer variable shared between TIM3 and TIM4 handler.
25
static char chtTimer = 0;
8 mjames 26
 
45 mjames 27
void TIM3_IRQHandler(void)
31 mjames 28
{
29
  if (__HAL_TIM_GET_FLAG(&htim3, TIM_FLAG_UPDATE))
45 mjames 30
  {
31
    __HAL_TIM_CLEAR_FLAG(&htim3, TIM_FLAG_UPDATE);
32
 
33
    tim3triggered = 0;
34
    if (chtTimer >= 3) // every 300mS
31 mjames 35
    {
45 mjames 36
      chtTimer = 0;
6 mjames 37
 
45 mjames 38
      resetTempCS();
39
      for (int instance = 0; instance < NUM_SPI_TEMP_SENS; instance++)
40
      {
41
        uint8_t buffer[2];
41 mjames 42
 
45 mjames 43
        nextTempCS();
44
        HAL_SPI_Receive(&hspi1, buffer, 2, 2);
6 mjames 45
 
45 mjames 46
        uint16_t obs = (buffer[0] << 8) | buffer[1];
6 mjames 47
 
45 mjames 48
        // good observation if the status bit is clear, and the reading is less than 1023
6 mjames 49
 
45 mjames 50
        uint16_t temp_c = obs >> 5;
6 mjames 51
 
55 mjames 52
        if (((obs & 7) == 0) && (temp_c > 0) && (temp_c < 250))
53
          AddTempReading(temp_c, instance);
45 mjames 54
      }
55
      nextTempCS(); // clock CS one more time to deselect all chips
31 mjames 56
    }
45 mjames 57
  }
6 mjames 58
}
59
 
37 mjames 60
// 100mS periodic sampler handler
45 mjames 61
void TIM4_IRQHandler(void)
37 mjames 62
{
63
  static char blink = 0;
64
  if (__HAL_TIM_GET_FLAG(&htim4, TIM_FLAG_UPDATE))
45 mjames 65
  {
66
    __HAL_TIM_CLEAR_FLAG(&htim4, TIM_FLAG_UPDATE);
37 mjames 67
 
45 mjames 68
    blink = !blink;
69
    HAL_GPIO_WritePin(LED_Blink_GPIO_Port, LED_Blink_Pin,
70
                      blink ? GPIO_PIN_SET : GPIO_PIN_RESET);
37 mjames 71
 
45 mjames 72
    if (periodPulse == 1)
73
    {
74
      triggerTim3();
37 mjames 75
    }
45 mjames 76
    // indicate that timer 4 firing is owning the timer 3 trigger
77
    periodPulse = 1;
37 mjames 78
 
45 mjames 79
    chtTimer++;
80
  }
37 mjames 81
}