Subversion Repositories EngineBay2

Rev

Rev 36 | Rev 41 | 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.  
  12. unsigned volatile long RPM_Time[RPM_SAMPLES];  // sampled on both  edges
  13. unsigned volatile char RPM_Level[RPM_SAMPLES]; // active level when sampled
  14. unsigned volatile long RPM_Count; // incremented every reading
  15.  
  16. // this is set if there is a timer timeout interrupt
  17. unsigned char volatile periodPulse = 0;
  18.  
  19. static void
  20. triggerTim3 (void)
  21. {
  22.   htim3.Instance->CNT = 0;
  23.   htim3.Instance->CR1 |= TIM_CR1_CEN;
  24.  
  25. }
  26.  
  27. void
  28. TIM2_IRQHandler (void)
  29. {
  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.       int CB_level = HAL_GPIO_ReadPin (CB_Pulse_GPIO_Port,
  35.                                        CB_Pulse_Pin);
  36.       RPM_Level[RPM_Count] = CB_level;
  37.       RPM_Count = (RPM_Count + 1) % RPM_SAMPLES;
  38.  
  39.       if ( CB_level == 0)
  40.         {
  41.           periodPulse = 0;
  42.           triggerTim3 ();
  43.         }
  44.     }
  45. }
  46.  
  47.  
  48. // timer variable shared between TIM3 and TIM4 handler.
  49. static char chtTimer = 0;
  50.  
  51. void
  52. TIM3_IRQHandler (void)
  53. {
  54.   if (__HAL_TIM_GET_FLAG(&htim3, TIM_FLAG_UPDATE))
  55.     {
  56.       __HAL_TIM_CLEAR_FLAG(&htim3, TIM_FLAG_UPDATE);
  57.  
  58.       if (chtTimer >= 3)  // every 300mS
  59.         {
  60.           chtTimer = 0;
  61.  
  62.           for (int instance = 0; instance < 2; instance++)
  63.             {
  64.               uint8_t buffer[2];
  65.               uint16_t Pin =
  66.                   (instance == 0) ? SPI_NS_Temp_Pin : SPI_NS_Temp2_Pin;
  67.  
  68.               HAL_GPIO_WritePin (SPI_NS_Temp_GPIO_Port, Pin, GPIO_PIN_RESET);
  69.  
  70.               HAL_SPI_Receive (&hspi1, buffer, 2, 2);
  71.  
  72.               HAL_GPIO_WritePin (SPI_NS_Temp_GPIO_Port, Pin, GPIO_PIN_SET);
  73.  
  74.               uint16_t obs = (buffer[0] << 8) | buffer[1];
  75.  
  76.               // good observation if the status bit is clear, and the reading is less than 1023
  77.  
  78.               uint16_t temp_c = obs >> 5;
  79.  
  80.               uint8_t good = ((obs & 7) == 0) && (temp_c > 0) && (temp_c < 250);
  81.  
  82.               if (good)
  83.                 {
  84.                   CHT_Observations[instance] = temp_c;
  85.                 }
  86.             }
  87.         }
  88.     }
  89. }
  90.  
  91. // 100mS periodic sampler handler
  92. void
  93. TIM4_IRQHandler (void)
  94. {
  95.   static char blink = 0;
  96.   if (__HAL_TIM_GET_FLAG(&htim4, TIM_FLAG_UPDATE))
  97.     {
  98.       __HAL_TIM_CLEAR_FLAG(&htim4, TIM_FLAG_UPDATE);
  99.  
  100.       blink = !blink;
  101.       HAL_GPIO_WritePin (LED_Blink_GPIO_Port, LED_Blink_Pin,
  102.                          blink ? GPIO_PIN_SET : GPIO_PIN_RESET);
  103.  
  104.       TimerFlag = 1;
  105.       if (NoSerialInCTR < 5)
  106.         {
  107.           NoSerialInCTR++;
  108.           if (NoSerialInCTR == 5)
  109.             {
  110.               NoSerialIn = 1;
  111.             }
  112.         }
  113.  
  114.       if (periodPulse == 1)
  115.         {
  116.         triggerTim3 ();
  117.  
  118.         }
  119.       periodPulse = 1;
  120.  
  121.  
  122.       chtTimer++;
  123.     }
  124.  
  125. }
  126.  
  127.  
  128. void
  129. resetSerialTimeout (void)
  130. {
  131.   __disable_irq ();
  132.   NoSerialInCTR = 0;
  133.   NoSerialIn = 0;
  134.   __enable_irq ();
  135. }
  136.  
  137.