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