/*
* misc.c
*
* Created on: 21 Sep 2016
* Author: Mike
*/
#include "stm32f1xx_hal.h"
#include "misc.h"
#include "main.h"
unsigned volatile long RPM_Time[RPM_SAMPLES]; // sampled on both edges
unsigned volatile long RPM_Count; // incremented every reading
// this is set if there is a timer timeout interrupt
unsigned char volatile periodPulse = 0;
unsigned char volatile tim3triggered = 0;
static void
triggerTim3(void)
{
htim3.Instance->CNT = 0;
htim3.Instance->CR1 |= TIM_CR1_CEN;
}
void TIM2_IRQHandler(void)
{
static char level = 0;
char valid = 0;
uint16_t high_count = 0;
uint16_t low_count = 0;
// rising edge CB pulse
if (__HAL_TIM_GET_FLAG(&htim2, TIM_FLAG_CC1))
{
__HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_CC1);
low_count = __HAL_TIM_GET_COMPARE(&htim2, TIM_CHANNEL_1);
valid = 1; // record we have a low_count val
// trigger timer some time after falling edge
if (tim3triggered == 0)
{
tim3triggered = 1;
triggerTim3();
}
}
// falling edge trigger CB pulse
if (__HAL_TIM_GET_FLAG(&htim2, TIM_FLAG_CC2))
{
__HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_CC2);
high_count = __HAL_TIM_GET_COMPARE(&htim2, TIM_CHANNEL_2);
valid |= 2;
// indicate that CB pulse is owning the timer 3 timing
periodPulse = 0;
}
switch (valid)
{
case 1:
// count width of a low period
RPM_Time[RPM_Count] = low_count;
RPM_Count = (RPM_Count + 1) % RPM_SAMPLES;
level = 0; // remember level
break;
case 2:
// count width of a high period
RPM_Time[RPM_Count] = high_count | RPM_FLAG;
RPM_Count = (RPM_Count + 1) % RPM_SAMPLES;
level = 1; // remember level
break;
case 3:
if (level == 1) // next level = 0 ,then 1 again
{
RPM_Time[RPM_Count] = low_count;
RPM_Count = (RPM_Count + 1) % RPM_SAMPLES;
RPM_Time[RPM_Count] = high_count | RPM_FLAG;
RPM_Count = (RPM_Count + 1) % RPM_SAMPLES;
}
else
{
RPM_Time[RPM_Count] = high_count | RPM_FLAG;
RPM_Count = (RPM_Count + 1) % RPM_SAMPLES;
RPM_Time[RPM_Count] = low_count;
RPM_Count = (RPM_Count + 1) % RPM_SAMPLES;
}
break;
default:
break;
}
}
// 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);
TimerFlag = 1;
if (NoSerialInCTR < 5)
{
NoSerialInCTR++;
if (NoSerialInCTR == 5)
{
NoSerialIn = 1;
}
}
if (periodPulse == 1)
{
triggerTim3();
}
// indicate that timer 4 firing is owning the timer 3 trigger
periodPulse = 1;
chtTimer++;
}
}
void resetSerialTimeout(void)
{
__disable_irq();
NoSerialInCTR = 0;
NoSerialIn = 0;
__enable_irq();
}