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