Rev 71 | Rev 73 | Go to most recent revision | Details | Compare with Previous | 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" |
| 65 | mjames | 31 | #include <string.h> |
| 2 | mjames | 32 | |
| 33 | /* USER CODE END Includes */ |
||
| 34 | |||
| 50 | mjames | 35 | /* Private typedef -----------------------------------------------------------*/ |
| 36 | /* USER CODE BEGIN PTD */ |
||
| 37 | |||
| 38 | /* USER CODE END PTD */ |
||
| 39 | |||
| 40 | /* Private define ------------------------------------------------------------*/ |
||
| 41 | /* USER CODE BEGIN PD */ |
||
| 42 | /* USER CODE END PD */ |
||
| 43 | |||
| 44 | /* Private macro -------------------------------------------------------------*/ |
||
| 45 | /* USER CODE BEGIN PM */ |
||
| 46 | |||
| 47 | /* USER CODE END PM */ |
||
| 48 | |||
| 2 | mjames | 49 | /* Private variables ---------------------------------------------------------*/ |
| 65 | mjames | 50 | I2C_HandleTypeDef hi2c1; |
| 51 | |||
| 62 | mjames | 52 | SPI_HandleTypeDef hspi1; |
| 2 | mjames | 53 | |
| 50 | mjames | 54 | TIM_HandleTypeDef htim2; |
| 44 | mjames | 55 | TIM_HandleTypeDef htim3; |
| 56 | TIM_HandleTypeDef htim9; |
||
| 57 | |||
| 60 | mjames | 58 | UART_HandleTypeDef huart4; |
| 3 | mjames | 59 | UART_HandleTypeDef huart1; |
| 2 | mjames | 60 | UART_HandleTypeDef huart2; |
| 23 | mjames | 61 | UART_HandleTypeDef huart3; |
| 2 | mjames | 62 | |
| 63 | /* USER CODE BEGIN PV */ |
||
| 64 | /* Private variables ---------------------------------------------------------*/ |
||
| 65 | |||
| 50 | mjames | 66 | context_t contexts[MAX_DISPLAYS]; |
| 67 | |||
| 70 | mjames | 68 | ///@brief timeout when the ignition is switched off |
| 24 | mjames | 69 | #define IGNITION_OFF_TIMEOUT 30000UL |
| 70 | |||
| 70 | mjames | 71 | /// @brief 1000mS per logger period, print average per period |
| 72 | #define LOGGER_INTERVAL 1000UL |
||
| 14 | mjames | 73 | |
| 70 | mjames | 74 | /// @brief about 10 seconds after twiddle, save the dial position. |
| 75 | const int DialTimeout = 100; |
||
| 18 | mjames | 76 | |
| 70 | mjames | 77 | /// @brief Data storage for readings |
| 56 | mjames | 78 | info_t Info[MAXRDG]; |
| 79 | |||
| 70 | mjames | 80 | /// @brief Define a null item |
| 81 | const info_t nullInfo = {.Max = 0, |
||
| 82 | .Min = 0xFFF, |
||
| 83 | .sum = 0, |
||
| 84 | .count = 0, |
||
| 85 | .updated = 0, |
||
| 86 | .lastUpdated = 0, |
||
| 87 | .observation = PLX_MAX_OBS, |
||
| 88 | .instance = PLX_MAX_INST}; |
||
| 89 | |||
| 56 | mjames | 90 | /// \brief storage for incoming data |
| 50 | mjames | 91 | data_t Data; |
| 56 | mjames | 92 | |
| 27 | mjames | 93 | uint32_t Latch_Timer = IGNITION_OFF_TIMEOUT; |
| 24 | mjames | 94 | |
| 58 | mjames | 95 | // location for GPS data |
| 96 | Location loc; |
||
| 97 | |||
| 2 | mjames | 98 | /* USER CODE END PV */ |
| 99 | |||
| 100 | /* Private function prototypes -----------------------------------------------*/ |
||
| 58 | mjames | 101 | void SystemClock_Config(void); |
| 102 | static void MX_GPIO_Init(void); |
||
| 103 | static void MX_SPI1_Init(void); |
||
| 104 | static void MX_USART1_UART_Init(void); |
||
| 105 | static void MX_USART2_UART_Init(void); |
||
| 106 | static void MX_USART3_UART_Init(void); |
||
| 107 | static void MX_TIM3_Init(void); |
||
| 108 | static void MX_TIM9_Init(void); |
||
| 109 | static void MX_TIM2_Init(void); |
||
| 60 | mjames | 110 | static void MX_UART4_Init(void); |
| 65 | mjames | 111 | static void MX_I2C1_Init(void); |
| 2 | mjames | 112 | /* USER CODE BEGIN PFP */ |
| 113 | |||
| 7 | mjames | 114 | // the dial is the switch number we are using. |
| 115 | // suppress is the ItemIndex we wish to suppress on this display |
||
| 60 | mjames | 116 | int DisplayCurrent(int dial, int suppress) |
| 7 | mjames | 117 | { |
| 60 | mjames | 118 | return cc_display(dial, suppress); |
| 50 | mjames | 119 | } |
| 30 | mjames | 120 | |
| 70 | mjames | 121 | /// \note HC-05 only accepts : 9600,19200,38400,57600,115200,230400,460800 baud |
| 56 | mjames | 122 | /// \brief Setup Bluetooth module |
| 60 | mjames | 123 | void initModule(usart_ctl *ctl, uint32_t baudRate) |
| 53 | mjames | 124 | { |
| 125 | char initBuf[30]; |
||
| 126 | // switch to command mode |
||
| 70 | mjames | 127 | HAL_GPIO_WritePin(BT_RESET_GPIO_Port, BT_RESET_Pin, GPIO_PIN_RESET); |
| 60 | mjames | 128 | HAL_Delay(500); |
| 129 | setBaud(ctl, 38400); |
||
| 70 | mjames | 130 | int initLen = small_sprintf(initBuf, "AT+UART=%lu,0,0\n", baudRate); |
| 131 | const char buf[] = "AT+RESET\n"; |
||
| 60 | mjames | 132 | sendString(ctl, initBuf, initLen); |
| 70 | mjames | 133 | HAL_Delay(500); |
| 134 | initLen = small_sprintf(initBuf, buf); |
||
| 135 | sendString(ctl, initBuf, initLen); |
||
| 136 | |||
| 60 | mjames | 137 | TxWaitEmpty(ctl); |
| 70 | mjames | 138 | |
| 139 | // clear the button press |
||
| 140 | HAL_GPIO_WritePin(BT_RESET_GPIO_Port, BT_RESET_Pin, GPIO_PIN_SET); |
||
| 141 | |||
| 53 | mjames | 142 | // switch back to normal comms at new baud rate |
| 60 | mjames | 143 | setBaud(ctl, baudRate); |
| 144 | HAL_Delay(100); |
||
| 145 | } |
||
| 53 | mjames | 146 | |
| 60 | mjames | 147 | // workspace for RMC data read from GPS module. |
| 148 | uint8_t rmc_buff[80]; |
||
| 62 | mjames | 149 | volatile uint16_t rmc_length; |
| 60 | mjames | 150 | |
| 151 | uint8_t rmc_callback(uint8_t *data, uint16_t length) |
||
| 152 | { |
||
| 62 | mjames | 153 | rmc_length = length < sizeof(rmc_buff) ? length : sizeof(rmc_buff); |
| 60 | mjames | 154 | memcpy(rmc_buff, data, length); |
| 62 | mjames | 155 | return 0; |
| 53 | mjames | 156 | } |
| 157 | |||
| 63 | mjames | 158 | // check if bluetooth connected |
| 159 | uint8_t btConnected() |
||
| 160 | { |
||
| 65 | mjames | 161 | return HAL_GPIO_ReadPin(BT_STATE_GPIO_Port, BT_STATE_Pin) == GPIO_PIN_SET; |
| 63 | mjames | 162 | } |
| 163 | |||
| 70 | mjames | 164 | /// @brief return true if this slot is unused |
| 165 | /// @param ptr pointer to the slot to |
||
| 166 | uint8_t isUnused(int index) |
||
| 167 | { |
||
| 168 | if (index < 0 || index > MAXRDG) |
||
| 169 | return false; |
||
| 170 | |||
| 171 | return Info[index].instance == PLX_MAX_INST && Info[index].observation == PLX_MAX_OBS; |
||
| 172 | } |
||
| 173 | |||
| 174 | /// @brief Determine if an entry is currently valid |
||
| 175 | /// @param index the number of the array entry to display |
||
| 176 | /// @return true if the entry contains data which is fresh |
||
| 177 | uint8_t isValid(int index) |
||
| 178 | { |
||
| 179 | if (index < 0 || index > MAXRDG) |
||
| 180 | return false; |
||
| 181 | if (isUnused(index)) |
||
| 182 | return false; |
||
| 183 | |||
| 184 | uint32_t age = HAL_GetTick() - Info[index].lastUpdated; |
||
| 185 | |||
| 186 | if (age > 300) |
||
| 187 | return false; |
||
| 188 | |||
| 189 | return true; |
||
| 190 | } |
||
| 191 | |||
| 50 | mjames | 192 | /* USER CODE END PFP */ |
| 14 | mjames | 193 | |
| 50 | mjames | 194 | /* Private user code ---------------------------------------------------------*/ |
| 195 | /* USER CODE BEGIN 0 */ |
||
| 14 | mjames | 196 | |
| 7 | mjames | 197 | /* USER CODE END 0 */ |
| 2 | mjames | 198 | |
| 50 | mjames | 199 | /** |
| 62 | mjames | 200 | * @brief The application entry point. |
| 201 | * @retval int |
||
| 202 | */ |
||
| 58 | mjames | 203 | int main(void) |
| 7 | mjames | 204 | { |
| 16 | mjames | 205 | /* USER CODE BEGIN 1 */ |
| 60 | mjames | 206 | __HAL_RCC_SPI1_CLK_ENABLE(); |
| 207 | __HAL_RCC_USART1_CLK_ENABLE(); // PLX main port |
||
| 208 | __HAL_RCC_USART2_CLK_ENABLE(); // debug port |
||
| 209 | __HAL_RCC_USART3_CLK_ENABLE(); // Bluetooth port |
||
| 61 | mjames | 210 | __HAL_RCC_UART4_CLK_ENABLE(); // NMEA0183 port |
| 2 | mjames | 211 | |
| 50 | mjames | 212 | __HAL_RCC_TIM3_CLK_ENABLE(); |
| 2 | mjames | 213 | |
| 50 | mjames | 214 | __HAL_RCC_TIM9_CLK_ENABLE(); |
| 23 | mjames | 215 | |
| 16 | mjames | 216 | /* USER CODE END 1 */ |
| 2 | mjames | 217 | |
| 50 | mjames | 218 | /* MCU Configuration--------------------------------------------------------*/ |
| 6 | mjames | 219 | |
| 16 | mjames | 220 | /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ |
| 58 | mjames | 221 | HAL_Init(); |
| 2 | mjames | 222 | |
| 50 | mjames | 223 | /* USER CODE BEGIN Init */ |
| 224 | |||
| 225 | /* USER CODE END Init */ |
||
| 226 | |||
| 16 | mjames | 227 | /* Configure the system clock */ |
| 58 | mjames | 228 | SystemClock_Config(); |
| 2 | mjames | 229 | |
| 50 | mjames | 230 | /* USER CODE BEGIN SysInit */ |
| 59 | mjames | 231 | // Switch handler called on sysTick interrupt. |
| 60 | mjames | 232 | InitSwitches(); |
| 50 | mjames | 233 | |
| 234 | /* USER CODE END SysInit */ |
||
| 235 | |||
| 16 | mjames | 236 | /* Initialize all configured peripherals */ |
| 58 | mjames | 237 | MX_GPIO_Init(); |
| 238 | MX_SPI1_Init(); |
||
| 239 | MX_USART1_UART_Init(); |
||
| 240 | MX_USART2_UART_Init(); |
||
| 241 | MX_USART3_UART_Init(); |
||
| 242 | MX_TIM3_Init(); |
||
| 243 | MX_TIM9_Init(); |
||
| 244 | MX_TIM2_Init(); |
||
| 60 | mjames | 245 | MX_UART4_Init(); |
| 65 | mjames | 246 | MX_I2C1_Init(); |
| 16 | mjames | 247 | /* USER CODE BEGIN 2 */ |
| 2 | mjames | 248 | |
| 50 | mjames | 249 | /* Turn on USART1 IRQ */ |
| 60 | mjames | 250 | HAL_NVIC_SetPriority(USART1_IRQn, 2, 0); |
| 251 | HAL_NVIC_EnableIRQ(USART1_IRQn); |
||
| 4 | mjames | 252 | |
| 50 | mjames | 253 | /* Turn on USART2 IRQ */ |
| 60 | mjames | 254 | HAL_NVIC_SetPriority(USART2_IRQn, 4, 0); |
| 255 | HAL_NVIC_EnableIRQ(USART2_IRQn); |
||
| 2 | mjames | 256 | |
| 50 | mjames | 257 | /* turn on USART3 IRQ */ |
| 60 | mjames | 258 | HAL_NVIC_SetPriority(USART3_IRQn, 4, 0); |
| 259 | HAL_NVIC_EnableIRQ(USART3_IRQn); |
||
| 4 | mjames | 260 | |
| 60 | mjames | 261 | /* turn on UART4 IRQ */ |
| 262 | HAL_NVIC_SetPriority(UART4_IRQn, 4, 0); |
||
| 263 | HAL_NVIC_EnableIRQ(UART4_IRQn); |
||
| 264 | |||
| 50 | mjames | 265 | /* setup the USART control blocks */ |
| 60 | mjames | 266 | init_usart_ctl(&uc1, &huart1); |
| 267 | init_usart_ctl(&uc2, &huart2); |
||
| 268 | init_usart_ctl(&uc3, &huart3); |
||
| 269 | init_usart_ctl(&uc4, &huart4); |
||
| 23 | mjames | 270 | |
| 60 | mjames | 271 | EnableSerialRxInterrupt(&uc1); |
| 272 | EnableSerialRxInterrupt(&uc2); |
||
| 273 | EnableSerialRxInterrupt(&uc3); |
||
| 274 | EnableSerialRxInterrupt(&uc4); |
||
| 23 | mjames | 275 | |
| 60 | mjames | 276 | HAL_TIM_Encoder_Start(&htim3, TIM_CHANNEL_ALL); |
| 23 | mjames | 277 | |
| 60 | mjames | 278 | HAL_TIM_Encoder_Start(&htim9, TIM_CHANNEL_ALL); |
| 44 | mjames | 279 | |
| 70 | mjames | 280 | initModule(&uc3, 38400); |
| 2 | mjames | 281 | |
| 58 | mjames | 282 | // Initialise UART for 4800 baud NMEA |
| 60 | mjames | 283 | setBaud(&uc2, 4800); |
| 58 | mjames | 284 | |
| 60 | mjames | 285 | // Initialuse UART4 for 4800 baud NMEA. |
| 286 | setBaud(&uc4, 4800); |
||
| 23 | mjames | 287 | |
| 60 | mjames | 288 | cc_init(); |
| 289 | |||
| 50 | mjames | 290 | int i; |
| 291 | for (i = 0; i < 2; i++) |
||
| 60 | mjames | 292 | { |
| 65 | mjames | 293 | contexts[i].knobPos = -1; // set the knob position |
| 60 | mjames | 294 | } |
| 7 | mjames | 295 | |
| 50 | mjames | 296 | /* reset the display timeout, latch on power from accessories */ |
| 297 | Latch_Timer = IGNITION_OFF_TIMEOUT; |
||
| 60 | mjames | 298 | HAL_GPIO_WritePin(POWER_LATCH_GPIO_Port, POWER_LATCH_Pin, GPIO_PIN_RESET); |
| 16 | mjames | 299 | |
| 60 | mjames | 300 | setRmcCallback(&rmc_callback); |
| 301 | |||
| 66 | mjames | 302 | // data timeout |
| 303 | uint32_t timeout = 0; // |
||
| 304 | |||
| 305 | uint32_t nextTick = 0; |
||
| 306 | uint8_t log = 0; |
||
| 307 | // PLX decoder protocols |
||
| 308 | char PLXPacket = 0; |
||
| 70 | mjames | 309 | |
| 66 | mjames | 310 | for (i = 0; i < MAXRDG; i++) |
| 311 | { |
||
| 70 | mjames | 312 | Info[i] = nullInfo; |
| 66 | mjames | 313 | } |
| 314 | |||
| 315 | int PLXPtr = 0; |
||
| 316 | int logCount = 0; |
||
| 317 | |||
| 71 | mjames | 318 | uint32_t resetCounter = 0 ; // record time at which both reset buttons were first pressed. |
| 70 | mjames | 319 | |
| 16 | mjames | 320 | /* USER CODE END 2 */ |
| 7 | mjames | 321 | |
| 16 | mjames | 322 | /* Infinite loop */ |
| 323 | /* USER CODE BEGIN WHILE */ |
||
| 52 | mjames | 324 | while (1) |
| 60 | mjames | 325 | { |
| 326 | |||
| 327 | /* while ignition is on, keep resetting power latch timer */ |
||
| 328 | if (HAL_GPIO_ReadPin(IGNITION_GPIO_Port, IGNITION_Pin) == GPIO_PIN_RESET) |
||
| 52 | mjames | 329 | { |
| 60 | mjames | 330 | Latch_Timer = HAL_GetTick() + IGNITION_OFF_TIMEOUT; |
| 331 | } |
||
| 332 | else |
||
| 333 | { |
||
| 334 | /* if the ignition has been off for a while, then turn off power */ |
||
| 335 | if (HAL_GetTick() > Latch_Timer) |
||
| 336 | { |
||
| 337 | HAL_GPIO_WritePin(POWER_LATCH_GPIO_Port, POWER_LATCH_Pin, |
||
| 338 | GPIO_PIN_RESET); |
||
| 339 | } |
||
| 340 | } |
||
| 7 | mjames | 341 | |
| 66 | mjames | 342 | // Handle the bluetooth pairing / reset function by pressing both buttons. |
| 343 | if ((push_pos[0] == 1) && (push_pos[1] == 1)) |
||
| 60 | mjames | 344 | { |
| 66 | mjames | 345 | HAL_GPIO_WritePin(BT_BUTTON_GPIO_Port, BT_BUTTON_Pin, |
| 346 | GPIO_PIN_RESET); |
||
| 70 | mjames | 347 | if (resetCounter == 0) |
| 348 | resetCounter = HAL_GetTick(); |
||
| 60 | mjames | 349 | } |
| 66 | mjames | 350 | else |
| 351 | { |
||
| 352 | HAL_GPIO_WritePin(BT_BUTTON_GPIO_Port, BT_BUTTON_Pin, |
||
| 353 | GPIO_PIN_SET); |
||
| 70 | mjames | 354 | |
| 355 | if (resetCounter != 0) |
||
| 356 | { |
||
| 357 | // Held down reset button for 10 seconds, clear NVRAM. |
||
| 358 | if ((HAL_GetTick() - resetCounter) > 10000) |
||
| 359 | { |
||
| 360 | for (i = 0; i < 2; i++) |
||
| 361 | { |
||
| 362 | contexts[i].knobPos = -1; // set the knob position |
||
| 363 | contexts[i].dial_timer = 1; // timeout immediately when decremented |
||
| 364 | } |
||
| 365 | erase_nvram(); |
||
| 366 | } |
||
| 367 | resetCounter = 0; |
||
| 368 | } |
||
| 66 | mjames | 369 | } |
| 58 | mjames | 370 | |
| 66 | mjames | 371 | // poll GPS Position/time on UART4 |
| 372 | (void)updateLocation(&loc, &uc4); |
||
| 373 | if (loc.valid == 'V') |
||
| 374 | memset(loc.time, '-', 6); |
||
| 60 | mjames | 375 | |
| 66 | mjames | 376 | // if permitted, log data from RMC packet |
| 377 | if (btConnected()) |
||
| 60 | mjames | 378 | { |
| 66 | mjames | 379 | // Any RMC data, send it, reset the logger timeout |
| 380 | if (rmc_length) |
||
| 62 | mjames | 381 | { |
| 66 | mjames | 382 | sendString(&uc3, (const char *)rmc_buff, rmc_length); |
| 62 | mjames | 383 | rmc_length = 0; |
| 384 | nextTick = HAL_GetTick() + LOGGER_INTERVAL; |
||
| 66 | mjames | 385 | log = 1; // send out associated data over Bluetooth because triggered by recieving RMC |
| 386 | logCount = 0; // first sample set this second numbered 0 |
||
| 62 | mjames | 387 | } |
| 65 | mjames | 388 | |
| 66 | mjames | 389 | // Timeout for data logging regularly |
| 390 | if (HAL_GetTick() > nextTick) |
||
| 62 | mjames | 391 | { |
| 392 | nextTick = HAL_GetTick() + LOGGER_INTERVAL; |
||
| 66 | mjames | 393 | logCount++; |
| 394 | if (logCount > (1000 / LOGGER_INTERVAL)) |
||
| 395 | logCount = 0; |
||
| 62 | mjames | 396 | log = 1; |
| 397 | } |
||
| 398 | |||
| 66 | mjames | 399 | if (log) |
| 60 | mjames | 400 | { |
| 66 | mjames | 401 | log = 0; |
| 402 | // Send items to BT if it is in connected state |
||
| 71 | mjames | 403 | for (int i = 0; i < MAXRDG; ++i) |
| 66 | mjames | 404 | { |
| 71 | mjames | 405 | if (!isValid(i)) |
| 406 | continue; |
||
| 66 | mjames | 407 | char outbuff[100]; |
| 408 | |||
| 409 | int cnt = small_sprintf(outbuff, |
||
| 410 | "$PLLOG,%d,%d,%d,%ld", |
||
| 411 | logCount, |
||
| 412 | Info[i].observation, |
||
| 413 | Info[i].instance, |
||
| 414 | Info[i].count == 0 ? 0 : Info[i].sum / Info[i].count); |
||
| 415 | |||
| 416 | // NMEA style checksum |
||
| 417 | int ck; |
||
| 418 | int sum = 0; |
||
| 419 | for (ck = 1; ck < cnt; ck++) |
||
| 420 | sum += outbuff[ck]; |
||
| 421 | cnt += small_sprintf(outbuff + cnt, "*%02X\n", |
||
| 422 | sum & 0xFF); |
||
| 423 | sendString(&uc3, outbuff, cnt); |
||
| 424 | } |
||
| 60 | mjames | 425 | } |
| 66 | mjames | 426 | } |
| 427 | |||
| 428 | // determine if we are getting any data from the interface |
||
| 429 | uint16_t cc = SerialCharsReceived(&uc1); |
||
| 430 | int chr; |
||
| 431 | if (cc == 0) |
||
| 432 | { |
||
| 433 | timeout++; |
||
| 434 | if (btConnected() && (timeout % 1000 == 0)) |
||
| 60 | mjames | 435 | { |
| 66 | mjames | 436 | const char msg[] = "Timeout\r\n"; |
| 437 | sendString(&uc3, msg, sizeof(msg)); |
||
| 60 | mjames | 438 | } |
| 27 | mjames | 439 | |
| 66 | mjames | 440 | if (timeout > 60000) |
| 60 | mjames | 441 | { |
| 27 | mjames | 442 | |
| 66 | mjames | 443 | // do turn off screen |
| 60 | mjames | 444 | } |
| 66 | mjames | 445 | // wait for a bit if nothing came in. |
| 446 | HAL_Delay(10); |
||
| 447 | } |
||
| 62 | mjames | 448 | |
| 66 | mjames | 449 | /// process the observation list |
| 450 | for (chr = 0; chr < cc; chr++) |
||
| 451 | { |
||
| 68 | mjames | 452 | char c = GetCharSerial(&uc1); |
| 66 | mjames | 453 | |
| 454 | if (c == PLX_Start) // at any time if the start byte appears, reset the pointers |
||
| 60 | mjames | 455 | { |
| 66 | mjames | 456 | PLXPtr = 0; // reset the pointer |
| 457 | PLXPacket = 1; |
||
| 458 | timeout = 0; // Reset the timer |
||
| 67 | mjames | 459 | continue; |
| 66 | mjames | 460 | } |
| 67 | mjames | 461 | if (c == PLX_Stop) |
| 66 | mjames | 462 | { |
| 463 | if (PLXPacket) |
||
| 464 | { |
||
| 465 | // we can now decode the selected parameter |
||
| 70 | mjames | 466 | int PLXNewItems = PLXPtr / sizeof(PLX_SensorInfo); // total items in last reading batch |
| 24 | mjames | 467 | |
| 70 | mjames | 468 | // process items |
| 469 | for (i = 0; i < PLXNewItems; i++) |
||
| 60 | mjames | 470 | { |
| 70 | mjames | 471 | // search to see if the item already has a slot in the Info[] array |
| 472 | // match the observation and instance: if found, update entry |
||
| 473 | enum PLX_Observations observation = ConvPLX(Data.Sensor[i].AddrH, |
||
| 474 | Data.Sensor[i].AddrL); |
||
| 7 | mjames | 475 | |
| 70 | mjames | 476 | char instance = Data.Sensor[i].Instance; |
| 477 | |||
| 478 | // validate the current item, discard out of range |
||
| 479 | |||
| 480 | if ((instance > PLX_MAX_INST) || (observation > PLX_MAX_OBS)) |
||
| 481 | continue; |
||
| 482 | |||
| 483 | // search for the item in the list |
||
| 484 | int j; |
||
| 485 | for (j = 0; j < MAXRDG; ++j) |
||
| 60 | mjames | 486 | { |
| 70 | mjames | 487 | if ((Info[j].observation == observation) && (Info[j].instance == instance)) |
| 488 | break; |
||
| 60 | mjames | 489 | } |
| 70 | mjames | 490 | // fallen off the end of the list of existing items without a match, so j points at next new item |
| 491 | // |
||
| 492 | // Find an unused slot |
||
| 493 | |||
| 494 | if (j == MAXRDG) |
||
| 66 | mjames | 495 | { |
| 70 | mjames | 496 | int k; |
| 497 | { |
||
| 498 | for (k = 0; k < MAXRDG; ++k) |
||
| 499 | if (!isValid(k)) |
||
| 500 | { |
||
| 501 | j = k; // found a spare slot |
||
| 502 | break; |
||
| 503 | } |
||
| 504 | } |
||
| 505 | if (k == MAXRDG) |
||
| 506 | continue; // abandon this iteration |
||
| 66 | mjames | 507 | } |
| 70 | mjames | 508 | |
| 509 | // give up if we are going to fall off the end of the array |
||
| 510 | if (j > MAXRDG) |
||
| 511 | break; |
||
| 512 | |||
| 513 | Info[j].observation = observation; |
||
| 514 | |||
| 515 | Info[j].instance = instance; |
||
| 516 | Info[j].data = ConvPLX(Data.Sensor[j].ReadingH, |
||
| 517 | Data.Sensor[j].ReadingL); |
||
| 518 | if (Info[j].data > Info[j].Max) |
||
| 519 | { |
||
| 520 | Info[j].Max = Info[j].data; |
||
| 521 | } |
||
| 522 | if (Info[j].data < Info[j].Min) |
||
| 523 | { |
||
| 524 | Info[j].Min = Info[j].data; |
||
| 525 | } |
||
| 66 | mjames | 526 | // take an average |
| 70 | mjames | 527 | Info[j].sum += Info[j].data; |
| 528 | Info[j].count++; |
||
| 66 | mjames | 529 | // note the last update time |
| 70 | mjames | 530 | Info[j].lastUpdated = HAL_GetTick(); |
| 531 | Info[j].updated = 1; // it has been updated |
||
| 60 | mjames | 532 | } |
| 533 | PLXPtr = 0; |
||
| 534 | PLXPacket = 0; |
||
| 70 | mjames | 535 | |
| 536 | // scan through and invalidate all old items |
||
| 537 | for (int i = 0; i < MAXRDG; ++i) |
||
| 538 | { |
||
| 539 | if (!isValid(i)) |
||
| 540 | Info[i] = nullInfo; |
||
| 541 | } |
||
| 542 | |||
| 543 | break; // something to process |
||
| 60 | mjames | 544 | } |
| 545 | } |
||
| 67 | mjames | 546 | if (c > PLX_Stop) // illegal char, restart reading |
| 547 | { |
||
| 548 | PLXPacket = 0; |
||
| 549 | PLXPtr = 0; |
||
| 550 | continue; |
||
| 551 | } |
||
| 552 | if (PLXPacket && PLXPtr < sizeof(Data.Bytes)) |
||
| 553 | { |
||
| 554 | Data.Bytes[PLXPtr++] = c; |
||
| 555 | } |
||
| 556 | } |
||
| 71 | mjames | 557 | |
| 558 | // handle switch rotation |
||
| 559 | for (i = 0; i < MAX_DIALS; ++i) |
||
| 560 | { |
||
| 561 | int delta = get_dial_diff(i); |
||
| 562 | int pos = contexts[i].knobPos; |
||
| 563 | if(pos < 0) |
||
| 564 | break; // dont process until we have read NVRAM for the first time . |
||
| 565 | int start = pos; |
||
| 566 | // move in positive direction |
||
| 567 | while (delta > 0) |
||
| 568 | { |
||
| 569 | // skip invalid items, dont count |
||
| 570 | if(pos<MAXRDG-1) |
||
| 571 | pos++; |
||
| 572 | else |
||
| 573 | pos=0; |
||
| 574 | |||
| 575 | if (isValid(pos)) |
||
| 576 | delta--; // count a valid item |
||
| 577 | |||
| 578 | // wrap |
||
| 579 | if (pos == start) |
||
| 580 | break; |
||
| 581 | } |
||
| 582 | |||
| 583 | // move in negative direction |
||
| 584 | while (delta < 0) |
||
| 585 | |||
| 586 | { |
||
| 587 | // skip invalid items, dont count |
||
| 588 | if(pos>0) |
||
| 589 | pos--; |
||
| 590 | else |
||
| 591 | pos=MAXRDG-1; |
||
| 592 | |||
| 593 | if (isValid(pos)) |
||
| 594 | delta++; // count a valid item |
||
| 595 | |||
| 596 | // wrap |
||
| 597 | if (pos == start) |
||
| 598 | break; |
||
| 599 | } |
||
| 600 | |||
| 601 | contexts[i].knobPos = pos; |
||
| 602 | if (pos != start) |
||
| 603 | contexts[i].dial_timer = DialTimeout; |
||
| 604 | } |
||
| 605 | |||
| 67 | mjames | 606 | int suppress = -1; |
| 607 | for (i = 0; i < MAX_DISPLAYS; i++) |
||
| 608 | { // now to display the information |
||
| 609 | suppress = DisplayCurrent(i, suppress); |
||
| 23 | mjames | 610 | |
| 67 | mjames | 611 | cc_check_nvram(i); |
| 60 | mjames | 612 | } |
| 66 | mjames | 613 | } |
| 71 | mjames | 614 | } |
| 615 | /* USER CODE END WHILE */ |
||
| 52 | mjames | 616 | |
| 71 | mjames | 617 | /* USER CODE BEGIN 3 */ |
| 66 | mjames | 618 | |
| 71 | mjames | 619 | /* USER CODE END 3 */ |
| 67 | mjames | 620 | |
| 50 | mjames | 621 | /** |
| 62 | mjames | 622 | * @brief System Clock Configuration |
| 623 | * @retval None |
||
| 624 | */ |
||
| 58 | mjames | 625 | void SystemClock_Config(void) |
| 5 | mjames | 626 | { |
| 58 | mjames | 627 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
| 628 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
||
| 2 | mjames | 629 | |
| 50 | mjames | 630 | /** Configure the main internal regulator output voltage |
| 62 | mjames | 631 | */ |
| 29 | mjames | 632 | __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); |
| 61 | mjames | 633 | |
| 50 | mjames | 634 | /** Initializes the RCC Oscillators according to the specified parameters |
| 62 | mjames | 635 | * in the RCC_OscInitTypeDef structure. |
| 636 | */ |
||
| 44 | mjames | 637 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; |
| 59 | mjames | 638 | RCC_OscInitStruct.HSEState = RCC_HSE_ON; |
| 16 | mjames | 639 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
| 44 | mjames | 640 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; |
| 641 | RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12; |
||
| 29 | mjames | 642 | RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV3; |
| 58 | mjames | 643 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) |
| 644 | { |
||
| 645 | Error_Handler(); |
||
| 646 | } |
||
| 61 | mjames | 647 | |
| 50 | mjames | 648 | /** Initializes the CPU, AHB and APB buses clocks |
| 62 | mjames | 649 | */ |
| 650 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; |
||
| 16 | mjames | 651 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
| 652 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
||
| 29 | mjames | 653 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; |
| 16 | mjames | 654 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; |
| 50 | mjames | 655 | |
| 58 | mjames | 656 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) |
| 657 | { |
||
| 658 | Error_Handler(); |
||
| 659 | } |
||
| 2 | mjames | 660 | } |
| 661 | |||
| 50 | mjames | 662 | /** |
| 65 | mjames | 663 | * @brief I2C1 Initialization Function |
| 664 | * @param None |
||
| 665 | * @retval None |
||
| 666 | */ |
||
| 667 | static void MX_I2C1_Init(void) |
||
| 668 | { |
||
| 669 | |||
| 670 | /* USER CODE BEGIN I2C1_Init 0 */ |
||
| 671 | |||
| 672 | /* USER CODE END I2C1_Init 0 */ |
||
| 673 | |||
| 674 | /* USER CODE BEGIN I2C1_Init 1 */ |
||
| 675 | |||
| 676 | /* USER CODE END I2C1_Init 1 */ |
||
| 677 | hi2c1.Instance = I2C1; |
||
| 678 | hi2c1.Init.ClockSpeed = 100000; |
||
| 679 | hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2; |
||
| 680 | hi2c1.Init.OwnAddress1 = 0; |
||
| 681 | hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; |
||
| 682 | hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; |
||
| 683 | hi2c1.Init.OwnAddress2 = 0; |
||
| 684 | hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; |
||
| 685 | hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; |
||
| 686 | if (HAL_I2C_Init(&hi2c1) != HAL_OK) |
||
| 687 | { |
||
| 688 | Error_Handler(); |
||
| 689 | } |
||
| 690 | /* USER CODE BEGIN I2C1_Init 2 */ |
||
| 691 | |||
| 692 | /* USER CODE END I2C1_Init 2 */ |
||
| 693 | } |
||
| 694 | |||
| 695 | /** |
||
| 62 | mjames | 696 | * @brief SPI1 Initialization Function |
| 697 | * @param None |
||
| 698 | * @retval None |
||
| 699 | */ |
||
| 58 | mjames | 700 | static void MX_SPI1_Init(void) |
| 5 | mjames | 701 | { |
| 2 | mjames | 702 | |
| 50 | mjames | 703 | /* USER CODE BEGIN SPI1_Init 0 */ |
| 704 | |||
| 705 | /* USER CODE END SPI1_Init 0 */ |
||
| 706 | |||
| 707 | /* USER CODE BEGIN SPI1_Init 1 */ |
||
| 708 | |||
| 709 | /* USER CODE END SPI1_Init 1 */ |
||
| 710 | /* SPI1 parameter configuration*/ |
||
| 16 | mjames | 711 | hspi1.Instance = SPI1; |
| 712 | hspi1.Init.Mode = SPI_MODE_MASTER; |
||
| 713 | hspi1.Init.Direction = SPI_DIRECTION_1LINE; |
||
| 714 | hspi1.Init.DataSize = SPI_DATASIZE_8BIT; |
||
| 715 | hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH; |
||
| 716 | hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; |
||
| 717 | hspi1.Init.NSS = SPI_NSS_SOFT; |
||
| 50 | mjames | 718 | hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; |
| 16 | mjames | 719 | hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; |
| 720 | hspi1.Init.TIMode = SPI_TIMODE_DISABLE; |
||
| 721 | hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; |
||
| 722 | hspi1.Init.CRCPolynomial = 10; |
||
| 58 | mjames | 723 | if (HAL_SPI_Init(&hspi1) != HAL_OK) |
| 724 | { |
||
| 725 | Error_Handler(); |
||
| 726 | } |
||
| 50 | mjames | 727 | /* USER CODE BEGIN SPI1_Init 2 */ |
| 2 | mjames | 728 | |
| 50 | mjames | 729 | /* USER CODE END SPI1_Init 2 */ |
| 2 | mjames | 730 | } |
| 731 | |||
| 50 | mjames | 732 | /** |
| 62 | mjames | 733 | * @brief TIM2 Initialization Function |
| 734 | * @param None |
||
| 735 | * @retval None |
||
| 736 | */ |
||
| 58 | mjames | 737 | static void MX_TIM2_Init(void) |
| 50 | mjames | 738 | { |
| 739 | |||
| 740 | /* USER CODE BEGIN TIM2_Init 0 */ |
||
| 741 | |||
| 742 | /* USER CODE END TIM2_Init 0 */ |
||
| 743 | |||
| 58 | mjames | 744 | TIM_ClockConfigTypeDef sClockSourceConfig = {0}; |
| 745 | TIM_MasterConfigTypeDef sMasterConfig = {0}; |
||
| 50 | mjames | 746 | |
| 747 | /* USER CODE BEGIN TIM2_Init 1 */ |
||
| 748 | |||
| 749 | /* USER CODE END TIM2_Init 1 */ |
||
| 750 | htim2.Instance = TIM2; |
||
| 751 | htim2.Init.Prescaler = 0; |
||
| 752 | htim2.Init.CounterMode = TIM_COUNTERMODE_UP; |
||
| 753 | htim2.Init.Period = 65535; |
||
| 754 | htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
||
| 755 | htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
||
| 58 | mjames | 756 | if (HAL_TIM_Base_Init(&htim2) != HAL_OK) |
| 757 | { |
||
| 758 | Error_Handler(); |
||
| 759 | } |
||
| 50 | mjames | 760 | sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; |
| 58 | mjames | 761 | if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK) |
| 762 | { |
||
| 763 | Error_Handler(); |
||
| 764 | } |
||
| 50 | mjames | 765 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; |
| 766 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
||
| 58 | mjames | 767 | if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK) |
| 768 | { |
||
| 769 | Error_Handler(); |
||
| 770 | } |
||
| 50 | mjames | 771 | /* USER CODE BEGIN TIM2_Init 2 */ |
| 772 | |||
| 773 | /* USER CODE END TIM2_Init 2 */ |
||
| 774 | } |
||
| 775 | |||
| 776 | /** |
||
| 62 | mjames | 777 | * @brief TIM3 Initialization Function |
| 778 | * @param None |
||
| 779 | * @retval None |
||
| 780 | */ |
||
| 58 | mjames | 781 | static void MX_TIM3_Init(void) |
| 44 | mjames | 782 | { |
| 783 | |||
| 50 | mjames | 784 | /* USER CODE BEGIN TIM3_Init 0 */ |
| 44 | mjames | 785 | |
| 50 | mjames | 786 | /* USER CODE END TIM3_Init 0 */ |
| 787 | |||
| 58 | mjames | 788 | TIM_Encoder_InitTypeDef sConfig = {0}; |
| 789 | TIM_MasterConfigTypeDef sMasterConfig = {0}; |
||
| 50 | mjames | 790 | |
| 791 | /* USER CODE BEGIN TIM3_Init 1 */ |
||
| 792 | |||
| 793 | /* USER CODE END TIM3_Init 1 */ |
||
| 44 | mjames | 794 | htim3.Instance = TIM3; |
| 795 | htim3.Init.Prescaler = 0; |
||
| 796 | htim3.Init.CounterMode = TIM_COUNTERMODE_UP; |
||
| 50 | mjames | 797 | htim3.Init.Period = 65535; |
| 798 | htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
||
| 799 | htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
||
| 44 | mjames | 800 | sConfig.EncoderMode = TIM_ENCODERMODE_TI1; |
| 50 | mjames | 801 | sConfig.IC1Polarity = TIM_ICPOLARITY_RISING; |
| 44 | mjames | 802 | sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI; |
| 803 | sConfig.IC1Prescaler = TIM_ICPSC_DIV1; |
||
| 804 | sConfig.IC1Filter = 15; |
||
| 50 | mjames | 805 | sConfig.IC2Polarity = TIM_ICPOLARITY_RISING; |
| 44 | mjames | 806 | sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI; |
| 807 | sConfig.IC2Prescaler = TIM_ICPSC_DIV1; |
||
| 808 | sConfig.IC2Filter = 15; |
||
| 58 | mjames | 809 | if (HAL_TIM_Encoder_Init(&htim3, &sConfig) != HAL_OK) |
| 810 | { |
||
| 811 | Error_Handler(); |
||
| 812 | } |
||
| 44 | mjames | 813 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; |
| 814 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
||
| 58 | mjames | 815 | if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK) |
| 816 | { |
||
| 817 | Error_Handler(); |
||
| 818 | } |
||
| 50 | mjames | 819 | /* USER CODE BEGIN TIM3_Init 2 */ |
| 44 | mjames | 820 | |
| 50 | mjames | 821 | /* USER CODE END TIM3_Init 2 */ |
| 44 | mjames | 822 | } |
| 823 | |||
| 50 | mjames | 824 | /** |
| 62 | mjames | 825 | * @brief TIM9 Initialization Function |
| 826 | * @param None |
||
| 827 | * @retval None |
||
| 828 | */ |
||
| 58 | mjames | 829 | static void MX_TIM9_Init(void) |
| 44 | mjames | 830 | { |
| 831 | |||
| 50 | mjames | 832 | /* USER CODE BEGIN TIM9_Init 0 */ |
| 44 | mjames | 833 | |
| 50 | mjames | 834 | /* USER CODE END TIM9_Init 0 */ |
| 835 | |||
| 58 | mjames | 836 | TIM_Encoder_InitTypeDef sConfig = {0}; |
| 837 | TIM_MasterConfigTypeDef sMasterConfig = {0}; |
||
| 50 | mjames | 838 | |
| 839 | /* USER CODE BEGIN TIM9_Init 1 */ |
||
| 840 | |||
| 841 | /* USER CODE END TIM9_Init 1 */ |
||
| 44 | mjames | 842 | htim9.Instance = TIM9; |
| 843 | htim9.Init.Prescaler = 0; |
||
| 844 | htim9.Init.CounterMode = TIM_COUNTERMODE_UP; |
||
| 50 | mjames | 845 | htim9.Init.Period = 65535; |
| 846 | htim9.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
||
| 847 | htim9.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
||
| 44 | mjames | 848 | sConfig.EncoderMode = TIM_ENCODERMODE_TI1; |
| 50 | mjames | 849 | sConfig.IC1Polarity = TIM_ICPOLARITY_RISING; |
| 44 | mjames | 850 | sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI; |
| 851 | sConfig.IC1Prescaler = TIM_ICPSC_DIV1; |
||
| 852 | sConfig.IC1Filter = 15; |
||
| 50 | mjames | 853 | sConfig.IC2Polarity = TIM_ICPOLARITY_RISING; |
| 44 | mjames | 854 | sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI; |
| 855 | sConfig.IC2Prescaler = TIM_ICPSC_DIV1; |
||
| 50 | mjames | 856 | sConfig.IC2Filter = 0; |
| 58 | mjames | 857 | if (HAL_TIM_Encoder_Init(&htim9, &sConfig) != HAL_OK) |
| 858 | { |
||
| 859 | Error_Handler(); |
||
| 860 | } |
||
| 44 | mjames | 861 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; |
| 862 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
||
| 58 | mjames | 863 | if (HAL_TIMEx_MasterConfigSynchronization(&htim9, &sMasterConfig) != HAL_OK) |
| 864 | { |
||
| 865 | Error_Handler(); |
||
| 866 | } |
||
| 50 | mjames | 867 | /* USER CODE BEGIN TIM9_Init 2 */ |
| 44 | mjames | 868 | |
| 50 | mjames | 869 | /* USER CODE END TIM9_Init 2 */ |
| 60 | mjames | 870 | } |
| 50 | mjames | 871 | |
| 60 | mjames | 872 | /** |
| 62 | mjames | 873 | * @brief UART4 Initialization Function |
| 874 | * @param None |
||
| 875 | * @retval None |
||
| 876 | */ |
||
| 60 | mjames | 877 | static void MX_UART4_Init(void) |
| 878 | { |
||
| 879 | |||
| 880 | /* USER CODE BEGIN UART4_Init 0 */ |
||
| 881 | |||
| 882 | /* USER CODE END UART4_Init 0 */ |
||
| 883 | |||
| 884 | /* USER CODE BEGIN UART4_Init 1 */ |
||
| 885 | |||
| 886 | /* USER CODE END UART4_Init 1 */ |
||
| 887 | huart4.Instance = UART4; |
||
| 888 | huart4.Init.BaudRate = 4800; |
||
| 889 | huart4.Init.WordLength = UART_WORDLENGTH_8B; |
||
| 890 | huart4.Init.StopBits = UART_STOPBITS_1; |
||
| 891 | huart4.Init.Parity = UART_PARITY_NONE; |
||
| 892 | huart4.Init.Mode = UART_MODE_TX_RX; |
||
| 893 | huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE; |
||
| 894 | huart4.Init.OverSampling = UART_OVERSAMPLING_16; |
||
| 895 | if (HAL_UART_Init(&huart4) != HAL_OK) |
||
| 896 | { |
||
| 897 | Error_Handler(); |
||
| 898 | } |
||
| 899 | /* USER CODE BEGIN UART4_Init 2 */ |
||
| 900 | |||
| 901 | /* USER CODE END UART4_Init 2 */ |
||
| 44 | mjames | 902 | } |
| 903 | |||
| 50 | mjames | 904 | /** |
| 62 | mjames | 905 | * @brief USART1 Initialization Function |
| 906 | * @param None |
||
| 907 | * @retval None |
||
| 908 | */ |
||
| 58 | mjames | 909 | static void MX_USART1_UART_Init(void) |
| 5 | mjames | 910 | { |
| 3 | mjames | 911 | |
| 50 | mjames | 912 | /* USER CODE BEGIN USART1_Init 0 */ |
| 913 | |||
| 914 | /* USER CODE END USART1_Init 0 */ |
||
| 915 | |||
| 916 | /* USER CODE BEGIN USART1_Init 1 */ |
||
| 917 | |||
| 918 | /* USER CODE END USART1_Init 1 */ |
||
| 16 | mjames | 919 | huart1.Instance = USART1; |
| 920 | huart1.Init.BaudRate = 19200; |
||
| 921 | huart1.Init.WordLength = UART_WORDLENGTH_8B; |
||
| 44 | mjames | 922 | huart1.Init.StopBits = UART_STOPBITS_1; |
| 16 | mjames | 923 | huart1.Init.Parity = UART_PARITY_NONE; |
| 924 | huart1.Init.Mode = UART_MODE_TX_RX; |
||
| 925 | huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; |
||
| 926 | huart1.Init.OverSampling = UART_OVERSAMPLING_16; |
||
| 58 | mjames | 927 | if (HAL_UART_Init(&huart1) != HAL_OK) |
| 928 | { |
||
| 929 | Error_Handler(); |
||
| 930 | } |
||
| 50 | mjames | 931 | /* USER CODE BEGIN USART1_Init 2 */ |
| 3 | mjames | 932 | |
| 50 | mjames | 933 | /* USER CODE END USART1_Init 2 */ |
| 3 | mjames | 934 | } |
| 935 | |||
| 50 | mjames | 936 | /** |
| 62 | mjames | 937 | * @brief USART2 Initialization Function |
| 938 | * @param None |
||
| 939 | * @retval None |
||
| 940 | */ |
||
| 58 | mjames | 941 | static void MX_USART2_UART_Init(void) |
| 5 | mjames | 942 | { |
| 2 | mjames | 943 | |
| 50 | mjames | 944 | /* USER CODE BEGIN USART2_Init 0 */ |
| 945 | |||
| 946 | /* USER CODE END USART2_Init 0 */ |
||
| 947 | |||
| 948 | /* USER CODE BEGIN USART2_Init 1 */ |
||
| 949 | |||
| 950 | /* USER CODE END USART2_Init 1 */ |
||
| 16 | mjames | 951 | huart2.Instance = USART2; |
| 952 | huart2.Init.BaudRate = 115200; |
||
| 953 | huart2.Init.WordLength = UART_WORDLENGTH_8B; |
||
| 954 | huart2.Init.StopBits = UART_STOPBITS_1; |
||
| 955 | huart2.Init.Parity = UART_PARITY_NONE; |
||
| 956 | huart2.Init.Mode = UART_MODE_TX_RX; |
||
| 957 | huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; |
||
| 958 | huart2.Init.OverSampling = UART_OVERSAMPLING_16; |
||
| 58 | mjames | 959 | if (HAL_UART_Init(&huart2) != HAL_OK) |
| 960 | { |
||
| 961 | Error_Handler(); |
||
| 962 | } |
||
| 50 | mjames | 963 | /* USER CODE BEGIN USART2_Init 2 */ |
| 2 | mjames | 964 | |
| 50 | mjames | 965 | /* USER CODE END USART2_Init 2 */ |
| 2 | mjames | 966 | } |
| 967 | |||
| 50 | mjames | 968 | /** |
| 62 | mjames | 969 | * @brief USART3 Initialization Function |
| 970 | * @param None |
||
| 971 | * @retval None |
||
| 972 | */ |
||
| 58 | mjames | 973 | static void MX_USART3_UART_Init(void) |
| 23 | mjames | 974 | { |
| 975 | |||
| 50 | mjames | 976 | /* USER CODE BEGIN USART3_Init 0 */ |
| 977 | |||
| 978 | /* USER CODE END USART3_Init 0 */ |
||
| 979 | |||
| 980 | /* USER CODE BEGIN USART3_Init 1 */ |
||
| 981 | |||
| 982 | /* USER CODE END USART3_Init 1 */ |
||
| 23 | mjames | 983 | huart3.Instance = USART3; |
| 58 | mjames | 984 | huart3.Init.BaudRate = 19200; |
| 23 | mjames | 985 | huart3.Init.WordLength = UART_WORDLENGTH_8B; |
| 50 | mjames | 986 | huart3.Init.StopBits = UART_STOPBITS_1; |
| 44 | mjames | 987 | huart3.Init.Parity = UART_PARITY_NONE; |
| 23 | mjames | 988 | huart3.Init.Mode = UART_MODE_TX_RX; |
| 989 | huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE; |
||
| 990 | huart3.Init.OverSampling = UART_OVERSAMPLING_16; |
||
| 58 | mjames | 991 | if (HAL_UART_Init(&huart3) != HAL_OK) |
| 992 | { |
||
| 993 | Error_Handler(); |
||
| 994 | } |
||
| 50 | mjames | 995 | /* USER CODE BEGIN USART3_Init 2 */ |
| 23 | mjames | 996 | |
| 50 | mjames | 997 | /* USER CODE END USART3_Init 2 */ |
| 23 | mjames | 998 | } |
| 999 | |||
| 50 | mjames | 1000 | /** |
| 62 | mjames | 1001 | * @brief GPIO Initialization Function |
| 1002 | * @param None |
||
| 1003 | * @retval None |
||
| 1004 | */ |
||
| 58 | mjames | 1005 | static void MX_GPIO_Init(void) |
| 5 | mjames | 1006 | { |
| 58 | mjames | 1007 | GPIO_InitTypeDef GPIO_InitStruct = {0}; |
| 2 | mjames | 1008 | |
| 16 | mjames | 1009 | /* GPIO Ports Clock Enable */ |
| 29 | mjames | 1010 | __HAL_RCC_GPIOH_CLK_ENABLE(); |
| 1011 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
||
| 1012 | __HAL_RCC_GPIOC_CLK_ENABLE(); |
||
| 1013 | __HAL_RCC_GPIOB_CLK_ENABLE(); |
||
| 2 | mjames | 1014 | |
| 16 | mjames | 1015 | /*Configure GPIO pin Output Level */ |
| 70 | mjames | 1016 | HAL_GPIO_WritePin(GPIOA, SPI_NSS1_Pin | BT_RESET_Pin, GPIO_PIN_SET); |
| 2 | mjames | 1017 | |
| 16 | mjames | 1018 | /*Configure GPIO pin Output Level */ |
| 62 | mjames | 1019 | HAL_GPIO_WritePin(GPIOA, SPI_CD_Pin | BT_BUTTON_Pin, GPIO_PIN_RESET); |
| 2 | mjames | 1020 | |
| 50 | mjames | 1021 | /*Configure GPIO pin Output Level */ |
| 62 | mjames | 1022 | HAL_GPIO_WritePin(GPIOC, SPI_RESET_Pin | POWER_LATCH_Pin | USB_PWR_Pin, GPIO_PIN_RESET); |
| 50 | mjames | 1023 | |
| 1024 | /*Configure GPIO pin Output Level */ |
||
| 58 | mjames | 1025 | HAL_GPIO_WritePin(SPI_NSS2_GPIO_Port, SPI_NSS2_Pin, GPIO_PIN_SET); |
| 50 | mjames | 1026 | |
| 1027 | /*Configure GPIO pins : SPI_NSS1_Pin SPI_CD_Pin */ |
||
| 62 | mjames | 1028 | GPIO_InitStruct.Pin = SPI_NSS1_Pin | SPI_CD_Pin; |
| 16 | mjames | 1029 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
| 29 | mjames | 1030 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 16 | mjames | 1031 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
| 58 | mjames | 1032 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
| 2 | mjames | 1033 | |
| 24 | mjames | 1034 | /*Configure GPIO pins : SPI_RESET_Pin SPI_NSS2_Pin POWER_LATCH_Pin USB_PWR_Pin */ |
| 62 | mjames | 1035 | GPIO_InitStruct.Pin = SPI_RESET_Pin | SPI_NSS2_Pin | POWER_LATCH_Pin | USB_PWR_Pin; |
| 16 | mjames | 1036 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
| 29 | mjames | 1037 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 16 | mjames | 1038 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
| 58 | mjames | 1039 | HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); |
| 2 | mjames | 1040 | |
| 61 | mjames | 1041 | /*Configure GPIO pins : BT_STATE_Pin SW1_PUSH_Pin SW2_PUSH_Pin */ |
| 62 | mjames | 1042 | GPIO_InitStruct.Pin = BT_STATE_Pin | SW1_PUSH_Pin | SW2_PUSH_Pin; |
| 16 | mjames | 1043 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
| 32 | mjames | 1044 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
| 58 | mjames | 1045 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
| 5 | mjames | 1046 | |
| 32 | mjames | 1047 | /*Configure GPIO pin : IGNITION_Pin */ |
| 1048 | GPIO_InitStruct.Pin = IGNITION_Pin; |
||
| 1049 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
||
| 1050 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
| 58 | mjames | 1051 | HAL_GPIO_Init(IGNITION_GPIO_Port, &GPIO_InitStruct); |
| 32 | mjames | 1052 | |
| 70 | mjames | 1053 | /*Configure GPIO pins : BT_BUTTON_Pin BT_RESET_Pin */ |
| 1054 | GPIO_InitStruct.Pin = BT_BUTTON_Pin | BT_RESET_Pin; |
||
| 37 | mjames | 1055 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD; |
| 1056 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
| 1057 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
| 70 | mjames | 1058 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
| 2 | mjames | 1059 | } |
| 1060 | |||
| 1061 | /* USER CODE BEGIN 4 */ |
||
| 1062 | |||
| 1063 | /* USER CODE END 4 */ |
||
| 1064 | |||
| 5 | mjames | 1065 | /** |
| 62 | mjames | 1066 | * @brief This function is executed in case of error occurrence. |
| 1067 | * @retval None |
||
| 1068 | */ |
||
| 58 | mjames | 1069 | void Error_Handler(void) |
| 5 | mjames | 1070 | { |
| 50 | mjames | 1071 | /* USER CODE BEGIN Error_Handler_Debug */ |
| 1072 | /* User can add his own implementation to report the HAL error return state */ |
||
| 1073 | |||
| 1074 | /* USER CODE END Error_Handler_Debug */ |
||
| 30 | mjames | 1075 | } |
| 5 | mjames | 1076 | |
| 62 | mjames | 1077 | #ifdef USE_FULL_ASSERT |
| 2 | mjames | 1078 | /** |
| 62 | mjames | 1079 | * @brief Reports the name of the source file and the source line number |
| 1080 | * where the assert_param error has occurred. |
||
| 1081 | * @param file: pointer to the source file name |
||
| 1082 | * @param line: assert_param error line source number |
||
| 1083 | * @retval None |
||
| 1084 | */ |
||
| 50 | mjames | 1085 | void assert_failed(uint8_t *file, uint32_t line) |
| 29 | mjames | 1086 | { |
| 1087 | /* USER CODE BEGIN 6 */ |
||
| 50 | mjames | 1088 | /* User can add his own implementation to report the file name and line number, |
| 1089 | tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ |
||
| 29 | mjames | 1090 | /* USER CODE END 6 */ |
| 1091 | } |
||
| 50 | mjames | 1092 | #endif /* USE_FULL_ASSERT */ |