Subversion Repositories EngineBay2

Rev

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