Rev 45 | Rev 48 | 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 | |
47 | mjames | 11 | unsigned volatile long RPM_Time[RPM_SAMPLES]; // sampled on both edges |
12 | unsigned volatile long RPM_Count; // incremented every reading |
||
3 | mjames | 13 | |
29 | mjames | 14 | // this is set if there is a timer timeout interrupt |
31 | mjames | 15 | unsigned char volatile periodPulse = 0; |
29 | mjames | 16 | |
39 | mjames | 17 | unsigned char volatile tim3triggered = 0; |
18 | |||
31 | mjames | 19 | static void |
45 | mjames | 20 | triggerTim3(void) |
29 | mjames | 21 | { |
45 | mjames | 22 | htim3.Instance->CNT = 0; |
23 | htim3.Instance->CR1 |= TIM_CR1_CEN; |
||
29 | mjames | 24 | } |
25 | |||
45 | mjames | 26 | void TIM2_IRQHandler(void) |
8 | mjames | 27 | { |
47 | mjames | 28 | static char level = 0; |
29 | char valid = 0; |
||
30 | uint16_t high_count = 0; |
||
31 | uint16_t low_count = 0; |
||
32 | // rising edge CB pulse |
||
31 | mjames | 33 | if (__HAL_TIM_GET_FLAG(&htim2, TIM_FLAG_CC1)) |
45 | mjames | 34 | { |
35 | __HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_CC1); |
||
47 | mjames | 36 | low_count = __HAL_TIM_GET_COMPARE(&htim2, TIM_CHANNEL_1); |
37 | valid = 1; // record we have a low_count val |
||
38 | |||
45 | mjames | 39 | // trigger timer some time after falling edge |
40 | if (tim3triggered == 0) |
||
31 | mjames | 41 | { |
45 | mjames | 42 | tim3triggered = 1; |
43 | triggerTim3(); |
||
31 | mjames | 44 | } |
45 | mjames | 45 | } |
46 | // falling edge trigger CB pulse |
||
41 | mjames | 47 | if (__HAL_TIM_GET_FLAG(&htim2, TIM_FLAG_CC2)) |
45 | mjames | 48 | { |
49 | __HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_CC2); |
||
47 | mjames | 50 | |
51 | high_count = __HAL_TIM_GET_COMPARE(&htim2, TIM_CHANNEL_2); |
||
52 | valid |= 2; |
||
45 | mjames | 53 | // indicate that CB pulse is owning the timer 3 timing |
54 | periodPulse = 0; |
||
55 | } |
||
47 | mjames | 56 | |
57 | switch (valid) |
||
58 | { |
||
59 | case 1: |
||
60 | // count width of a low period |
||
61 | RPM_Time[RPM_Count] = low_count; |
||
62 | RPM_Count = (RPM_Count + 1) % RPM_SAMPLES; |
||
63 | level = 0; // remember level |
||
64 | break; |
||
65 | case 2: |
||
66 | // count width of a high period |
||
67 | RPM_Time[RPM_Count] = high_count | RPM_FLAG; |
||
68 | RPM_Count = (RPM_Count + 1) % RPM_SAMPLES; |
||
69 | level = 1; // remember level |
||
70 | break; |
||
71 | case 3: |
||
72 | if (level == 1) // next level = 0 ,then 1 again |
||
73 | { |
||
74 | RPM_Time[RPM_Count] = low_count; |
||
75 | RPM_Count = (RPM_Count + 1) % RPM_SAMPLES; |
||
76 | RPM_Time[RPM_Count] = high_count | RPM_FLAG; |
||
77 | RPM_Count = (RPM_Count + 1) % RPM_SAMPLES; |
||
78 | } |
||
79 | else |
||
80 | { |
||
81 | RPM_Time[RPM_Count] = high_count | RPM_FLAG; |
||
82 | RPM_Count = (RPM_Count + 1) % RPM_SAMPLES; |
||
83 | RPM_Time[RPM_Count] = low_count; |
||
84 | RPM_Count = (RPM_Count + 1) % RPM_SAMPLES; |
||
85 | } |
||
86 | break; |
||
87 | default: |
||
88 | break; |
||
89 | } |
||
8 | mjames | 90 | } |
91 | |||
37 | mjames | 92 | // timer variable shared between TIM3 and TIM4 handler. |
93 | static char chtTimer = 0; |
||
8 | mjames | 94 | |
45 | mjames | 95 | void TIM3_IRQHandler(void) |
31 | mjames | 96 | { |
97 | if (__HAL_TIM_GET_FLAG(&htim3, TIM_FLAG_UPDATE)) |
||
45 | mjames | 98 | { |
99 | __HAL_TIM_CLEAR_FLAG(&htim3, TIM_FLAG_UPDATE); |
||
100 | |||
101 | tim3triggered = 0; |
||
102 | if (chtTimer >= 3) // every 300mS |
||
31 | mjames | 103 | { |
45 | mjames | 104 | chtTimer = 0; |
6 | mjames | 105 | |
45 | mjames | 106 | resetTempCS(); |
107 | for (int instance = 0; instance < NUM_SPI_TEMP_SENS; instance++) |
||
108 | { |
||
109 | uint8_t buffer[2]; |
||
41 | mjames | 110 | |
45 | mjames | 111 | nextTempCS(); |
112 | HAL_SPI_Receive(&hspi1, buffer, 2, 2); |
||
6 | mjames | 113 | |
45 | mjames | 114 | uint16_t obs = (buffer[0] << 8) | buffer[1]; |
6 | mjames | 115 | |
45 | mjames | 116 | // good observation if the status bit is clear, and the reading is less than 1023 |
6 | mjames | 117 | |
45 | mjames | 118 | uint16_t temp_c = obs >> 5; |
6 | mjames | 119 | |
45 | mjames | 120 | uint8_t good = ((obs & 7) == 0) && (temp_c > 0) && (temp_c < 250); |
6 | mjames | 121 | |
45 | mjames | 122 | if (good) |
123 | { |
||
124 | Temp_Observations[instance] = temp_c; |
||
125 | } |
||
126 | } |
||
127 | nextTempCS(); // clock CS one more time to deselect all chips |
||
31 | mjames | 128 | } |
45 | mjames | 129 | } |
6 | mjames | 130 | } |
131 | |||
37 | mjames | 132 | // 100mS periodic sampler handler |
45 | mjames | 133 | void TIM4_IRQHandler(void) |
37 | mjames | 134 | { |
135 | static char blink = 0; |
||
136 | if (__HAL_TIM_GET_FLAG(&htim4, TIM_FLAG_UPDATE)) |
||
45 | mjames | 137 | { |
138 | __HAL_TIM_CLEAR_FLAG(&htim4, TIM_FLAG_UPDATE); |
||
37 | mjames | 139 | |
45 | mjames | 140 | blink = !blink; |
141 | HAL_GPIO_WritePin(LED_Blink_GPIO_Port, LED_Blink_Pin, |
||
142 | blink ? GPIO_PIN_SET : GPIO_PIN_RESET); |
||
37 | mjames | 143 | |
45 | mjames | 144 | TimerFlag = 1; |
145 | if (NoSerialInCTR < 5) |
||
146 | { |
||
147 | NoSerialInCTR++; |
||
148 | if (NoSerialInCTR == 5) |
||
149 | { |
||
150 | NoSerialIn = 1; |
||
151 | } |
||
152 | } |
||
37 | mjames | 153 | |
45 | mjames | 154 | if (periodPulse == 1) |
155 | { |
||
156 | triggerTim3(); |
||
37 | mjames | 157 | } |
45 | mjames | 158 | // indicate that timer 4 firing is owning the timer 3 trigger |
159 | periodPulse = 1; |
||
37 | mjames | 160 | |
45 | mjames | 161 | chtTimer++; |
162 | } |
||
37 | mjames | 163 | } |
164 | |||
45 | mjames | 165 | void resetSerialTimeout(void) |
31 | mjames | 166 | { |
45 | mjames | 167 | __disable_irq(); |
31 | mjames | 168 | NoSerialInCTR = 0; |
169 | NoSerialIn = 0; |
||
45 | mjames | 170 | __enable_irq(); |
6 | mjames | 171 | } |