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