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