Subversion Repositories EngineBay2

Rev

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. // this is set if there is a timer timeout interrupt
  12. unsigned char volatile periodPulse = 0;
  13.  
  14. // this is set when timer 3 was triggerd
  15. unsigned char volatile tim3triggered = 0;
  16.  
  17. // this is exported
  18. void
  19. triggerTim3(void)
  20. {
  21.   htim3.Instance->CNT = 0;
  22.   htim3.Instance->CR1 |= TIM_CR1_CEN;
  23. }
  24.  
  25. // timer variable shared between TIM3 and TIM4 handler.
  26. static char chtTimer = 0;
  27.  
  28. void TIM3_IRQHandler(void)
  29. {
  30.   if (__HAL_TIM_GET_FLAG(&htim3, TIM_FLAG_UPDATE))
  31.   {
  32.     __HAL_TIM_CLEAR_FLAG(&htim3, TIM_FLAG_UPDATE);
  33.  
  34.     tim3triggered = 0;
  35.     if (chtTimer >= 3) // every 300mS
  36.     {
  37.       chtTimer = 0;
  38.  
  39.       resetTempCS();
  40.       for (int instance = 0; instance < NUM_SPI_TEMP_SENS; instance++)
  41.       {
  42.         uint8_t buffer[2];
  43.  
  44.         nextTempCS();
  45.         HAL_SPI_Receive(&hspi1, buffer, 2, 2);
  46.  
  47.         uint16_t obs = (buffer[0] << 8) | buffer[1];
  48.  
  49.         // good observation if the status bit is clear, and the reading is less than 1023
  50.  
  51.         uint16_t temp_c = obs >> 5;
  52.  
  53.         uint8_t good = ((obs & 7) == 0) && (temp_c > 0) && (temp_c < 250);
  54.  
  55.         if (good)
  56.         {
  57.           Temp_Observations[instance] = temp_c;
  58.         }
  59.       }
  60.       nextTempCS(); // clock CS one more time to deselect all chips
  61.     }
  62.   }
  63. }
  64.  
  65. // 100mS periodic sampler handler
  66. void TIM4_IRQHandler(void)
  67. {
  68.   static char blink = 0;
  69.   if (__HAL_TIM_GET_FLAG(&htim4, TIM_FLAG_UPDATE))
  70.   {
  71.     __HAL_TIM_CLEAR_FLAG(&htim4, TIM_FLAG_UPDATE);
  72.  
  73.     blink = !blink;
  74.     HAL_GPIO_WritePin(LED_Blink_GPIO_Port, LED_Blink_Pin,
  75.                       blink ? GPIO_PIN_SET : GPIO_PIN_RESET);
  76.  
  77.  
  78.     if (periodPulse == 1)
  79.     {
  80.       triggerTim3();
  81.     }
  82.     // indicate that timer 4 firing is owning the timer 3 trigger
  83.     periodPulse = 1;
  84.  
  85.     chtTimer++;
  86.   }
  87. }
  88.