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