Rev 24 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 2 | mjames | 1 | /** |
| 27 | mjames | 2 | ****************************************************************************** |
| 3 | * File Name : main.c |
||
| 4 | * Description : Main program body |
||
| 5 | ****************************************************************************** |
||
| 6 | * |
||
| 7 | * COPYRIGHT(c) 2017 STMicroelectronics |
||
| 8 | * |
||
| 9 | * Redistribution and use in source and binary forms, with or without modification, |
||
| 10 | * are permitted provided that the following conditions are met: |
||
| 11 | * 1. Redistributions of source code must retain the above copyright notice, |
||
| 12 | * this list of conditions and the following disclaimer. |
||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
||
| 14 | * this list of conditions and the following disclaimer in the documentation |
||
| 15 | * and/or other materials provided with the distribution. |
||
| 16 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
||
| 17 | * may be used to endorse or promote products derived from this software |
||
| 18 | * without specific prior written permission. |
||
| 19 | * |
||
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
||
| 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
| 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
||
| 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
||
| 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
||
| 26 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||
| 27 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
||
| 28 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||
| 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
| 30 | * |
||
| 31 | ****************************************************************************** |
||
| 32 | */ |
||
| 2 | mjames | 33 | /* Includes ------------------------------------------------------------------*/ |
| 34 | #include "stm32f1xx_hal.h" |
||
| 35 | |||
| 36 | /* USER CODE BEGIN Includes */ |
||
| 37 | #include "ap_math.h" |
||
| 38 | #include "serial.h" |
||
| 39 | #include "SSD1306.h" |
||
| 7 | mjames | 40 | #include "Font.h" |
| 2 | mjames | 41 | #include "dials.h" |
| 4 | mjames | 42 | #include "switches.h" |
| 2 | mjames | 43 | #include <math.h> |
| 4 | mjames | 44 | #include "plx.h" |
| 16 | mjames | 45 | #include "eeprom.h" |
| 20 | mjames | 46 | #include "displayinfo.h" |
| 27 | mjames | 47 | #include "small_printf.h" |
| 2 | mjames | 48 | |
| 49 | /* USER CODE END Includes */ |
||
| 50 | |||
| 51 | /* Private variables ---------------------------------------------------------*/ |
||
| 52 | SPI_HandleTypeDef hspi1; |
||
| 53 | |||
| 3 | mjames | 54 | UART_HandleTypeDef huart1; |
| 2 | mjames | 55 | UART_HandleTypeDef huart2; |
| 23 | mjames | 56 | UART_HandleTypeDef huart3; |
| 2 | mjames | 57 | |
| 58 | /* USER CODE BEGIN PV */ |
||
| 59 | /* Private variables ---------------------------------------------------------*/ |
||
| 18 | mjames | 60 | #define MAXRDG 32 |
| 2 | mjames | 61 | |
| 24 | mjames | 62 | /* timeout when the ignition is switched off */ |
| 63 | #define IGNITION_OFF_TIMEOUT 30000UL |
||
| 64 | |||
| 7 | mjames | 65 | int OldObservation[2] = |
| 27 | mjames | 66 | { -1, -1 }; // illegal initial value |
| 7 | mjames | 67 | int OldObservationIndex[2] = |
| 27 | mjames | 68 | { -1, -1 }; // if more than one sensor this will be printed |
| 7 | mjames | 69 | int16_t dial0[2] = |
| 27 | mjames | 70 | { 0, 0 }; |
| 7 | mjames | 71 | int16_t dial1[2] = |
| 27 | mjames | 72 | { -1, -1 }; |
| 14 | mjames | 73 | |
| 18 | mjames | 74 | uint16_t dial_timer[2] = |
| 27 | mjames | 75 | { 0, 0 }; |
| 18 | mjames | 76 | |
| 77 | static const int DialTimeout = 50; // about 20 seconds after twiddle, save the dial position. |
||
| 78 | |||
| 16 | mjames | 79 | /* Virtual address defined by the user: 0xFFFF value is prohibited */ |
| 27 | mjames | 80 | uint16_t VirtAddVarTab[NumbOfVar] = |
| 81 | { 0x1111, 0x2222 }; |
||
| 14 | mjames | 82 | |
| 7 | mjames | 83 | union |
| 84 | { |
||
| 27 | mjames | 85 | PLX_SensorInfo Sensor[MAXRDG]; |
| 86 | char Bytes[MAXRDG * sizeof(PLX_SensorInfo)]; |
||
| 7 | mjames | 87 | } Data; |
| 88 | int Max[MAXRDG]; |
||
| 89 | int Min[MAXRDG]; |
||
| 90 | int PLXItems; |
||
| 24 | mjames | 91 | |
| 27 | mjames | 92 | uint32_t Latch_Timer = IGNITION_OFF_TIMEOUT; |
| 24 | mjames | 93 | |
| 2 | mjames | 94 | /* USER CODE END PV */ |
| 95 | |||
| 96 | /* Private function prototypes -----------------------------------------------*/ |
||
| 27 | mjames | 97 | void |
| 98 | SystemClock_Config (void); |
||
| 99 | void |
||
| 100 | Error_Handler (void); |
||
| 101 | static void |
||
| 102 | MX_GPIO_Init (void); |
||
| 103 | static void |
||
| 104 | MX_SPI1_Init (void); |
||
| 105 | static void |
||
| 106 | MX_USART2_UART_Init (void); |
||
| 107 | static void |
||
| 108 | MX_USART1_UART_Init (void); |
||
| 109 | static void |
||
| 110 | MX_USART3_UART_Init (void); |
||
| 2 | mjames | 111 | |
| 112 | /* USER CODE BEGIN PFP */ |
||
| 113 | /* Private function prototypes -----------------------------------------------*/ |
||
| 114 | |||
| 115 | /* USER CODE END PFP */ |
||
| 116 | |||
| 117 | /* USER CODE BEGIN 0 */ |
||
| 118 | /* dummy function */ |
||
| 27 | mjames | 119 | void |
| 120 | _init (void) |
||
| 6 | mjames | 121 | { |
| 2 | mjames | 122 | |
| 123 | } |
||
| 7 | mjames | 124 | // the dial is the switch number we are using. |
| 125 | // suppress is the ItemIndex we wish to suppress on this display |
||
| 27 | mjames | 126 | int |
| 127 | DisplayCurrent (int dial, int suppress) |
||
| 7 | mjames | 128 | { |
| 27 | mjames | 129 | char buff[10]; |
| 130 | int i; |
||
| 131 | int rc; |
||
| 132 | select_display (dial); // pick the display we are using |
||
| 133 | int ItemIndex = dial_pos[dial] / 4; |
||
| 14 | mjames | 134 | |
| 27 | mjames | 135 | // wrap around count if dial too far to the right |
| 136 | if (ItemIndex >= PLXItems) |
||
| 137 | { |
||
| 138 | dial_pos[dial] = 0; |
||
| 139 | ItemIndex = 0; |
||
| 140 | } |
||
| 141 | if (ItemIndex < 0) |
||
| 142 | { |
||
| 143 | ItemIndex = PLXItems - 1; |
||
| 144 | dial_pos[dial] = (PLXItems - 1) * 4; |
||
| 145 | } |
||
| 2 | mjames | 146 | |
| 27 | mjames | 147 | // check for item suppression |
| 148 | if (ItemIndex == suppress) |
||
| 149 | { |
||
| 150 | dial1[dial] = -1; |
||
| 151 | OldObservation[dial] = -1; |
||
| 152 | OldObservationIndex[dial] = -1; |
||
| 9 | mjames | 153 | |
| 27 | mjames | 154 | clearDisplay (); |
| 155 | display (); |
||
| 156 | return -1; // we suppressed this display |
||
| 157 | } |
||
| 158 | // do not try to convert if no items in buffer |
||
| 159 | if (PLXItems > 0) |
||
| 160 | { |
||
| 161 | int DataVal = ConvPLX (Data.Sensor[ItemIndex].ReadingH, |
||
| 162 | Data.Sensor[ItemIndex].ReadingL); // data reading |
||
| 163 | int Observation = ConvPLX (Data.Sensor[ItemIndex].AddrH, |
||
| 164 | Data.Sensor[ItemIndex].AddrL); |
||
| 165 | int ObservationIndex = ConvPLX (0, Data.Sensor[ItemIndex].Instance); |
||
| 166 | // now to convert the readings and format strings |
||
| 167 | // find out limits |
||
| 168 | char * msg; |
||
| 169 | int len; |
||
| 9 | mjames | 170 | |
| 27 | mjames | 171 | // if the user presses the dial then reset min/max to current value |
| 172 | if (push_pos[dial] == 1) |
||
| 7 | mjames | 173 | { |
| 27 | mjames | 174 | Max[ItemIndex] = DataVal; |
| 175 | Min[ItemIndex] = DataVal; // 12 bit max value |
||
| 176 | } |
||
| 2 | mjames | 177 | |
| 27 | mjames | 178 | if (Observation < PLX_MAX_OBS) |
| 7 | mjames | 179 | { |
| 27 | mjames | 180 | if (Observation != OldObservation[dial] |
| 181 | || ObservationIndex != OldObservationIndex[dial]) |
||
| 182 | { |
||
| 2 | mjames | 183 | |
| 27 | mjames | 184 | dial_timer[dial] = DialTimeout; |
| 185 | |||
| 186 | dial1[dial] = -1; |
||
| 187 | clearDisplay (); |
||
| 188 | dial_draw_scale (DisplayInfo[Observation].Low, |
||
| 189 | DisplayInfo[Observation].High, 12, 1, |
||
| 190 | DisplayInfo[Observation].TickScale); |
||
| 191 | |||
| 192 | msg = DisplayInfo[Observation].name; |
||
| 193 | len = 7; |
||
| 194 | int len1 = ObservationIndex > 0 ? len - 1 : len; |
||
| 195 | for (i = 0; i < len1 && msg[i]; i++) |
||
| 9 | mjames | 196 | { |
| 27 | mjames | 197 | buff[i] = msg[i]; |
| 9 | mjames | 198 | } |
| 27 | mjames | 199 | if (ObservationIndex > 0 && i < len) |
| 200 | { |
||
| 201 | buff[i++] = ObservationIndex + '1'; |
||
| 202 | } |
||
| 9 | mjames | 203 | |
| 27 | mjames | 204 | print_large_string (buff, 64 - i * 4, 48, i); // this prints spaces for \0 at end of string |
| 9 | mjames | 205 | |
| 27 | mjames | 206 | // print suffix if present. |
| 207 | font_gotoxy (15, 4); |
||
| 208 | int i = 0; |
||
| 209 | while (DisplayInfo[Observation].suffix[i]) |
||
| 7 | mjames | 210 | { |
| 27 | mjames | 211 | font_putchar (DisplayInfo[Observation].suffix[i++]); |
| 212 | } |
||
| 14 | mjames | 213 | |
| 27 | mjames | 214 | OldObservation[dial] = Observation; |
| 215 | OldObservationIndex[dial] = ObservationIndex; |
||
| 216 | // |
||
| 217 | display (); |
||
| 14 | mjames | 218 | |
| 27 | mjames | 219 | } |
| 220 | else |
||
| 221 | { |
||
| 222 | // check for timer timeout on consistent timer |
||
| 223 | if (dial_timer[dial]) |
||
| 224 | { |
||
| 225 | dial_timer[dial]--; |
||
| 14 | mjames | 226 | |
| 27 | mjames | 227 | if (dial_timer[dial] == 0) |
| 228 | { |
||
| 229 | uint16_t curr_val = dial_pos[dial]; |
||
| 230 | rc = EE_ReadVariable (VirtAddVarTab[dial], &curr_val); |
||
| 231 | if ((rc != 0) || (curr_val != dial_pos[dial])) |
||
| 232 | { |
||
| 233 | //__disable_irq(); |
||
| 234 | HAL_FLASH_Unlock (); |
||
| 2 | mjames | 235 | |
| 27 | mjames | 236 | rc = EE_WriteVariable (VirtAddVarTab[dial], |
| 237 | dial_pos[dial]); |
||
| 238 | HAL_FLASH_Lock (); |
||
| 239 | //__enable_irq(); |
||
| 7 | mjames | 240 | } |
| 27 | mjames | 241 | } |
| 242 | } |
||
| 2 | mjames | 243 | |
| 27 | mjames | 244 | } |
| 21 | mjames | 245 | |
| 27 | mjames | 246 | double max_rdg; |
| 247 | double min_rdg; |
||
| 248 | double cur_rdg; |
||
| 249 | int int_rdg; |
||
| 250 | int int_max; |
||
| 251 | int int_min; |
||
| 18 | mjames | 252 | |
| 27 | mjames | 253 | max_rdg = ConveriMFDRaw2Data (Observation, |
| 254 | DisplayInfo[Observation].Units, |
||
| 255 | Max[ItemIndex]); |
||
| 256 | min_rdg = ConveriMFDRaw2Data (Observation, |
||
| 257 | DisplayInfo[Observation].Units, |
||
| 258 | Min[ItemIndex]); |
||
| 259 | cur_rdg = ConveriMFDRaw2Data (Observation, |
||
| 260 | DisplayInfo[Observation].Units, |
||
| 261 | DataVal); |
||
| 18 | mjames | 262 | |
| 27 | mjames | 263 | int dp_pos; // where to print the decimal place |
| 264 | float scale = 1.0; |
||
| 265 | switch (DisplayInfo[Observation].DP) |
||
| 266 | { |
||
| 267 | case 0: |
||
| 268 | scale = 1.0; |
||
| 269 | dp_pos = 100; |
||
| 270 | break; |
||
| 271 | case 1: |
||
| 272 | scale = 10.0; |
||
| 273 | dp_pos = 1; |
||
| 274 | break; |
||
| 275 | case 2: |
||
| 276 | scale = 100.0; |
||
| 277 | dp_pos = 2; |
||
| 278 | break; |
||
| 279 | } |
||
| 280 | int_rdg = (int) (cur_rdg * scale); |
||
| 281 | int_max = (int) (max_rdg * scale); |
||
| 282 | int_min = (int) (min_rdg * scale); |
||
| 2 | mjames | 283 | |
| 27 | mjames | 284 | cur_rdg -= DisplayInfo[Observation].Low; |
| 285 | cur_rdg = 100 * cur_rdg |
||
| 286 | / (DisplayInfo[Observation].High - DisplayInfo[Observation].Low); |
||
| 2 | mjames | 287 | |
| 27 | mjames | 288 | dial0[dial] = (int) cur_rdg; |
| 2 | mjames | 289 | |
| 27 | mjames | 290 | /* old needle un-draw */ |
| 291 | if (dial1[dial] >= 0) |
||
| 292 | { |
||
| 293 | dial_draw_needle (dial1[dial]); |
||
| 294 | } |
||
| 295 | dial_draw_needle (dial0[dial]); |
||
| 296 | // print value overlaid by needle |
||
| 297 | // this is actual reading |
||
| 298 | print_digits (30, 30, 5, dp_pos, int_rdg); |
||
| 299 | font_gotoxy (0, 0); |
||
| 300 | font_digits (5, dp_pos, int_min); |
||
| 2 | mjames | 301 | |
| 27 | mjames | 302 | font_gotoxy (0, 1); |
| 303 | font_puts ("Min"); |
||
| 2 | mjames | 304 | |
| 27 | mjames | 305 | font_gotoxy (15, 0); |
| 306 | font_digits (5, dp_pos, int_max); |
||
| 307 | font_gotoxy (18, 1); |
||
| 308 | font_puts ("Max"); |
||
| 15 | mjames | 309 | |
| 27 | mjames | 310 | dial1[dial] = dial0[dial]; |
| 2 | mjames | 311 | |
| 27 | mjames | 312 | display (); |
| 9 | mjames | 313 | |
| 7 | mjames | 314 | } |
| 27 | mjames | 315 | } |
| 316 | return ItemIndex; |
||
| 7 | mjames | 317 | } |
| 318 | /* USER CODE END 0 */ |
||
| 2 | mjames | 319 | |
| 27 | mjames | 320 | int |
| 321 | main (void) |
||
| 7 | mjames | 322 | { |
| 2 | mjames | 323 | |
| 16 | mjames | 324 | /* USER CODE BEGIN 1 */ |
| 2 | mjames | 325 | |
| 27 | mjames | 326 | GPIO_InitTypeDef GPIO_InitStruct; |
| 2 | mjames | 327 | |
| 27 | mjames | 328 | __HAL_RCC_SPI1_CLK_ENABLE() |
| 329 | ; |
||
| 330 | __HAL_RCC_USART1_CLK_ENABLE() |
||
| 331 | ; // PLX main port |
||
| 332 | __HAL_RCC_USART2_CLK_ENABLE() |
||
| 333 | ; // debug port |
||
| 334 | __HAL_RCC_USART3_CLK_ENABLE (); // Bluetooth port |
||
| 23 | mjames | 335 | |
| 16 | mjames | 336 | /* USER CODE END 1 */ |
| 2 | mjames | 337 | |
| 16 | mjames | 338 | /* MCU Configuration----------------------------------------------------------*/ |
| 6 | mjames | 339 | |
| 16 | mjames | 340 | /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ |
| 27 | mjames | 341 | HAL_Init (); |
| 2 | mjames | 342 | |
| 16 | mjames | 343 | /* Configure the system clock */ |
| 27 | mjames | 344 | SystemClock_Config (); |
| 2 | mjames | 345 | |
| 16 | mjames | 346 | /* Initialize all configured peripherals */ |
| 27 | mjames | 347 | MX_GPIO_Init (); |
| 348 | MX_SPI1_Init (); |
||
| 349 | MX_USART2_UART_Init (); |
||
| 350 | MX_USART1_UART_Init (); |
||
| 351 | MX_USART3_UART_Init (); |
||
| 2 | mjames | 352 | |
| 16 | mjames | 353 | /* USER CODE BEGIN 2 */ |
| 2 | mjames | 354 | |
| 27 | mjames | 355 | /* Turn on USART1 IRQ */ |
| 356 | HAL_NVIC_SetPriority (USART1_IRQn, 2, 0); |
||
| 357 | HAL_NVIC_EnableIRQ (USART1_IRQn); |
||
| 4 | mjames | 358 | |
| 27 | mjames | 359 | /* Turn on USART2 IRQ */ |
| 360 | HAL_NVIC_SetPriority (USART2_IRQn, 4, 0); |
||
| 361 | HAL_NVIC_EnableIRQ (USART2_IRQn); |
||
| 2 | mjames | 362 | |
| 27 | mjames | 363 | /* turn on USART3 IRQ */ |
| 364 | HAL_NVIC_SetPriority (USART3_IRQn, 4, 0); |
||
| 365 | HAL_NVIC_EnableIRQ (USART3_IRQn); |
||
| 4 | mjames | 366 | |
| 27 | mjames | 367 | /* setup the USART control blocks */ |
| 368 | init_usart_ctl (&uc1, huart1.Instance); |
||
| 369 | init_usart_ctl (&uc2, huart2.Instance); |
||
| 370 | init_usart_ctl (&uc3, huart3.Instance); |
||
| 23 | mjames | 371 | |
| 27 | mjames | 372 | EnableSerialRxInterrupt (&uc1); |
| 373 | EnableSerialRxInterrupt (&uc2); |
||
| 374 | EnableSerialRxInterrupt (&uc3); |
||
| 23 | mjames | 375 | |
| 27 | mjames | 376 | /* Unlock the Flash to enable the flash control register access *************/ |
| 377 | HAL_FLASH_Unlock (); |
||
| 23 | mjames | 378 | |
| 27 | mjames | 379 | //__disable_irq(); |
| 380 | EE_Init (); |
||
| 381 | //__enable_irq(); |
||
| 23 | mjames | 382 | |
| 27 | mjames | 383 | HAL_FLASH_Lock (); |
| 2 | mjames | 384 | |
| 27 | mjames | 385 | InitSwitches (); |
| 23 | mjames | 386 | |
| 27 | mjames | 387 | int i; |
| 388 | uint16_t rc; |
||
| 389 | for (i = 0; i < 2; i++) |
||
| 390 | { |
||
| 391 | uint16_t val; |
||
| 7 | mjames | 392 | |
| 27 | mjames | 393 | uint16_t rc = EE_ReadVariable (VirtAddVarTab[i], &val); |
| 16 | mjames | 394 | |
| 27 | mjames | 395 | if (rc == 0) |
| 17 | mjames | 396 | { |
| 27 | mjames | 397 | dial_pos[i] = val; |
| 17 | mjames | 398 | } |
| 27 | mjames | 399 | else |
| 400 | { |
||
| 401 | break; |
||
| 402 | } |
||
| 403 | } |
||
| 17 | mjames | 404 | |
| 27 | mjames | 405 | ap_init (); // set up the approximate math library |
| 18 | mjames | 406 | |
| 27 | mjames | 407 | int disp; |
| 18 | mjames | 408 | |
| 27 | mjames | 409 | ssd1306_begin (1, 0); |
| 410 | dial_origin (64, 60); |
||
| 411 | dial_size (60); |
||
| 7 | mjames | 412 | |
| 27 | mjames | 413 | /* reset the display timeout, latch on power from accessories */ |
| 414 | Latch_Timer = IGNITION_OFF_TIMEOUT; |
||
| 415 | HAL_GPIO_WritePin (POWER_LATCH_GPIO_Port, POWER_LATCH_Pin, GPIO_PIN_RESET); |
||
| 7 | mjames | 416 | |
| 27 | mjames | 417 | for (disp = 0; disp < 2; disp++) |
| 418 | { |
||
| 419 | select_display (disp); |
||
| 420 | clearDisplay (); |
||
| 421 | dim (0); |
||
| 422 | //font_puts( |
||
| 423 | // "Hello world !!\rThis text is a test of the text rendering library in a 5*7 font"); |
||
| 10 | mjames | 424 | |
| 27 | mjames | 425 | dial_draw_scale (0, 10, 12, 5, 1); |
| 426 | char buffer[] = "Display "; |
||
| 427 | buffer[8] = disp + '1'; |
||
| 428 | print_large_string (buffer, 20, 30, 9); |
||
| 10 | mjames | 429 | |
| 27 | mjames | 430 | display (); |
| 4 | mjames | 431 | |
| 27 | mjames | 432 | } |
| 4 | mjames | 433 | |
| 16 | mjames | 434 | /* USER CODE END 2 */ |
| 7 | mjames | 435 | |
| 16 | mjames | 436 | /* Infinite loop */ |
| 437 | /* USER CODE BEGIN WHILE */ |
||
| 27 | mjames | 438 | uint32_t Ticks = HAL_GetTick () + 100; |
| 7 | mjames | 439 | |
| 27 | mjames | 440 | /* while ignition is on, keep resetting power latch timer */ |
| 441 | if (HAL_GPIO_ReadPin (IGNITION_GPIO_Port, IGNITION_Pin) == GPIO_PIN_RESET) |
||
| 442 | { |
||
| 443 | Latch_Timer = HAL_GetTick () + IGNITION_OFF_TIMEOUT; |
||
| 444 | } |
||
| 445 | else |
||
| 446 | { |
||
| 447 | /* if the ignition has been off for a while, then turn off power */ |
||
| 448 | if (HAL_GetTick () > Latch_Timer) |
||
| 24 | mjames | 449 | { |
| 27 | mjames | 450 | HAL_GPIO_WritePin (POWER_LATCH_GPIO_Port, POWER_LATCH_Pin, |
| 451 | GPIO_PIN_RESET); |
||
| 24 | mjames | 452 | } |
| 27 | mjames | 453 | } |
| 454 | |||
| 455 | uint32_t timeout = 0; // |
||
| 456 | // PLX decoder protocols |
||
| 457 | char PLXPacket = 0; |
||
| 458 | for (i = 0; i < MAXRDG; i++) |
||
| 459 | { |
||
| 460 | Max[i] = 0; |
||
| 461 | Min[i] = 0xFFF; // 12 bit max value |
||
| 462 | } |
||
| 463 | |||
| 464 | int PLXPtr = 0; |
||
| 465 | |||
| 466 | while (1) |
||
| 467 | { |
||
| 468 | // poll switche |
||
| 469 | HandleSwitches (); |
||
| 470 | // Handle the bluetooth pairing function by pressing both buttons. |
||
| 471 | if ((push_pos[0] == 1) && (push_pos[1] == 1)) |
||
| 24 | mjames | 472 | { |
| 27 | mjames | 473 | HAL_GPIO_WritePin (BT_BUTTON_GPIO_Port, BT_BUTTON_Pin, GPIO_PIN_SET); |
| 24 | mjames | 474 | } |
| 27 | mjames | 475 | else |
| 476 | { |
||
| 477 | HAL_GPIO_WritePin (BT_BUTTON_GPIO_Port, BT_BUTTON_Pin, |
||
| 478 | GPIO_PIN_RESET); |
||
| 24 | mjames | 479 | |
| 7 | mjames | 480 | } |
| 481 | |||
| 27 | mjames | 482 | uint16_t cc = SerialCharsReceived (&uc1); |
| 483 | int chr; |
||
| 484 | if (cc == 0) |
||
| 485 | { |
||
| 486 | timeout++; |
||
| 487 | if (timeout % 1000 == 0) |
||
| 488 | { |
||
| 489 | PutCharSerial (&uc3, '+'); |
||
| 490 | } |
||
| 491 | if (timeout > 60000) |
||
| 492 | { |
||
| 493 | // do turn off screen |
||
| 494 | } |
||
| 7 | mjames | 495 | |
| 27 | mjames | 496 | } |
| 497 | for (chr = 0; chr < cc; chr++) |
||
| 7 | mjames | 498 | { |
| 27 | mjames | 499 | char c = GetCharSerial (&uc1); |
| 500 | timeout = 0; |
||
| 501 | |||
| 502 | if (c == PLX_Start) // at any time if the start byte appears, reset the pointers |
||
| 503 | { |
||
| 504 | PLXPtr = 0; // reset the pointer |
||
| 505 | PLXPacket = 1; |
||
| 506 | } |
||
| 507 | else if (c == PLX_Stop) |
||
| 508 | { |
||
| 509 | if (PLXPacket) |
||
| 23 | mjames | 510 | { |
| 27 | mjames | 511 | // we can now decode the selected parameter |
| 512 | PLXItems = PLXPtr / sizeof(PLX_SensorInfo); // total |
||
| 513 | // saturate the rotary switch position |
||
| 7 | mjames | 514 | |
| 27 | mjames | 515 | int DataVal; |
| 516 | // process min/max |
||
| 517 | for (i = 0; i < PLXItems; i++) |
||
| 518 | { |
||
| 519 | // Send item to BT |
||
| 520 | uint16_t addr = ConvPLX (Data.Sensor[i].AddrH, |
||
| 521 | Data.Sensor[i].AddrL); |
||
| 522 | uint8_t inst = Data.Sensor[i].Instance; |
||
| 523 | uint16_t reading = ConvPLX (Data.Sensor[i].ReadingH, |
||
| 524 | Data.Sensor[i].ReadingL); |
||
| 9 | mjames | 525 | |
| 27 | mjames | 526 | char outbuff[100]; |
| 527 | int cnt = simple_sprintf (outbuff, "%d,%d,%d\n", addr, inst, |
||
| 528 | reading); |
||
| 529 | int i; |
||
| 530 | for (i = 0; i < cnt; i++) |
||
| 23 | mjames | 531 | |
| 532 | { |
||
| 27 | mjames | 533 | PutCharSerial (&uc3, outbuff[i]); |
| 23 | mjames | 534 | } |
| 27 | mjames | 535 | DataVal = ConvPLX (Data.Sensor[i].ReadingH, |
| 536 | Data.Sensor[i].ReadingL); |
||
| 537 | if (DataVal > Max[i]) |
||
| 23 | mjames | 538 | { |
| 27 | mjames | 539 | Max[i] = DataVal; |
| 23 | mjames | 540 | } |
| 27 | mjames | 541 | if (DataVal < Min[i]) |
| 6 | mjames | 542 | { |
| 27 | mjames | 543 | Min[i] = DataVal; |
| 4 | mjames | 544 | } |
| 27 | mjames | 545 | } |
| 7 | mjames | 546 | |
| 27 | mjames | 547 | // now to display the information |
| 548 | int suppress = DisplayCurrent (0, -1); |
||
| 549 | DisplayCurrent (1, suppress); |
||
| 6 | mjames | 550 | } |
| 27 | mjames | 551 | PLXPtr = 0; |
| 552 | PLXPacket = 0; |
||
| 553 | } |
||
| 554 | else if (c > PLX_Stop) // illegal char, restart reading |
||
| 555 | { |
||
| 556 | PLXPacket = 0; |
||
| 557 | PLXPtr = 0; |
||
| 558 | } |
||
| 559 | else if (PLXPtr < sizeof(Data.Bytes)) |
||
| 560 | { |
||
| 561 | Data.Bytes[PLXPtr++] = c; |
||
| 562 | } |
||
| 563 | } |
||
| 4 | mjames | 564 | |
| 27 | mjames | 565 | HAL_Delay (1); |
| 566 | } |
||
| 16 | mjames | 567 | /* USER CODE END WHILE */ |
| 2 | mjames | 568 | |
| 16 | mjames | 569 | /* USER CODE BEGIN 3 */ |
| 6 | mjames | 570 | |
| 16 | mjames | 571 | /* USER CODE END 3 */ |
| 572 | |||
| 2 | mjames | 573 | } |
| 574 | |||
| 575 | /** System Clock Configuration |
||
| 27 | mjames | 576 | */ |
| 577 | void |
||
| 578 | SystemClock_Config (void) |
||
| 5 | mjames | 579 | { |
| 2 | mjames | 580 | |
| 16 | mjames | 581 | RCC_OscInitTypeDef RCC_OscInitStruct; |
| 582 | RCC_ClkInitTypeDef RCC_ClkInitStruct; |
||
| 2 | mjames | 583 | |
| 16 | mjames | 584 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; |
| 585 | RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS; |
||
| 586 | RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; |
||
| 587 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
||
| 588 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; |
||
| 589 | RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; |
||
| 27 | mjames | 590 | if (HAL_RCC_OscConfig (&RCC_OscInitStruct) != HAL_OK) |
| 591 | { |
||
| 592 | Error_Handler (); |
||
| 593 | } |
||
| 2 | mjames | 594 | |
| 27 | mjames | 595 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |
| 596 | | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; |
||
| 16 | mjames | 597 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
| 598 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
||
| 599 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; |
||
| 600 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; |
||
| 27 | mjames | 601 | if (HAL_RCC_ClockConfig (&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) |
| 602 | { |
||
| 603 | Error_Handler (); |
||
| 604 | } |
||
| 2 | mjames | 605 | |
| 27 | mjames | 606 | HAL_SYSTICK_Config (HAL_RCC_GetHCLKFreq () / 1000); |
| 2 | mjames | 607 | |
| 27 | mjames | 608 | HAL_SYSTICK_CLKSourceConfig (SYSTICK_CLKSOURCE_HCLK); |
| 2 | mjames | 609 | |
| 16 | mjames | 610 | /* SysTick_IRQn interrupt configuration */ |
| 27 | mjames | 611 | HAL_NVIC_SetPriority (SysTick_IRQn, 0, 0); |
| 2 | mjames | 612 | } |
| 613 | |||
| 614 | /* SPI1 init function */ |
||
| 27 | mjames | 615 | static void |
| 616 | MX_SPI1_Init (void) |
||
| 5 | mjames | 617 | { |
| 2 | mjames | 618 | |
| 16 | mjames | 619 | hspi1.Instance = SPI1; |
| 620 | hspi1.Init.Mode = SPI_MODE_MASTER; |
||
| 621 | hspi1.Init.Direction = SPI_DIRECTION_1LINE; |
||
| 622 | hspi1.Init.DataSize = SPI_DATASIZE_8BIT; |
||
| 623 | hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH; |
||
| 624 | hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; |
||
| 625 | hspi1.Init.NSS = SPI_NSS_SOFT; |
||
| 626 | hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; |
||
| 627 | hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; |
||
| 628 | hspi1.Init.TIMode = SPI_TIMODE_DISABLE; |
||
| 629 | hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; |
||
| 630 | hspi1.Init.CRCPolynomial = 10; |
||
| 27 | mjames | 631 | if (HAL_SPI_Init (&hspi1) != HAL_OK) |
| 632 | { |
||
| 633 | Error_Handler (); |
||
| 634 | } |
||
| 2 | mjames | 635 | |
| 636 | } |
||
| 637 | |||
| 3 | mjames | 638 | /* USART1 init function */ |
| 27 | mjames | 639 | static void |
| 640 | MX_USART1_UART_Init (void) |
||
| 5 | mjames | 641 | { |
| 3 | mjames | 642 | |
| 16 | mjames | 643 | huart1.Instance = USART1; |
| 644 | huart1.Init.BaudRate = 19200; |
||
| 645 | huart1.Init.WordLength = UART_WORDLENGTH_8B; |
||
| 646 | huart1.Init.StopBits = UART_STOPBITS_1; |
||
| 647 | huart1.Init.Parity = UART_PARITY_NONE; |
||
| 648 | huart1.Init.Mode = UART_MODE_TX_RX; |
||
| 649 | huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; |
||
| 650 | huart1.Init.OverSampling = UART_OVERSAMPLING_16; |
||
| 27 | mjames | 651 | if (HAL_UART_Init (&huart1) != HAL_OK) |
| 652 | { |
||
| 653 | Error_Handler (); |
||
| 654 | } |
||
| 3 | mjames | 655 | |
| 656 | } |
||
| 657 | |||
| 2 | mjames | 658 | /* USART2 init function */ |
| 27 | mjames | 659 | static void |
| 660 | MX_USART2_UART_Init (void) |
||
| 5 | mjames | 661 | { |
| 2 | mjames | 662 | |
| 16 | mjames | 663 | huart2.Instance = USART2; |
| 664 | huart2.Init.BaudRate = 115200; |
||
| 665 | huart2.Init.WordLength = UART_WORDLENGTH_8B; |
||
| 666 | huart2.Init.StopBits = UART_STOPBITS_1; |
||
| 667 | huart2.Init.Parity = UART_PARITY_NONE; |
||
| 668 | huart2.Init.Mode = UART_MODE_TX_RX; |
||
| 669 | huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; |
||
| 670 | huart2.Init.OverSampling = UART_OVERSAMPLING_16; |
||
| 27 | mjames | 671 | if (HAL_UART_Init (&huart2) != HAL_OK) |
| 672 | { |
||
| 673 | Error_Handler (); |
||
| 674 | } |
||
| 2 | mjames | 675 | |
| 676 | } |
||
| 677 | |||
| 23 | mjames | 678 | /* USART3 init function */ |
| 27 | mjames | 679 | static void |
| 680 | MX_USART3_UART_Init (void) |
||
| 23 | mjames | 681 | { |
| 682 | |||
| 683 | huart3.Instance = USART3; |
||
| 684 | huart3.Init.BaudRate = 19200; |
||
| 685 | huart3.Init.WordLength = UART_WORDLENGTH_8B; |
||
| 686 | huart3.Init.StopBits = UART_STOPBITS_1; |
||
| 687 | huart3.Init.Parity = UART_PARITY_NONE; |
||
| 688 | huart3.Init.Mode = UART_MODE_TX_RX; |
||
| 689 | huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE; |
||
| 690 | huart3.Init.OverSampling = UART_OVERSAMPLING_16; |
||
| 27 | mjames | 691 | if (HAL_UART_Init (&huart3) != HAL_OK) |
| 692 | { |
||
| 693 | Error_Handler (); |
||
| 694 | } |
||
| 23 | mjames | 695 | |
| 696 | } |
||
| 697 | |||
| 7 | mjames | 698 | /** Configure pins as |
| 27 | mjames | 699 | * Analog |
| 700 | * Input |
||
| 701 | * Output |
||
| 702 | * EVENT_OUT |
||
| 703 | * EXTI |
||
| 704 | */ |
||
| 705 | static void |
||
| 706 | MX_GPIO_Init (void) |
||
| 5 | mjames | 707 | { |
| 2 | mjames | 708 | |
| 16 | mjames | 709 | GPIO_InitTypeDef GPIO_InitStruct; |
| 2 | mjames | 710 | |
| 16 | mjames | 711 | /* GPIO Ports Clock Enable */ |
| 27 | mjames | 712 | __HAL_RCC_GPIOD_CLK_ENABLE() |
| 713 | ; |
||
| 714 | __HAL_RCC_GPIOA_CLK_ENABLE() |
||
| 715 | ; |
||
| 716 | __HAL_RCC_GPIOC_CLK_ENABLE() |
||
| 717 | ; |
||
| 718 | __HAL_RCC_GPIOB_CLK_ENABLE() |
||
| 719 | ; |
||
| 2 | mjames | 720 | |
| 16 | mjames | 721 | /*Configure GPIO pin Output Level */ |
| 27 | mjames | 722 | HAL_GPIO_WritePin (SPI_NSS1_GPIO_Port, SPI_NSS1_Pin, GPIO_PIN_SET); |
| 2 | mjames | 723 | |
| 16 | mjames | 724 | /*Configure GPIO pin Output Level */ |
| 27 | mjames | 725 | HAL_GPIO_WritePin (GPIOA, SPI1CD_Pin | BT_BUTTON_Pin, GPIO_PIN_RESET); |
| 2 | mjames | 726 | |
| 16 | mjames | 727 | /*Configure GPIO pin Output Level */ |
| 27 | mjames | 728 | HAL_GPIO_WritePin (GPIOC, SPI_RESET_Pin | POWER_LATCH_Pin | USB_PWR_Pin, |
| 729 | GPIO_PIN_RESET); |
||
| 2 | mjames | 730 | |
| 16 | mjames | 731 | /*Configure GPIO pin Output Level */ |
| 27 | mjames | 732 | HAL_GPIO_WritePin (SPI_NSS2_GPIO_Port, SPI_NSS2_Pin, GPIO_PIN_SET); |
| 2 | mjames | 733 | |
| 23 | mjames | 734 | /*Configure GPIO pins : SPI_NSS1_Pin SPI1CD_Pin BT_BUTTON_Pin */ |
| 27 | mjames | 735 | GPIO_InitStruct.Pin = SPI_NSS1_Pin | SPI1CD_Pin | BT_BUTTON_Pin; |
| 16 | mjames | 736 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
| 737 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
| 27 | mjames | 738 | HAL_GPIO_Init (GPIOA, &GPIO_InitStruct); |
| 2 | mjames | 739 | |
| 24 | mjames | 740 | /*Configure GPIO pins : SPI_RESET_Pin SPI_NSS2_Pin POWER_LATCH_Pin USB_PWR_Pin */ |
| 27 | mjames | 741 | GPIO_InitStruct.Pin = SPI_RESET_Pin | SPI_NSS2_Pin | POWER_LATCH_Pin |
| 742 | | USB_PWR_Pin; |
||
| 16 | mjames | 743 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
| 744 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
| 27 | mjames | 745 | HAL_GPIO_Init (GPIOC, &GPIO_InitStruct); |
| 2 | mjames | 746 | |
| 16 | mjames | 747 | /*Configure GPIO pins : SW1_PUSH_Pin SW1_I_Pin SW1_Q_Pin SW2_PUSH_Pin */ |
| 27 | mjames | 748 | GPIO_InitStruct.Pin = SW1_PUSH_Pin | SW1_I_Pin | SW1_Q_Pin | SW2_PUSH_Pin; |
| 16 | mjames | 749 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
| 750 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
||
| 27 | mjames | 751 | HAL_GPIO_Init (GPIOB, &GPIO_InitStruct); |
| 5 | mjames | 752 | |
| 16 | mjames | 753 | /*Configure GPIO pins : SW2_I_Pin SW2_Q_Pin */ |
| 27 | mjames | 754 | GPIO_InitStruct.Pin = SW2_I_Pin | SW2_Q_Pin; |
| 16 | mjames | 755 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
| 756 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
||
| 27 | mjames | 757 | HAL_GPIO_Init (GPIOC, &GPIO_InitStruct); |
| 5 | mjames | 758 | |
| 24 | mjames | 759 | /*Configure GPIO pin : IGNITION_Pin */ |
| 760 | GPIO_InitStruct.Pin = IGNITION_Pin; |
||
| 761 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
||
| 762 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
| 27 | mjames | 763 | HAL_GPIO_Init (IGNITION_GPIO_Port, &GPIO_InitStruct); |
| 24 | mjames | 764 | |
| 2 | mjames | 765 | } |
| 766 | |||
| 767 | /* USER CODE BEGIN 4 */ |
||
| 768 | |||
| 769 | /* USER CODE END 4 */ |
||
| 770 | |||
| 5 | mjames | 771 | /** |
| 27 | mjames | 772 | * @brief This function is executed in case of error occurrence. |
| 773 | * @param None |
||
| 774 | * @retval None |
||
| 775 | */ |
||
| 776 | void |
||
| 777 | Error_Handler (void) |
||
| 5 | mjames | 778 | { |
| 16 | mjames | 779 | /* USER CODE BEGIN Error_Handler */ |
| 27 | mjames | 780 | /* User can add his own implementation to report the HAL error return state */ |
| 781 | while (1) |
||
| 782 | { |
||
| 783 | } |
||
| 784 | /* USER CODE END Error_Handler */ |
||
| 5 | mjames | 785 | } |
| 786 | |||
| 2 | mjames | 787 | #ifdef USE_FULL_ASSERT |
| 788 | |||
| 789 | /** |
||
| 27 | mjames | 790 | * @brief Reports the name of the source file and the source line number |
| 791 | * where the assert_param error has occurred. |
||
| 792 | * @param file: pointer to the source file name |
||
| 793 | * @param line: assert_param error line source number |
||
| 794 | * @retval None |
||
| 795 | */ |
||
| 2 | mjames | 796 | void assert_failed(uint8_t* file, uint32_t line) |
| 27 | mjames | 797 | { |
| 798 | /* USER CODE BEGIN 6 */ |
||
| 799 | /* User can add his own implementation to report the file name and line number, |
||
| 800 | ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ |
||
| 801 | /* USER CODE END 6 */ |
||
| 2 | mjames | 802 | |
| 27 | mjames | 803 | } |
| 2 | mjames | 804 | |
| 805 | #endif |
||
| 806 | |||
| 807 | /** |
||
| 27 | mjames | 808 | * @} |
| 809 | */ |
||
| 2 | mjames | 810 | |
| 811 | /** |
||
| 27 | mjames | 812 | * @} |
| 813 | */ |
||
| 2 | mjames | 814 | |
| 815 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |