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