comparison BootLoader/Src/tComm_mini.c @ 1048:493a5903ec20 GasConsumption

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