Subversion Repositories EngineBay2

Rev

Rev 44 | Rev 48 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * misc.c
  3.  *
  4.  *  Created on: 21 Sep 2016
  5.  *      Author: Mike
  6.  */
  7. #include "stm32f1xx_hal.h"
  8. #include "misc.h"
  9. #include "main.h"
  10.  
  11. unsigned volatile long RPM_Time[RPM_SAMPLES];  // sampled on both  edges
  12. unsigned volatile char RPM_Level[RPM_SAMPLES]; // active level when sampled
  13. unsigned volatile long RPM_Count;              // incremented every reading
  14.  
  15. // this is set if there is a timer timeout interrupt
  16. unsigned char volatile periodPulse = 0;
  17.  
  18. unsigned char volatile tim3triggered = 0;
  19.  
  20. static void
  21. triggerTim3(void)
  22. {
  23.   htim3.Instance->CNT = 0;
  24.   htim3.Instance->CR1 |= TIM_CR1_CEN;
  25. }
  26.  
  27. void TIM2_IRQHandler(void)
  28. {
  29.   // rising edge trigger CB pulse
  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.     RPM_Level[RPM_Count] = 1; // rising so it is high now.
  35.     RPM_Count = (RPM_Count + 1) % RPM_SAMPLES;
  36.     // trigger timer some time after falling edge
  37.     if (tim3triggered == 0)
  38.  
  39.     {
  40.       tim3triggered = 1;
  41.       triggerTim3();
  42.     }
  43.   }
  44.   // falling edge trigger CB pulse
  45.   if (__HAL_TIM_GET_FLAG(&htim2, TIM_FLAG_CC2))
  46.   {
  47.     __HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_CC2);
  48.     RPM_Time[RPM_Count] = __HAL_TIM_GET_COMPARE(&htim2, TIM_CHANNEL_2);
  49.     RPM_Level[RPM_Count] = 0; // falling so it is low now.
  50.     RPM_Count = (RPM_Count + 1) % RPM_SAMPLES;
  51.     // indicate that CB pulse is owning the timer 3 timing
  52.     periodPulse = 0;
  53.   }
  54. }
  55.  
  56. // timer variable shared between TIM3 and TIM4 handler.
  57. static char chtTimer = 0;
  58.  
  59. void TIM3_IRQHandler(void)
  60. {
  61.   if (__HAL_TIM_GET_FLAG(&htim3, TIM_FLAG_UPDATE))
  62.   {
  63.     __HAL_TIM_CLEAR_FLAG(&htim3, TIM_FLAG_UPDATE);
  64.  
  65.     tim3triggered = 0;
  66.     if (chtTimer >= 3) // every 300mS
  67.     {
  68.       chtTimer = 0;
  69.  
  70.       resetTempCS();
  71.       for (int instance = 0; instance < NUM_SPI_TEMP_SENS; instance++)
  72.       {
  73.         uint8_t buffer[2];
  74.  
  75.         nextTempCS();
  76.         HAL_SPI_Receive(&hspi1, buffer, 2, 2);
  77.  
  78.         uint16_t obs = (buffer[0] << 8) | buffer[1];
  79.  
  80.         // good observation if the status bit is clear, and the reading is less than 1023
  81.  
  82.         uint16_t temp_c = obs >> 5;
  83.  
  84.         uint8_t good = ((obs & 7) == 0) && (temp_c > 0) && (temp_c < 250);
  85.  
  86.         if (good)
  87.         {
  88.           Temp_Observations[instance] = temp_c;
  89.         }
  90.       }
  91.       nextTempCS(); // clock CS one more time to deselect all chips
  92.     }
  93.   }
  94. }
  95.  
  96. // 100mS periodic sampler handler
  97. void TIM4_IRQHandler(void)
  98. {
  99.   static char blink = 0;
  100.   if (__HAL_TIM_GET_FLAG(&htim4, TIM_FLAG_UPDATE))
  101.   {
  102.     __HAL_TIM_CLEAR_FLAG(&htim4, TIM_FLAG_UPDATE);
  103.  
  104.     blink = !blink;
  105.     HAL_GPIO_WritePin(LED_Blink_GPIO_Port, LED_Blink_Pin,
  106.                       blink ? GPIO_PIN_SET : GPIO_PIN_RESET);
  107.  
  108.     TimerFlag = 1;
  109.     if (NoSerialInCTR < 5)
  110.     {
  111.       NoSerialInCTR++;
  112.       if (NoSerialInCTR == 5)
  113.       {
  114.         NoSerialIn = 1;
  115.       }
  116.     }
  117.  
  118.     if (periodPulse == 1)
  119.     {
  120.       triggerTim3();
  121.     }
  122.     // indicate that timer 4 firing is owning the timer 3 trigger
  123.     periodPulse = 1;
  124.  
  125.     chtTimer++;
  126.   }
  127. }
  128.  
  129. void resetSerialTimeout(void)
  130. {
  131.   __disable_irq();
  132.   NoSerialInCTR = 0;
  133.   NoSerialIn = 0;
  134.   __enable_irq();
  135. }
  136.