Subversion Repositories EngineBay2

Rev

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