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