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