comparison BootLoader/Src/tComm_mini.c @ 985:aeafa631147d BootloaderOstc5

Exchange shared files with Bootloader specific mini version
author Ideenmodellierer
date Sun, 30 Mar 2025 21:32:37 +0200 (5 weeks ago)
parents
children 39f28cd9dc46
comparison
equal deleted inserted replaced
984:41136649b90d 985:aeafa631147d
1 ///////////////////////////////////////////////////////////////////////////////
2 /// -*- coding: UTF-8 -*-
3 ///
4 /// \file Discovery/Src/tComm.c
5 /// \brief Main file for communication reduced to needs of the bootloader
6 /// \author heinrichs weikamp gmbh
7 /// \date 29-Mar-2025
8 ///
9 /// \details
10 ///
11 /// $Id$
12 ///////////////////////////////////////////////////////////////////////////////
13 /// \par Copyright (c) 2014-2018 Heinrichs Weikamp gmbh
14 ///
15 /// This program is free software: you can redistribute it and/or modify
16 /// it under the terms of the GNU General Public License as published by
17 /// the Free Software Foundation, either version 3 of the License, or
18 /// (at your option) any later version.
19 ///
20 /// This program is distributed in the hope that it will be useful,
21 /// but WITHOUT ANY WARRANTY; without even the implied warranty of
22 /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 /// GNU General Public License for more details.
24 ///
25 /// You should have received a copy of the GNU General Public License
26 /// along with this program. If not, see <http://www.gnu.org/licenses/>.
27 //////////////////////////////////////////////////////////////////////////////
28
29 /**
30 ==============================================================================
31 ##### How to use #####
32 ==============================================================================
33 ==============================================================================
34 ##### History #####
35 ==============================================================================
36 160211 added 4 bytes Serial in update Files after checksum prior to binary
37 160211 0x6B changed to version only
38 160623 fixed 0x72 (in V1.0.9)
39 160623 fixed rebuild menu (before update) for V1.0.10
40
41 ==============================================================================
42 ##### CTS / RTS #####
43 ==============================================================================
44 RTS is Output, CTS is Input
45
46 BlueMod Pin D7 UART-RTS# is Output
47 connected to STM32F429 PA11 CTS (Input)
48 also STM32 PA12 RTS is connected to BlueMod UART-CTS# F3
49
50 see BlueMod_SR_HWreference_r06.pdf, page 156
51 and MAIN_CPU STM32F4 Reference manual DM00031020.pdf, page 990
52
53
54 ==============================================================================
55 ##### Codes #####
56 ==============================================================================
57 [0x73] upload CPU2 firmware in SDRAM and update CPU2
58
59 [0x74] upload MainCPU firmware in EEPROM and start bootloader
60
61 */
62
63 /* Includes ------------------------------------------------------------------*/
64
65 #include "tComm.h"
66
67 #include "externCPU2bootloader.h"
68 #include "externLogbookFlash.h"
69 #include "gfx_colors.h"
70 #include "gfx_engine.h"
71 #include "gfx_fonts.h"
72 #include "ostc.h"
73
74 #include "base_bootloader.h"
75 #include "firmwareEraseProgram.h"
76 #include "text_multilanguage.h"
77
78 #ifdef SPECIALPROGRAMM
79 # include "firmwareEraseProgram.h"
80 #endif
81 #include <stdlib.h>
82 #include <string.h>
83
84
85 /* Private variables ---------------------------------------------------------*/
86 GFX_DrawCfgScreen tCscreen;
87 GFX_DrawCfgWindow tCwindow;
88
89 uint8_t receiveStartByteUart = 0;
90 uint8_t bluetoothActiveLastTime = 0;
91
92 uint8_t StartListeningToUART = 0;
93 char display_text[256] = { 0 };
94
95 uint8_t setForcedBluetoothName = 0;
96
97 uint8_t updateSettingsAndMenuOnExit = 0;
98
99 /* Private types -------------------------------------------------------------*/
100 #define BYTE_DOWNLOAD_MODE (0xBB)
101 #define BYTE_SERVICE_MODE (0xAA)
102
103 #define UART_OPERATION_TIMEOUT (500u) /* Timeout for common read / write operations (ms) */
104 #define UART_TIMEOUT_SECONDS (120u) /* Timeout for keeping connection open and waiting for data */
105 #define UART_TIMEOUT_LARGE_BLOCK (6000u) /* Timeout (ms) for reception of an 16K data block (typical RX time ~4,5seconds) */
106
107 #define UART_CMD_BUF_SIZE (30u) /* size of buffer for command exchange */
108
109 const uint8_t id_Region1_firmware = 0xFF;
110 const uint8_t id_RTE = 0xFE;
111 const uint8_t id_FONT = 0x10;
112 const uint8_t id_FONT_OLD = 0x00;
113
114 static BlueModTmpConfig_t BmTmpConfig = BM_CONFIG_OFF; /* Config BlueMod without storing the changes */
115 static uint8_t EvaluateBluetoothSignalStrength = 0;
116
117 /* Private function prototypes -----------------------------------------------*/
118 static void tComm_Error_Handler(void);
119 static uint8_t select_mode(uint8_t aRxByte);
120 static uint8_t tComm_CheckAnswerOK(void);
121 static uint8_t tComm_HandleBlueModConfig(void);
122 static void tComm_EvaluateBluetoothStrength(void);
123 uint8_t receive_update_flex(uint8_t isRTEupdateALLOWED);
124 uint8_t receive_update_data_flex(uint8_t* pBuffer1, uint8_t* pBuffer2, uint8_t RTEupdateALLOWED);
125 uint8_t receive_update_data_mainCPU_firmware(void);
126 uint8_t receive_update_data_mainCPU_variable_firmware(void);
127 uint8_t receive_update_data_mainCPU_firmware_subroutine(uint8_t region, uint8_t* pBuffer1, uint8_t* pBuffer2);
128 HAL_StatusTypeDef receive_uart_large_size(UART_HandleTypeDef *huart, uint8_t *pData, uint32_t Size);
129 static uint8_t openComm(uint8_t aRxByte);
130 uint8_t HW_Set_Bluetooth_Name(uint16_t serial, uint8_t withEscapeSequence);
131 uint8_t prompt4D4C(uint8_t mode);
132 uint8_t tComm_GetBTCmdStr(BTCmd cmdId, char* pCmdStr);
133
134
135 static uint8_t receive_update_data_cpu2(void);
136 uint8_t receive_update_data_cpu2_sub(uint8_t* pBuffer);
137
138 /* Exported functions --------------------------------------------------------*/
139
140 void tComm_init(void)
141 {
142 tCscreen.FBStartAdress = 0;
143 tCscreen.ImageHeight = 480;
144 tCscreen.ImageWidth = 800;
145 tCscreen.LayerIndex = 1;
146
147 tCwindow.Image = &tCscreen;
148 tCwindow.WindowNumberOfTextLines = 6;
149 tCwindow.WindowLineSpacing = 65;
150 tCwindow.WindowTab = 400;
151 tCwindow.WindowX0 = 20;
152 tCwindow.WindowX1 = 779;
153
154 tCwindow.WindowY0 = 0;
155 tCwindow.WindowY1 = 479;
156
157 StartListeningToUART = 1;
158 }
159
160 uint8_t tComm_control(void)
161 {
162 uint8_t answer = 0;
163
164 if(BmTmpConfig != BM_CONFIG_DONE)
165 {
166 tComm_HandleBlueModConfig();
167 }
168 else
169 {
170 /*##-2- Put UART peripheral in reception process ###########################*/
171
172 if((UartReady == RESET) && StartListeningToUART)
173 {
174 StartListeningToUART = 0;
175 if(HAL_UART_Receive_IT(&UartHandle, &receiveStartByteUart, 1) != HAL_OK)
176 tComm_Error_Handler();
177 }
178 /* Reset transmission flag */
179 if(UartReady == SET)
180 {
181 UartReady = RESET;
182 if((receiveStartByteUart == BYTE_DOWNLOAD_MODE) || (receiveStartByteUart == BYTE_SERVICE_MODE))
183 answer = openComm(receiveStartByteUart);
184 StartListeningToUART = 1;
185 return answer;
186 }
187 }
188 return 0;
189 }
190
191
192 void tComm_refresh(void)
193 {
194 char localString[255];
195
196 if(tCscreen.FBStartAdress == 0)
197 {
198 GFX_hwBackgroundOn();
199 tCscreen.FBStartAdress = getFrame(18);
200 write_content_simple(&tCscreen, 0, 800, 480-24, &FontT24,"Exit",CLUT_ButtonSurfaceScreen);
201 write_content_simple(&tCscreen, 800 - 70, 800, 480-24, &FontT24,"Signal",CLUT_ButtonSurfaceScreen);
202
203 if(receiveStartByteUart == BYTE_SERVICE_MODE)
204 GFX_write_string(&FontT48, &tCwindow, "Service mode enabled",2);
205 else
206 GFX_write_string(&FontT48, &tCwindow, "Download mode enabled",2);
207 GFX_SetFramesTopBottom(tCscreen.FBStartAdress, 0,480);
208 display_text[0] = 0;
209 display_text[255] = 0;
210 }
211 else if(display_text[255])
212 {
213 display_text[(uint8_t)display_text[255]] = 0;
214 localString[0] = TXT_MINIMAL;
215 strcpy (&localString[1],display_text);
216 releaseFrame(18,tCscreen.FBStartAdress);
217 tCscreen.FBStartAdress = getFrame(18);
218 write_content_simple(&tCscreen, 0, 800, 480-24, &FontT24,"Exit",CLUT_ButtonSurfaceScreen);
219 write_content_simple(&tCscreen, 800 - 70, 800, 480-24, &FontT24,"Signal",CLUT_ButtonSurfaceScreen);
220 GFX_write_string(&FontT48, &tCwindow, localString,2);
221 GFX_SetFrameTop(tCscreen.FBStartAdress);
222 display_text[0] = 0;
223 display_text[255] = 0;
224 }
225 }
226
227
228 void tComm_verlauf(uint8_t percentage_complete)
229 {
230 uint32_t pDestination;
231
232 pDestination = (uint32_t)tCscreen.FBStartAdress;
233 pDestination += 150 * tCscreen.ImageHeight * 2;
234 pDestination += 100 * 2;
235
236 if(percentage_complete > 100)
237 percentage_complete = 100;
238
239 int i = 1;
240 while(i<=percentage_complete)
241 {
242 i += 1;
243 for(int y=0;y<4;y++)
244 {
245 for(int x=0;x<40;x++)
246 {
247 *(__IO uint16_t*)pDestination = 0xFF00 + 00;
248 pDestination += 2;
249 }
250 pDestination += (tCscreen.ImageHeight - 40 )* 2;
251 }
252 pDestination += tCscreen.ImageHeight * 2; // one spare line
253 }
254 }
255
256
257 void tComm_exit(void)
258 {
259 SStateList status;
260 get_globalStateList(&status);
261
262 releaseFrame(18,tCscreen.FBStartAdress);
263 tCscreen.FBStartAdress = 0;
264 GFX_hwBackgroundOff();
265
266 if(setForcedBluetoothName)
267 {
268 setForcedBluetoothName = 0;
269 MX_Bluetooth_PowerOff();
270 HAL_Delay(1000);
271 MX_Bluetooth_PowerOn();
272 tComm_Set_Bluetooth_Name(1);
273 tComm_StartBlueModConfig();
274 }
275
276 updateSettingsAndMenuOnExit = 0;
277
278 if(status.base == BaseComm)
279 {
280 set_globalState_Base();
281 }
282 MX_Bluetooth_PowerOff(); // Power down Bluetooth on the way out
283 }
284
285
286 uint8_t tComm_Set_Bluetooth_Name(uint8_t force)
287 {
288 uint8_t answer = 0;
289
290 if(hardwareDataGetPointer()->secondarySerial != 0xFFFF)
291 {
292 if(force || (hardwareDataGetPointer()->secondary_bluetooth_name_set == 0xFF))
293 answer = HW_Set_Bluetooth_Name(hardwareDataGetPointer()->secondarySerial, 0);
294 if(answer == HAL_OK)
295 hardware_programmSecondaryBluetoothNameSet();
296 }
297 else
298 if(hardwareDataGetPointer()->primarySerial != 0xFFFF)
299 {
300 if(force || (hardwareDataGetPointer()->production_bluetooth_name_set == 0xFF))
301 answer = HW_Set_Bluetooth_Name(hardwareDataGetPointer()->primarySerial, 0);
302
303 if(answer == HAL_OK)
304 hardware_programmPrimaryBluetoothNameSet();
305
306 }
307 else /* no serial set at all => do default configuration of the module */
308 {
309 #define NINAB22103B00
310 #ifdef NINAB22103B00
311 answer = 0xFF;
312 #endif
313 }
314 return answer;
315 }
316
317
318 uint8_t HW_Set_Bluetooth_Name(uint16_t serial, uint8_t withEscapeSequence)
319 {
320 uint8_t answer = HAL_OK;
321 uint8_t aRxBuffer[50];
322 char aTxBufferName[50];
323
324 // char aTxFactoryDefaults[50] = "AT&F1\r";
325
326 char aTxBufferEscapeSequence[50] = "+++"; /* factory default */
327 // limit is 19 chars, with 7 chars shown in BLE advertising mode
328 //________________________123456789012345678901
329
330 tComm_GetBTCmdStr(BT_CMD_NAME, aTxBufferName);
331
332 char answerOkay[6] = "\r\nOK\r\n";
333
334 gfx_number_to_string(5,1,&aTxBufferName[15],serial);
335
336 // store active configuration in non-volatile memory
337 char aTxBufferWrite[50] = "AT&W\r";
338
339 // char aTxBufferReset[50] = "AT+RESET\r";
340
341
342 HAL_Delay(1010);
343 if(withEscapeSequence)
344 {
345 aRxBuffer[0] = 0;
346 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBufferEscapeSequence, 3, 2000)!= HAL_OK)
347 answer = HAL_ERROR;
348 HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, 3, 2000);
349 HAL_Delay(1010);
350
351 for(int i=0;i<3;i++)
352 if(aRxBuffer[i] != '+')
353 answer = HAL_ERROR;
354 }
355
356 aRxBuffer[0] = 0;
357 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBufferName, 21, 2000)!= HAL_OK)
358 answer = HAL_ERROR;
359 HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, 21+6, 2000);
360
361 for(int i=0;i<21;i++)
362 if(aRxBuffer[i] != aTxBufferName[i])
363 answer = HAL_ERROR;
364
365 for(int i=0;i<6;i++)
366 if(aRxBuffer[21+i] != answerOkay[i])
367 answer = HAL_ERROR;
368
369 HAL_Delay(200);
370
371 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBufferWrite, 5, 2000)!= HAL_OK)
372 answer = HAL_ERROR;
373 HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, 5+6, 2000);
374
375 for(int i=0;i<5;i++)
376 if(aRxBuffer[i] != aTxBufferWrite[i])
377 answer = HAL_ERROR;
378
379 for(int i=0;i<6;i++)
380 if(aRxBuffer[5+i] != answerOkay[i])
381 answer = HAL_ERROR;
382
383 answer = HAL_OK;
384 return answer;
385 }
386
387 uint8_t openComm(uint8_t aRxByte)
388 {
389 SStateList status;
390 uint8_t localRx;
391 uint8_t timeoutCounter = 0;
392 uint8_t answer = 0;
393 uint8_t service_mode_last_three_bytes[3];
394 uint8_t service_mode_response[5] =
395 {
396 0x4B,
397 0xAB,
398 0xCD,
399 0xEF,
400 0x4C
401 };
402 uint8_t download_mode_response[2] =
403 {
404 0xBB,
405 0x4D
406 };
407
408 if((aRxByte != BYTE_DOWNLOAD_MODE) && (aRxByte != BYTE_SERVICE_MODE))
409 return 0;
410
411 set_globalState(StUART_STANDARD);
412
413 /* service mode is four bytes
414 0xAA 0xAB 0xCD 0xEF
415 answer is
416 */
417 localRx = aRxByte;
418
419 if(aRxByte == BYTE_SERVICE_MODE)
420 {
421 if((HAL_UART_Receive(&UartHandle, (uint8_t*)service_mode_last_three_bytes, 3, 2000)!= HAL_OK))
422 answer = 0x00;
423 else
424 {
425 if((service_mode_last_three_bytes[0] != 0xAB) || (service_mode_last_three_bytes[1] != 0xCD) || (service_mode_last_three_bytes[2] != 0xEF))
426 answer = 0x00;
427 else
428 {
429 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)service_mode_response, 5, 2000)!= HAL_OK)
430 answer = 0x00;
431 else
432 answer = prompt4D4C(receiveStartByteUart);
433 }
434 }
435 }
436 else //if(aRxByte == BYTE_SERVICE_MODE)
437 {
438 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)download_mode_response, 2, 2000)!= HAL_OK)
439 answer = 0x00;
440 else
441 answer = prompt4D4C(receiveStartByteUart);
442 }
443
444 while((answer == prompt4D4C(receiveStartByteUart)) && (timeoutCounter < UART_TIMEOUT_SECONDS)) /* try receive once a second */
445 {
446 if(HAL_UART_Receive(&UartHandle, (uint8_t*)&localRx, 1, UART_OPERATION_TIMEOUT)!= HAL_OK)
447 {
448 timeoutCounter++;
449 get_globalStateList(&status);
450 if (status.base != BaseComm)
451 {
452 timeoutCounter = UART_TIMEOUT_SECONDS; /* Abort action triggered outside main loop => exit */
453 }
454 if(EvaluateBluetoothSignalStrength)
455 {
456 tComm_EvaluateBluetoothStrength();
457 }
458 }
459 else
460 {
461 answer = select_mode(localRx);
462 timeoutCounter = 0;
463 }
464 }
465 set_returnFromComm();
466 return 1;
467 }
468
469
470 uint8_t prompt4D4C(uint8_t mode)
471 {
472 if(mode == BYTE_SERVICE_MODE)
473 return 0x4C;
474 else
475 return 0x4D;
476 }
477
478
479 uint8_t select_mode(uint8_t type)
480 {
481 uint8_t dummyForBootloader[256] = {0};
482 uint8_t count;
483 uint8_t aTxBuffer[128];
484 uint8_t aRxBuffer[68];
485 uint8_t answer;
486 aTxBuffer[0] = type;
487 aTxBuffer[1] = prompt4D4C(receiveStartByteUart);
488 uint8_t tempHigh, tempLow;
489 count = 0;
490
491 // service mode only commands
492 if(receiveStartByteUart == BYTE_SERVICE_MODE)
493 {
494 // first part
495 switch(type)
496 {
497 // start communication (again)
498 case 0xAA:
499 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, 2, 1000)!= HAL_OK)
500 return 0;
501 else
502 return prompt4D4C(receiveStartByteUart);
503 default:
504 break;
505 }
506
507
508 // Exit communication on Text like RING, CONNECT, ... or 0xFF command
509 if((type < 0x60) || (type == 0xFF))
510 return 0;
511
512 // return of command for (almost) all commands
513 switch(type)
514 {
515 // not supported yet case 0x20: // send hi:lo:temp1 bytes starting from ext_flash_address:3
516 // not supported yet case 0x22: // Resets all logbook pointers and the logbook (!)
517 // not supported yet case 0x23: // Resets battery gauge registers
518 // not supported yet case 0x30: // write bytes starting from ext_flash_address:3 (Stop when timeout)
519 // not supported yet case 0x40: // erases 4kB block from ext_flash_address:3 (Warning: No confirmation or built-in security here...)
520 // not supported yet case 0x42: // erases range in 4kB steps (Get 3 bytes address and 1byte amount of 4kB blocks)
521 // not supported yet case 0x50: // sends firmware from external flash from 0x3E0000 to 0x3FD000 (118784bytes) via comm
522 case 0xFE: // hw unit_tests
523 case 0x71: // hw read manufacturing data
524 case 0x73: // hw update FLEX
525 case 0x79: // hw read device data
526 case 0x74: // hw update Firmware
527 case 0x75: // hw update RTE
528 case 0x76: // hw update Fonts
529 case 0x80: // hw write manufacturing data
530 case 0x81: // hw write second serial
531 case 0x82: // hw set bluetooth name
532 case 0xC1: // Start low-level bootloader
533 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, 1, UART_OPERATION_TIMEOUT)!= HAL_OK)
534 return 0;
535 break;
536 default:
537 break;
538 }
539
540 // now send content or update firmware
541 switch(type)
542 {
543 case 0xFE:
544 // work to do :-) 12. Oct. 2015
545 // 256 bytes output
546 memset(aTxBuffer,0,128);
547 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, 128,5000)!= HAL_OK)
548 return 0;
549 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, 128,5000)!= HAL_OK)
550 return 0;
551 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart);
552 break;
553
554 case 0x71:
555 memcpy(aTxBuffer,hardwareDataGetPointer(),64);
556 count += 64;
557 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart);
558 break;
559
560 case 0x73:
561 answer = receive_update_flex(0);
562 if(answer == 0)
563 return 0;
564 else if(answer == 2) // 2 = RTE without bootToBootloader
565 {
566 aTxBuffer[0] = 0xFF;
567 HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, 1,10000);
568 return 0;
569 }
570 else
571 {
572 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart);
573 if(answer == 1) /* 0xFF is checksum error, 2 = RTE without bootToBootloader */
574 {
575 extern uint8_t bootToBootloader;
576 bootToBootloader = 1;
577 }
578 }
579 break;
580
581 case 0x79:
582 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, 1,10000)!= HAL_OK)
583 return 0;
584 ext_flash_read_fixed_16_devicedata_blocks_formated_128byte_total(aTxBuffer);
585 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, 128,5000)!= HAL_OK)
586 return 0;
587 aTxBuffer[0] = prompt4D4C(receiveStartByteUart);
588 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, 1,10000)!= HAL_OK)
589 return 0;
590 else
591 return prompt4D4C(receiveStartByteUart);
592
593 case 0x82:
594 setForcedBluetoothName = 1;
595 return 0;
596
597 case 0x74:
598 answer = receive_update_data_mainCPU_firmware();
599 if(answer != 0)
600 {
601 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart);
602 if(answer == 1) // 0xFF is checksum error
603 {
604 extern uint8_t bootToBootloader;
605 bootToBootloader = 1;
606 }
607 }
608 else
609 return 0;
610 break;
611
612 case 0x75:
613 receive_update_data_cpu2();
614 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart);
615 break;
616
617 case 0x76:
618 answer = receive_update_data_mainCPU_variable_firmware();
619 if(answer != 0)
620 {
621 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart);
622 if(answer == 1) // 0xFF is checksum error
623 {
624 extern uint8_t bootToBootloader;
625 bootToBootloader = 1;
626 }
627 }
628 else
629 return 0;
630 break;
631
632 case 0x80:
633 if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, 52, 5000)!= HAL_OK)
634 return 0;
635 if(hardware_programmProductionData(aRxBuffer) == HAL_OK)
636 {
637 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart);
638 }
639 else
640 return 0;
641 break;
642
643 case 0x81:
644 if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, 12, 1000)!= HAL_OK)
645 return 0;
646 if(hardware_programmSecondarySerial(aRxBuffer) == HAL_OK)
647 {
648 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart);
649 }
650 else
651 return 0;
652 break;
653
654 }
655
656 // was service command? Yes, finish and exit
657 if(count)
658 {
659 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, count,10000)!= HAL_OK)
660 return 0;
661 else
662 return prompt4D4C(receiveStartByteUart);
663 }
664 }
665
666
667 // download mode commands
668 switch(type)
669 {
670 // return of command for almost all commands
671 case 0x60: // get model + features
672 case 0x61: // get all headers full (256 bytes)
673 case 0x62: // set clock
674 case 0x63: // set custom text
675 case 0x66: // get dive profile
676 case 0x69: // get serial, old version numbering, custom text
677 case 0x6A: // get model
678 case 0x6B: // get specific firmware version
679 case 0x6C: /* Display Bluetooth signal strength */
680 case 0x6D: // get all compact headers (16 byte)
681 case 0x6E: // display text
682 case 0x70: // read min, default, max setting
683 case 0x72: // read setting
684 case 0x77: // write setting
685 case 0x78: // reset all settings
686 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, 1, 1000)!= HAL_OK)
687 return 0;
688 break;
689
690 // start communication (again)
691 case 0xBB:
692 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, 2, 1000)!= HAL_OK)
693 return 0;
694 else
695 return prompt4D4C(receiveStartByteUart);
696
697 // stop communication
698 case 0xFF:
699 HAL_UART_Transmit(&UartHandle, (uint8_t*)&aTxBuffer, 1, 1000);
700 return 0;
701
702 default:
703 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart);
704 break;
705 }
706
707 switch(type)
708 {
709 case 0x62:
710 if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, 6, 2000)!= HAL_OK)
711 return 0;
712 break;
713 case 0x63:
714 if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, 60, 5000)!= HAL_OK)
715 return 0;
716 break;
717 case 0x66:
718 if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, 1, 1000)!= HAL_OK)
719 return 0;
720 break;
721 case 0x6B:
722 if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, 1, 1000)!= HAL_OK)
723 return 0;
724 break;
725 case 0x6E:
726 if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, 16, 5000)!= HAL_OK)
727 return 0;
728 break;
729 case 0x77:
730 if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, 5, 5000)!= HAL_OK)
731 return 0;
732 break;
733 case 0x72:
734 if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, 1, 5000)!= HAL_OK)
735 return 0;
736 break;
737 case 0x70:
738 if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, 1, 5000)!= HAL_OK)
739 return 0;
740 break;
741 }
742
743 switch(type)
744 {
745 /* common to standard and bootloader */
746
747 // get model + features
748 case 0x60:
749 aTxBuffer[count++] = 0x00; // hardware descriptor HIGH byte
750 aTxBuffer[count++] = 0x3B; // hardware descriptor LOW byte // 0x3B is OSTC4 // 0x1A is OTSC3
751 aTxBuffer[count++] = 0x00; // feature descriptor HIGH byte
752 aTxBuffer[count++] = 0x00; // feature descriptor LOW byte
753 aTxBuffer[count++] = 0x43; // model id
754 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart);
755 break;
756
757 // get model
758 case 0x6A:
759 aTxBuffer[count++] = 0x3B; // 0x3B is OSTC4 // 0x1A is OTSC3
760 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart);
761 break;
762
763 // get all firmware version and status (OSTC4 only)
764 case 0x6B:
765 switch(*aRxBuffer)
766 {
767 case 0xFF:
768 // firmware
769 aTxBuffer[count++] = firmwareDataGetPointer()->versionFirst;
770 aTxBuffer[count++] = firmwareDataGetPointer()->versionSecond;
771 aTxBuffer[count++] = firmwareDataGetPointer()->versionThird;
772 aTxBuffer[count++] = firmwareDataGetPointer()->versionBeta;
773 break;
774 case 0xFE:
775 // RTE
776 getActualRTEandFONTversion(&tempHigh, &tempLow, 0, 0); // RTE
777 aTxBuffer[count++] = tempHigh;
778 aTxBuffer[count++] = tempLow;
779 aTxBuffer[count++] = 0;
780 aTxBuffer[count++] = 0;
781 break;
782 case 0x10:
783 getActualRTEandFONTversion( 0, 0, &tempHigh, &tempLow); // font
784 aTxBuffer[count++] = tempHigh;
785 aTxBuffer[count++] = tempLow;
786 aTxBuffer[count++] = 0;
787 aTxBuffer[count++] = 0;
788 break;
789 default:
790 // not supported
791 aTxBuffer[count++] = 0xFF;
792 aTxBuffer[count++] = 0xFF;
793 aTxBuffer[count++] = 0xFF;
794 aTxBuffer[count++] = 0xFF;
795 break;
796 /* Jef Driesen Test
797 default:
798 // not supported
799 aTxBuffer[count++] = 0x1;
800 aTxBuffer[count++] = 0x1;
801 aTxBuffer[count++] = 0x1;
802 aTxBuffer[count++] = 0x1;
803 break;
804 */
805 }
806 /*
807 // serial
808 aTxBuffer[count++] = pSettings->serialLow;
809 aTxBuffer[count++] = pSettings->serialHigh;
810 // batch code (date)
811 hardwareBatchCode(&tempHigh, &tempLow);
812 aTxBuffer[count++] = tempLow;
813 aTxBuffer[count++] = tempHigh;
814 // status and status detail (future feature)
815 aTxBuffer[count++] = 0;
816 aTxBuffer[count++] = 0;
817 aTxBuffer[count++] = 0;
818 aTxBuffer[count++] = 0;
819 */
820 // prompt
821 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart);
822 break;
823
824 /* Trigger Bluetooth signal strength evaluation */
825 case 0x6C: tComm_EvaluateBluetoothStrength();
826 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart);
827 break;
828 // display text
829 case 0x6E:
830 for(int i=0;i<16;i++)
831 display_text[i] = aRxBuffer[i];
832 display_text[15] = 0;
833 display_text[255] = 16;
834 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart);
835 break;
836
837 // version / identify
838 case 0x69:
839 aTxBuffer[count++] = 0;//pSettings->serialLow;
840 aTxBuffer[count++] = 0;//pSettings->serialHigh;
841 aTxBuffer[count++] = 0;//firmwareVersion_16bit_low();
842 aTxBuffer[count++] = 0;//firmwareVersion_16bit_high();
843 memset(&aTxBuffer[count], 0, 60);
844 count += 60;
845 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart);
846 break;
847
848 /* bootloader dummies */
849 // full headers (256 byte)
850 case 0x61:
851 for(int StepBackwards = 0;StepBackwards<256;StepBackwards++)
852 {
853 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)dummyForBootloader, 256,5000)!= HAL_OK)
854 return 0;
855 }
856 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart);
857 break;
858 // compact headers (16 byte)
859 case 0x6D:
860 for(int StepBackwards = 0;StepBackwards<256;StepBackwards++)
861 {
862 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)dummyForBootloader, 16,5000)!= HAL_OK)
863 return 0;
864 }
865 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart);
866 break;
867 // set clock & date
868 case 0x62:
869 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart);
870 break;
871 // set custom text
872 case 0x63:
873 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart);
874 break;
875 // get dive profile
876 case 0x66:
877 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)dummyForBootloader, 256,5000)!= HAL_OK)
878 return 0;
879 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart);
880 break;
881 // read min,default,max setting
882 // read settings
883
884
885 case 0x72:
886 memcpy(&aTxBuffer[count], dummyForBootloader, 4);
887 count += 4;
888 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart);
889 break;
890 // write settings
891 case 0x77:
892 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart);
893 break;
894 }
895
896 if(count)
897 {
898 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, count,10000)!= HAL_OK)
899 return 0;
900 else
901 return prompt4D4C(receiveStartByteUart);
902 }
903 return 0;
904 }
905
906 #define BLOCKSIZE 0x1000
907
908 HAL_StatusTypeDef receive_uart_large_size(UART_HandleTypeDef *huart, uint8_t *pData, uint32_t Size)
909 {
910 uint16_t length_4k_blocks;
911 uint16_t length_4k_remainder;
912 uint32_t temp;
913 HAL_StatusTypeDef result = HAL_OK;
914 uint32_t pDataLocal;
915
916 length_4k_blocks = (uint16_t) (Size / BLOCKSIZE);
917 temp = length_4k_blocks;
918 temp *= BLOCKSIZE;
919 length_4k_remainder = (uint16_t) ( Size - temp);
920
921 pDataLocal = (uint32_t)pData;
922
923
924 while((result == HAL_OK) && length_4k_blocks)
925 {
926 result = HAL_UART_Receive(&UartHandle, (uint8_t *)pDataLocal, BLOCKSIZE , UART_TIMEOUT_LARGE_BLOCK);
927 pDataLocal += BLOCKSIZE;
928 length_4k_blocks--;
929 }
930
931 if((result == HAL_OK) && length_4k_remainder)
932 {
933 result = HAL_UART_Receive(&UartHandle, (uint8_t *)pDataLocal, length_4k_remainder , UART_TIMEOUT_LARGE_BLOCK);
934 }
935 return result;
936 }
937
938
939 /* for safety reason (memory blocking this code is main and sub */
940
941 uint8_t receive_update_data_cpu2(void)
942 {
943 uint8_t answer;
944
945 uint8_t* pBuffer = (uint8_t*)getFrame(20);
946 answer = receive_update_data_cpu2_sub(pBuffer);
947 releaseFrame(20,(uint32_t)pBuffer);
948 return answer;
949 }
950
951
952 uint8_t receive_update_data_cpu2_sub(uint8_t* pBuffer)
953 {
954 uint8_t sBuffer[10];
955 uint32_t length, offsetTotal, checksum, checksumCalc;
956 uint8_t id;
957 const uint8_t id_RTE = 0xFE;
958
959 //Get length
960 if(HAL_UART_Receive(&UartHandle, pBuffer, 4,5000)!= HAL_OK) // 58000
961 {
962 return 0;
963 }
964 length = 256 * 256 * 256 * (uint32_t)pBuffer[0] + 256 * 256 * (uint32_t)pBuffer[1] + 256 * (uint32_t)pBuffer[2] + pBuffer[3];
965
966 //Get id
967 if(HAL_UART_Receive(&UartHandle, pBuffer, 4,5000)!= HAL_OK) // 58000
968 {
969 return 0;
970 }
971 id = pBuffer[0];
972 offsetTotal = 256 * 256 * 256 * (uint32_t)pBuffer[0] + 256 * 256 * (uint32_t)pBuffer[1] + 256 * (uint32_t)pBuffer[2] + pBuffer[3];
973
974 // get checksum, bytes are in different order on Dev C++ code!!!
975 if(HAL_UART_Receive(&UartHandle, sBuffer, 4,5000)!= HAL_OK) // 58000
976 {
977 return 0;
978 }
979 checksum = 256 * 256 * 256 * (uint32_t)sBuffer[3] + 256 * 256 * (uint32_t)sBuffer[2] + 256 * (uint32_t)sBuffer[1] + sBuffer[0];
980 checksumCalc = length + offsetTotal;
981
982 // no need to get code if checksum == length is wrong
983 if(checksumCalc != checksum)
984 {
985 return 0;
986 }
987
988 //get Code
989 if(receive_uart_large_size(&UartHandle, pBuffer, length)!= HAL_OK)
990 {
991 return 0;
992 }
993
994 //get Checksum
995 if(HAL_UART_Receive(&UartHandle, sBuffer, 4,5000)!= HAL_OK) // 580000
996 {
997 return 0;
998 }
999 checksum = 256 * 256 * 256 *(uint32_t)sBuffer[0] + 256 * 256 * (uint32_t)sBuffer[1] + 256 * (uint32_t)sBuffer[2] + sBuffer[3];
1000 // uint32_t checksumCalc = crc32c_checksum(pBuffer, length,0,0);
1001 checksumCalc = CRC_CalcBlockCRC((uint32_t*)pBuffer, length/4);
1002
1003 if(checksum != checksumCalc)
1004 {
1005 return 0;
1006 }
1007
1008 if(id != id_RTE)
1009 {
1010 strcpy(display_text,"wrong data.");
1011 display_text[255] = 32;
1012 return 0;
1013 }
1014
1015 strcpy(display_text," RTE update.");
1016 display_text[255] = 32;
1017
1018 return extCPU2bootloader(pBuffer,length,display_text);
1019 }
1020
1021 uint8_t receive_update_flex(uint8_t isRTEupdateALLOWED)
1022 {
1023 uint8_t answer;
1024
1025 uint8_t* pBuffer1 = (uint8_t*)getFrame(20);
1026 uint8_t* pBuffer2 = (uint8_t*)getFrame(20);
1027
1028 answer = receive_update_data_flex(pBuffer1, pBuffer2, isRTEupdateALLOWED);
1029
1030 releaseFrame(20,(uint32_t)pBuffer1);
1031 releaseFrame(20,(uint32_t)pBuffer2);
1032
1033 return answer;
1034 }
1035
1036 uint8_t receive_update_data_mainCPU_firmware(void)
1037 {
1038 uint8_t answer;
1039
1040 uint8_t* pBuffer1 = (uint8_t*)getFrame(20);
1041
1042 answer = receive_update_data_mainCPU_firmware_subroutine(1, pBuffer1, 0);
1043
1044 releaseFrame(20,(uint32_t)pBuffer1);
1045
1046 return answer;
1047 }
1048
1049 /* multi buffer (long data) not tested yet */
1050 uint8_t receive_update_data_mainCPU_variable_firmware(void)
1051 {
1052 uint8_t answer;
1053
1054 uint8_t* pBuffer1 = (uint8_t*)getFrame(20);
1055 uint8_t* pBuffer2 = (uint8_t*)getFrame(20);
1056
1057 answer = receive_update_data_mainCPU_firmware_subroutine(2, pBuffer1, pBuffer2);
1058
1059 releaseFrame(20,(uint32_t)pBuffer1);
1060 releaseFrame(20,(uint32_t)pBuffer2);
1061
1062 return answer;
1063 }
1064
1065 uint8_t receive_update_data_flex(uint8_t* pBuffer1, uint8_t* pBuffer2, uint8_t RTEupdateALLOWED)
1066 {
1067 uint8_t sBuffer[10];
1068 uint8_t serialBuffer[10];
1069 uint32_t length1, length2, lengthCompare, offsetCompare, ByteCompareStatus;
1070 uint32_t lengthTotal, offsetTotal;
1071 uint32_t checksum, checksumCalc = 0;
1072 uint8_t id;
1073 const uint8_t id_Region1_firmware = 0xFF;
1074 const uint8_t id_RTE = 0xFE;
1075 uint8_t textpointer = 0;
1076
1077 //Get length
1078 if(HAL_UART_Receive(&UartHandle, sBuffer, 4,5000)!= HAL_OK) // 58000
1079 {
1080 return 0;
1081 }
1082 lengthTotal = 256 * 256 * 256 * (uint32_t)sBuffer[0] + 256 * 256 * (uint32_t)sBuffer[1] + 256 * (uint32_t)sBuffer[2] + sBuffer[3];
1083
1084 //Get offset and/or id (id is 0xFF for RTE, 0xFE for firmware and offset if var)
1085 if(HAL_UART_Receive(&UartHandle, sBuffer, 4,5000)!= HAL_OK) // 58000
1086 {
1087 return 0;
1088 }
1089 id = sBuffer[0];
1090
1091 checksumCalc = 256 * 256 * 256 * (uint32_t)sBuffer[0] + 256 * 256 * (uint32_t)sBuffer[1] + 256 * (uint32_t)sBuffer[2] + sBuffer[3];
1092 checksumCalc += lengthTotal;
1093 //old, does no longer work because of the fonts: checksumCalc = lengthTotal + offsetTotal;
1094
1095 if((id != id_Region1_firmware) && (id != id_RTE) && (id != id_FONT) && (id != id_FONT_OLD))
1096 {
1097 return 0;
1098 }
1099
1100 // neu 110212
1101 if(id == id_FONT)
1102 offsetTotal = 256 * 256 * 256 * (uint32_t)sBuffer[1] + 256 * 256 * (uint32_t)sBuffer[2] + 256 * (uint32_t)sBuffer[3];
1103 else
1104 offsetTotal = 256 * 256 * 256 * (uint32_t)sBuffer[0] + 256 * 256 * (uint32_t)sBuffer[1] + 256 * (uint32_t)sBuffer[2] + sBuffer[3];
1105
1106 // get checksum, bytes are in different order on Dev C++ code!!!
1107 if(HAL_UART_Receive(&UartHandle, sBuffer, 4,5000)!= HAL_OK) // 58000
1108 {
1109 return 0;
1110 }
1111 checksum = 256 * 256 * 256 * (uint32_t)sBuffer[3] + 256 * 256 * (uint32_t)sBuffer[2] + 256 * (uint32_t)sBuffer[1] + sBuffer[0];
1112
1113
1114 if(checksumCalc != checksum)
1115 {
1116 uint8_t ptr = 0;
1117 strcpy(&display_text[ptr]," checksum error");
1118 ptr += 15;
1119 strcpy(&display_text[ptr],"\n\r");
1120 ptr += 2;
1121 ptr += gfx_number_to_string(10,0,&display_text[ptr],checksumCalc);
1122 display_text[ptr] = 0;
1123 display_text[255] = ptr + 1;
1124 return 0xFF;
1125 }
1126
1127 //Get serial (new since 160211)
1128 if(HAL_UART_Receive(&UartHandle, serialBuffer, 4,5000)!= HAL_OK)
1129 {
1130 return 0;
1131 }
1132
1133 if(lengthTotal > 768000)
1134 {
1135 length1 = 768000;
1136 length2 = lengthTotal - length1;
1137 }
1138 else
1139 {
1140 length1 = lengthTotal;
1141 length2 = 0;
1142 }
1143
1144 if((pBuffer2 == 0) && (length2 != 0))
1145 return 0;
1146
1147 //get Code
1148 if(receive_uart_large_size(&UartHandle, pBuffer1, length1)!= HAL_OK)
1149 return 0;
1150
1151 if(length2)
1152 if(receive_uart_large_size(&UartHandle, pBuffer2, length2)!= HAL_OK)
1153 return 0;
1154
1155 //get Checksum
1156 if(HAL_UART_Receive(&UartHandle, sBuffer, 4,5000)!= HAL_OK) // 58000
1157 return 0;
1158
1159 checksum = 256 * 256 * 256 *(uint32_t)sBuffer[0] + 256 * 256 * (uint32_t)sBuffer[1] + 256 * (uint32_t)sBuffer[2] + sBuffer[3];
1160 // uint32_t checksumCalc = crc32c_checksum(pBuffer1, length1, pBuffer2, length2);
1161 if(length2)
1162 checksumCalc = CRC_CalcBlockCRC_moreThan768000((uint32_t*)pBuffer1, (uint32_t*)pBuffer2, lengthTotal/4);
1163 else
1164 checksumCalc = CRC_CalcBlockCRC((uint32_t*)pBuffer1, length1/4);
1165
1166 /* check id now */
1167 /*
1168 if(region == 2)
1169 {
1170 if((id == id_Region1_firmware) || (id == id_RTE))
1171 {
1172 strcpy(display_text,"wrong data.");
1173 display_text[255] = 32;
1174 return 0;
1175 }
1176 }
1177 else
1178 {
1179 if(id != id_Region1_firmware)
1180 {
1181 strcpy(display_text,"wrong data.");
1182 display_text[255] = 32;
1183 return 0;
1184 }
1185 }
1186 */
1187 /* test checksum */
1188 if(checksum != checksumCalc)
1189 {
1190 uint8_t ptr = 0;
1191 strcpy(&display_text[ptr]," checksum error");
1192 ptr += 15;
1193 strcpy(&display_text[ptr],"\n\r");
1194 display_text[ptr] = 0;
1195 display_text[255] = ptr + 1;
1196 return 0xFF;
1197 }
1198
1199 if(id == id_Region1_firmware)
1200 {
1201 uint8_t ptr = 0;
1202 display_text[ptr++] = 'V';
1203 ptr += gfx_number_to_string(2,0,&display_text[ptr],pBuffer1[0x10000] & 0x1F);
1204 display_text[ptr++] = '.';
1205 ptr += gfx_number_to_string(2,0,&display_text[ptr],pBuffer1[0x10001] & 0x1F);
1206 display_text[ptr++] = '.';
1207 ptr += gfx_number_to_string(2,0,&display_text[ptr],pBuffer1[0x10002] & 0x1F);
1208 display_text[ptr++] = ' ';
1209 if(pBuffer1[0x10003])
1210 {
1211 strcpy(&display_text[ptr],"beta ");
1212 ptr +=5;
1213 }
1214 strcpy(&display_text[ptr],"\n\rpreparing for install.");
1215 ptr += 25;
1216 display_text[255] = ptr + 1;
1217 }
1218 else if(id == id_RTE)
1219 {
1220 if(RTEupdateALLOWED)
1221 {
1222 strcpy(display_text," RTE update.\n\r");
1223 textpointer = 0;
1224 while((display_text[textpointer] != 0) && (textpointer < 50))
1225 textpointer++;
1226 display_text[255] = textpointer+1;
1227 return extCPU2bootloader(pBuffer1,length1,display_text);
1228 }
1229 else
1230 return 0xFF;
1231 }
1232 else
1233 //if(region == 2)
1234 {
1235 uint8_t ptr = 0;
1236 ptr += gfx_number_to_string(7,0,&display_text[ptr],lengthTotal);
1237 strcpy(&display_text[ptr]," bytes with ");
1238 ptr += 12;
1239 ptr += gfx_number_to_string(7,0,&display_text[ptr],offsetTotal);
1240 strcpy(&display_text[ptr]," offset");
1241 ptr += 7;
1242 strcpy(&display_text[ptr],"\n\rpreparing for install.");
1243 ptr += 25;
1244 display_text[255] = ptr + 1;
1245 }
1246
1247
1248 // only non RTE !!
1249 uint8_t* pBufferCompare = (uint8_t*)getFrame(20);
1250 ByteCompareStatus = 0;
1251
1252 if(id == id_Region1_firmware)
1253 {
1254 /* standard firmware limited to 768000 */
1255 if(ext_flash_read_firmware(pBufferCompare,4,0) != 0xFFFFFFFF)
1256 ext_flash_erase_firmware();
1257 ext_flash_write_firmware(pBuffer1, length1);
1258 lengthCompare = ext_flash_read_firmware(pBufferCompare,768000,0);
1259
1260 if(lengthCompare != length1)
1261 ByteCompareStatus = 10000;
1262 for(int i = 0; i < length1; i++)
1263 {
1264 if(pBuffer1[0] != pBufferCompare[0])
1265 ByteCompareStatus++;
1266 }
1267 }
1268 else
1269 //if(region == 2)
1270 {
1271 /* upper region firmware can be larger (1MB) */
1272 if(ext_flash_read_firmware2(0, pBufferCompare,4, 0,0) != 0xFFFFFFFF)
1273 ext_flash_erase_firmware2();
1274 ext_flash_write_firmware2(offsetTotal, pBuffer1, length1, pBuffer2, length2);
1275 lengthCompare = ext_flash_read_firmware2(&offsetCompare, pBufferCompare,768000, 0,768000);
1276
1277 if(lengthCompare != length1 + length2)
1278 ByteCompareStatus = 10000;
1279 if(offsetTotal != offsetCompare)
1280 ByteCompareStatus += 20000;
1281 for(int i = 0; i < length1; i++)
1282 {
1283 if(pBuffer1[0] != pBufferCompare[0])
1284 ByteCompareStatus++;
1285 }
1286
1287 lengthCompare = ext_flash_read_firmware2(0, 0,768000, pBufferCompare,768000);
1288 for(int i = 0; i < length2; i++)
1289 {
1290 if(pBuffer2[0] != pBufferCompare[0])
1291 ByteCompareStatus++;
1292 }
1293 }
1294
1295 releaseFrame(20,(uint32_t)pBufferCompare);
1296
1297 if(ByteCompareStatus != 0)
1298 {
1299 strcpy(&display_text[0],"\n\rcopy error.");
1300 display_text[255] = 21;
1301 return 0;
1302 }
1303 else
1304 {
1305 strcpy(&display_text[0],"\n\rready to install.");
1306 display_text[255] = 21;
1307 return 1;
1308 }
1309 }
1310
1311
1312 uint8_t receive_update_data_mainCPU_firmware_subroutine(uint8_t region, uint8_t* pBuffer1, uint8_t* pBuffer2)
1313 {
1314 uint8_t sBuffer[10];
1315 uint32_t length1, length2, lengthCompare, offsetCompare, ByteCompareStatus;
1316 uint32_t lengthTotal, offsetTotal, checksum, checksumCalc = 0;
1317 uint8_t id;
1318
1319 //Get length
1320 if(HAL_UART_Receive(&UartHandle, sBuffer, 4,5000)!= HAL_OK) // 58000
1321 return 0;
1322
1323 lengthTotal = 256 * 256 * 256 * (uint32_t)sBuffer[0] + 256 * 256 * (uint32_t)sBuffer[1] + 256 * (uint32_t)sBuffer[2] + sBuffer[3];
1324
1325 //Get offset and/or id (id is 0xFF for RTE, 0xFE for firmware and offset if var)
1326 if(HAL_UART_Receive(&UartHandle, sBuffer, 4,5000)!= HAL_OK) // 58000
1327 return 0;
1328
1329 id = sBuffer[0];
1330
1331 checksumCalc = 256 * 256 * 256 * (uint32_t)sBuffer[0] + 256 * 256 * (uint32_t)sBuffer[1] + 256 * (uint32_t)sBuffer[2] + sBuffer[3];
1332 checksumCalc += lengthTotal;
1333
1334 if((id != id_Region1_firmware) && (id != id_RTE) && (id != id_FONT) && (id != id_FONT_OLD))
1335 return 0;
1336
1337 if(id == id_FONT)
1338 offsetTotal = 256 * 256 * 256 * (uint32_t)sBuffer[1] + 256 * 256 * (uint32_t)sBuffer[2] + 256 * (uint32_t)sBuffer[3];
1339 // alt, prior to id for font
1340 else
1341 offsetTotal = 256 * 256 * 256 * (uint32_t)sBuffer[0] + 256 * 256 * (uint32_t)sBuffer[1] + 256 * (uint32_t)sBuffer[2] + sBuffer[3];
1342
1343 // get checksum, bytes are in different order on Dev C++ code!!!
1344 if(HAL_UART_Receive(&UartHandle, sBuffer, 4,5000)!= HAL_OK) // 58000
1345 return 0;
1346
1347 checksum = 256 * 256 * 256 * (uint32_t)sBuffer[3] + 256 * 256 * (uint32_t)sBuffer[2] + 256 * (uint32_t)sBuffer[1] + sBuffer[0];
1348
1349 //old: checksumCalc = lengthTotal + offsetTotal;
1350
1351 if(checksumCalc != checksum)
1352 {
1353 uint8_t ptr = 0;
1354 strcpy(&display_text[ptr]," checksum error");
1355 ptr += 15;
1356 strcpy(&display_text[ptr],"\n\r");
1357 ptr += 2;
1358 ptr += gfx_number_to_string(10,0,&display_text[ptr],checksumCalc);
1359 display_text[ptr] = 0;
1360 display_text[255] = ptr + 1;
1361 return 0xFF;
1362 }
1363
1364 if(lengthTotal > 768000)
1365 {
1366 length1 = 768000;
1367 length2 = lengthTotal - length1;
1368 }
1369 else
1370 {
1371 length1 = lengthTotal;
1372 length2 = 0;
1373 }
1374
1375 if((pBuffer2 == 0) && (length2 != 0))
1376 return 0;
1377
1378 //get Code
1379 if(receive_uart_large_size(&UartHandle, pBuffer1, length1)!= HAL_OK)
1380 return 0;
1381
1382 if(length2)
1383 if(receive_uart_large_size(&UartHandle, pBuffer2, length2)!= HAL_OK)
1384 return 0;
1385
1386 //get Checksum
1387 if(HAL_UART_Receive(&UartHandle, sBuffer, 4,5000)!= HAL_OK) // 58000
1388 return 0;
1389
1390 checksum = 256 * 256 * 256 *(uint32_t)sBuffer[0] + 256 * 256 * (uint32_t)sBuffer[1] + 256 * (uint32_t)sBuffer[2] + sBuffer[3];
1391 // uint32_t checksumCalc = crc32c_checksum(pBuffer1, length1, pBuffer2, length2);
1392 if(length2)
1393 checksumCalc = CRC_CalcBlockCRC_moreThan768000((uint32_t*)pBuffer1, (uint32_t*)pBuffer2, lengthTotal/4);
1394 else
1395 checksumCalc = CRC_CalcBlockCRC((uint32_t*)pBuffer1, length1/4);
1396
1397 /* check id now */
1398 if(region == 2)
1399 {
1400 if((id == id_Region1_firmware) || (id == id_RTE))
1401 {
1402 strcpy(display_text,"wrong data.");
1403 display_text[255] = 32;
1404 return 0;
1405 }
1406 }
1407 else
1408 {
1409 if(id != id_Region1_firmware)
1410 {
1411 strcpy(display_text,"wrong data.");
1412 display_text[255] = 32;
1413 return 0;
1414 }
1415 }
1416
1417 /* test checksum */
1418 if(checksum != checksumCalc)
1419 {
1420 uint8_t ptr = 0;
1421 strcpy(&display_text[ptr]," pruefsummen error");
1422 ptr += 15;
1423 strcpy(&display_text[ptr],"\n\r");
1424 display_text[ptr] = 0;
1425 display_text[255] = ptr + 1;
1426 return 0xFF;
1427 }
1428
1429 if(region == 2)
1430 {
1431 uint8_t ptr = 0;
1432 ptr += gfx_number_to_string(7,0,&display_text[ptr],lengthTotal);
1433 strcpy(&display_text[ptr]," bytes with ");
1434 ptr += 12;
1435 ptr += gfx_number_to_string(7,0,&display_text[ptr],offsetTotal);
1436 strcpy(&display_text[ptr]," offset");
1437 ptr += 7;
1438 strcpy(&display_text[ptr],"\n\rpreparing for install.");
1439 ptr += 25;
1440 display_text[255] = ptr + 1;
1441
1442 }
1443 else
1444 {
1445 uint8_t ptr = 0;
1446 display_text[ptr++] = 'V';
1447 ptr += gfx_number_to_string(2,0,&display_text[ptr],pBuffer1[0x10000] & 0x1F);
1448 display_text[ptr++] = '.';
1449 ptr += gfx_number_to_string(2,0,&display_text[ptr],pBuffer1[0x10001] & 0x1F);
1450 display_text[ptr++] = '.';
1451 ptr += gfx_number_to_string(2,0,&display_text[ptr],pBuffer1[0x10002] & 0x1F);
1452 display_text[ptr++] = ' ';
1453 if(pBuffer1[0x10003])
1454 {
1455 strcpy(&display_text[ptr],"beta ");
1456 ptr +=5;
1457 }
1458 strcpy(&display_text[ptr],"\n\rpreparing for install.");
1459 ptr += 25;
1460 display_text[255] = ptr + 1;
1461 }
1462
1463 uint8_t* pBufferCompare = (uint8_t*)getFrame(20);
1464 ByteCompareStatus = 0;
1465
1466 if(region == 2)
1467 {
1468 /* upper region firmware can be larger (1MB) */
1469 if(ext_flash_read_firmware2(0, pBufferCompare,4, 0,0) != 0xFFFFFFFF)
1470 ext_flash_erase_firmware2();
1471 ext_flash_write_firmware2(offsetTotal, pBuffer1, length1, pBuffer2, length2);
1472 lengthCompare = ext_flash_read_firmware2(&offsetCompare, pBufferCompare,768000, 0,768000);
1473
1474 if(lengthCompare != length1 + length2)
1475 ByteCompareStatus = 10000;
1476 if(offsetTotal != offsetCompare)
1477 ByteCompareStatus += 20000;
1478 for(int i = 0; i < length1; i++)
1479 {
1480 if(pBuffer1[0] != pBufferCompare[0])
1481 ByteCompareStatus++;
1482 }
1483
1484 lengthCompare = ext_flash_read_firmware2(0, 0,768000, pBufferCompare,768000);
1485 for(int i = 0; i < length2; i++)
1486 {
1487 if(pBuffer2[0] != pBufferCompare[0])
1488 ByteCompareStatus++;
1489 }
1490 }
1491 else
1492 {
1493 /* standard firmware limited to 768000 */
1494 if(ext_flash_read_firmware(pBufferCompare,4,0) != 0xFFFFFFFF)
1495 ext_flash_erase_firmware();
1496 ext_flash_write_firmware(pBuffer1, length1);
1497 lengthCompare = ext_flash_read_firmware(pBufferCompare,768000,0);
1498
1499 if(lengthCompare != length1)
1500 ByteCompareStatus = 10000;
1501 for(int i = 0; i < length1; i++)
1502 {
1503 if(pBuffer1[0] != pBufferCompare[0])
1504 ByteCompareStatus++;
1505 }
1506 }
1507
1508 releaseFrame(20,(uint32_t)pBufferCompare);
1509
1510 if(ByteCompareStatus != 0)
1511 {
1512 strcpy(&display_text[0],"\n\rcopy error.");
1513 display_text[255] = 21;
1514 return 0;
1515 }
1516 else
1517 {
1518 strcpy(&display_text[0],"\n\rready to install.");
1519 display_text[255] = 21;
1520 return 1;
1521 }
1522 }
1523
1524 void tComm_RequestBluetoothStrength(void)
1525 {
1526 EvaluateBluetoothSignalStrength = 1;
1527 }
1528
1529 /* read, validate the modul answer and flush rx que if necessary */
1530 uint8_t tComm_CheckAnswerOK()
1531 {
1532 char answerOkay[] = "\r\nOK\r\n";
1533 char aRxBuffer[UART_CMD_BUF_SIZE];
1534 uint8_t sizeAnswer = sizeof(answerOkay) -1;
1535 uint8_t result = HAL_OK;
1536 uint8_t indexRef = 0;
1537 uint8_t indexBuf = 0;
1538 uint8_t answer;
1539
1540 memset(aRxBuffer,0,UART_CMD_BUF_SIZE);
1541 if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, sizeAnswer, UART_OPERATION_TIMEOUT) == HAL_OK)
1542 {
1543 do
1544 {
1545 if(answerOkay[indexRef] == aRxBuffer[indexBuf])
1546 {
1547 indexRef++;
1548 }
1549 else
1550 {
1551 if(indexRef != 0)
1552 {
1553 indexRef = 0;
1554 }
1555 }
1556 indexBuf++;
1557 }while(indexBuf < sizeAnswer);
1558
1559 if(indexRef != sizeAnswer) /* unexpected answer => there might be characters left in RX que => read and check all rx bytes */
1560 {
1561 do
1562 {
1563 answer = HAL_ERROR;
1564 if (indexBuf < UART_CMD_BUF_SIZE)
1565 {
1566 answer = HAL_UART_Receive(&UartHandle, (uint8_t*)&aRxBuffer[indexBuf], 1, 10);
1567
1568 if(answerOkay[indexRef] == aRxBuffer[indexBuf])
1569 {
1570 indexRef++;
1571 }
1572 else
1573 {
1574 if(indexRef != 0)
1575 {
1576 indexRef = 0;
1577 }
1578 }
1579 indexBuf++;
1580 }
1581 }while((answer == HAL_OK) && (indexRef != sizeAnswer));
1582 if(indexRef != sizeAnswer)
1583 {
1584 result = HAL_ERROR;
1585 }
1586 }
1587 }
1588 else
1589 {
1590 result = HAL_ERROR;
1591 }
1592 return result;
1593
1594 }
1595
1596
1597 void tComm_EvaluateBluetoothStrength(void)
1598 {
1599 char aTxBufferBarSSI[] = "AT+BARSSI\r";
1600 char aTxBufferEscapeSequence[] = "+++";
1601 char aTxBufferEnd[] = "ATO\r";
1602 uint8_t sizeRequest = sizeof(aTxBufferBarSSI) -1;
1603
1604 uint8_t answer = HAL_OK;
1605 char aRxBuffer[UART_CMD_BUF_SIZE];
1606 char SignalStr[UART_CMD_BUF_SIZE];
1607 uint8_t index = 0;
1608 uint8_t strindex = 0;
1609 int8_t sigqual = 0;
1610
1611 HAL_Delay(200);
1612 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBufferEscapeSequence, 3, 2000)== HAL_OK)
1613 {
1614 if(tComm_CheckAnswerOK() == HAL_OK)
1615 {
1616 HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBufferBarSSI,sizeRequest , 2000);
1617 {
1618 index = 0;
1619 do /* Answer is not the common one. Instead the signal strength is received => read all available bytes one by one*/
1620 {
1621 answer = HAL_UART_Receive(&UartHandle, (uint8_t*)&aRxBuffer[index], 1, 100);
1622 if(index < UART_CMD_BUF_SIZE)
1623 {
1624 index++;
1625 }
1626 }while(answer == HAL_OK);
1627
1628 if((aRxBuffer[index] != 'E') && (aRxBuffer[index] != 0)) /* E represents the first letter of the string ERROR */
1629 {
1630 index = 0;
1631 strindex = 0;
1632 do
1633 {
1634 SignalStr[strindex++] = aRxBuffer[index++];
1635 }while ((index < UART_CMD_BUF_SIZE - 1) && (aRxBuffer[index] != '\r'));
1636 SignalStr[strindex] = 0; /* terminate String */
1637 sigqual = strtol(SignalStr,NULL,0);
1638 /* Map db to abstract Bargraph */
1639 if(sigqual > 0)
1640 {
1641 sprintf(SignalStr,"Bluetooth ||||||||");
1642 }
1643 else
1644 {
1645 sprintf(SignalStr,"Bluetooth |");
1646 strindex = strlen(SignalStr);
1647 sigqual *=-1;
1648 sigqual = 100 - sigqual; /* invert because of negative db value */
1649 while(sigqual / 10 > 0 )
1650 {
1651 SignalStr[strindex++] = '|';
1652 sigqual -= 10;
1653 }
1654 SignalStr[strindex] = 0;
1655 }
1656 strcpy(display_text,SignalStr);
1657 display_text[255] = strlen(SignalStr);
1658 EvaluateBluetoothSignalStrength = 0;
1659 }
1660 }
1661 HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBufferEnd, 4, 2000); /* exit terminal mode */
1662 index = 0;
1663 do /* module will answer with current connection state */
1664 {
1665 answer = HAL_UART_Receive(&UartHandle, (uint8_t*)&aRxBuffer[index], 1, 100);
1666 if(index < UART_CMD_BUF_SIZE)
1667 {
1668 index++;
1669 }
1670 }while(answer == HAL_OK);
1671 }
1672 }
1673 }
1674
1675
1676 void tComm_StartBlueModBaseInit()
1677 {
1678 BmTmpConfig = BM_INIT_TRIGGER_ON;
1679 }
1680
1681
1682 uint8_t tComm_GetBTCmdStr(BTCmd cmdId, char* pCmdStr)
1683 {
1684 uint8_t ret = 0;
1685
1686 switch (cmdId)
1687 {
1688 case BT_CMD_ECHO: sprintf(pCmdStr,"ATE0\r");
1689 ret = 1;
1690 break;
1691 case BT_CMD_BAUDRATE_115: strcpy(pCmdStr,"AT+UMRS=115200,1,8,1,1,1\r");
1692 ret = 1;
1693 break;
1694
1695 case BT_CMD_BAUDRATE_460: strcpy(pCmdStr,"AT+UMRS=460800,1,8,1,1,1\r");
1696 ret = 1;
1697 break;
1698 case BT_CMD_NAME: strcpy(pCmdStr,"AT+UBTLN=OSTC5-12345\r");
1699 ret = 1;
1700 break;
1701 case BT_CMD_EXIT_CMD: strcpy(pCmdStr,"ATO1\r");
1702 ret = 1;
1703 break;
1704 default:
1705 break;
1706 }
1707 return ret;
1708 }
1709
1710 void tComm_StartBlueModConfig()
1711 {
1712 uint8_t answer = HAL_OK;
1713 uint8_t RxBuffer[UART_CMD_BUF_SIZE];
1714 uint8_t index = 0;
1715
1716 BmTmpConfig = BM_CONFIG_ECHO;
1717 do /* flush RX buffer */
1718 {
1719 answer = HAL_UART_Receive(&UartHandle, (uint8_t*)&RxBuffer[index], 1, 10);
1720 if(index < UART_CMD_BUF_SIZE) index++;
1721 }while(answer == HAL_OK);
1722 }
1723
1724 uint8_t tComm_HandleBlueModConfig()
1725 {
1726 static uint8_t RestartModule = 1; /* used to do power off / on cycle */
1727 static uint8_t ConfigRetryCnt = 0; /* Retry count without power cycle */
1728
1729 char TxBuffer[UART_CMD_BUF_SIZE];
1730 uint8_t CmdSize = 0;
1731
1732 uint8_t result = HAL_OK;
1733
1734 memset(TxBuffer, 0, sizeof(TxBuffer));
1735
1736 switch (BmTmpConfig)
1737 {
1738 case BM_CONFIG_ECHO: tComm_GetBTCmdStr (BT_CMD_ECHO, TxBuffer);
1739 break;
1740 case BM_CONFIG_SILENCE: tComm_GetBTCmdStr (BT_CMD_SILENCE, TxBuffer);
1741 break;
1742 case BM_CONFIG_ESCAPE_DELAY: tComm_GetBTCmdStr (BT_CMD_ESCAPE_DELAY, TxBuffer);
1743 break;
1744 case BM_CONFIG_SIGNAL_POLL: tComm_GetBTCmdStr(BT_CMD_SIGNAL_POLL, TxBuffer);
1745 break;
1746 case BM_CONFIG_BAUD:
1747 #ifdef ENABLE_FAST_COMM
1748 tComm_GetBTCmdStr(BT_CMD_BAUDRATE_460, TxBuffer);
1749 #else
1750 BmTmpConfig = BM_CONFIG_DONE;
1751 #endif
1752 break;
1753 case BM_CONFIG_RETRY: ConfigRetryCnt--;
1754 HAL_Delay(1);
1755 if(ConfigRetryCnt == 0)
1756 {
1757 MX_Bluetooth_PowerOn();
1758 tComm_StartBlueModConfig();
1759 }
1760 break;
1761 case BM_CONFIG_DONE:
1762 case BM_CONFIG_OFF:
1763 ConfigRetryCnt = 0;
1764 RestartModule = 1;
1765 break;
1766 /* the procedure below is just needed for the initial bluetooth module initialization */
1767 case BM_INIT_TRIGGER_ON: HAL_Delay(2000);
1768 HAL_GPIO_WritePin(BLE_UBLOX_DSR_GPIO_PORT,BLE_UBLOX_DSR_PIN,GPIO_PIN_RESET);
1769 BmTmpConfig++;
1770 break;
1771 case BM_INIT_TRIGGER_OFF: HAL_Delay(1);
1772 HAL_GPIO_WritePin(BLE_UBLOX_DSR_GPIO_PORT,BLE_UBLOX_DSR_PIN,GPIO_PIN_SET);
1773 HAL_Delay(2000);
1774 BmTmpConfig++;
1775 break;
1776 case BM_INIT_ECHO: sprintf(TxBuffer,"ATE0\r");
1777 break;
1778 case BM_INIT_FACTORY: sprintf(TxBuffer,"AT+UFACTORY\r"); /*Set to factory defined configuration */
1779 break;
1780 case BM_INIT_MODE: sprintf(TxBuffer,"AT+UMSM=1\r"); /* start in Data mode */
1781 break;
1782 case BM_INIT_BLE: sprintf(TxBuffer,"AT+UBTLE=2\r"); /* Bluetooth low energy Peripheral */
1783 break;
1784 case BM_INIT_NAME: sprintf(TxBuffer,"AT+UBTLN=OSTC5-12345\r"); /* Bluetooth name */
1785 if(hardwareDataGetPointer()->primarySerial != 0xFFFF) /* module reinit? => restore old name */
1786 {
1787 gfx_number_to_string(5,1,&TxBuffer[15],hardwareDataGetPointer()->primarySerial);
1788 }
1789 break;
1790 case BM_INIT_SSP_IDO_OFF: sprintf(TxBuffer,"AT+UDSC=0,0\r"); /* Disable SPP Server on ID0 */
1791 break;
1792 case BM_INIT_SSP_IDO_ON: sprintf(TxBuffer,"AT+UDSC=0,3\r"); /* SPP Server on ID0 */
1793 break;
1794 case BM_INIT_SSP_ID1_OFF: sprintf(TxBuffer,"AT+UDSC=1,0\r"); /* Disable SPS Server on ID1 */
1795 break;
1796 case BM_INIT_SSP_ID1_ON: sprintf(TxBuffer,"AT+UDSC=1,6\r"); /* SPS Server on ID1 */
1797 break;
1798 case BM_INIT_STORE: sprintf(TxBuffer,"AT&W\r"); /* write settings into eeprom */
1799 break;
1800 case BM_INIT_RESTART: sprintf(TxBuffer,"AT+CPWROFF\r"); /* reboot module */
1801 break;
1802 case BM_INIT_DONE: BmTmpConfig = BM_CONFIG_ECHO;
1803 break;
1804 default:
1805 break;
1806 }
1807 if(TxBuffer[0] != 0) /* forward command to module */
1808 {
1809 CmdSize = strlen(TxBuffer);
1810 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)TxBuffer,CmdSize, 2000) == HAL_OK)
1811 {
1812 result = tComm_CheckAnswerOK();
1813
1814 if((BmTmpConfig == BM_CONFIG_BAUD) && (result == HAL_OK) && (UartHandle.Init.BaudRate != 460800)) /* is com already switched to fast speed? */
1815 {
1816 HAL_UART_DeInit(&UartHandle);
1817 HAL_Delay(1);
1818 UartHandle.Init.BaudRate = 460800;
1819 HAL_UART_Init(&UartHandle);
1820 }
1821 else if((BmTmpConfig == BM_CONFIG_BAUD) && (result == HAL_OK) && (UartHandle.Init.BaudRate == 460800)) /* This shut not happen because default speed is 115200 => update module configuration */
1822 {
1823 tComm_GetBTCmdStr(BT_CMD_BAUDRATE_115, TxBuffer);
1824
1825 CmdSize = strlen(TxBuffer);
1826 HAL_UART_Transmit(&UartHandle, (uint8_t*)TxBuffer,CmdSize, 2000);
1827 HAL_UART_DeInit(&UartHandle);
1828 HAL_Delay(10);
1829 UartHandle.Init.BaudRate = 115200;
1830 HAL_UART_Init(&UartHandle);
1831 sprintf(TxBuffer,"AT&W\r"); /* write configuration */
1832 CmdSize = strlen(TxBuffer);
1833 HAL_UART_Transmit(&UartHandle, (uint8_t*)TxBuffer,CmdSize, 2000);
1834 }
1835 if(result == HAL_OK)
1836 {
1837 BmTmpConfig++;
1838 if(BmTmpConfig == BM_CONFIG_RETRY)
1839 {
1840 BmTmpConfig = BM_CONFIG_DONE;
1841 }
1842 }
1843 if(BmTmpConfig == BM_CONFIG_ECHO)
1844 {
1845 BmTmpConfig = BM_CONFIG_DONE;
1846 ConfigRetryCnt = 0;
1847 RestartModule = 1;
1848 }
1849 }
1850 }
1851 else /* no command for the configuration step found => skip step */
1852 {
1853 if((BmTmpConfig > BM_CONFIG_OFF) && (BmTmpConfig < BM_CONFIG_DONE))
1854 {
1855 BmTmpConfig++;
1856 }
1857 }
1858 if(result != HAL_OK)
1859 {
1860 ConfigRetryCnt++;
1861 if(ConfigRetryCnt > 3) /* Configuration failed => switch off module */
1862 {
1863 MX_Bluetooth_PowerOff();
1864 if(RestartModule)
1865 {
1866 RestartModule = 0; /* only one try */
1867 ConfigRetryCnt = 200; /* used for delay to startup module again */
1868
1869 if((BmTmpConfig == BM_CONFIG_ECHO) || (BmTmpConfig == BM_INIT_ECHO)) /* the module did not answer even once => try again with alternative baud rate */
1870 {
1871 HAL_UART_DeInit(&UartHandle);
1872 HAL_Delay(1);
1873 UartHandle.Init.BaudRate = 460800;
1874 HAL_UART_Init(&UartHandle);
1875 }
1876 BmTmpConfig = BM_CONFIG_RETRY;
1877 }
1878 else /* even restarting module failed => switch bluetooth off */
1879 {
1880 ConfigRetryCnt = 0;
1881 BmTmpConfig = BM_CONFIG_OFF;
1882 }
1883 }
1884 }
1885 return result;
1886 }
1887
1888 static void tComm_Error_Handler(void)
1889 {
1890 while(1)
1891 {}
1892 }