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