/*
* misc.c
*
* Created on: 21 Sep 2016
* Author: Mike
*/
#include "stm32f1xx_hal.h"
#include "misc.h"
#include "main.h"
// this is set if there is a timer timeout interrupt
unsigned char volatile periodPulse = 0;
// this is set when timer 3 was triggerd
unsigned char volatile tim3triggered = 0;
// this is exported
void
triggerTim3(void)
{
htim3.Instance->CNT = 0;
htim3.Instance->CR1 |= TIM_CR1_CEN;
}
// timer variable shared between TIM3 and TIM4 handler.
static char chtTimer = 0;
void TIM3_IRQHandler(void)
{
if (__HAL_TIM_GET_FLAG(&htim3, TIM_FLAG_UPDATE))
{
__HAL_TIM_CLEAR_FLAG(&htim3, TIM_FLAG_UPDATE);
tim3triggered = 0;
if (chtTimer >= 3) // every 300mS
{
chtTimer = 0;
resetTempCS();
for (int instance = 0; instance < NUM_SPI_TEMP_SENS; instance++)
{
uint8_t buffer[2];
nextTempCS();
HAL_SPI_Receive(&hspi1, buffer, 2, 2);
uint16_t obs = (buffer[0] << 8) | buffer[1];
// good observation if the status bit is clear, and the reading is less than 1023
uint16_t temp_c = obs >> 5;
uint8_t good = ((obs & 7) == 0) && (temp_c > 0) && (temp_c < 250);
if (good)
{
Temp_Observations[instance] = temp_c;
}
}
nextTempCS(); // clock CS one more time to deselect all chips
}
}
}
// 100mS periodic sampler handler
void TIM4_IRQHandler(void)
{
static char blink = 0;
if (__HAL_TIM_GET_FLAG(&htim4, TIM_FLAG_UPDATE))
{
__HAL_TIM_CLEAR_FLAG(&htim4, TIM_FLAG_UPDATE);
blink = !blink;
HAL_GPIO_WritePin(LED_Blink_GPIO_Port, LED_Blink_Pin,
blink ? GPIO_PIN_SET : GPIO_PIN_RESET);
if (periodPulse == 1)
{
triggerTim3();
}
// indicate that timer 4 firing is owning the timer 3 trigger
periodPulse = 1;
chtTimer++;
}
}