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