Rev 44 | Rev 46 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
38 | mjames | 1 | /* USER CODE BEGIN Header */ |
2 | /** |
||
3 | ****************************************************************************** |
||
4 | * @file : main.c |
||
5 | * @brief : Main program body |
||
6 | ****************************************************************************** |
||
7 | * @attention |
||
8 | * |
||
9 | * <h2><center>© Copyright (c) 2021 STMicroelectronics. |
||
10 | * All rights reserved.</center></h2> |
||
11 | * |
||
12 | * This software component is licensed by ST under BSD 3-Clause license, |
||
13 | * the "License"; You may not use this file except in compliance with the |
||
14 | * License. You may obtain a copy of the License at: |
||
15 | * opensource.org/licenses/BSD-3-Clause |
||
16 | * |
||
17 | ****************************************************************************** |
||
18 | */ |
||
19 | /* USER CODE END Header */ |
||
20 | /* Includes ------------------------------------------------------------------*/ |
||
21 | #include "main.h" |
||
22 | |||
23 | /* Private includes ----------------------------------------------------------*/ |
||
24 | /* USER CODE BEGIN Includes */ |
||
25 | #include "libSerial/serial.h" |
||
26 | #include "libPLX/plx.h" |
||
27 | #include "misc.h" |
||
28 | |||
29 | /* USER CODE END Includes */ |
||
30 | |||
31 | /* Private typedef -----------------------------------------------------------*/ |
||
32 | /* USER CODE BEGIN PTD */ |
||
33 | |||
34 | /* USER CODE END PTD */ |
||
35 | |||
36 | /* Private define ------------------------------------------------------------*/ |
||
37 | /* USER CODE BEGIN PD */ |
||
38 | /* USER CODE END PD */ |
||
39 | |||
40 | /* Private macro -------------------------------------------------------------*/ |
||
41 | /* USER CODE BEGIN PM */ |
||
42 | #define ADC_CHANNELS 7 |
||
43 | |||
39 | mjames | 44 | #define ADC_MAP_CHAN 2 |
45 | |||
46 | #define ADC_PRESSURE_CHAN 3 |
||
47 | |||
48 | #define ADC_REF_CHAN 5 |
||
49 | |||
50 | #define ADC_TEMP_CHAN 6 |
||
51 | |||
38 | mjames | 52 | // with a dwell angle of 45 degrees , 4 cylinders and a maximum RPM of 5000 |
53 | // freq = 5000/60 * 2 = 166Hz. |
||
54 | // the TIM2 counter counts in 10uS increments, |
||
55 | // TODO this is wrong algo. Accept FIRST pulse, skip shorter pulses |
||
45 | mjames | 56 | // Accept the first pulse with over 1ms (1/1000 sec) duration as the closure |
57 | #define BREAKER_MIN (RPM_COUNT_RATE / 1000) |
||
38 | mjames | 58 | |
59 | #define RPM_AVERAGE 4 |
||
60 | |||
61 | // wait for about 1 second to decide whether or not starter is on |
||
62 | |||
63 | #define STARTER_LIMIT 10 |
||
64 | |||
65 | /* USER CODE END PM */ |
||
66 | |||
67 | /* Private variables ---------------------------------------------------------*/ |
||
45 | mjames | 68 | ADC_HandleTypeDef hadc1; |
38 | mjames | 69 | DMA_HandleTypeDef hdma_adc1; |
70 | |||
71 | CAN_HandleTypeDef hcan; |
||
72 | |||
73 | SPI_HandleTypeDef hspi1; |
||
74 | |||
75 | TIM_HandleTypeDef htim2; |
||
76 | TIM_HandleTypeDef htim3; |
||
77 | TIM_HandleTypeDef htim4; |
||
78 | |||
79 | UART_HandleTypeDef huart1; |
||
80 | |||
81 | /* USER CODE BEGIN PV */ |
||
82 | |||
83 | volatile char TimerFlag = 0; |
||
84 | |||
85 | volatile char NoSerialInCTR = 0; // Missing characters coming in on USART1 |
||
86 | volatile char NoSerialIn = 0; |
||
87 | |||
39 | mjames | 88 | // scale for filtered samples |
89 | #define Scale 1024.0 |
||
90 | |||
38 | mjames | 91 | // storage for ADC |
45 | mjames | 92 | uint16_t ADC_Samples[ADC_CHANNELS] = {[0 ... ADC_CHANNELS - 1] = 0}; |
38 | mjames | 93 | |
45 | mjames | 94 | uint32_t FILT_Samples[ADC_CHANNELS] = {[0 ... ADC_CHANNELS - 1] = 0}; // filtered ADC samples * Scale |
38 | mjames | 95 | |
39 | mjames | 96 | #define NOM_VREF 3.3 |
97 | // initial ADC vref |
||
45 | mjames | 98 | float adc_vref = NOM_VREF; |
39 | mjames | 99 | |
100 | // internal bandgap voltage reference |
||
45 | mjames | 101 | const float STM32REF = 1.2; // 1.2V typical |
39 | mjames | 102 | |
103 | // scale factor initially assuming |
||
45 | mjames | 104 | float ADC_Scale = 1 / (Scale * 4096) * NOM_VREF; |
39 | mjames | 105 | |
38 | mjames | 106 | // Rev counter processing from original RevCounter Project |
107 | uint16_t RPM_Diff = 0; |
||
108 | uint16_t RPM_Count_Latch = 0; |
||
109 | // accumulators |
||
110 | uint16_t RPM_Pulsecount = 0; |
||
111 | unsigned int RPM_FilteredWidth = 0; |
||
112 | |||
113 | // last time we detected end of dwell i.e. ignition pulse |
||
114 | uint16_t last_dwell_end = 0; |
||
115 | uint16_t RPM_Period[RPM_AVERAGE]; |
||
116 | unsigned int RPM_Period_Ptr = 0; |
||
117 | |||
118 | unsigned int Coded_RPM = 0; |
||
119 | unsigned int Coded_CHT = 0; |
||
120 | |||
42 | mjames | 121 | uint32_t PowerTempTimer; |
38 | mjames | 122 | |
123 | uint16_t Starter_Debounce = 0; |
||
124 | |||
125 | /* USER CODE END PV */ |
||
126 | |||
127 | /* Private function prototypes -----------------------------------------------*/ |
||
128 | void SystemClock_Config(void); |
||
129 | static void MX_GPIO_Init(void); |
||
130 | static void MX_DMA_Init(void); |
||
131 | static void MX_ADC1_Init(void); |
||
132 | static void MX_CAN_Init(void); |
||
133 | static void MX_SPI1_Init(void); |
||
134 | static void MX_TIM2_Init(void); |
||
135 | static void MX_TIM3_Init(void); |
||
136 | static void MX_TIM4_Init(void); |
||
137 | static void MX_USART1_UART_Init(void); |
||
138 | /* USER CODE BEGIN PFP */ |
||
139 | |||
140 | /* USER CODE END PFP */ |
||
141 | |||
142 | /* Private user code ---------------------------------------------------------*/ |
||
143 | /* USER CODE BEGIN 0 */ |
||
144 | |||
45 | mjames | 145 | void plx_sendword(int x) |
38 | mjames | 146 | { |
45 | mjames | 147 | PutCharSerial(&uc1, ((x) >> 6) & 0x3F); |
148 | PutCharSerial(&uc1, (x)&0x3F); |
||
38 | mjames | 149 | } |
150 | |||
45 | mjames | 151 | void filter_ADC_samples() |
38 | mjames | 152 | { |
153 | int i; |
||
154 | for (i = 0; i < ADC_CHANNELS; i++) |
||
45 | mjames | 155 | { |
156 | FILT_Samples[i] += (ADC_Samples[i] * Scale - FILT_Samples[i]) / 2; |
||
157 | } |
||
38 | mjames | 158 | } |
159 | |||
39 | mjames | 160 | /****! |
161 | * @brief this reads the reference voltage within the STM32L151 |
||
162 | * Powers up reference voltage and temperature sensor, waits 3mS and takes reading |
||
163 | * Requires that the ADC be powered up |
||
164 | */ |
||
165 | |||
45 | mjames | 166 | void CalibrateADC(void) |
39 | mjames | 167 | { |
45 | mjames | 168 | float adc_val = FILT_Samples[ADC_REF_CHAN]; // as set up in device config |
39 | mjames | 169 | |
45 | mjames | 170 | float adc_vref = STM32REF * (4096.0 * Scale) / adc_val; // the estimate for checking |
39 | mjames | 171 | |
45 | mjames | 172 | ADC_Scale = 1 / (Scale * 4096) * adc_vref; |
39 | mjames | 173 | } |
174 | |||
45 | mjames | 175 | void ProcessRPM(int instance) |
38 | mjames | 176 | { |
45 | mjames | 177 | // compute the timer values |
178 | // snapshot timers |
||
179 | unsigned short RPM_Pulsewidth; |
||
38 | mjames | 180 | // current RPM pulse next slot index |
41 | mjames | 181 | unsigned short RPM_Count_Val; |
45 | mjames | 182 | __disable_irq(); // copy the counter value |
38 | mjames | 183 | RPM_Count_Val = RPM_Count; |
45 | mjames | 184 | __enable_irq(); |
185 | // do calculations |
||
186 | // if there is only one entry, cannot get difference |
||
38 | mjames | 187 | if (RPM_Count_Latch != RPM_Count_Val) |
45 | mjames | 188 | { |
189 | while (1) |
||
38 | mjames | 190 | { |
45 | mjames | 191 | unsigned int base_time; |
192 | unsigned int new_time; |
||
193 | // if we are at N-1, stop. |
||
194 | unsigned int next_count = (RPM_Count_Latch + 1) % RPM_SAMPLES; |
||
195 | if (next_count == RPM_Count_Val) |
||
196 | { |
||
197 | break; // completed loop |
||
198 | } |
||
199 | char pulse_level = RPM_Level[RPM_Count_Latch]; |
||
200 | base_time = RPM_Time[RPM_Count_Latch]; |
||
201 | new_time = RPM_Time[next_count]; |
||
202 | RPM_Count_Latch = next_count; |
||
38 | mjames | 203 | |
45 | mjames | 204 | RPM_Pulsewidth = new_time - base_time; // not wrapped |
38 | mjames | 205 | |
45 | mjames | 206 | // if the pulse was low, |
207 | if (pulse_level == 0 && RPM_Pulsewidth > BREAKER_MIN) |
||
208 | { |
||
38 | mjames | 209 | |
45 | mjames | 210 | RPM_Diff = new_time - last_dwell_end; |
38 | mjames | 211 | |
45 | mjames | 212 | RPM_Period[RPM_Period_Ptr] = RPM_Diff; |
213 | RPM_Period_Ptr = (RPM_Period_Ptr + 1) % RPM_AVERAGE; |
||
214 | if (RPM_Pulsecount < RPM_AVERAGE) |
||
215 | RPM_Pulsecount++; // count one pulse |
||
216 | last_dwell_end = new_time; |
||
217 | } |
||
38 | mjames | 218 | } |
45 | mjames | 219 | } |
38 | mjames | 220 | |
221 | if (RPM_Pulsecount == RPM_AVERAGE) |
||
45 | mjames | 222 | { |
223 | // now have time for N pulses in clocks |
||
224 | // need to scale by 19.55: one unit is 19.55 RPM |
||
225 | // 1Hz is 30 RPM |
||
226 | int i; |
||
227 | RPM_FilteredWidth = 0; |
||
228 | for (i = 0; i < RPM_AVERAGE; i++) |
||
229 | RPM_FilteredWidth += RPM_Period[i]; |
||
38 | mjames | 230 | |
45 | mjames | 231 | Coded_RPM = (Scale * 30.0 * RPM_AVERAGE * RPM_COUNT_RATE) / (19.55 * RPM_FilteredWidth); |
38 | mjames | 232 | |
233 | #if !defined MY_DEBUG |
||
45 | mjames | 234 | // reset here unless we want to debug |
235 | RPM_Pulsecount = 0; |
||
236 | RPM_FilteredWidth = 0; |
||
38 | mjames | 237 | #endif |
45 | mjames | 238 | } |
38 | mjames | 239 | |
45 | mjames | 240 | // send the current RPM *calculation |
241 | plx_sendword(PLX_RPM); |
||
242 | PutCharSerial(&uc1, instance); |
||
243 | plx_sendword(Coded_RPM / Scale); |
||
38 | mjames | 244 | } |
245 | |||
246 | // this uses a MAX6675 which is a simple 16 bit read |
||
247 | // SPI is configured for 8 bits so I can use an OLED display if I need it |
||
248 | // must wait > 0.22 seconds between conversion attempts as this is the measurement time |
||
249 | // |
||
250 | |||
251 | FunctionalState CHT_Enable = ENABLE; |
||
252 | |||
253 | #define CORR 3 |
||
254 | |||
45 | mjames | 255 | uint16_t Temp_Observations[NUM_SPI_TEMP_SENS] = {[0 ... NUM_SPI_TEMP_SENS - 1] = 0}; |
38 | mjames | 256 | |
42 | mjames | 257 | /// \param item The array index to send |
258 | /// \param instance The instance to send over the bus |
||
259 | /// \param type the code to use for this observation |
||
45 | mjames | 260 | void ProcessTemp(char item, int instance, enum PLX_Observations type) |
38 | mjames | 261 | { |
42 | mjames | 262 | if (item > NUM_SPI_TEMP_SENS) |
263 | return; |
||
45 | mjames | 264 | plx_sendword(type); |
265 | PutCharSerial(&uc1, instance); |
||
266 | plx_sendword(Temp_Observations[(int)item]); |
||
38 | mjames | 267 | } |
268 | |||
42 | mjames | 269 | /// \brief Reset the temperature chip select system |
270 | void resetTempCS(void) |
||
271 | { |
||
45 | mjames | 272 | HAL_GPIO_WritePin(SPI_CS_D_GPIO_Port, SPI_CS_D_Pin, GPIO_PIN_SET); |
273 | HAL_GPIO_WritePin(SPI_CS_Clk_GPIO_Port, SPI_CS_Clk_Pin, |
||
274 | GPIO_PIN_SET); |
||
42 | mjames | 275 | |
45 | mjames | 276 | for (int i = 0; i < 8; i++) |
277 | { |
||
278 | HAL_GPIO_WritePin(SPI_CS_Clk_GPIO_Port, SPI_CS_Clk_Pin, |
||
279 | GPIO_PIN_RESET); |
||
280 | HAL_GPIO_WritePin(SPI_CS_Clk_GPIO_Port, SPI_CS_Clk_Pin, |
||
281 | GPIO_PIN_SET); |
||
282 | } |
||
42 | mjames | 283 | |
45 | mjames | 284 | // prepare for selecting next pin |
285 | HAL_GPIO_WritePin(SPI_CS_D_GPIO_Port, SPI_CS_D_Pin, GPIO_PIN_RESET); |
||
42 | mjames | 286 | } |
287 | |||
288 | void nextTempCS(void) |
||
289 | { |
||
45 | mjames | 290 | HAL_GPIO_WritePin(SPI_CS_Clk_GPIO_Port, SPI_CS_Clk_Pin, |
291 | GPIO_PIN_RESET); |
||
292 | HAL_GPIO_WritePin(SPI_CS_Clk_GPIO_Port, SPI_CS_Clk_Pin, |
||
293 | GPIO_PIN_SET); |
||
294 | HAL_GPIO_WritePin(SPI_CS_D_GPIO_Port, SPI_CS_D_Pin, GPIO_PIN_SET); |
||
42 | mjames | 295 | } |
296 | |||
45 | mjames | 297 | void EnableTempSensors(FunctionalState state) |
38 | mjames | 298 | |
299 | { |
||
300 | GPIO_InitTypeDef GPIO_InitStruct; |
||
301 | |||
302 | CHT_Enable = state; |
||
303 | |||
304 | /* enable SPI in live mode : assume it and its GPIOs are already initialised in SPI mode */ |
||
305 | if (state == ENABLE) |
||
45 | mjames | 306 | { |
307 | HAL_GPIO_WritePin(ENA_AUX_5V_GPIO_Port, ENA_AUX_5V_Pin, GPIO_PIN_SET); |
||
38 | mjames | 308 | |
45 | mjames | 309 | resetTempCS(); |
42 | mjames | 310 | |
45 | mjames | 311 | /* put the SPI pins back into SPI AF mode */ |
312 | GPIO_InitStruct.Pin = SPI1_MOSI_Pin | SPI1_MISO_Pin | SPI1_SCK_Pin; |
||
313 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
||
314 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
315 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; |
||
316 | HAL_GPIO_Init(SPI1_SCK_GPIO_Port, &GPIO_InitStruct); |
||
317 | } |
||
38 | mjames | 318 | else |
45 | mjames | 319 | { |
320 | /* Power down the SPI interface taking signals all low */ |
||
321 | HAL_GPIO_WritePin(ENA_AUX_5V_GPIO_Port, ENA_AUX_5V_Pin, GPIO_PIN_RESET); |
||
38 | mjames | 322 | |
45 | mjames | 323 | HAL_GPIO_WritePin(SPI1_SCK_GPIO_Port, |
324 | SPI1_MOSI_Pin | SPI1_MISO_Pin | SPI1_SCK_Pin, |
||
325 | GPIO_PIN_RESET); |
||
38 | mjames | 326 | |
45 | mjames | 327 | /* put the SPI pins back into GPIO mode */ |
328 | GPIO_InitStruct.Pin = SPI1_MOSI_Pin | SPI1_MISO_Pin | SPI1_SCK_Pin; |
||
329 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
||
330 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
331 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; |
||
332 | HAL_GPIO_Init(SPI1_SCK_GPIO_Port, &GPIO_InitStruct); |
||
333 | } |
||
38 | mjames | 334 | } |
335 | |||
336 | // 1023 is 20.00 volts. |
||
45 | mjames | 337 | void ProcessBatteryVoltage(int instance) |
38 | mjames | 338 | { |
339 | float reading = FILT_Samples[instance] * ADC_Scale; |
||
340 | reading = reading * 7.8125; // real voltage |
||
45 | mjames | 341 | reading = reading * 51.15; // PLC scaling = 1023/20 |
38 | mjames | 342 | |
45 | mjames | 343 | plx_sendword(PLX_Volts); |
344 | PutCharSerial(&uc1, instance); |
||
345 | plx_sendword((uint16_t)reading); |
||
38 | mjames | 346 | } |
347 | |||
45 | mjames | 348 | void ProcessCPUTemperature(int instance) |
38 | mjames | 349 | { |
45 | mjames | 350 | // this is defined in the STM32F103 reference manual . # |
39 | mjames | 351 | // V25 = 1.43 volts |
352 | // Avg_slope = 4.3mV /degree C |
||
353 | // temperature = {(V25 - VSENSE) / Avg_Slope} + 25 |
||
38 | mjames | 354 | |
355 | /* get the ADC reading corresponding to ADC channel 16 after turning on the ADC */ |
||
356 | |||
39 | mjames | 357 | float temp_val = FILT_Samples[ADC_TEMP_CHAN] * ADC_Scale; |
38 | mjames | 358 | /* renormalise temperature value to account for different ADC Vref : normalise to that which we would get for a 3000mV reference */ |
45 | mjames | 359 | temp_val = (1.43 - temp_val) / 4.3e-3 + 25; |
38 | mjames | 360 | |
45 | mjames | 361 | int32_t result = temp_val; |
38 | mjames | 362 | |
45 | mjames | 363 | // int32_t result = 800 * ((int32_t) temp_val - TS_CAL30); |
364 | // result = result / (TS_CAL110 - TS_CAL30) + 300; |
||
39 | mjames | 365 | |
45 | mjames | 366 | plx_sendword(PLX_FluidTemp); |
367 | PutCharSerial(&uc1, instance); |
||
368 | plx_sendword(result); |
||
38 | mjames | 369 | } |
370 | |||
371 | // the MAP sensor is giving us a reading of |
||
372 | // 4.6 volts for 1019mB or 2.27 volts at the ADC input (resistive divider by 2.016) |
||
373 | // I believe the sensor reads 4.5V at 1000kPa and 0.5V at 0kPa |
||
374 | // Calibration is a bit off |
||
375 | // Real Displayed |
||
376 | // 989 968 |
||
377 | // 994.1 986 |
||
378 | // 992.3 984 |
||
379 | |||
45 | mjames | 380 | void ProcessMAP(int instance) |
38 | mjames | 381 | { |
45 | mjames | 382 | // Using ADC_Samples[3] as the MAP input |
39 | mjames | 383 | float reading = FILT_Samples[ADC_MAP_CHAN] * ADC_Scale; |
45 | mjames | 384 | reading = reading * 2.016; // real voltage |
38 | mjames | 385 | // values computed from slope / intercept of map.ods |
45 | mjames | 386 | // reading = (reading) * 56.23 + 743.2; // do not assume 0.5 volt offset : reading from 0 to 4.5 instead of 0.5 to 4.5 |
38 | mjames | 387 | // using a pressure gauge. |
45 | mjames | 388 | reading = (reading)*150 + 326; |
38 | mjames | 389 | |
45 | mjames | 390 | plx_sendword(PLX_MAP); |
391 | PutCharSerial(&uc1, instance); |
||
392 | plx_sendword((uint16_t)reading); |
||
38 | mjames | 393 | } |
394 | |||
395 | // the Oil pressi sensor is giving us a reading of |
||
396 | // 4.5 volts for 100 PSI or 2.25 volts at the ADC input (resistive divider by 2.016) |
||
397 | // I believe the sensor reads 4.5V at 100PSI and 0.5V at 0PSI |
||
398 | // an observation of 1024 is 200PSI, so observation of 512 is 100 PSI. |
||
399 | |||
45 | mjames | 400 | void ProcessOilPress(int instance) |
38 | mjames | 401 | { |
45 | mjames | 402 | // Using ADC_Samples[2] as the MAP input |
39 | mjames | 403 | float reading = FILT_Samples[ADC_PRESSURE_CHAN] * ADC_Scale; |
45 | mjames | 404 | reading = reading * 2.00; // real voltage |
405 | reading = (reading - 0.5) * 512 / 4; // this is 1023 * 100/200 |
||
38 | mjames | 406 | |
45 | mjames | 407 | plx_sendword(PLX_FluidPressure); |
408 | PutCharSerial(&uc1, instance); |
||
409 | plx_sendword((uint16_t)reading); |
||
38 | mjames | 410 | } |
411 | |||
45 | mjames | 412 | void ProcessTiming(int instance) |
38 | mjames | 413 | { |
45 | mjames | 414 | plx_sendword(PLX_Timing); |
415 | PutCharSerial(&uc1, instance); |
||
416 | plx_sendword(64 - 15); // make it negative |
||
38 | mjames | 417 | } |
418 | |||
419 | /* USER CODE END 0 */ |
||
420 | |||
421 | /** |
||
422 | * @brief The application entry point. |
||
423 | * @retval int |
||
424 | */ |
||
425 | int main(void) |
||
426 | { |
||
427 | /* USER CODE BEGIN 1 */ |
||
428 | |||
429 | /* USER CODE END 1 */ |
||
430 | |||
431 | /* MCU Configuration--------------------------------------------------------*/ |
||
432 | |||
433 | /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ |
||
434 | HAL_Init(); |
||
435 | |||
436 | /* USER CODE BEGIN Init */ |
||
437 | |||
438 | /* USER CODE END Init */ |
||
439 | |||
440 | /* Configure the system clock */ |
||
441 | SystemClock_Config(); |
||
442 | |||
443 | /* USER CODE BEGIN SysInit */ |
||
444 | |||
445 | /* USER CODE END SysInit */ |
||
446 | |||
447 | /* Initialize all configured peripherals */ |
||
448 | MX_GPIO_Init(); |
||
449 | MX_DMA_Init(); |
||
450 | MX_ADC1_Init(); |
||
451 | MX_CAN_Init(); |
||
452 | MX_SPI1_Init(); |
||
453 | MX_TIM2_Init(); |
||
454 | MX_TIM3_Init(); |
||
455 | MX_TIM4_Init(); |
||
456 | MX_USART1_UART_Init(); |
||
457 | /* USER CODE BEGIN 2 */ |
||
45 | mjames | 458 | HAL_MspInit(); |
38 | mjames | 459 | |
460 | // Not using HAL USART code |
||
45 | mjames | 461 | __HAL_RCC_USART1_CLK_ENABLE(); // PLX comms port |
38 | mjames | 462 | /* setup the USART control blocks */ |
45 | mjames | 463 | init_usart_ctl(&uc1, &huart1); |
38 | mjames | 464 | |
45 | mjames | 465 | EnableSerialRxInterrupt(&uc1); |
38 | mjames | 466 | |
45 | mjames | 467 | HAL_SPI_MspInit(&hspi1); |
38 | mjames | 468 | |
45 | mjames | 469 | HAL_ADC_MspInit(&hadc1); |
38 | mjames | 470 | |
45 | mjames | 471 | HAL_ADC_Start_DMA(&hadc1, (uint32_t *)ADC_Samples, ADC_CHANNELS); |
38 | mjames | 472 | |
45 | mjames | 473 | HAL_ADC_Start_IT(&hadc1); |
38 | mjames | 474 | |
45 | mjames | 475 | HAL_TIM_Base_MspInit(&htim4); |
476 | HAL_TIM_Base_Start_IT(&htim4); |
||
38 | mjames | 477 | |
478 | // initialise all the STMCubeMX stuff |
||
45 | mjames | 479 | HAL_TIM_Base_MspInit(&htim2); |
38 | mjames | 480 | // Start the counter |
45 | mjames | 481 | HAL_TIM_Base_Start(&htim2); |
41 | mjames | 482 | // Start the input capture and the rising edge interrupt |
45 | mjames | 483 | HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_1); |
41 | mjames | 484 | // Start the input capture and the falling edge interrupt |
45 | mjames | 485 | HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_2); |
38 | mjames | 486 | |
45 | mjames | 487 | HAL_TIM_Base_MspInit(&htim3); |
38 | mjames | 488 | __HAL_TIM_ENABLE_IT(&htim3, TIM_IT_UPDATE); |
45 | mjames | 489 | uint32_t Ticks = HAL_GetTick() + 100; |
38 | mjames | 490 | int CalCounter = 0; |
491 | |||
45 | mjames | 492 | PowerTempTimer = HAL_GetTick() + 1000; /* wait 10 seconds before powering up the CHT sensor */ |
38 | mjames | 493 | |
45 | mjames | 494 | ResetRxBuffer(&uc1); |
38 | mjames | 495 | /* USER CODE END 2 */ |
496 | |||
497 | /* Infinite loop */ |
||
498 | /* USER CODE BEGIN WHILE */ |
||
499 | while (1) |
||
45 | mjames | 500 | { |
38 | mjames | 501 | /* USER CODE END WHILE */ |
502 | |||
503 | /* USER CODE BEGIN 3 */ |
||
504 | |||
45 | mjames | 505 | if (HAL_GetTick() > Ticks) |
506 | { |
||
507 | Ticks += 100; |
||
508 | filter_ADC_samples(); |
||
509 | // delay to calibrate ADC |
||
510 | if (CalCounter < 1000) |
||
511 | { |
||
512 | CalCounter += 100; |
||
513 | } |
||
38 | mjames | 514 | |
45 | mjames | 515 | if (CalCounter == 900) |
516 | { |
||
517 | CalibrateADC(); |
||
518 | } |
||
519 | } |
||
520 | /* when the starter motor is on then power down the CHT sensors as they seem to fail */ |
||
38 | mjames | 521 | |
45 | mjames | 522 | if (HAL_GPIO_ReadPin(STARTER_ON_GPIO_Port, STARTER_ON_Pin) == GPIO_PIN_RESET) |
523 | { |
||
524 | if (Starter_Debounce < STARTER_LIMIT) |
||
525 | { |
||
526 | Starter_Debounce++; |
||
527 | } |
||
528 | } |
||
529 | else |
||
530 | { |
||
531 | if (Starter_Debounce > 0) |
||
532 | { |
||
533 | Starter_Debounce--; |
||
534 | } |
||
535 | } |
||
38 | mjames | 536 | |
45 | mjames | 537 | if (Starter_Debounce == STARTER_LIMIT) |
538 | { |
||
539 | EnableTempSensors(DISABLE); |
||
540 | PowerTempTimer = HAL_GetTick() + 1000; |
||
541 | } |
||
542 | else |
||
543 | /* if the PowerTempTimer is set then wait for it to timeout, then power up CHT */ |
||
544 | { |
||
545 | if ((PowerTempTimer > 0) && (HAL_GetTick() > PowerTempTimer)) |
||
546 | { |
||
547 | EnableTempSensors(ENABLE); |
||
548 | PowerTempTimer = 0; |
||
549 | } |
||
550 | } |
||
38 | mjames | 551 | |
45 | mjames | 552 | // check to see if we have any incoming data, copy and append if so, if no data then create our own frames. |
553 | int c; |
||
554 | char send = 0; |
||
38 | mjames | 555 | |
45 | mjames | 556 | // poll the input for a stop bit or timeout |
557 | if (PollSerial(&uc1)) |
||
558 | { |
||
559 | resetSerialTimeout(); |
||
560 | c = GetCharSerial(&uc1); |
||
561 | if (c != PLX_Stop) |
||
562 | { |
||
563 | PutCharSerial(&uc1, c); // echo all but the stop bit |
||
564 | } |
||
565 | else |
||
566 | { // must be a stop character |
||
567 | send = 1; // start our sending process. |
||
568 | } |
||
569 | } |
||
38 | mjames | 570 | |
45 | mjames | 571 | // sort out auto-sending |
572 | if (TimerFlag) |
||
573 | { |
||
574 | TimerFlag = 0; |
||
575 | if (NoSerialIn) |
||
576 | { |
||
577 | PutCharSerial(&uc1, PLX_Start); |
||
578 | send = 1; |
||
579 | } |
||
580 | } |
||
581 | if (send) |
||
582 | { |
||
583 | send = 0; |
||
38 | mjames | 584 | |
45 | mjames | 585 | // send the observations |
586 | ProcessRPM(0); |
||
587 | ProcessTemp(0, 0, PLX_X_CHT); |
||
588 | ProcessTemp(1, 1, PLX_X_CHT); |
||
589 | ProcessTemp(2, 0, PLX_AIT); |
||
590 | ProcessTemp(3, 1, PLX_AIT); |
||
591 | ProcessBatteryVoltage(0); // Batt 1 |
||
592 | ProcessBatteryVoltage(1); // Batt 2 |
||
593 | ProcessCPUTemperature(0); // built in temperature sensor |
||
38 | mjames | 594 | |
45 | mjames | 595 | ProcessMAP(0); |
596 | ProcessOilPress(0); |
||
38 | mjames | 597 | |
45 | mjames | 598 | PutCharSerial(&uc1, PLX_Stop); |
38 | mjames | 599 | } |
45 | mjames | 600 | } |
38 | mjames | 601 | |
602 | /* USER CODE END 3 */ |
||
603 | } |
||
604 | |||
605 | /** |
||
606 | * @brief System Clock Configuration |
||
607 | * @retval None |
||
608 | */ |
||
609 | void SystemClock_Config(void) |
||
610 | { |
||
611 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
||
612 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
||
613 | RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; |
||
614 | |||
615 | /** Initializes the RCC Oscillators according to the specified parameters |
||
616 | * in the RCC_OscInitTypeDef structure. |
||
617 | */ |
||
618 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; |
||
619 | RCC_OscInitStruct.HSEState = RCC_HSE_ON; |
||
620 | RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; |
||
621 | RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
||
622 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
||
623 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; |
||
624 | RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; |
||
625 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) |
||
626 | { |
||
627 | Error_Handler(); |
||
628 | } |
||
45 | mjames | 629 | |
38 | mjames | 630 | /** Initializes the CPU, AHB and APB buses clocks |
631 | */ |
||
632 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |
||
633 | |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; |
||
634 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
||
635 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
||
636 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; |
||
637 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; |
||
638 | |||
639 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) |
||
640 | { |
||
641 | Error_Handler(); |
||
642 | } |
||
643 | PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC; |
||
644 | PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6; |
||
645 | if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) |
||
646 | { |
||
647 | Error_Handler(); |
||
648 | } |
||
649 | } |
||
650 | |||
651 | /** |
||
652 | * @brief ADC1 Initialization Function |
||
653 | * @param None |
||
654 | * @retval None |
||
655 | */ |
||
656 | static void MX_ADC1_Init(void) |
||
657 | { |
||
658 | |||
659 | /* USER CODE BEGIN ADC1_Init 0 */ |
||
660 | |||
661 | /* USER CODE END ADC1_Init 0 */ |
||
662 | |||
663 | ADC_ChannelConfTypeDef sConfig = {0}; |
||
664 | |||
665 | /* USER CODE BEGIN ADC1_Init 1 */ |
||
666 | |||
667 | /* USER CODE END ADC1_Init 1 */ |
||
45 | mjames | 668 | |
38 | mjames | 669 | /** Common config |
670 | */ |
||
671 | hadc1.Instance = ADC1; |
||
672 | hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE; |
||
673 | hadc1.Init.ContinuousConvMode = DISABLE; |
||
674 | hadc1.Init.DiscontinuousConvMode = DISABLE; |
||
675 | hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T3_TRGO; |
||
676 | hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; |
||
677 | hadc1.Init.NbrOfConversion = 7; |
||
678 | if (HAL_ADC_Init(&hadc1) != HAL_OK) |
||
679 | { |
||
680 | Error_Handler(); |
||
681 | } |
||
45 | mjames | 682 | |
38 | mjames | 683 | /** Configure Regular Channel |
684 | */ |
||
685 | sConfig.Channel = ADC_CHANNEL_0; |
||
686 | sConfig.Rank = ADC_REGULAR_RANK_1; |
||
39 | mjames | 687 | sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5; |
38 | mjames | 688 | if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) |
689 | { |
||
690 | Error_Handler(); |
||
691 | } |
||
45 | mjames | 692 | |
38 | mjames | 693 | /** Configure Regular Channel |
694 | */ |
||
695 | sConfig.Channel = ADC_CHANNEL_1; |
||
696 | sConfig.Rank = ADC_REGULAR_RANK_2; |
||
697 | if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) |
||
698 | { |
||
699 | Error_Handler(); |
||
700 | } |
||
45 | mjames | 701 | |
38 | mjames | 702 | /** Configure Regular Channel |
703 | */ |
||
704 | sConfig.Channel = ADC_CHANNEL_2; |
||
705 | sConfig.Rank = ADC_REGULAR_RANK_3; |
||
706 | if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) |
||
707 | { |
||
708 | Error_Handler(); |
||
709 | } |
||
45 | mjames | 710 | |
38 | mjames | 711 | /** Configure Regular Channel |
712 | */ |
||
713 | sConfig.Channel = ADC_CHANNEL_3; |
||
714 | sConfig.Rank = ADC_REGULAR_RANK_4; |
||
715 | if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) |
||
716 | { |
||
717 | Error_Handler(); |
||
718 | } |
||
45 | mjames | 719 | |
38 | mjames | 720 | /** Configure Regular Channel |
721 | */ |
||
39 | mjames | 722 | sConfig.Channel = ADC_CHANNEL_4; |
38 | mjames | 723 | sConfig.Rank = ADC_REGULAR_RANK_5; |
724 | if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) |
||
725 | { |
||
726 | Error_Handler(); |
||
727 | } |
||
45 | mjames | 728 | |
38 | mjames | 729 | /** Configure Regular Channel |
730 | */ |
||
731 | sConfig.Channel = ADC_CHANNEL_VREFINT; |
||
732 | sConfig.Rank = ADC_REGULAR_RANK_6; |
||
733 | if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) |
||
734 | { |
||
735 | Error_Handler(); |
||
736 | } |
||
45 | mjames | 737 | |
38 | mjames | 738 | /** Configure Regular Channel |
739 | */ |
||
39 | mjames | 740 | sConfig.Channel = ADC_CHANNEL_TEMPSENSOR; |
38 | mjames | 741 | sConfig.Rank = ADC_REGULAR_RANK_7; |
742 | if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) |
||
743 | { |
||
744 | Error_Handler(); |
||
745 | } |
||
746 | /* USER CODE BEGIN ADC1_Init 2 */ |
||
747 | |||
748 | /* USER CODE END ADC1_Init 2 */ |
||
749 | |||
750 | } |
||
751 | |||
752 | /** |
||
753 | * @brief CAN Initialization Function |
||
754 | * @param None |
||
755 | * @retval None |
||
756 | */ |
||
757 | static void MX_CAN_Init(void) |
||
758 | { |
||
759 | |||
760 | /* USER CODE BEGIN CAN_Init 0 */ |
||
761 | |||
762 | /* USER CODE END CAN_Init 0 */ |
||
763 | |||
764 | /* USER CODE BEGIN CAN_Init 1 */ |
||
765 | |||
766 | /* USER CODE END CAN_Init 1 */ |
||
767 | hcan.Instance = CAN1; |
||
768 | hcan.Init.Prescaler = 16; |
||
769 | hcan.Init.Mode = CAN_MODE_NORMAL; |
||
770 | hcan.Init.SyncJumpWidth = CAN_SJW_1TQ; |
||
771 | hcan.Init.TimeSeg1 = CAN_BS1_1TQ; |
||
772 | hcan.Init.TimeSeg2 = CAN_BS2_1TQ; |
||
773 | hcan.Init.TimeTriggeredMode = DISABLE; |
||
774 | hcan.Init.AutoBusOff = DISABLE; |
||
775 | hcan.Init.AutoWakeUp = DISABLE; |
||
776 | hcan.Init.AutoRetransmission = DISABLE; |
||
777 | hcan.Init.ReceiveFifoLocked = DISABLE; |
||
778 | hcan.Init.TransmitFifoPriority = DISABLE; |
||
779 | if (HAL_CAN_Init(&hcan) != HAL_OK) |
||
780 | { |
||
781 | Error_Handler(); |
||
782 | } |
||
783 | /* USER CODE BEGIN CAN_Init 2 */ |
||
784 | |||
785 | /* USER CODE END CAN_Init 2 */ |
||
786 | |||
787 | } |
||
788 | |||
789 | /** |
||
790 | * @brief SPI1 Initialization Function |
||
791 | * @param None |
||
792 | * @retval None |
||
793 | */ |
||
794 | static void MX_SPI1_Init(void) |
||
795 | { |
||
796 | |||
797 | /* USER CODE BEGIN SPI1_Init 0 */ |
||
798 | |||
799 | /* USER CODE END SPI1_Init 0 */ |
||
800 | |||
801 | /* USER CODE BEGIN SPI1_Init 1 */ |
||
802 | |||
803 | /* USER CODE END SPI1_Init 1 */ |
||
804 | /* SPI1 parameter configuration*/ |
||
805 | hspi1.Instance = SPI1; |
||
806 | hspi1.Init.Mode = SPI_MODE_MASTER; |
||
807 | hspi1.Init.Direction = SPI_DIRECTION_2LINES; |
||
808 | hspi1.Init.DataSize = SPI_DATASIZE_8BIT; |
||
809 | hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; |
||
810 | hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; |
||
811 | hspi1.Init.NSS = SPI_NSS_SOFT; |
||
41 | mjames | 812 | hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32; |
38 | mjames | 813 | hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; |
814 | hspi1.Init.TIMode = SPI_TIMODE_DISABLE; |
||
815 | hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; |
||
816 | hspi1.Init.CRCPolynomial = 10; |
||
817 | if (HAL_SPI_Init(&hspi1) != HAL_OK) |
||
818 | { |
||
819 | Error_Handler(); |
||
820 | } |
||
821 | /* USER CODE BEGIN SPI1_Init 2 */ |
||
822 | |||
823 | /* USER CODE END SPI1_Init 2 */ |
||
824 | |||
825 | } |
||
826 | |||
827 | /** |
||
828 | * @brief TIM2 Initialization Function |
||
829 | * @param None |
||
830 | * @retval None |
||
831 | */ |
||
832 | static void MX_TIM2_Init(void) |
||
833 | { |
||
834 | |||
835 | /* USER CODE BEGIN TIM2_Init 0 */ |
||
836 | |||
837 | /* USER CODE END TIM2_Init 0 */ |
||
838 | |||
839 | TIM_ClockConfigTypeDef sClockSourceConfig = {0}; |
||
840 | TIM_MasterConfigTypeDef sMasterConfig = {0}; |
||
841 | TIM_IC_InitTypeDef sConfigIC = {0}; |
||
842 | |||
843 | /* USER CODE BEGIN TIM2_Init 1 */ |
||
844 | |||
845 | /* USER CODE END TIM2_Init 1 */ |
||
846 | htim2.Instance = TIM2; |
||
41 | mjames | 847 | htim2.Init.Prescaler = 719; |
38 | mjames | 848 | htim2.Init.CounterMode = TIM_COUNTERMODE_UP; |
849 | htim2.Init.Period = 65535; |
||
850 | htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
||
851 | htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
||
852 | if (HAL_TIM_Base_Init(&htim2) != HAL_OK) |
||
853 | { |
||
854 | Error_Handler(); |
||
855 | } |
||
856 | sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; |
||
857 | if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK) |
||
858 | { |
||
859 | Error_Handler(); |
||
860 | } |
||
861 | if (HAL_TIM_IC_Init(&htim2) != HAL_OK) |
||
862 | { |
||
863 | Error_Handler(); |
||
864 | } |
||
865 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE; |
||
866 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
||
867 | if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK) |
||
868 | { |
||
869 | Error_Handler(); |
||
870 | } |
||
871 | sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING; |
||
872 | sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI; |
||
873 | sConfigIC.ICPrescaler = TIM_ICPSC_DIV1; |
||
874 | sConfigIC.ICFilter = 15; |
||
875 | if (HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_1) != HAL_OK) |
||
876 | { |
||
877 | Error_Handler(); |
||
878 | } |
||
41 | mjames | 879 | sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_FALLING; |
880 | sConfigIC.ICSelection = TIM_ICSELECTION_INDIRECTTI; |
||
881 | sConfigIC.ICFilter = 0; |
||
882 | if (HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_2) != HAL_OK) |
||
883 | { |
||
884 | Error_Handler(); |
||
885 | } |
||
38 | mjames | 886 | /* USER CODE BEGIN TIM2_Init 2 */ |
887 | |||
888 | /* USER CODE END TIM2_Init 2 */ |
||
889 | |||
890 | } |
||
891 | |||
892 | /** |
||
893 | * @brief TIM3 Initialization Function |
||
894 | * @param None |
||
895 | * @retval None |
||
896 | */ |
||
897 | static void MX_TIM3_Init(void) |
||
898 | { |
||
899 | |||
900 | /* USER CODE BEGIN TIM3_Init 0 */ |
||
901 | |||
902 | /* USER CODE END TIM3_Init 0 */ |
||
903 | |||
904 | TIM_ClockConfigTypeDef sClockSourceConfig = {0}; |
||
905 | TIM_MasterConfigTypeDef sMasterConfig = {0}; |
||
906 | TIM_OC_InitTypeDef sConfigOC = {0}; |
||
907 | |||
908 | /* USER CODE BEGIN TIM3_Init 1 */ |
||
909 | |||
910 | /* USER CODE END TIM3_Init 1 */ |
||
911 | htim3.Instance = TIM3; |
||
41 | mjames | 912 | htim3.Init.Prescaler = 719; |
38 | mjames | 913 | htim3.Init.CounterMode = TIM_COUNTERMODE_UP; |
41 | mjames | 914 | htim3.Init.Period = 199; |
38 | mjames | 915 | htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
916 | htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
||
917 | if (HAL_TIM_Base_Init(&htim3) != HAL_OK) |
||
918 | { |
||
919 | Error_Handler(); |
||
920 | } |
||
921 | sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; |
||
922 | if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK) |
||
923 | { |
||
924 | Error_Handler(); |
||
925 | } |
||
926 | if (HAL_TIM_OC_Init(&htim3) != HAL_OK) |
||
927 | { |
||
928 | Error_Handler(); |
||
929 | } |
||
930 | if (HAL_TIM_OnePulse_Init(&htim3, TIM_OPMODE_SINGLE) != HAL_OK) |
||
931 | { |
||
932 | Error_Handler(); |
||
933 | } |
||
934 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_OC1; |
||
935 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
||
936 | if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK) |
||
937 | { |
||
938 | Error_Handler(); |
||
939 | } |
||
940 | sConfigOC.OCMode = TIM_OCMODE_TIMING; |
||
41 | mjames | 941 | sConfigOC.Pulse = 198; |
38 | mjames | 942 | sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; |
943 | sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; |
||
944 | if (HAL_TIM_OC_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) |
||
945 | { |
||
946 | Error_Handler(); |
||
947 | } |
||
948 | /* USER CODE BEGIN TIM3_Init 2 */ |
||
949 | |||
950 | /* USER CODE END TIM3_Init 2 */ |
||
951 | |||
952 | } |
||
953 | |||
954 | /** |
||
955 | * @brief TIM4 Initialization Function |
||
956 | * @param None |
||
957 | * @retval None |
||
958 | */ |
||
959 | static void MX_TIM4_Init(void) |
||
960 | { |
||
961 | |||
962 | /* USER CODE BEGIN TIM4_Init 0 */ |
||
963 | |||
964 | /* USER CODE END TIM4_Init 0 */ |
||
965 | |||
966 | TIM_ClockConfigTypeDef sClockSourceConfig = {0}; |
||
967 | TIM_MasterConfigTypeDef sMasterConfig = {0}; |
||
968 | |||
969 | /* USER CODE BEGIN TIM4_Init 1 */ |
||
970 | |||
971 | /* USER CODE END TIM4_Init 1 */ |
||
972 | htim4.Instance = TIM4; |
||
41 | mjames | 973 | htim4.Init.Prescaler = 719; |
38 | mjames | 974 | htim4.Init.CounterMode = TIM_COUNTERMODE_UP; |
975 | htim4.Init.Period = 9999; |
||
976 | htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
||
977 | htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
||
978 | if (HAL_TIM_Base_Init(&htim4) != HAL_OK) |
||
979 | { |
||
980 | Error_Handler(); |
||
981 | } |
||
982 | sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; |
||
983 | if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK) |
||
984 | { |
||
985 | Error_Handler(); |
||
986 | } |
||
987 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE; |
||
988 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
||
989 | if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK) |
||
990 | { |
||
991 | Error_Handler(); |
||
992 | } |
||
993 | /* USER CODE BEGIN TIM4_Init 2 */ |
||
994 | |||
995 | /* USER CODE END TIM4_Init 2 */ |
||
996 | |||
997 | } |
||
998 | |||
999 | /** |
||
1000 | * @brief USART1 Initialization Function |
||
1001 | * @param None |
||
1002 | * @retval None |
||
1003 | */ |
||
1004 | static void MX_USART1_UART_Init(void) |
||
1005 | { |
||
1006 | |||
1007 | /* USER CODE BEGIN USART1_Init 0 */ |
||
1008 | |||
1009 | /* USER CODE END USART1_Init 0 */ |
||
1010 | |||
1011 | /* USER CODE BEGIN USART1_Init 1 */ |
||
1012 | |||
1013 | /* USER CODE END USART1_Init 1 */ |
||
1014 | huart1.Instance = USART1; |
||
1015 | huart1.Init.BaudRate = 19200; |
||
1016 | huart1.Init.WordLength = UART_WORDLENGTH_8B; |
||
1017 | huart1.Init.StopBits = UART_STOPBITS_1; |
||
1018 | huart1.Init.Parity = UART_PARITY_NONE; |
||
1019 | huart1.Init.Mode = UART_MODE_TX_RX; |
||
1020 | huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; |
||
1021 | huart1.Init.OverSampling = UART_OVERSAMPLING_16; |
||
1022 | if (HAL_UART_Init(&huart1) != HAL_OK) |
||
1023 | { |
||
1024 | Error_Handler(); |
||
1025 | } |
||
1026 | /* USER CODE BEGIN USART1_Init 2 */ |
||
1027 | |||
1028 | /* USER CODE END USART1_Init 2 */ |
||
1029 | |||
1030 | } |
||
1031 | |||
1032 | /** |
||
1033 | * Enable DMA controller clock |
||
1034 | */ |
||
1035 | static void MX_DMA_Init(void) |
||
1036 | { |
||
1037 | |||
1038 | /* DMA controller clock enable */ |
||
1039 | __HAL_RCC_DMA1_CLK_ENABLE(); |
||
1040 | |||
1041 | /* DMA interrupt init */ |
||
1042 | /* DMA1_Channel1_IRQn interrupt configuration */ |
||
1043 | HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0); |
||
1044 | HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn); |
||
1045 | |||
1046 | } |
||
1047 | |||
1048 | /** |
||
1049 | * @brief GPIO Initialization Function |
||
1050 | * @param None |
||
1051 | * @retval None |
||
1052 | */ |
||
1053 | static void MX_GPIO_Init(void) |
||
1054 | { |
||
1055 | GPIO_InitTypeDef GPIO_InitStruct = {0}; |
||
1056 | |||
1057 | /* GPIO Ports Clock Enable */ |
||
1058 | __HAL_RCC_GPIOC_CLK_ENABLE(); |
||
1059 | __HAL_RCC_GPIOD_CLK_ENABLE(); |
||
1060 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
||
1061 | __HAL_RCC_GPIOB_CLK_ENABLE(); |
||
1062 | |||
1063 | /*Configure GPIO pin Output Level */ |
||
1064 | HAL_GPIO_WritePin(LED_Blink_GPIO_Port, LED_Blink_Pin, GPIO_PIN_RESET); |
||
1065 | |||
1066 | /*Configure GPIO pin Output Level */ |
||
43 | mjames | 1067 | HAL_GPIO_WritePin(GPIOB, SPI_CS_Clk_Pin|SPI_CS_D_Pin|ENA_AUX_5V_Pin, GPIO_PIN_RESET); |
38 | mjames | 1068 | |
1069 | /*Configure GPIO pin : LED_Blink_Pin */ |
||
1070 | GPIO_InitStruct.Pin = LED_Blink_Pin; |
||
1071 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
||
1072 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
1073 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
1074 | HAL_GPIO_Init(LED_Blink_GPIO_Port, &GPIO_InitStruct); |
||
1075 | |||
43 | mjames | 1076 | /*Configure GPIO pins : SPI_CS_Clk_Pin SPI_CS_D_Pin ENA_AUX_5V_Pin */ |
1077 | GPIO_InitStruct.Pin = SPI_CS_Clk_Pin|SPI_CS_D_Pin|ENA_AUX_5V_Pin; |
||
38 | mjames | 1078 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
1079 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
1080 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
1081 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
||
1082 | |||
1083 | /*Configure GPIO pin : STARTER_ON_Pin */ |
||
1084 | GPIO_InitStruct.Pin = STARTER_ON_Pin; |
||
1085 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
||
1086 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
1087 | HAL_GPIO_Init(STARTER_ON_GPIO_Port, &GPIO_InitStruct); |
||
1088 | |||
1089 | } |
||
1090 | |||
1091 | /* USER CODE BEGIN 4 */ |
||
1092 | |||
1093 | /* USER CODE END 4 */ |
||
1094 | |||
1095 | /** |
||
1096 | * @brief This function is executed in case of error occurrence. |
||
1097 | * @retval None |
||
1098 | */ |
||
1099 | void Error_Handler(void) |
||
1100 | { |
||
1101 | /* USER CODE BEGIN Error_Handler_Debug */ |
||
45 | mjames | 1102 | /* User can add his own implementation to report the HAL error return state */ |
38 | mjames | 1103 | |
1104 | /* USER CODE END Error_Handler_Debug */ |
||
1105 | } |
||
1106 | |||
1107 | #ifdef USE_FULL_ASSERT |
||
1108 | /** |
||
1109 | * @brief Reports the name of the source file and the source line number |
||
1110 | * where the assert_param error has occurred. |
||
1111 | * @param file: pointer to the source file name |
||
1112 | * @param line: assert_param error line source number |
||
1113 | * @retval None |
||
1114 | */ |
||
1115 | void assert_failed(uint8_t *file, uint32_t line) |
||
1116 | { |
||
1117 | /* USER CODE BEGIN 6 */ |
||
1118 | /* User can add his own implementation to report the file name and line number, |
||
1119 | tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ |
||
1120 | /* USER CODE END 6 */ |
||
1121 | } |
||
1122 | #endif /* USE_FULL_ASSERT */ |