Go to most recent revision | Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 50 | mjames | 1 | /* USER CODE BEGIN Header */ |
| 2 | mjames | 2 | /** |
| 52 | mjames | 3 | ****************************************************************************** |
| 4 | * @file : main.c |
||
| 5 | * @brief : Main program body |
||
| 6 | ****************************************************************************** |
||
| 7 | * @attention |
||
| 8 | * |
||
| 9 | * <h2><center>© Copyright (c) 2020 STMicroelectronics. |
||
| 10 | * All rights reserved.</center></h2> |
||
| 11 | * |
||
| 12 | * This software component is licensed by ST under BSD 3-Clause license, |
||
| 13 | * the "License"; You may not use this file except in compliance with the |
||
| 14 | * License. You may obtain a copy of the License at: |
||
| 15 | * opensource.org/licenses/BSD-3-Clause |
||
| 16 | * |
||
| 17 | ****************************************************************************** |
||
| 18 | */ |
||
| 50 | mjames | 19 | /* USER CODE END Header */ |
| 2 | mjames | 20 | /* Includes ------------------------------------------------------------------*/ |
| 50 | mjames | 21 | #include "main.h" |
| 2 | mjames | 22 | |
| 50 | mjames | 23 | /* Private includes ----------------------------------------------------------*/ |
| 2 | mjames | 24 | /* USER CODE BEGIN Includes */ |
| 50 | mjames | 25 | |
| 26 | #include "libPLX/plx.h" |
||
| 27 | #include "libSerial/serial.H" |
||
| 28 | #include "libSmallPrintf/small_printf.h" |
||
| 4 | mjames | 29 | #include "switches.h" |
| 2 | mjames | 30 | |
| 31 | /* USER CODE END Includes */ |
||
| 32 | |||
| 50 | mjames | 33 | /* Private typedef -----------------------------------------------------------*/ |
| 34 | /* USER CODE BEGIN PTD */ |
||
| 35 | |||
| 36 | /* USER CODE END PTD */ |
||
| 37 | |||
| 38 | /* Private define ------------------------------------------------------------*/ |
||
| 39 | /* USER CODE BEGIN PD */ |
||
| 40 | /* USER CODE END PD */ |
||
| 41 | |||
| 42 | /* Private macro -------------------------------------------------------------*/ |
||
| 43 | /* USER CODE BEGIN PM */ |
||
| 44 | |||
| 45 | /* USER CODE END PM */ |
||
| 46 | |||
| 2 | mjames | 47 | /* Private variables ---------------------------------------------------------*/ |
| 48 | SPI_HandleTypeDef hspi1; |
||
| 49 | |||
| 50 | mjames | 50 | TIM_HandleTypeDef htim2; |
| 44 | mjames | 51 | TIM_HandleTypeDef htim3; |
| 52 | TIM_HandleTypeDef htim9; |
||
| 53 | |||
| 3 | mjames | 54 | UART_HandleTypeDef huart1; |
| 2 | mjames | 55 | UART_HandleTypeDef huart2; |
| 23 | mjames | 56 | UART_HandleTypeDef huart3; |
| 2 | mjames | 57 | |
| 58 | /* USER CODE BEGIN PV */ |
||
| 59 | /* Private variables ---------------------------------------------------------*/ |
||
| 60 | |||
| 50 | mjames | 61 | context_t contexts[MAX_DISPLAYS]; |
| 62 | |||
| 24 | mjames | 63 | /* timeout when the ignition is switched off */ |
| 64 | #define IGNITION_OFF_TIMEOUT 30000UL |
||
| 65 | |||
| 52 | mjames | 66 | #define LOGGER_INTERVAL 500UL |
| 14 | mjames | 67 | |
| 50 | mjames | 68 | const int DialTimeout = 50; // about 20 seconds after twiddle, save the dial position. |
| 18 | mjames | 69 | |
| 50 | mjames | 70 | uint16_t dial_nvram[MAX_DISPLAYS] __attribute__((section(".NVRAM_Data"))); |
| 14 | mjames | 71 | |
| 50 | mjames | 72 | data_t Data; |
| 7 | mjames | 73 | int Max[MAXRDG]; |
| 74 | int Min[MAXRDG]; |
||
| 75 | int PLXItems; |
||
| 24 | mjames | 76 | |
| 27 | mjames | 77 | uint32_t Latch_Timer = IGNITION_OFF_TIMEOUT; |
| 24 | mjames | 78 | |
| 2 | mjames | 79 | /* USER CODE END PV */ |
| 80 | |||
| 81 | /* Private function prototypes -----------------------------------------------*/ |
||
| 52 | mjames | 82 | void |
| 83 | SystemClock_Config (void); |
||
| 84 | static void |
||
| 85 | MX_GPIO_Init (void); |
||
| 86 | static void |
||
| 87 | MX_SPI1_Init (void); |
||
| 88 | static void |
||
| 89 | MX_USART1_UART_Init (void); |
||
| 90 | static void |
||
| 91 | MX_USART2_UART_Init (void); |
||
| 92 | static void |
||
| 93 | MX_USART3_UART_Init (void); |
||
| 94 | static void |
||
| 95 | MX_TIM3_Init (void); |
||
| 96 | static void |
||
| 97 | MX_TIM9_Init (void); |
||
| 98 | static void |
||
| 99 | MX_TIM2_Init (void); |
||
| 2 | mjames | 100 | /* USER CODE BEGIN PFP */ |
| 101 | |||
| 7 | mjames | 102 | // the dial is the switch number we are using. |
| 103 | // suppress is the ItemIndex we wish to suppress on this display |
||
| 52 | mjames | 104 | int |
| 105 | DisplayCurrent (int dial, int suppress) |
||
| 7 | mjames | 106 | { |
| 50 | mjames | 107 | if (PLXItems == 0) |
| 108 | return -1; |
||
| 14 | mjames | 109 | |
| 50 | mjames | 110 | int itemIndex = dial_pos[dial] % PLXItems; |
| 2 | mjames | 111 | |
| 52 | mjames | 112 | return cc_display (dial, itemIndex, suppress); |
| 50 | mjames | 113 | } |
| 30 | mjames | 114 | |
| 50 | mjames | 115 | /* USER CODE END PFP */ |
| 14 | mjames | 116 | |
| 50 | mjames | 117 | /* Private user code ---------------------------------------------------------*/ |
| 118 | /* USER CODE BEGIN 0 */ |
||
| 14 | mjames | 119 | |
| 7 | mjames | 120 | /* USER CODE END 0 */ |
| 2 | mjames | 121 | |
| 50 | mjames | 122 | /** |
| 52 | mjames | 123 | * @brief The application entry point. |
| 124 | * @retval int |
||
| 125 | */ |
||
| 126 | int |
||
| 127 | main (void) |
||
| 7 | mjames | 128 | { |
| 16 | mjames | 129 | /* USER CODE BEGIN 1 */ |
| 50 | mjames | 130 | __HAL_RCC_SPI1_CLK_ENABLE() |
| 131 | ; |
||
| 132 | __HAL_RCC_USART1_CLK_ENABLE() |
||
| 133 | ; // PLX main port |
||
| 134 | __HAL_RCC_USART2_CLK_ENABLE() |
||
| 135 | ; // debug port |
||
| 136 | __HAL_RCC_USART3_CLK_ENABLE () |
||
| 137 | ; // Bluetooth port |
||
| 2 | mjames | 138 | |
| 50 | mjames | 139 | __HAL_RCC_TIM3_CLK_ENABLE(); |
| 2 | mjames | 140 | |
| 50 | mjames | 141 | __HAL_RCC_TIM9_CLK_ENABLE(); |
| 23 | mjames | 142 | |
| 16 | mjames | 143 | /* USER CODE END 1 */ |
| 2 | mjames | 144 | |
| 50 | mjames | 145 | /* MCU Configuration--------------------------------------------------------*/ |
| 6 | mjames | 146 | |
| 16 | mjames | 147 | /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ |
| 52 | mjames | 148 | HAL_Init (); |
| 2 | mjames | 149 | |
| 50 | mjames | 150 | /* USER CODE BEGIN Init */ |
| 151 | |||
| 152 | /* USER CODE END Init */ |
||
| 153 | |||
| 16 | mjames | 154 | /* Configure the system clock */ |
| 52 | mjames | 155 | SystemClock_Config (); |
| 2 | mjames | 156 | |
| 50 | mjames | 157 | /* USER CODE BEGIN SysInit */ |
| 158 | |||
| 159 | /* USER CODE END SysInit */ |
||
| 160 | |||
| 16 | mjames | 161 | /* Initialize all configured peripherals */ |
| 52 | mjames | 162 | MX_GPIO_Init (); |
| 163 | MX_SPI1_Init (); |
||
| 164 | MX_USART1_UART_Init (); |
||
| 165 | MX_USART2_UART_Init (); |
||
| 166 | MX_USART3_UART_Init (); |
||
| 167 | MX_TIM3_Init (); |
||
| 168 | MX_TIM9_Init (); |
||
| 169 | MX_TIM2_Init (); |
||
| 16 | mjames | 170 | /* USER CODE BEGIN 2 */ |
| 2 | mjames | 171 | |
| 50 | mjames | 172 | /* Turn on USART1 IRQ */ |
| 52 | mjames | 173 | HAL_NVIC_SetPriority (USART1_IRQn, 2, 0); |
| 174 | HAL_NVIC_EnableIRQ (USART1_IRQn); |
||
| 4 | mjames | 175 | |
| 50 | mjames | 176 | /* Turn on USART2 IRQ */ |
| 52 | mjames | 177 | HAL_NVIC_SetPriority (USART2_IRQn, 4, 0); |
| 178 | HAL_NVIC_EnableIRQ (USART2_IRQn); |
||
| 2 | mjames | 179 | |
| 50 | mjames | 180 | /* turn on USART3 IRQ */ |
| 52 | mjames | 181 | HAL_NVIC_SetPriority (USART3_IRQn, 4, 0); |
| 182 | HAL_NVIC_EnableIRQ (USART3_IRQn); |
||
| 4 | mjames | 183 | |
| 50 | mjames | 184 | /* setup the USART control blocks */ |
| 52 | mjames | 185 | init_usart_ctl (&uc1, huart1.Instance); |
| 186 | init_usart_ctl (&uc2, huart2.Instance); |
||
| 187 | init_usart_ctl (&uc3, huart3.Instance); |
||
| 23 | mjames | 188 | |
| 52 | mjames | 189 | EnableSerialRxInterrupt (&uc1); |
| 190 | EnableSerialRxInterrupt (&uc2); |
||
| 191 | EnableSerialRxInterrupt (&uc3); |
||
| 23 | mjames | 192 | |
| 52 | mjames | 193 | HAL_TIM_Encoder_Start (&htim3, TIM_CHANNEL_ALL); |
| 23 | mjames | 194 | |
| 52 | mjames | 195 | HAL_TIM_Encoder_Start (&htim9, TIM_CHANNEL_ALL); |
| 44 | mjames | 196 | |
| 50 | mjames | 197 | // Switch handler called on sysTick interrupt. |
| 52 | mjames | 198 | InitSwitches (); |
| 2 | mjames | 199 | |
| 52 | mjames | 200 | cc_init (); |
| 23 | mjames | 201 | |
| 50 | mjames | 202 | int i; |
| 203 | for (i = 0; i < 2; i++) |
||
| 52 | mjames | 204 | { |
| 205 | dial_pos[i] = dial_nvram[i]; |
||
| 206 | } |
||
| 7 | mjames | 207 | |
| 50 | mjames | 208 | /* reset the display timeout, latch on power from accessories */ |
| 209 | Latch_Timer = IGNITION_OFF_TIMEOUT; |
||
| 52 | mjames | 210 | HAL_GPIO_WritePin (POWER_LATCH_GPIO_Port, POWER_LATCH_Pin, GPIO_PIN_RESET); |
| 16 | mjames | 211 | |
| 212 | /* USER CODE END 2 */ |
||
| 7 | mjames | 213 | |
| 16 | mjames | 214 | /* Infinite loop */ |
| 215 | /* USER CODE BEGIN WHILE */ |
||
| 52 | mjames | 216 | while (1) |
| 217 | { |
||
| 7 | mjames | 218 | |
| 52 | mjames | 219 | /* while ignition is on, keep resetting power latch timer */ |
| 220 | if (HAL_GPIO_ReadPin (IGNITION_GPIO_Port, IGNITION_Pin) == GPIO_PIN_RESET) |
||
| 221 | { |
||
| 222 | Latch_Timer = HAL_GetTick () + IGNITION_OFF_TIMEOUT; |
||
| 223 | } |
||
| 224 | else |
||
| 225 | { |
||
| 226 | /* if the ignition has been off for a while, then turn off power */ |
||
| 227 | if (HAL_GetTick () > Latch_Timer) |
||
| 228 | { |
||
| 229 | HAL_GPIO_WritePin (POWER_LATCH_GPIO_Port, POWER_LATCH_Pin, |
||
| 230 | GPIO_PIN_RESET); |
||
| 231 | } |
||
| 232 | } |
||
| 27 | mjames | 233 | |
| 52 | mjames | 234 | uint32_t timeout = 0; // |
| 27 | mjames | 235 | |
| 52 | mjames | 236 | uint32_t nextTick = HAL_GetTick () + LOGGER_INTERVAL; |
| 237 | uint8_t log = 0; |
||
| 238 | // PLX decoder protocols |
||
| 239 | char PLXPacket = 0; |
||
| 240 | for (i = 0; i < MAXRDG; i++) |
||
| 241 | { |
||
| 242 | Max[i] = 0; |
||
| 243 | Min[i] = 0xFFF; // 12 bit max value |
||
| 244 | } |
||
| 27 | mjames | 245 | |
| 52 | mjames | 246 | int PLXPtr = 0; |
| 24 | mjames | 247 | |
| 52 | mjames | 248 | while (1) |
| 249 | { |
||
| 250 | // Handle the bluetooth pairing function by pressing both buttons. |
||
| 251 | if ((push_pos[0] == 1) && (push_pos[1] == 1)) |
||
| 252 | { |
||
| 253 | HAL_GPIO_WritePin (BT_BUTTON_GPIO_Port, BT_BUTTON_Pin, |
||
| 254 | GPIO_PIN_RESET); |
||
| 255 | } |
||
| 256 | else |
||
| 257 | { |
||
| 258 | HAL_GPIO_WritePin (BT_BUTTON_GPIO_Port, BT_BUTTON_Pin, |
||
| 259 | GPIO_PIN_SET); |
||
| 260 | } |
||
| 7 | mjames | 261 | |
| 52 | mjames | 262 | uint16_t cc = SerialCharsReceived (&uc1); |
| 263 | int chr; |
||
| 264 | if (cc == 0) |
||
| 265 | { |
||
| 266 | timeout++; |
||
| 267 | if (timeout % 1000 == 0) |
||
| 268 | { |
||
| 269 | const char msg[] = "Timeout\r\n"; |
||
| 270 | char *p = msg; |
||
| 271 | while (*p) |
||
| 272 | { |
||
| 273 | PutCharSerial (&uc3, *p++); |
||
| 274 | } |
||
| 38 | mjames | 275 | |
| 52 | mjames | 276 | } |
| 38 | mjames | 277 | |
| 52 | mjames | 278 | if (timeout > 60000) |
| 279 | { |
||
| 7 | mjames | 280 | |
| 52 | mjames | 281 | // do turn off screen |
| 282 | } |
||
| 27 | mjames | 283 | |
| 52 | mjames | 284 | } |
| 285 | for (chr = 0; chr < cc; chr++) |
||
| 286 | { |
||
| 287 | char c = GetCharSerial (&uc1); |
||
| 7 | mjames | 288 | |
| 52 | mjames | 289 | if (c == PLX_Start) // at any time if the start byte appears, reset the pointers |
| 290 | { |
||
| 291 | PLXPtr = 0; // reset the pointer |
||
| 292 | PLXPacket = 1; |
||
| 293 | timeout = 0; // Reset the timer |
||
| 294 | if (HAL_GetTick () > nextTick) |
||
| 295 | { |
||
| 296 | nextTick = HAL_GetTick () + LOGGER_INTERVAL; |
||
| 297 | log = 1; |
||
| 298 | } |
||
| 299 | else |
||
| 300 | log = 0; |
||
| 301 | } |
||
| 302 | else if (c == PLX_Stop) |
||
| 303 | { |
||
| 304 | if (PLXPacket) |
||
| 305 | { |
||
| 306 | // we can now decode the selected parameter |
||
| 307 | PLXItems = PLXPtr / sizeof(PLX_SensorInfo); // total |
||
| 308 | // saturate the rotary switch position |
||
| 9 | mjames | 309 | |
| 52 | mjames | 310 | int DataVal; |
| 311 | // process min/max |
||
| 312 | for (i = 0; i < PLXItems; i++) |
||
| 313 | { |
||
| 314 | // Send item to BT |
||
| 315 | uint16_t addr = ConvPLX (Data.Sensor[i].AddrH, |
||
| 316 | Data.Sensor[i].AddrL); |
||
| 317 | uint8_t inst = Data.Sensor[i].Instance; |
||
| 318 | uint16_t reading = ConvPLX (Data.Sensor[i].ReadingH, |
||
| 319 | Data.Sensor[i].ReadingL); |
||
| 23 | mjames | 320 | |
| 52 | mjames | 321 | if (log) |
| 322 | { |
||
| 323 | char outbuff[100]; |
||
| 30 | mjames | 324 | |
| 52 | mjames | 325 | int cnt = small_sprintf (outbuff, "$LOG%d,%d,%d", addr, |
| 326 | inst, reading); |
||
| 4 | mjames | 327 | |
| 2 | mjames | 328 | |
| 6 | mjames | 329 | |
| 52 | mjames | 330 | int ck; |
| 331 | int sum = 0; |
||
| 332 | for (ck=1 ; ck < cnt ; ck++) |
||
| 333 | sum += outbuff[ck]; |
||
| 334 | cnt += small_sprintf(outbuff+cnt,"*%02X\n",sum & 0xFF); |
||
| 335 | for (ck = 0; ck <cnt; ck++) |
||
| 336 | PutCharSerial (&uc3, outbuff[ck]); |
||
| 50 | mjames | 337 | |
| 52 | mjames | 338 | } |
| 339 | DataVal = ConvPLX (Data.Sensor[i].ReadingH, |
||
| 340 | Data.Sensor[i].ReadingL); |
||
| 341 | if (DataVal > Max[i]) |
||
| 342 | { |
||
| 343 | Max[i] = DataVal; |
||
| 344 | } |
||
| 345 | if (DataVal < Min[i]) |
||
| 346 | { |
||
| 347 | Min[i] = DataVal; |
||
| 348 | } |
||
| 349 | } |
||
| 50 | mjames | 350 | |
| 52 | mjames | 351 | // now to display the information |
| 352 | int suppress = DisplayCurrent (0, -1); |
||
| 353 | DisplayCurrent (1, suppress); |
||
| 354 | } |
||
| 355 | PLXPtr = 0; |
||
| 356 | PLXPacket = 0; |
||
| 357 | } |
||
| 358 | else if (c > PLX_Stop) // illegal char, restart reading |
||
| 359 | { |
||
| 360 | PLXPacket = 0; |
||
| 361 | PLXPtr = 0; |
||
| 362 | } |
||
| 363 | else if (PLXPacket && PLXPtr < sizeof(Data.Bytes)) |
||
| 364 | { |
||
| 365 | Data.Bytes[PLXPtr++] = c; |
||
| 366 | } |
||
| 367 | |||
| 368 | } |
||
| 369 | |||
| 370 | HAL_Delay (1); |
||
| 371 | } |
||
| 372 | |||
| 373 | /* USER CODE END WHILE */ |
||
| 374 | |||
| 375 | /* USER CODE BEGIN 3 */ |
||
| 376 | } |
||
| 16 | mjames | 377 | /* USER CODE END 3 */ |
| 2 | mjames | 378 | } |
| 379 | |||
| 50 | mjames | 380 | /** |
| 52 | mjames | 381 | * @brief System Clock Configuration |
| 382 | * @retval None |
||
| 383 | */ |
||
| 384 | void |
||
| 385 | SystemClock_Config (void) |
||
| 5 | mjames | 386 | { |
| 52 | mjames | 387 | RCC_OscInitTypeDef RCC_OscInitStruct = |
| 388 | { 0 }; |
||
| 389 | RCC_ClkInitTypeDef RCC_ClkInitStruct = |
||
| 390 | { 0 }; |
||
| 2 | mjames | 391 | |
| 50 | mjames | 392 | /** Configure the main internal regulator output voltage |
| 52 | mjames | 393 | */ |
| 29 | mjames | 394 | __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); |
| 50 | mjames | 395 | /** Initializes the RCC Oscillators according to the specified parameters |
| 52 | mjames | 396 | * in the RCC_OscInitTypeDef structure. |
| 397 | */ |
||
| 44 | mjames | 398 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; |
| 399 | RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS; |
||
| 16 | mjames | 400 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
| 44 | mjames | 401 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; |
| 402 | RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12; |
||
| 29 | mjames | 403 | RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV3; |
| 52 | mjames | 404 | if (HAL_RCC_OscConfig (&RCC_OscInitStruct) != HAL_OK) |
| 405 | { |
||
| 406 | Error_Handler (); |
||
| 407 | } |
||
| 50 | mjames | 408 | /** Initializes the CPU, AHB and APB buses clocks |
| 52 | mjames | 409 | */ |
| 410 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |
||
| 411 | | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; |
||
| 16 | mjames | 412 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
| 413 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
||
| 29 | mjames | 414 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; |
| 16 | mjames | 415 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; |
| 50 | mjames | 416 | |
| 52 | mjames | 417 | if (HAL_RCC_ClockConfig (&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) |
| 418 | { |
||
| 419 | Error_Handler (); |
||
| 420 | } |
||
| 2 | mjames | 421 | } |
| 422 | |||
| 50 | mjames | 423 | /** |
| 52 | mjames | 424 | * @brief SPI1 Initialization Function |
| 425 | * @param None |
||
| 426 | * @retval None |
||
| 427 | */ |
||
| 428 | static void |
||
| 429 | MX_SPI1_Init (void) |
||
| 5 | mjames | 430 | { |
| 2 | mjames | 431 | |
| 50 | mjames | 432 | /* USER CODE BEGIN SPI1_Init 0 */ |
| 433 | |||
| 434 | /* USER CODE END SPI1_Init 0 */ |
||
| 435 | |||
| 436 | /* USER CODE BEGIN SPI1_Init 1 */ |
||
| 437 | |||
| 438 | /* USER CODE END SPI1_Init 1 */ |
||
| 439 | /* SPI1 parameter configuration*/ |
||
| 16 | mjames | 440 | hspi1.Instance = SPI1; |
| 441 | hspi1.Init.Mode = SPI_MODE_MASTER; |
||
| 442 | hspi1.Init.Direction = SPI_DIRECTION_1LINE; |
||
| 443 | hspi1.Init.DataSize = SPI_DATASIZE_8BIT; |
||
| 444 | hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH; |
||
| 445 | hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; |
||
| 446 | hspi1.Init.NSS = SPI_NSS_SOFT; |
||
| 50 | mjames | 447 | hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; |
| 16 | mjames | 448 | hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; |
| 449 | hspi1.Init.TIMode = SPI_TIMODE_DISABLE; |
||
| 450 | hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; |
||
| 451 | hspi1.Init.CRCPolynomial = 10; |
||
| 52 | mjames | 452 | if (HAL_SPI_Init (&hspi1) != HAL_OK) |
| 453 | { |
||
| 454 | Error_Handler (); |
||
| 455 | } |
||
| 50 | mjames | 456 | /* USER CODE BEGIN SPI1_Init 2 */ |
| 2 | mjames | 457 | |
| 50 | mjames | 458 | /* USER CODE END SPI1_Init 2 */ |
| 459 | |||
| 2 | mjames | 460 | } |
| 461 | |||
| 50 | mjames | 462 | /** |
| 52 | mjames | 463 | * @brief TIM2 Initialization Function |
| 464 | * @param None |
||
| 465 | * @retval None |
||
| 466 | */ |
||
| 467 | static void |
||
| 468 | MX_TIM2_Init (void) |
||
| 50 | mjames | 469 | { |
| 470 | |||
| 471 | /* USER CODE BEGIN TIM2_Init 0 */ |
||
| 472 | |||
| 473 | /* USER CODE END TIM2_Init 0 */ |
||
| 474 | |||
| 52 | mjames | 475 | TIM_ClockConfigTypeDef sClockSourceConfig = |
| 476 | { 0 }; |
||
| 477 | TIM_MasterConfigTypeDef sMasterConfig = |
||
| 478 | { 0 }; |
||
| 50 | mjames | 479 | |
| 480 | /* USER CODE BEGIN TIM2_Init 1 */ |
||
| 481 | |||
| 482 | /* USER CODE END TIM2_Init 1 */ |
||
| 483 | htim2.Instance = TIM2; |
||
| 484 | htim2.Init.Prescaler = 0; |
||
| 485 | htim2.Init.CounterMode = TIM_COUNTERMODE_UP; |
||
| 486 | htim2.Init.Period = 65535; |
||
| 487 | htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
||
| 488 | htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
||
| 52 | mjames | 489 | if (HAL_TIM_Base_Init (&htim2) != HAL_OK) |
| 490 | { |
||
| 491 | Error_Handler (); |
||
| 492 | } |
||
| 50 | mjames | 493 | sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; |
| 52 | mjames | 494 | if (HAL_TIM_ConfigClockSource (&htim2, &sClockSourceConfig) != HAL_OK) |
| 495 | { |
||
| 496 | Error_Handler (); |
||
| 497 | } |
||
| 50 | mjames | 498 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; |
| 499 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
||
| 52 | mjames | 500 | if (HAL_TIMEx_MasterConfigSynchronization (&htim2, &sMasterConfig) != HAL_OK) |
| 501 | { |
||
| 502 | Error_Handler (); |
||
| 503 | } |
||
| 50 | mjames | 504 | /* USER CODE BEGIN TIM2_Init 2 */ |
| 505 | |||
| 506 | /* USER CODE END TIM2_Init 2 */ |
||
| 507 | |||
| 508 | } |
||
| 509 | |||
| 510 | /** |
||
| 52 | mjames | 511 | * @brief TIM3 Initialization Function |
| 512 | * @param None |
||
| 513 | * @retval None |
||
| 514 | */ |
||
| 515 | static void |
||
| 516 | MX_TIM3_Init (void) |
||
| 44 | mjames | 517 | { |
| 518 | |||
| 50 | mjames | 519 | /* USER CODE BEGIN TIM3_Init 0 */ |
| 44 | mjames | 520 | |
| 50 | mjames | 521 | /* USER CODE END TIM3_Init 0 */ |
| 522 | |||
| 52 | mjames | 523 | TIM_Encoder_InitTypeDef sConfig = |
| 524 | { 0 }; |
||
| 525 | TIM_MasterConfigTypeDef sMasterConfig = |
||
| 526 | { 0 }; |
||
| 50 | mjames | 527 | |
| 528 | /* USER CODE BEGIN TIM3_Init 1 */ |
||
| 529 | |||
| 530 | /* USER CODE END TIM3_Init 1 */ |
||
| 44 | mjames | 531 | htim3.Instance = TIM3; |
| 532 | htim3.Init.Prescaler = 0; |
||
| 533 | htim3.Init.CounterMode = TIM_COUNTERMODE_UP; |
||
| 50 | mjames | 534 | htim3.Init.Period = 65535; |
| 535 | htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
||
| 536 | htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
||
| 44 | mjames | 537 | sConfig.EncoderMode = TIM_ENCODERMODE_TI1; |
| 50 | mjames | 538 | sConfig.IC1Polarity = TIM_ICPOLARITY_RISING; |
| 44 | mjames | 539 | sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI; |
| 540 | sConfig.IC1Prescaler = TIM_ICPSC_DIV1; |
||
| 541 | sConfig.IC1Filter = 15; |
||
| 50 | mjames | 542 | sConfig.IC2Polarity = TIM_ICPOLARITY_RISING; |
| 44 | mjames | 543 | sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI; |
| 544 | sConfig.IC2Prescaler = TIM_ICPSC_DIV1; |
||
| 545 | sConfig.IC2Filter = 15; |
||
| 52 | mjames | 546 | if (HAL_TIM_Encoder_Init (&htim3, &sConfig) != HAL_OK) |
| 547 | { |
||
| 548 | Error_Handler (); |
||
| 549 | } |
||
| 44 | mjames | 550 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; |
| 551 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
||
| 52 | mjames | 552 | if (HAL_TIMEx_MasterConfigSynchronization (&htim3, &sMasterConfig) != HAL_OK) |
| 553 | { |
||
| 554 | Error_Handler (); |
||
| 555 | } |
||
| 50 | mjames | 556 | /* USER CODE BEGIN TIM3_Init 2 */ |
| 44 | mjames | 557 | |
| 50 | mjames | 558 | /* USER CODE END TIM3_Init 2 */ |
| 559 | |||
| 44 | mjames | 560 | } |
| 561 | |||
| 50 | mjames | 562 | /** |
| 52 | mjames | 563 | * @brief TIM9 Initialization Function |
| 564 | * @param None |
||
| 565 | * @retval None |
||
| 566 | */ |
||
| 567 | static void |
||
| 568 | MX_TIM9_Init (void) |
||
| 44 | mjames | 569 | { |
| 570 | |||
| 50 | mjames | 571 | /* USER CODE BEGIN TIM9_Init 0 */ |
| 44 | mjames | 572 | |
| 50 | mjames | 573 | /* USER CODE END TIM9_Init 0 */ |
| 574 | |||
| 52 | mjames | 575 | TIM_Encoder_InitTypeDef sConfig = |
| 576 | { 0 }; |
||
| 577 | TIM_MasterConfigTypeDef sMasterConfig = |
||
| 578 | { 0 }; |
||
| 50 | mjames | 579 | |
| 580 | /* USER CODE BEGIN TIM9_Init 1 */ |
||
| 581 | |||
| 582 | /* USER CODE END TIM9_Init 1 */ |
||
| 44 | mjames | 583 | htim9.Instance = TIM9; |
| 584 | htim9.Init.Prescaler = 0; |
||
| 585 | htim9.Init.CounterMode = TIM_COUNTERMODE_UP; |
||
| 50 | mjames | 586 | htim9.Init.Period = 65535; |
| 587 | htim9.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
||
| 588 | htim9.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
||
| 44 | mjames | 589 | sConfig.EncoderMode = TIM_ENCODERMODE_TI1; |
| 50 | mjames | 590 | sConfig.IC1Polarity = TIM_ICPOLARITY_RISING; |
| 44 | mjames | 591 | sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI; |
| 592 | sConfig.IC1Prescaler = TIM_ICPSC_DIV1; |
||
| 593 | sConfig.IC1Filter = 15; |
||
| 50 | mjames | 594 | sConfig.IC2Polarity = TIM_ICPOLARITY_RISING; |
| 44 | mjames | 595 | sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI; |
| 596 | sConfig.IC2Prescaler = TIM_ICPSC_DIV1; |
||
| 50 | mjames | 597 | sConfig.IC2Filter = 0; |
| 52 | mjames | 598 | if (HAL_TIM_Encoder_Init (&htim9, &sConfig) != HAL_OK) |
| 599 | { |
||
| 600 | Error_Handler (); |
||
| 601 | } |
||
| 44 | mjames | 602 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; |
| 603 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
||
| 52 | mjames | 604 | if (HAL_TIMEx_MasterConfigSynchronization (&htim9, &sMasterConfig) != HAL_OK) |
| 605 | { |
||
| 606 | Error_Handler (); |
||
| 607 | } |
||
| 50 | mjames | 608 | /* USER CODE BEGIN TIM9_Init 2 */ |
| 44 | mjames | 609 | |
| 50 | mjames | 610 | /* USER CODE END TIM9_Init 2 */ |
| 611 | |||
| 44 | mjames | 612 | } |
| 613 | |||
| 50 | mjames | 614 | /** |
| 52 | mjames | 615 | * @brief USART1 Initialization Function |
| 616 | * @param None |
||
| 617 | * @retval None |
||
| 618 | */ |
||
| 619 | static void |
||
| 620 | MX_USART1_UART_Init (void) |
||
| 5 | mjames | 621 | { |
| 3 | mjames | 622 | |
| 50 | mjames | 623 | /* USER CODE BEGIN USART1_Init 0 */ |
| 624 | |||
| 625 | /* USER CODE END USART1_Init 0 */ |
||
| 626 | |||
| 627 | /* USER CODE BEGIN USART1_Init 1 */ |
||
| 628 | |||
| 629 | /* USER CODE END USART1_Init 1 */ |
||
| 16 | mjames | 630 | huart1.Instance = USART1; |
| 631 | huart1.Init.BaudRate = 19200; |
||
| 632 | huart1.Init.WordLength = UART_WORDLENGTH_8B; |
||
| 44 | mjames | 633 | huart1.Init.StopBits = UART_STOPBITS_1; |
| 16 | mjames | 634 | huart1.Init.Parity = UART_PARITY_NONE; |
| 635 | huart1.Init.Mode = UART_MODE_TX_RX; |
||
| 636 | huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; |
||
| 637 | huart1.Init.OverSampling = UART_OVERSAMPLING_16; |
||
| 52 | mjames | 638 | if (HAL_UART_Init (&huart1) != HAL_OK) |
| 639 | { |
||
| 640 | Error_Handler (); |
||
| 641 | } |
||
| 50 | mjames | 642 | /* USER CODE BEGIN USART1_Init 2 */ |
| 3 | mjames | 643 | |
| 50 | mjames | 644 | /* USER CODE END USART1_Init 2 */ |
| 645 | |||
| 3 | mjames | 646 | } |
| 647 | |||
| 50 | mjames | 648 | /** |
| 52 | mjames | 649 | * @brief USART2 Initialization Function |
| 650 | * @param None |
||
| 651 | * @retval None |
||
| 652 | */ |
||
| 653 | static void |
||
| 654 | MX_USART2_UART_Init (void) |
||
| 5 | mjames | 655 | { |
| 2 | mjames | 656 | |
| 50 | mjames | 657 | /* USER CODE BEGIN USART2_Init 0 */ |
| 658 | |||
| 659 | /* USER CODE END USART2_Init 0 */ |
||
| 660 | |||
| 661 | /* USER CODE BEGIN USART2_Init 1 */ |
||
| 662 | |||
| 663 | /* USER CODE END USART2_Init 1 */ |
||
| 16 | mjames | 664 | huart2.Instance = USART2; |
| 665 | huart2.Init.BaudRate = 115200; |
||
| 666 | huart2.Init.WordLength = UART_WORDLENGTH_8B; |
||
| 667 | huart2.Init.StopBits = UART_STOPBITS_1; |
||
| 668 | huart2.Init.Parity = UART_PARITY_NONE; |
||
| 669 | huart2.Init.Mode = UART_MODE_TX_RX; |
||
| 670 | huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; |
||
| 671 | huart2.Init.OverSampling = UART_OVERSAMPLING_16; |
||
| 52 | mjames | 672 | if (HAL_UART_Init (&huart2) != HAL_OK) |
| 673 | { |
||
| 674 | Error_Handler (); |
||
| 675 | } |
||
| 50 | mjames | 676 | /* USER CODE BEGIN USART2_Init 2 */ |
| 2 | mjames | 677 | |
| 50 | mjames | 678 | /* USER CODE END USART2_Init 2 */ |
| 679 | |||
| 2 | mjames | 680 | } |
| 681 | |||
| 50 | mjames | 682 | /** |
| 52 | mjames | 683 | * @brief USART3 Initialization Function |
| 684 | * @param None |
||
| 685 | * @retval None |
||
| 686 | */ |
||
| 687 | static void |
||
| 688 | MX_USART3_UART_Init (void) |
||
| 23 | mjames | 689 | { |
| 690 | |||
| 50 | mjames | 691 | /* USER CODE BEGIN USART3_Init 0 */ |
| 692 | |||
| 693 | /* USER CODE END USART3_Init 0 */ |
||
| 694 | |||
| 695 | /* USER CODE BEGIN USART3_Init 1 */ |
||
| 696 | |||
| 697 | /* USER CODE END USART3_Init 1 */ |
||
| 23 | mjames | 698 | huart3.Instance = USART3; |
| 52 | mjames | 699 | huart3.Init.BaudRate = 9600; |
| 23 | mjames | 700 | huart3.Init.WordLength = UART_WORDLENGTH_8B; |
| 50 | mjames | 701 | huart3.Init.StopBits = UART_STOPBITS_1; |
| 44 | mjames | 702 | huart3.Init.Parity = UART_PARITY_NONE; |
| 23 | mjames | 703 | huart3.Init.Mode = UART_MODE_TX_RX; |
| 704 | huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE; |
||
| 705 | huart3.Init.OverSampling = UART_OVERSAMPLING_16; |
||
| 52 | mjames | 706 | if (HAL_UART_Init (&huart3) != HAL_OK) |
| 707 | { |
||
| 708 | Error_Handler (); |
||
| 709 | } |
||
| 50 | mjames | 710 | /* USER CODE BEGIN USART3_Init 2 */ |
| 23 | mjames | 711 | |
| 50 | mjames | 712 | /* USER CODE END USART3_Init 2 */ |
| 713 | |||
| 23 | mjames | 714 | } |
| 715 | |||
| 50 | mjames | 716 | /** |
| 52 | mjames | 717 | * @brief GPIO Initialization Function |
| 718 | * @param None |
||
| 719 | * @retval None |
||
| 720 | */ |
||
| 721 | static void |
||
| 722 | MX_GPIO_Init (void) |
||
| 5 | mjames | 723 | { |
| 52 | mjames | 724 | GPIO_InitTypeDef GPIO_InitStruct = |
| 725 | { 0 }; |
||
| 2 | mjames | 726 | |
| 16 | mjames | 727 | /* GPIO Ports Clock Enable */ |
| 29 | mjames | 728 | __HAL_RCC_GPIOH_CLK_ENABLE(); |
| 729 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
||
| 730 | __HAL_RCC_GPIOC_CLK_ENABLE(); |
||
| 731 | __HAL_RCC_GPIOB_CLK_ENABLE(); |
||
| 2 | mjames | 732 | |
| 16 | mjames | 733 | /*Configure GPIO pin Output Level */ |
| 52 | mjames | 734 | HAL_GPIO_WritePin (SPI_NSS1_GPIO_Port, SPI_NSS1_Pin, GPIO_PIN_SET); |
| 2 | mjames | 735 | |
| 16 | mjames | 736 | /*Configure GPIO pin Output Level */ |
| 52 | mjames | 737 | HAL_GPIO_WritePin (GPIOA, SPI_CD_Pin | BT_BUTTON_Pin, GPIO_PIN_RESET); |
| 2 | mjames | 738 | |
| 50 | mjames | 739 | /*Configure GPIO pin Output Level */ |
| 52 | mjames | 740 | HAL_GPIO_WritePin (GPIOC, SPI_RESET_Pin | POWER_LATCH_Pin | USB_PWR_Pin, |
| 741 | GPIO_PIN_RESET); |
||
| 50 | mjames | 742 | |
| 743 | /*Configure GPIO pin Output Level */ |
||
| 52 | mjames | 744 | HAL_GPIO_WritePin (SPI_NSS2_GPIO_Port, SPI_NSS2_Pin, GPIO_PIN_SET); |
| 50 | mjames | 745 | |
| 746 | /*Configure GPIO pins : SPI_NSS1_Pin SPI_CD_Pin */ |
||
| 52 | mjames | 747 | GPIO_InitStruct.Pin = SPI_NSS1_Pin | SPI_CD_Pin; |
| 16 | mjames | 748 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
| 29 | mjames | 749 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 16 | mjames | 750 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
| 52 | mjames | 751 | HAL_GPIO_Init (GPIOA, &GPIO_InitStruct); |
| 2 | mjames | 752 | |
| 24 | mjames | 753 | /*Configure GPIO pins : SPI_RESET_Pin SPI_NSS2_Pin POWER_LATCH_Pin USB_PWR_Pin */ |
| 52 | mjames | 754 | GPIO_InitStruct.Pin = SPI_RESET_Pin | SPI_NSS2_Pin | POWER_LATCH_Pin |
| 755 | | USB_PWR_Pin; |
||
| 16 | mjames | 756 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
| 29 | mjames | 757 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 16 | mjames | 758 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
| 52 | mjames | 759 | HAL_GPIO_Init (GPIOC, &GPIO_InitStruct); |
| 2 | mjames | 760 | |
| 44 | mjames | 761 | /*Configure GPIO pins : SW1_PUSH_Pin SW2_PUSH_Pin */ |
| 52 | mjames | 762 | GPIO_InitStruct.Pin = SW1_PUSH_Pin | SW2_PUSH_Pin; |
| 16 | mjames | 763 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
| 32 | mjames | 764 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
| 52 | mjames | 765 | HAL_GPIO_Init (GPIOB, &GPIO_InitStruct); |
| 5 | mjames | 766 | |
| 32 | mjames | 767 | /*Configure GPIO pin : IGNITION_Pin */ |
| 768 | GPIO_InitStruct.Pin = IGNITION_Pin; |
||
| 769 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
||
| 770 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
| 52 | mjames | 771 | HAL_GPIO_Init (IGNITION_GPIO_Port, &GPIO_InitStruct); |
| 32 | mjames | 772 | |
| 37 | mjames | 773 | /*Configure GPIO pin : BT_BUTTON_Pin */ |
| 774 | GPIO_InitStruct.Pin = BT_BUTTON_Pin; |
||
| 775 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD; |
||
| 776 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
| 777 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
| 52 | mjames | 778 | HAL_GPIO_Init (BT_BUTTON_GPIO_Port, &GPIO_InitStruct); |
| 37 | mjames | 779 | |
| 2 | mjames | 780 | } |
| 781 | |||
| 782 | /* USER CODE BEGIN 4 */ |
||
| 783 | |||
| 784 | /* USER CODE END 4 */ |
||
| 785 | |||
| 5 | mjames | 786 | /** |
| 52 | mjames | 787 | * @brief This function is executed in case of error occurrence. |
| 788 | * @retval None |
||
| 789 | */ |
||
| 790 | void |
||
| 791 | Error_Handler (void) |
||
| 5 | mjames | 792 | { |
| 50 | mjames | 793 | /* USER CODE BEGIN Error_Handler_Debug */ |
| 794 | /* User can add his own implementation to report the HAL error return state */ |
||
| 795 | |||
| 796 | /* USER CODE END Error_Handler_Debug */ |
||
| 30 | mjames | 797 | } |
| 5 | mjames | 798 | |
| 50 | mjames | 799 | #ifdef USE_FULL_ASSERT |
| 2 | mjames | 800 | /** |
| 50 | mjames | 801 | * @brief Reports the name of the source file and the source line number |
| 802 | * where the assert_param error has occurred. |
||
| 803 | * @param file: pointer to the source file name |
||
| 804 | * @param line: assert_param error line source number |
||
| 805 | * @retval None |
||
| 806 | */ |
||
| 807 | void assert_failed(uint8_t *file, uint32_t line) |
||
| 29 | mjames | 808 | { |
| 809 | /* USER CODE BEGIN 6 */ |
||
| 50 | mjames | 810 | /* User can add his own implementation to report the file name and line number, |
| 811 | tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ |
||
| 29 | mjames | 812 | /* USER CODE END 6 */ |
| 813 | } |
||
| 50 | mjames | 814 | #endif /* USE_FULL_ASSERT */ |
| 2 | mjames | 815 | |
| 816 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |