Subversion Repositories EngineBay2

Rev

Rev 39 | Rev 42 | 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
  28. TIM2_IRQHandler (void)
  29. {
  30. // rising edge trigger CB pulse
  31.   if (__HAL_TIM_GET_FLAG(&htim2, TIM_FLAG_CC1))
  32.     {
  33.       __HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_CC1);
  34.       RPM_Time[RPM_Count] = __HAL_TIM_GET_COMPARE(&htim2, TIM_CHANNEL_1);
  35.       RPM_Level[RPM_Count] = 1; // rising so it is high now.
  36.       RPM_Count = (RPM_Count + 1) % RPM_SAMPLES;
  37.       // trigger timer some time after falling edge
  38.       if(tim3triggered ==0 )
  39.  
  40.         {
  41.           tim3triggered = 1;
  42.           triggerTim3 ();
  43.         }
  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.       RPM_Time[RPM_Count] = __HAL_TIM_GET_COMPARE(&htim2, TIM_CHANNEL_2);
  51.       RPM_Level[RPM_Count] = 0; // falling so it is low now.
  52.       RPM_Count = (RPM_Count + 1) % RPM_SAMPLES;
  53.       // indicate that CB pulse is owning the timer 3 timing
  54.       periodPulse = 0;
  55.     }
  56.  
  57. }
  58.  
  59. // timer variable shared between TIM3 and TIM4 handler.
  60. static char chtTimer = 0;
  61.  
  62. void
  63. TIM3_IRQHandler (void)
  64. {
  65.   if (__HAL_TIM_GET_FLAG(&htim3, TIM_FLAG_UPDATE))
  66.     {
  67.       __HAL_TIM_CLEAR_FLAG(&htim3, TIM_FLAG_UPDATE);
  68.  
  69.  
  70.       tim3triggered = 0;
  71.       if (chtTimer >= 3)  // every 300mS
  72.         {
  73.           chtTimer = 0;
  74.  
  75.           for (int instance = 0; instance < 2; instance++)
  76.             {
  77.               uint8_t buffer[2];
  78.               uint16_t Pin =
  79.                   (instance == 0) ? SPI_NS_Temp_Pin : SPI_NS_Temp2_Pin;
  80.  
  81.               HAL_GPIO_WritePin (SPI_NS_Temp_GPIO_Port, Pin, GPIO_PIN_RESET);
  82.  
  83.               HAL_SPI_Receive (&hspi1, buffer, 2, 2);
  84.  
  85.               HAL_GPIO_WritePin (SPI_NS_Temp_GPIO_Port, Pin, GPIO_PIN_SET);
  86.  
  87.               uint16_t obs = (buffer[0] << 8) | buffer[1];
  88.  
  89.               // good observation if the status bit is clear, and the reading is less than 1023
  90.  
  91.               uint16_t temp_c = obs >> 5;
  92.  
  93.               uint8_t good = ((obs & 7) == 0) && (temp_c > 0) && (temp_c < 250);
  94.  
  95.               if (good)
  96.                 {
  97.                   CHT_Observations[instance] = temp_c;
  98.                 }
  99.             }
  100.         }
  101.     }
  102. }
  103.  
  104. // 100mS periodic sampler handler
  105. void
  106. TIM4_IRQHandler (void)
  107. {
  108.   static char blink = 0;
  109.   if (__HAL_TIM_GET_FLAG(&htim4, TIM_FLAG_UPDATE))
  110.     {
  111.       __HAL_TIM_CLEAR_FLAG(&htim4, TIM_FLAG_UPDATE);
  112.  
  113.       blink = !blink;
  114.       HAL_GPIO_WritePin (LED_Blink_GPIO_Port, LED_Blink_Pin,
  115.                          blink ? GPIO_PIN_SET : GPIO_PIN_RESET);
  116.  
  117.       TimerFlag = 1;
  118.       if (NoSerialInCTR < 5)
  119.         {
  120.           NoSerialInCTR++;
  121.           if (NoSerialInCTR == 5)
  122.             {
  123.               NoSerialIn = 1;
  124.             }
  125.         }
  126.  
  127.       if (periodPulse == 1)
  128.         {
  129.           triggerTim3 ();
  130.  
  131.         }
  132.       // indicate that timer 4 firing is owning the timer 3 trigger
  133.       periodPulse = 1;
  134.  
  135.       chtTimer++;
  136.     }
  137.  
  138. }
  139.  
  140. void
  141. resetSerialTimeout (void)
  142. {
  143.   __disable_irq ();
  144.   NoSerialInCTR = 0;
  145.   NoSerialIn = 0;
  146.   __enable_irq ();
  147. }
  148.  
  149.