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