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