Subversion Repositories EngineBay2

Rev

Rev 41 | Rev 44 | 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.           resetTempCS();
  76.           for (int instance = 0; instance < NUM_SPI_TEMP_SENS; instance++)
  77.             {
  78.               uint8_t buffer[2];
  79.  
  80.               nextTempCS();
  81.               HAL_SPI_Receive (&hspi1, buffer, 2, 2);
  82.  
  83.  
  84.               uint16_t obs = (buffer[0] << 8) | buffer[1];
  85.  
  86.               // good observation if the status bit is clear, and the reading is less than 1023
  87.  
  88.               uint16_t temp_c = obs >> 5;
  89.  
  90.               uint8_t good = ((obs & 7) == 0) && (temp_c > 0) && (temp_c < 250);
  91.  
  92.               if (good)
  93.                 {
  94.                   Temp_Observations[instance] = temp_c;
  95.                 }
  96.             }
  97.           nextTempCS(); // clock CS one more time to deselect all chips
  98.         }
  99.     }
  100. }
  101.  
  102. // 100mS periodic sampler handler
  103. void
  104. TIM4_IRQHandler (void)
  105. {
  106.   static char blink = 0;
  107.   if (__HAL_TIM_GET_FLAG(&htim4, TIM_FLAG_UPDATE))
  108.     {
  109.       __HAL_TIM_CLEAR_FLAG(&htim4, TIM_FLAG_UPDATE);
  110.  
  111.       blink = !blink;
  112.       HAL_GPIO_WritePin (LED_Blink_GPIO_Port, LED_Blink_Pin,
  113.                          blink ? GPIO_PIN_SET : GPIO_PIN_RESET);
  114.  
  115.       TimerFlag = 1;
  116.       if (NoSerialInCTR < 5)
  117.         {
  118.           NoSerialInCTR++;
  119.           if (NoSerialInCTR == 5)
  120.             {
  121.               NoSerialIn = 1;
  122.             }
  123.         }
  124.  
  125.       if (periodPulse == 1)
  126.         {
  127.           triggerTim3 ();
  128.  
  129.         }
  130.       // indicate that timer 4 firing is owning the timer 3 trigger
  131.       periodPulse = 1;
  132.  
  133.       chtTimer++;
  134.     }
  135.  
  136. }
  137.  
  138. void
  139. resetSerialTimeout (void)
  140. {
  141.   __disable_irq ();
  142.   NoSerialInCTR = 0;
  143.   NoSerialIn = 0;
  144.   __enable_irq ();
  145. }
  146.  
  147.