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