Subversion Repositories EngineBay2

Rev

Rev 45 | 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 long RPM_Count;             // incremented every reading
  13.  
  14. // this is set if there is a timer timeout interrupt
  15. unsigned char volatile periodPulse = 0;
  16.  
  17. unsigned char volatile tim3triggered = 0;
  18.  
  19. static void
  20. triggerTim3(void)
  21. {
  22.   htim3.Instance->CNT = 0;
  23.   htim3.Instance->CR1 |= TIM_CR1_CEN;
  24. }
  25.  
  26. void TIM2_IRQHandler(void)
  27. {
  28.   static char level = 0;
  29.   char valid = 0;
  30.   uint16_t high_count = 0;
  31.   uint16_t low_count = 0;
  32.   // rising edge CB pulse
  33.   if (__HAL_TIM_GET_FLAG(&htim2, TIM_FLAG_CC1))
  34.   {
  35.     __HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_CC1);
  36.     low_count = __HAL_TIM_GET_COMPARE(&htim2, TIM_CHANNEL_1);
  37.     valid = 1; // record we have a low_count val
  38.  
  39.     // trigger timer some time after falling edge
  40.     if (tim3triggered == 0)
  41.     {
  42.       tim3triggered = 1;
  43.       triggerTim3();
  44.     }
  45.   }
  46.   // falling edge trigger CB pulse
  47.   if (__HAL_TIM_GET_FLAG(&htim2, TIM_FLAG_CC2))
  48.   {
  49.     __HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_CC2);
  50.  
  51.     high_count = __HAL_TIM_GET_COMPARE(&htim2, TIM_CHANNEL_2);
  52.     valid |= 2;
  53.     // indicate that CB pulse is owning the timer 3 timing
  54.     periodPulse = 0;
  55.   }
  56.  
  57.   switch (valid)
  58.   {
  59.   case 1:
  60.     // count width of a low period
  61.     RPM_Time[RPM_Count] = low_count;
  62.     RPM_Count = (RPM_Count + 1) % RPM_SAMPLES;
  63.     level = 0; // remember level
  64.     break;
  65.   case 2:
  66.     // count width of a high period
  67.     RPM_Time[RPM_Count] = high_count | RPM_FLAG;
  68.     RPM_Count = (RPM_Count + 1) % RPM_SAMPLES;
  69.     level = 1; // remember level
  70.     break;
  71.   case 3:
  72.     if (level == 1) // next level = 0 ,then 1 again
  73.     {
  74.       RPM_Time[RPM_Count] = low_count;
  75.       RPM_Count = (RPM_Count + 1) % RPM_SAMPLES;
  76.       RPM_Time[RPM_Count] = high_count | RPM_FLAG;
  77.       RPM_Count = (RPM_Count + 1) % RPM_SAMPLES;
  78.     }
  79.     else
  80.     {
  81.       RPM_Time[RPM_Count] = high_count | RPM_FLAG;
  82.       RPM_Count = (RPM_Count + 1) % RPM_SAMPLES;
  83.       RPM_Time[RPM_Count] = low_count;
  84.       RPM_Count = (RPM_Count + 1) % RPM_SAMPLES;
  85.     }
  86.     break;
  87.   default:
  88.     break;
  89.   }
  90. }
  91.  
  92. // timer variable shared between TIM3 and TIM4 handler.
  93. static char chtTimer = 0;
  94.  
  95. void TIM3_IRQHandler(void)
  96. {
  97.   if (__HAL_TIM_GET_FLAG(&htim3, TIM_FLAG_UPDATE))
  98.   {
  99.     __HAL_TIM_CLEAR_FLAG(&htim3, TIM_FLAG_UPDATE);
  100.  
  101.     tim3triggered = 0;
  102.     if (chtTimer >= 3) // every 300mS
  103.     {
  104.       chtTimer = 0;
  105.  
  106.       resetTempCS();
  107.       for (int instance = 0; instance < NUM_SPI_TEMP_SENS; instance++)
  108.       {
  109.         uint8_t buffer[2];
  110.  
  111.         nextTempCS();
  112.         HAL_SPI_Receive(&hspi1, buffer, 2, 2);
  113.  
  114.         uint16_t obs = (buffer[0] << 8) | buffer[1];
  115.  
  116.         // good observation if the status bit is clear, and the reading is less than 1023
  117.  
  118.         uint16_t temp_c = obs >> 5;
  119.  
  120.         uint8_t good = ((obs & 7) == 0) && (temp_c > 0) && (temp_c < 250);
  121.  
  122.         if (good)
  123.         {
  124.           Temp_Observations[instance] = temp_c;
  125.         }
  126.       }
  127.       nextTempCS(); // clock CS one more time to deselect all chips
  128.     }
  129.   }
  130. }
  131.  
  132. // 100mS periodic sampler handler
  133. void TIM4_IRQHandler(void)
  134. {
  135.   static char blink = 0;
  136.   if (__HAL_TIM_GET_FLAG(&htim4, TIM_FLAG_UPDATE))
  137.   {
  138.     __HAL_TIM_CLEAR_FLAG(&htim4, TIM_FLAG_UPDATE);
  139.  
  140.     blink = !blink;
  141.     HAL_GPIO_WritePin(LED_Blink_GPIO_Port, LED_Blink_Pin,
  142.                       blink ? GPIO_PIN_SET : GPIO_PIN_RESET);
  143.  
  144.     TimerFlag = 1;
  145.     if (NoSerialInCTR < 5)
  146.     {
  147.       NoSerialInCTR++;
  148.       if (NoSerialInCTR == 5)
  149.       {
  150.         NoSerialIn = 1;
  151.       }
  152.     }
  153.  
  154.     if (periodPulse == 1)
  155.     {
  156.       triggerTim3();
  157.     }
  158.     // indicate that timer 4 firing is owning the timer 3 trigger
  159.     periodPulse = 1;
  160.  
  161.     chtTimer++;
  162.   }
  163. }
  164.  
  165. void resetSerialTimeout(void)
  166. {
  167.   __disable_irq();
  168.   NoSerialInCTR = 0;
  169.   NoSerialIn = 0;
  170.   __enable_irq();
  171. }
  172.