Rev 37 | Rev 41 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 3 | mjames | 1 | /* |
| 2 | * misc.c |
||
| 3 | * |
||
| 4 | * Created on: 21 Sep 2016 |
||
| 5 | * Author: Mike |
||
| 6 | */ |
||
| 37 | mjames | 7 | #include "stm32f1xx_hal.h" |
| 9 | mjames | 8 | #include "misc.h" |
| 9 | #include "main.h" |
||
| 3 | mjames | 10 | |
| 9 | mjames | 11 | |
| 28 | mjames | 12 | unsigned volatile long RPM_Time[RPM_SAMPLES]; // sampled on both edges |
| 13 | unsigned volatile long RPM_Count; // incremented every reading |
||
| 3 | mjames | 14 | |
| 29 | mjames | 15 | // this is set if there is a timer timeout interrupt |
| 31 | mjames | 16 | unsigned char volatile periodPulse = 0; |
| 29 | mjames | 17 | |
| 39 | mjames | 18 | unsigned char volatile tim3triggered = 0; |
| 19 | |||
| 31 | mjames | 20 | static void |
| 21 | triggerTim3 (void) |
||
| 29 | mjames | 22 | { |
| 31 | mjames | 23 | htim3.Instance->CNT = 0; |
| 24 | htim3.Instance->CR1 |= TIM_CR1_CEN; |
||
| 29 | mjames | 25 | |
| 26 | } |
||
| 27 | |||
| 31 | mjames | 28 | void |
| 29 | TIM2_IRQHandler (void) |
||
| 8 | mjames | 30 | { |
| 31 | mjames | 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_Count = (RPM_Count + 1) % RPM_SAMPLES; |
||
| 36 | |||
| 39 | mjames | 37 | if ( tim3triggered == 0) |
| 9 | mjames | 38 | { |
| 39 | mjames | 39 | tim3triggered = 1; // make it a monostable |
| 31 | mjames | 40 | periodPulse = 0; |
| 41 | triggerTim3 (); |
||
| 9 | mjames | 42 | } |
| 31 | mjames | 43 | } |
| 8 | mjames | 44 | } |
| 45 | |||
| 46 | |||
| 37 | mjames | 47 | // timer variable shared between TIM3 and TIM4 handler. |
| 48 | static char chtTimer = 0; |
||
| 8 | mjames | 49 | |
| 31 | mjames | 50 | void |
| 51 | TIM3_IRQHandler (void) |
||
| 52 | { |
||
| 53 | if (__HAL_TIM_GET_FLAG(&htim3, TIM_FLAG_UPDATE)) |
||
| 54 | { |
||
| 55 | __HAL_TIM_CLEAR_FLAG(&htim3, TIM_FLAG_UPDATE); |
||
| 6 | mjames | 56 | |
| 39 | mjames | 57 | tim3triggered = 0; |
| 31 | mjames | 58 | if (chtTimer >= 3) // every 300mS |
| 59 | { |
||
| 60 | chtTimer = 0; |
||
| 6 | mjames | 61 | |
| 31 | mjames | 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; |
||
| 6 | mjames | 67 | |
| 31 | mjames | 68 | HAL_GPIO_WritePin (SPI_NS_Temp_GPIO_Port, Pin, GPIO_PIN_RESET); |
| 6 | mjames | 69 | |
| 31 | mjames | 70 | HAL_SPI_Receive (&hspi1, buffer, 2, 2); |
| 6 | mjames | 71 | |
| 31 | mjames | 72 | HAL_GPIO_WritePin (SPI_NS_Temp_GPIO_Port, Pin, GPIO_PIN_SET); |
| 6 | mjames | 73 | |
| 31 | mjames | 74 | uint16_t obs = (buffer[0] << 8) | buffer[1]; |
| 6 | mjames | 75 | |
| 31 | mjames | 76 | // good observation if the status bit is clear, and the reading is less than 1023 |
| 6 | mjames | 77 | |
| 31 | mjames | 78 | uint16_t temp_c = obs >> 5; |
| 6 | mjames | 79 | |
| 31 | mjames | 80 | uint8_t good = ((obs & 7) == 0) && (temp_c > 0) && (temp_c < 250); |
| 6 | mjames | 81 | |
| 31 | mjames | 82 | if (good) |
| 83 | { |
||
| 84 | CHT_Observations[instance] = temp_c; |
||
| 6 | mjames | 85 | } |
| 31 | mjames | 86 | } |
| 6 | mjames | 87 | } |
| 31 | mjames | 88 | } |
| 6 | mjames | 89 | } |
| 90 | |||
| 37 | mjames | 91 | // 100mS periodic sampler handler |
| 31 | mjames | 92 | void |
| 37 | mjames | 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 |
||
| 31 | mjames | 129 | resetSerialTimeout (void) |
| 130 | { |
||
| 131 | __disable_irq (); |
||
| 132 | NoSerialInCTR = 0; |
||
| 133 | NoSerialIn = 0; |
||
| 134 | __enable_irq (); |
||
| 6 | mjames | 135 | } |
| 136 |