Rev 39 | Rev 42 | 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 | |
28 | mjames | 11 | unsigned volatile long RPM_Time[RPM_SAMPLES]; // sampled on both edges |
41 | mjames | 12 | unsigned volatile char RPM_Level[RPM_SAMPLES]; // active level when sampled |
28 | mjames | 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 | { |
41 | mjames | 23 | htim3.Instance->CNT = 0; |
24 | htim3.Instance->CR1 |= TIM_CR1_CEN; |
||
29 | mjames | 25 | } |
26 | |||
31 | mjames | 27 | void |
28 | TIM2_IRQHandler (void) |
||
8 | mjames | 29 | { |
41 | mjames | 30 | // rising edge trigger CB pulse |
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); |
||
41 | mjames | 35 | RPM_Level[RPM_Count] = 1; // rising so it is high now. |
31 | mjames | 36 | RPM_Count = (RPM_Count + 1) % RPM_SAMPLES; |
41 | mjames | 37 | // trigger timer some time after falling edge |
38 | if(tim3triggered ==0 ) |
||
31 | mjames | 39 | |
9 | mjames | 40 | { |
41 | mjames | 41 | tim3triggered = 1; |
42 | triggerTim3 (); |
||
9 | mjames | 43 | } |
41 | mjames | 44 | |
31 | mjames | 45 | } |
41 | mjames | 46 | // falling edge trigger CB pulse |
47 | if (__HAL_TIM_GET_FLAG(&htim2, TIM_FLAG_CC2)) |
||
48 | { |
||
49 | __HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_CC2); |
||
50 | RPM_Time[RPM_Count] = __HAL_TIM_GET_COMPARE(&htim2, TIM_CHANNEL_2); |
||
51 | RPM_Level[RPM_Count] = 0; // falling so it is low now. |
||
52 | RPM_Count = (RPM_Count + 1) % RPM_SAMPLES; |
||
53 | // indicate that CB pulse is owning the timer 3 timing |
||
54 | periodPulse = 0; |
||
55 | } |
||
56 | |||
8 | mjames | 57 | } |
58 | |||
37 | mjames | 59 | // timer variable shared between TIM3 and TIM4 handler. |
60 | static char chtTimer = 0; |
||
8 | mjames | 61 | |
31 | mjames | 62 | void |
63 | TIM3_IRQHandler (void) |
||
64 | { |
||
65 | if (__HAL_TIM_GET_FLAG(&htim3, TIM_FLAG_UPDATE)) |
||
66 | { |
||
67 | __HAL_TIM_CLEAR_FLAG(&htim3, TIM_FLAG_UPDATE); |
||
6 | mjames | 68 | |
41 | mjames | 69 | |
39 | mjames | 70 | tim3triggered = 0; |
31 | mjames | 71 | if (chtTimer >= 3) // every 300mS |
72 | { |
||
73 | chtTimer = 0; |
||
6 | mjames | 74 | |
31 | mjames | 75 | for (int instance = 0; instance < 2; instance++) |
76 | { |
||
77 | uint8_t buffer[2]; |
||
78 | uint16_t Pin = |
||
79 | (instance == 0) ? SPI_NS_Temp_Pin : SPI_NS_Temp2_Pin; |
||
6 | mjames | 80 | |
31 | mjames | 81 | HAL_GPIO_WritePin (SPI_NS_Temp_GPIO_Port, Pin, GPIO_PIN_RESET); |
6 | mjames | 82 | |
31 | mjames | 83 | HAL_SPI_Receive (&hspi1, buffer, 2, 2); |
6 | mjames | 84 | |
31 | mjames | 85 | HAL_GPIO_WritePin (SPI_NS_Temp_GPIO_Port, Pin, GPIO_PIN_SET); |
6 | mjames | 86 | |
31 | mjames | 87 | uint16_t obs = (buffer[0] << 8) | buffer[1]; |
6 | mjames | 88 | |
31 | mjames | 89 | // good observation if the status bit is clear, and the reading is less than 1023 |
6 | mjames | 90 | |
31 | mjames | 91 | uint16_t temp_c = obs >> 5; |
6 | mjames | 92 | |
31 | mjames | 93 | uint8_t good = ((obs & 7) == 0) && (temp_c > 0) && (temp_c < 250); |
6 | mjames | 94 | |
31 | mjames | 95 | if (good) |
96 | { |
||
97 | CHT_Observations[instance] = temp_c; |
||
6 | mjames | 98 | } |
31 | mjames | 99 | } |
6 | mjames | 100 | } |
31 | mjames | 101 | } |
6 | mjames | 102 | } |
103 | |||
37 | mjames | 104 | // 100mS periodic sampler handler |
31 | mjames | 105 | void |
37 | mjames | 106 | TIM4_IRQHandler (void) |
107 | { |
||
108 | static char blink = 0; |
||
109 | if (__HAL_TIM_GET_FLAG(&htim4, TIM_FLAG_UPDATE)) |
||
110 | { |
||
111 | __HAL_TIM_CLEAR_FLAG(&htim4, TIM_FLAG_UPDATE); |
||
112 | |||
113 | blink = !blink; |
||
114 | HAL_GPIO_WritePin (LED_Blink_GPIO_Port, LED_Blink_Pin, |
||
115 | blink ? GPIO_PIN_SET : GPIO_PIN_RESET); |
||
116 | |||
117 | TimerFlag = 1; |
||
118 | if (NoSerialInCTR < 5) |
||
119 | { |
||
120 | NoSerialInCTR++; |
||
121 | if (NoSerialInCTR == 5) |
||
122 | { |
||
123 | NoSerialIn = 1; |
||
124 | } |
||
125 | } |
||
126 | |||
127 | if (periodPulse == 1) |
||
128 | { |
||
41 | mjames | 129 | triggerTim3 (); |
37 | mjames | 130 | |
131 | } |
||
41 | mjames | 132 | // indicate that timer 4 firing is owning the timer 3 trigger |
37 | mjames | 133 | periodPulse = 1; |
134 | |||
135 | chtTimer++; |
||
136 | } |
||
137 | |||
138 | } |
||
139 | |||
140 | void |
||
31 | mjames | 141 | resetSerialTimeout (void) |
142 | { |
||
143 | __disable_irq (); |
||
144 | NoSerialInCTR = 0; |
||
145 | NoSerialIn = 0; |
||
146 | __enable_irq (); |
||
6 | mjames | 147 | } |
148 |