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