Mercurial > public > ostc4
annotate Discovery/Src/tComm.c @ 399:523892f62ce0 ImproveBluetooth
Revert change to keep connection open in case of BlueMod messages:
Some problems occure within Subsurface in case the connection is kept open => Reverted
author | ideenmodellierer |
---|---|
date | Sat, 21 Dec 2019 20:21:26 +0100 |
parents | 2d980f765034 |
children | 7bcbbaa42cbf |
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 | |
593 // service mode only commands | |
594 if(receiveStartByteUart == BYTE_SERVICE_MODE) | |
595 { | |
596 // first part | |
597 switch(type) | |
598 { | |
599 // start communication (again) | |
600 case 0xAA: | |
601 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, 2, 1000)!= HAL_OK) | |
602 return 0; | |
603 else | |
604 return prompt4D4C(receiveStartByteUart); | |
605 | |
606 /* | |
607 // update firmware main preparation | |
608 case 0x74: | |
609 ext_flash_erase_firmware_if_not_empty(); | |
610 break; | |
611 | |
612 // update firmware main with variable full access memory location preparation | |
613 case 0x76: | |
614 ext_flash_erase_firmware2_if_not_empty(); | |
615 break; | |
616 */ | |
617 default: | |
618 break; | |
619 } | |
620 | |
621 #ifndef BOOTLOADER_STANDALONE | |
622 uint32_t logCopyDataPtr = 0; | |
623 convert_Type logCopyDataLength; | |
624 uint32_t logCopyDataPtrTemp = 0; | |
625 uint32_t logCopyDataLengthTemp = 0; | |
626 uint8_t logDummyByte = 0; | |
627 uint8_t logStepBackwards = 0; | |
628 convert16_Type totalDiveCount; | |
629 logCopyDataLength.u32bit = 0; | |
630 totalDiveCount.u16bit = 0; | |
631 #endif | |
632 | |
399
523892f62ce0
Revert change to keep connection open in case of BlueMod messages:
ideenmodellierer
parents:
398
diff
changeset
|
633 // Exit communication on Text like RING, CONNECT, ... or 0xFF command |
523892f62ce0
Revert change to keep connection open in case of BlueMod messages:
ideenmodellierer
parents:
398
diff
changeset
|
634 if((type < 0x60) || (type == 0xFF)) |
523892f62ce0
Revert change to keep connection open in case of BlueMod messages:
ideenmodellierer
parents:
398
diff
changeset
|
635 return 0; |
523892f62ce0
Revert change to keep connection open in case of BlueMod messages:
ideenmodellierer
parents:
398
diff
changeset
|
636 |
38 | 637 // return of command for (almost) all commands |
638 switch(type) | |
639 { | |
640 // not supported yet case 0x20: // send hi:lo:temp1 bytes starting from ext_flash_address:3 | |
641 // not supported yet case 0x22: // Resets all logbook pointers and the logbook (!) | |
642 // not supported yet case 0x23: // Resets battery gauge registers | |
643 // not supported yet case 0x30: // write bytes starting from ext_flash_address:3 (Stop when timeout) | |
644 // not supported yet case 0x40: // erases 4kB block from ext_flash_address:3 (Warning: No confirmation or built-in security here...) | |
645 // not supported yet case 0x42: // erases range in 4kB steps (Get 3 bytes address and 1byte amount of 4kB blocks) | |
646 // not supported yet case 0x50: // sends firmware from external flash from 0x3E0000 to 0x3FD000 (118784bytes) via comm | |
647 case 0xFE: // hw unit_tests | |
648 case 0x71: // hw read manufacturing data | |
649 case 0x73: // hw update FLEX | |
650 case 0x79: // hw read device data | |
651 #ifdef BOOTLOADER_STANDALONE | |
652 case 0x74: // hw update Firmware | |
653 case 0x75: // hw update RTE | |
654 case 0x76: // hw update Fonts | |
655 case 0x80: // hw write manufacturing data | |
656 case 0x81: // hw write second serial | |
657 case 0x82: // hw set bluetooth name | |
658 #else | |
659 case 0x83: // hw copy logbook entry - read | |
660 case 0x84: // hw copy logbook entry - write | |
661 case 0x85: // hw read entire logbook memory | |
662 case 0x86: // hw overwrite entire logbook memory | |
663 case 0x87: // hw ext_flash_repair_SPECIAL_dive_numbers_starting_count_with memory(x) | |
664 | |
665 #endif | |
666 case 0xC1: // Start low-level bootloader | |
667 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, 1, 1000)!= HAL_OK) | |
668 return 0; | |
669 break; | |
670 default: | |
671 break; | |
672 } | |
673 | |
674 // now send content or update firmware | |
675 switch(type) | |
676 { | |
677 case 0xFE: | |
678 // work to do :-) 12. Oct. 2015 | |
679 // 256 bytes output | |
680 memset(aTxBuffer,0,128); | |
681 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, 128,5000)!= HAL_OK) | |
682 return 0; | |
683 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, 128,5000)!= HAL_OK) | |
684 return 0; | |
685 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
686 break; | |
687 | |
688 case 0x71: | |
689 memcpy(aTxBuffer,hardwareDataGetPointer(),64); | |
690 count += 64; | |
691 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
692 break; | |
693 | |
694 case 0x73: | |
695 #ifndef BOOTLOADER_STANDALONE | |
696 answer = receive_update_flex(1); | |
697 #else | |
698 answer = receive_update_flex(0); | |
699 #endif | |
700 if(answer == 0) | |
701 return 0; | |
702 else if(answer == 2) // 2 = RTE without bootToBootloader | |
703 { | |
704 aTxBuffer[0] = 0xFF; | |
705 HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, 1,10000); | |
706 return 0; | |
707 } | |
708 else | |
709 { | |
710 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
711 if(answer == 1) /* 0xFF is checksum error, 2 = RTE without bootToBootloader */ | |
712 { | |
713 extern uint8_t bootToBootloader; | |
714 bootToBootloader = 1; | |
715 } | |
716 } | |
717 break; | |
718 | |
719 case 0x79: | |
720 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, 1,10000)!= HAL_OK) | |
721 return 0; | |
722 ext_flash_read_fixed_16_devicedata_blocks_formated_128byte_total(aTxBuffer); | |
723 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, 128,5000)!= HAL_OK) | |
724 return 0; | |
725 aTxBuffer[0] = prompt4D4C(receiveStartByteUart); | |
726 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, 1,10000)!= HAL_OK) | |
727 return 0; | |
728 else | |
729 return prompt4D4C(receiveStartByteUart); | |
730 | |
731 case 0x82: | |
732 #ifdef BOOTLOADER_STANDALONE | |
733 setForcedBluetoothName = 1; | |
734 return 0; | |
735 #else | |
736 settingsGetPointer()->debugModeOnStart = 1; | |
737 extern uint8_t bootToBootloader; | |
738 bootToBootloader = 1; | |
739 return prompt4D4C(receiveStartByteUart); | |
740 #endif | |
741 | |
742 #ifdef BOOTLOADER_STANDALONE | |
743 case 0x74: | |
744 answer = receive_update_data_mainCPU_firmware(); | |
745 if(answer != 0) | |
746 { | |
747 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
748 if(answer == 1) // 0xFF is checksum error | |
749 { | |
750 extern uint8_t bootToBootloader; | |
751 bootToBootloader = 1; | |
752 } | |
753 } | |
754 else | |
755 return 0; | |
756 break; | |
757 | |
758 case 0x75: | |
759 receive_update_data_cpu2(); | |
760 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
761 break; | |
762 | |
763 case 0x76: | |
764 answer = receive_update_data_mainCPU_variable_firmware(); | |
765 if(answer != 0) | |
766 { | |
767 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
768 if(answer == 1) // 0xFF is checksum error | |
769 { | |
770 extern uint8_t bootToBootloader; | |
771 bootToBootloader = 1; | |
772 } | |
773 } | |
774 else | |
775 return 0; | |
776 break; | |
777 | |
778 case 0x80: | |
779 if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, 52, 5000)!= HAL_OK) | |
780 return 0; | |
781 if(hardware_programmProductionData(aRxBuffer) == HAL_OK) | |
782 { | |
783 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
784 } | |
785 else | |
786 return 0; | |
787 break; | |
788 | |
789 case 0x81: | |
790 if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, 12, 1000)!= HAL_OK) | |
791 return 0; | |
792 if(hardware_programmSecondarySerial(aRxBuffer) == HAL_OK) | |
793 { | |
794 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
795 } | |
796 else | |
797 return 0; | |
798 break; | |
799 | |
800 #else | |
801 | |
802 #ifdef SPECIALPROGRAMM | |
803 case 0x80: | |
804 if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, 52, 5000)!= HAL_OK) | |
805 return 0; | |
806 if(hardware_programmProductionData(aRxBuffer) == HAL_OK) | |
807 { | |
808 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
809 } | |
810 else | |
811 return 0; | |
812 break; | |
813 #endif | |
814 case 0x83: | |
815 if(HAL_UART_Receive(&UartHandle, &logStepBackwards, 1, 1000)!= HAL_OK) | |
816 return 0; | |
817 logCopyDataPtr = getFrame(98); | |
818 logCopyDataPtrTemp = logCopyDataPtr; | |
819 logCopyDataLength.u32bit = ext_flash_read_dive_raw_with_double_header_1K((uint8_t *)logCopyDataPtr, 1000000,logStepBackwards); | |
820 answer = HAL_OK; | |
821 if(answer == HAL_OK) | |
822 answer = HAL_UART_Transmit(&UartHandle, &(logCopyDataLength.u8bit.byteLow), 1,2000); | |
823 if(answer == HAL_OK) | |
824 answer = HAL_UART_Transmit(&UartHandle, &(logCopyDataLength.u8bit.byteMidLow), 1,2000); | |
825 if(answer == HAL_OK) | |
826 answer = HAL_UART_Transmit(&UartHandle, &(logCopyDataLength.u8bit.byteMidHigh), 1,2000); | |
827 if(answer == HAL_OK) | |
828 answer = HAL_UART_Transmit(&UartHandle, &(logCopyDataLength.u8bit.byteHigh), 1,2000); | |
829 logCopyDataLengthTemp = logCopyDataLength.u32bit; | |
830 while((logCopyDataLengthTemp >= 0xFFFF) && (answer == HAL_OK)) | |
831 { | |
832 answer = HAL_UART_Transmit(&UartHandle, (uint8_t *)logCopyDataPtrTemp, 0xFFFF,30000); | |
833 logCopyDataLengthTemp -= 0xFFFF; | |
834 logCopyDataPtrTemp += 0xFFFF; | |
835 } | |
836 if((logCopyDataLengthTemp > 0) && (answer == HAL_OK)) | |
837 answer = HAL_UART_Transmit(&UartHandle, (uint8_t *)logCopyDataPtrTemp, (uint16_t)logCopyDataLengthTemp,30000); | |
838 releaseFrame(98,logCopyDataPtr); | |
839 if(answer == HAL_OK) | |
840 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
841 else | |
842 return 0; | |
843 break; | |
844 | |
845 case 0x84: | |
846 logCopyDataPtr = getFrame(98); | |
847 logCopyDataPtrTemp = logCopyDataPtr; | |
848 answer = HAL_OK; | |
849 if(answer == HAL_OK) | |
850 answer = HAL_UART_Receive(&UartHandle, &logDummyByte, 1,2000); | |
851 if(answer == HAL_OK) | |
852 answer = HAL_UART_Receive(&UartHandle, &(logCopyDataLength.u8bit.byteLow), 1,2000); | |
853 if(answer == HAL_OK) | |
854 answer = HAL_UART_Receive(&UartHandle, &(logCopyDataLength.u8bit.byteMidLow), 1,2000); | |
855 if(answer == HAL_OK) | |
856 answer = HAL_UART_Receive(&UartHandle, &(logCopyDataLength.u8bit.byteMidHigh), 1,2000); | |
857 if(answer == HAL_OK) | |
858 answer = HAL_UART_Receive(&UartHandle, &(logCopyDataLength.u8bit.byteHigh), 1,2000); | |
859 logCopyDataLengthTemp = logCopyDataLength.u32bit; | |
860 while((logCopyDataLengthTemp >= 0xFFFF) && (answer == HAL_OK)) | |
861 { | |
862 answer = HAL_UART_Receive(&UartHandle, (uint8_t *)logCopyDataPtrTemp, 0xFFFF,30000); | |
863 logCopyDataLengthTemp -= 0xFFFF; | |
864 logCopyDataPtrTemp += 0xFFFF; | |
865 } | |
866 if((logCopyDataLengthTemp > 0) && (answer == HAL_OK)) | |
867 answer = HAL_UART_Receive(&UartHandle, (uint8_t *)logCopyDataPtrTemp, (uint16_t)logCopyDataLengthTemp,30000); | |
868 if(answer == HAL_OK) | |
869 ext_flash_write_dive_raw_with_double_header_1K((uint8_t *)logCopyDataPtr, logCopyDataLength.u32bit); | |
870 releaseFrame(98,logCopyDataPtr); | |
871 if(answer == HAL_OK) | |
872 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
873 else | |
874 return 0; | |
875 break; | |
876 | |
877 case 0x85: | |
878 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
879 logCopyDataPtr = getFrame(98); | |
880 ext_flash_read_header_memory((uint8_t *)logCopyDataPtr); | |
881 for(int i=0;i<8;i++) | |
882 HAL_UART_Transmit(&UartHandle, (uint8_t *)(logCopyDataPtr + (0x8000 * i)), (uint16_t)0x8000,60000); | |
883 releaseFrame(98,logCopyDataPtr); | |
884 break; | |
885 | |
886 case 0x86: | |
887 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
888 logCopyDataPtr = getFrame(98); | |
889 for(int i=0;i<8;i++) | |
890 HAL_UART_Receive(&UartHandle, (uint8_t *)(logCopyDataPtr + (0x8000 * i)), (uint16_t)0x8000,60000); | |
891 ext_flash_write_header_memory((uint8_t *)logCopyDataPtr); | |
892 releaseFrame(98,logCopyDataPtr); | |
893 break; | |
894 | |
895 case 0x87: | |
896 if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, 4, 1000)!= HAL_OK) | |
897 return 0; | |
898 if(((aRxBuffer[0] ^ aRxBuffer[2]) != 0xFF) || ((aRxBuffer[1] ^ aRxBuffer[3]) != 0xFF)) | |
899 return 0; | |
900 totalDiveCount.u8bit.byteLow = aRxBuffer[1]; | |
901 totalDiveCount.u8bit.byteHigh = aRxBuffer[0]; | |
902 ext_flash_repair_SPECIAL_dive_numbers_starting_count_with(totalDiveCount.u16bit); | |
903 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
904 break; | |
905 #endif | |
906 } | |
907 | |
908 // was service command? Yes, finish and exit | |
909 if(count) | |
910 { | |
911 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, count,10000)!= HAL_OK) | |
912 return 0; | |
913 else | |
914 return prompt4D4C(receiveStartByteUart); | |
915 } | |
916 } | |
917 | |
918 | |
919 // download mode commands | |
920 switch(type) | |
921 { | |
922 // return of command for almost all commands | |
923 case 0x60: // get model + features | |
924 case 0x61: // get all headers full (256 bytes) | |
925 case 0x62: // set clock | |
926 case 0x63: // set custom text | |
927 case 0x66: // get dive profile | |
928 case 0x69: // get serial, old version numbering, custom text | |
929 case 0x6A: // get model | |
930 case 0x6B: // get specific firmware version | |
396 | 931 case 0x6C: /* Display Bluetooth signal strength */ |
38 | 932 case 0x6D: // get all compact headers (16 byte) |
933 case 0x6E: // display text | |
934 case 0x70: // read min, default, max setting | |
935 case 0x72: // read setting | |
936 case 0x77: // write setting | |
937 case 0x78: // reset all settings | |
938 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, 1, 1000)!= HAL_OK) | |
939 return 0; | |
940 break; | |
941 | |
942 // start communication (again) | |
943 case 0xBB: | |
944 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, 2, 1000)!= HAL_OK) | |
945 return 0; | |
946 else | |
947 return prompt4D4C(receiveStartByteUart); | |
948 | |
949 // stop communication | |
950 case 0xFF: | |
951 HAL_UART_Transmit(&UartHandle, (uint8_t*)&aTxBuffer, 1, 1000); | |
952 return 0; | |
953 | |
954 default: | |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
955 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); |
38 | 956 break; |
957 } | |
958 | |
959 switch(type) | |
960 { | |
961 case 0x62: | |
962 if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, 6, 2000)!= HAL_OK) | |
963 return 0; | |
964 break; | |
965 case 0x63: | |
966 if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, 60, 5000)!= HAL_OK) | |
967 return 0; | |
968 break; | |
969 case 0x66: | |
970 if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, 1, 1000)!= HAL_OK) | |
971 return 0; | |
972 break; | |
973 case 0x6B: | |
974 if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, 1, 1000)!= HAL_OK) | |
975 return 0; | |
976 break; | |
977 case 0x6E: | |
978 if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, 16, 5000)!= HAL_OK) | |
979 return 0; | |
980 break; | |
981 case 0x77: | |
982 if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, 5, 5000)!= HAL_OK) | |
983 return 0; | |
984 break; | |
985 case 0x72: | |
986 if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, 1, 5000)!= HAL_OK) | |
987 return 0; | |
988 break; | |
989 case 0x70: | |
990 if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, 1, 5000)!= HAL_OK) | |
991 return 0; | |
992 break; | |
993 } | |
994 | |
995 switch(type) | |
996 { | |
997 /* common to standard and bootloader */ | |
998 | |
999 // get model + features | |
1000 case 0x60: | |
1001 aTxBuffer[count++] = 0x00; // hardware descriptor HIGH byte | |
1002 aTxBuffer[count++] = 0x3B; // hardware descriptor LOW byte // 0x3B is OSTC4 // 0x1A is OTSC3 | |
1003 aTxBuffer[count++] = 0x00; // feature descriptor HIGH byte | |
138 | 1004 aTxBuffer[count++] = 0x00; // feature descriptor LOW byte |
38 | 1005 aTxBuffer[count++] = 0x43; // model id |
1006 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1007 break; | |
1008 | |
1009 // get model | |
1010 case 0x6A: | |
1011 aTxBuffer[count++] = 0x3B; // 0x3B is OSTC4 // 0x1A is OTSC3 | |
1012 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1013 break; | |
1014 | |
1015 // get all firmware version and status (OSTC4 only) | |
1016 case 0x6B: | |
1017 switch(*aRxBuffer) | |
1018 { | |
1019 case 0xFF: | |
1020 // firmware | |
1021 aTxBuffer[count++] = firmwareDataGetPointer()->versionFirst; | |
1022 aTxBuffer[count++] = firmwareDataGetPointer()->versionSecond; | |
1023 aTxBuffer[count++] = firmwareDataGetPointer()->versionThird; | |
1024 aTxBuffer[count++] = firmwareDataGetPointer()->versionBeta; | |
1025 break; | |
1026 case 0xFE: | |
1027 // RTE | |
1028 getActualRTEandFONTversion(&tempHigh, &tempLow, 0, 0); // RTE | |
1029 aTxBuffer[count++] = tempHigh; | |
1030 aTxBuffer[count++] = tempLow; | |
1031 aTxBuffer[count++] = 0; | |
1032 aTxBuffer[count++] = 0; | |
1033 break; | |
1034 case 0x10: | |
1035 getActualRTEandFONTversion( 0, 0, &tempHigh, &tempLow); // font | |
1036 aTxBuffer[count++] = tempHigh; | |
1037 aTxBuffer[count++] = tempLow; | |
1038 aTxBuffer[count++] = 0; | |
1039 aTxBuffer[count++] = 0; | |
1040 break; | |
1041 default: | |
1042 // not supported | |
1043 aTxBuffer[count++] = 0xFF; | |
1044 aTxBuffer[count++] = 0xFF; | |
1045 aTxBuffer[count++] = 0xFF; | |
1046 aTxBuffer[count++] = 0xFF; | |
1047 break; | |
1048 /* Jef Driesen Test | |
1049 default: | |
1050 // not supported | |
1051 aTxBuffer[count++] = 0x1; | |
1052 aTxBuffer[count++] = 0x1; | |
1053 aTxBuffer[count++] = 0x1; | |
1054 aTxBuffer[count++] = 0x1; | |
1055 break; | |
1056 */ | |
1057 } | |
1058 /* | |
1059 // serial | |
1060 aTxBuffer[count++] = pSettings->serialLow; | |
1061 aTxBuffer[count++] = pSettings->serialHigh; | |
1062 // batch code (date) | |
1063 hardwareBatchCode(&tempHigh, &tempLow); | |
1064 aTxBuffer[count++] = tempLow; | |
1065 aTxBuffer[count++] = tempHigh; | |
1066 // status and status detail (future feature) | |
1067 aTxBuffer[count++] = 0; | |
1068 aTxBuffer[count++] = 0; | |
1069 aTxBuffer[count++] = 0; | |
1070 aTxBuffer[count++] = 0; | |
1071 */ | |
1072 // prompt | |
1073 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1074 break; | |
1075 | |
396 | 1076 /* Trigger Bluetooth signal strength evaluation */ |
1077 case 0x6C: tComm_EvaluateBluetoothStrength(); | |
1078 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1079 break; | |
38 | 1080 // display text |
1081 case 0x6E: | |
1082 for(int i=0;i<16;i++) | |
1083 display_text[i] = aRxBuffer[i]; | |
1084 display_text[15] = 0; | |
1085 display_text[255] = 16; | |
1086 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1087 break; | |
1088 | |
1089 // version / identify | |
1090 case 0x69: | |
1091 #ifndef BOOTLOADER_STANDALONE | |
1092 aTxBuffer[count++] = pSettings->serialLow; | |
1093 aTxBuffer[count++] = pSettings->serialHigh; | |
1094 aTxBuffer[count++] = firmwareVersion_16bit_low(); | |
1095 aTxBuffer[count++] = firmwareVersion_16bit_high(); | |
1096 memcpy(&aTxBuffer[count], pSettings->customtext, 60); | |
1097 #else | |
1098 aTxBuffer[count++] = 0;//pSettings->serialLow; | |
1099 aTxBuffer[count++] = 0;//pSettings->serialHigh; | |
1100 aTxBuffer[count++] = 0;//firmwareVersion_16bit_low(); | |
1101 aTxBuffer[count++] = 0;//firmwareVersion_16bit_high(); | |
1102 memset(&aTxBuffer[count], 0, 60); | |
1103 #endif | |
1104 count += 60; | |
1105 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1106 break; | |
1107 | |
1108 #ifndef BOOTLOADER_STANDALONE | |
1109 //Reset all setting | |
1110 case 0x78: | |
1111 set_settings_to_Standard(); | |
1112 updateSettingsAndMenuOnExit = 1; | |
1113 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1114 break; | |
1115 #endif | |
1116 | |
1117 #ifndef BOOTLOADER_STANDALONE | |
1118 // full headers (256 byte) | |
1119 case 0x61: | |
1120 for(int StepBackwards = 255; StepBackwards > -1; StepBackwards--) | |
1121 { | |
1122 logbook_getHeader(StepBackwards, &logbookHeader); | |
1123 plogbookHeaderOSTC3 = logbook_build_ostc3header(&logbookHeader); | |
1124 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)plogbookHeaderOSTC3, 256,5000)!= HAL_OK) | |
1125 return 0; | |
1126 } | |
1127 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1128 break; | |
1129 | |
1130 // compact headers (16 byte) | |
1131 case 0x6D: | |
1132 for(int StepBackwards = 255; StepBackwards > -1; StepBackwards--) | |
1133 { | |
1134 logbook_getHeader(StepBackwards, &logbookHeader); | |
1135 plogbookHeaderOSTC3compact = logbook_build_ostc3header_compact(&logbookHeader); | |
1136 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)plogbookHeaderOSTC3compact, 16,5000)!= HAL_OK) | |
1137 return 0; | |
1138 } | |
1139 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1140 break; | |
1141 | |
1142 // set clock & date | |
1143 case 0x62: | |
1144 // ToDo | |
1145 stimestructure.Hours = aRxBuffer[0]; | |
1146 stimestructure.Minutes = aRxBuffer[1]; | |
1147 stimestructure.Seconds = aRxBuffer[2]; | |
1148 sdatestructure.Month = aRxBuffer[3]; | |
1149 sdatestructure.Date = aRxBuffer[4]; | |
1150 sdatestructure.Year = aRxBuffer[5]; // This parameter must be a number between Min_Data = 0 and Max_Data = 99 | |
1151 setWeekday(&sdatestructure); | |
1152 | |
1153 if( ( stimestructure.Hours < 24 ) | |
1154 &&( stimestructure.Minutes < 60 ) | |
1155 &&( stimestructure.Seconds < 60 ) | |
1156 &&( sdatestructure.Month < 13 ) | |
1157 &&( sdatestructure.Date < 32 ) | |
1158 &&( sdatestructure.Year < 100 )) | |
1159 { | |
1160 setTime(stimestructure); | |
1161 setDate(sdatestructure); | |
1162 set_globalState(StUART_RTECONNECT); | |
1163 HAL_Delay(1); | |
1164 set_globalState(StUART_STANDARD); | |
1165 } | |
1166 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1167 break; | |
1168 | |
1169 case 0x63: | |
1170 for(int i=0;i<60;i++) | |
1171 pSettings->customtext[i] = aRxBuffer[i]; | |
1172 pSettings->customtext[59] = 0; | |
1173 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1174 break; | |
1175 | |
1176 // get dive profile | |
1177 case 0x66: | |
1178 logbook_getHeader(255 - aRxBuffer[0], &logbookHeader); | |
1179 plogbookHeaderOSTC3 = logbook_build_ostc3header(&logbookHeader); | |
1180 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)plogbookHeaderOSTC3, 256,5000)!= HAL_OK) | |
1181 return 0; | |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1182 ext_flash_open_read_sample(255 - aRxBuffer[0], &sampleTotalLength); |
38 | 1183 while(sampleTotalLength >= 128) |
1184 { | |
1185 ext_flash_read_next_sample_part(aTxBuffer,128); | |
1186 sampleTotalLength -= 128; | |
1187 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, 128,5000)!= HAL_OK) | |
1188 return 0; | |
1189 } | |
1190 if(sampleTotalLength) | |
1191 { | |
1192 ext_flash_read_next_sample_part(aTxBuffer,sampleTotalLength); | |
1193 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, sampleTotalLength,5000)!= HAL_OK) | |
1194 return 0; | |
1195 } | |
1196 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1197 break; | |
1198 | |
1199 // read min,default,max setting | |
1200 case 0x70: | |
1201 count += readDataLimits__8and16BitValues_4and7BytesOutput(aRxBuffer[0],&aTxBuffer[count]); | |
1202 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1203 break; | |
1204 | |
1205 // read setting | |
1206 case 0x72: | |
1207 readData(aRxBuffer[0],&aTxBuffer[count]); | |
1208 count += 4; | |
1209 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1210 break; | |
1211 | |
1212 // write setting | |
1213 case 0x77: | |
1214 writeData(aRxBuffer); | |
1215 updateSettingsAndMenuOnExit = 1; | |
1216 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1217 break; | |
1218 #else | |
1219 /* bootloader dummies */ | |
1220 // full headers (256 byte) | |
1221 case 0x61: | |
1222 for(int StepBackwards = 0;StepBackwards<256;StepBackwards++) | |
1223 { | |
1224 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)dummyForBootloader, 256,5000)!= HAL_OK) | |
1225 return 0; | |
1226 } | |
1227 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1228 break; | |
1229 // compact headers (16 byte) | |
1230 case 0x6D: | |
1231 for(int StepBackwards = 0;StepBackwards<256;StepBackwards++) | |
1232 { | |
1233 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)dummyForBootloader, 16,5000)!= HAL_OK) | |
1234 return 0; | |
1235 } | |
1236 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1237 break; | |
1238 // set clock & date | |
1239 case 0x62: | |
1240 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1241 break; | |
1242 // set custom text | |
1243 case 0x63: | |
1244 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1245 break; | |
1246 // get dive profile | |
1247 case 0x66: | |
1248 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)dummyForBootloader, 256,5000)!= HAL_OK) | |
1249 return 0; | |
1250 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1251 break; | |
1252 // read min,default,max setting | |
1253 // read settings | |
1254 | |
1255 | |
1256 case 0x72: | |
1257 memcpy(&aTxBuffer[count], dummyForBootloader, 4); | |
1258 count += 4; | |
1259 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1260 break; | |
1261 // write settings | |
1262 case 0x77: | |
1263 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1264 break; | |
1265 #endif | |
1266 } | |
1267 | |
1268 if(count) | |
1269 { | |
1270 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, count,10000)!= HAL_OK) | |
1271 return 0; | |
1272 else | |
1273 return prompt4D4C(receiveStartByteUart); | |
1274 } | |
1275 return 0; | |
1276 } | |
1277 | |
396 | 1278 #define BLOCKSIZE 0x1000 |
38 | 1279 |
1280 HAL_StatusTypeDef receive_uart_large_size(UART_HandleTypeDef *huart, uint8_t *pData, uint32_t Size) | |
1281 { | |
396 | 1282 uint16_t length_4k_blocks; |
1283 uint16_t length_4k_remainder; | |
38 | 1284 uint32_t temp; |
1285 HAL_StatusTypeDef result = HAL_OK; | |
1286 uint32_t pDataLocal; | |
1287 | |
396 | 1288 length_4k_blocks = (uint16_t) (Size / BLOCKSIZE); |
1289 temp = length_4k_blocks; | |
1290 temp *= BLOCKSIZE; | |
1291 length_4k_remainder = (uint16_t) ( Size - temp); | |
38 | 1292 |
1293 pDataLocal = (uint32_t)pData; | |
1294 | |
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
|
1295 |
396 | 1296 while((result == HAL_OK) && length_4k_blocks) |
38 | 1297 { |
396 | 1298 result = HAL_UART_Receive(&UartHandle, (uint8_t *)pDataLocal, BLOCKSIZE , UART_TIMEOUT_LARGE_BLOCK); |
1299 pDataLocal += BLOCKSIZE; | |
1300 length_4k_blocks--; | |
38 | 1301 } |
396 | 1302 |
1303 if((result == HAL_OK) && length_4k_remainder) | |
38 | 1304 { |
396 | 1305 result = HAL_UART_Receive(&UartHandle, (uint8_t *)pDataLocal, length_4k_remainder , UART_TIMEOUT_LARGE_BLOCK); |
38 | 1306 } |
1307 return result; | |
1308 } | |
1309 | |
1310 | |
1311 /* for safety reason (memory blocking this code is main and sub */ | |
1312 | |
1313 #ifdef BOOTLOADER_STANDALONE | |
1314 | |
1315 uint8_t receive_update_data_cpu2(void) | |
1316 { | |
1317 uint8_t answer; | |
1318 | |
1319 uint8_t* pBuffer = (uint8_t*)getFrame(20); | |
1320 answer = receive_update_data_cpu2_sub(pBuffer); | |
1321 releaseFrame(20,(uint32_t)pBuffer); | |
1322 return answer; | |
1323 } | |
1324 | |
1325 | |
1326 uint8_t receive_update_data_cpu2_sub(uint8_t* pBuffer) | |
1327 { | |
1328 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
|
1329 uint32_t length, offsetTotal, checksum, checksumCalc; |
38 | 1330 uint8_t id; |
1331 const uint8_t id_RTE = 0xFE; | |
1332 | |
1333 //Get length | |
1334 if(HAL_UART_Receive(&UartHandle, pBuffer, 4,5000)!= HAL_OK) // 58000 | |
1335 { | |
1336 return 0; | |
1337 } | |
1338 length = 256 * 256 * 256 * (uint32_t)pBuffer[0] + 256 * 256 * (uint32_t)pBuffer[1] + 256 * (uint32_t)pBuffer[2] + pBuffer[3]; | |
1339 | |
1340 //Get id | |
1341 if(HAL_UART_Receive(&UartHandle, pBuffer, 4,5000)!= HAL_OK) // 58000 | |
1342 { | |
1343 return 0; | |
1344 } | |
1345 id = pBuffer[0]; | |
1346 offsetTotal = 256 * 256 * 256 * (uint32_t)pBuffer[0] + 256 * 256 * (uint32_t)pBuffer[1] + 256 * (uint32_t)pBuffer[2] + pBuffer[3]; | |
1347 | |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1348 // get checksum, bytes are in different order on Dev C++ code!!! |
38 | 1349 if(HAL_UART_Receive(&UartHandle, sBuffer, 4,5000)!= HAL_OK) // 58000 |
1350 { | |
1351 return 0; | |
1352 } | |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1353 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
|
1354 checksumCalc = length + offsetTotal; |
38 | 1355 |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1356 // 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
|
1357 if(checksumCalc != checksum) |
38 | 1358 { |
1359 return 0; | |
1360 } | |
1361 | |
1362 //get Code | |
1363 if(receive_uart_large_size(&UartHandle, pBuffer, length)!= HAL_OK) | |
1364 { | |
1365 return 0; | |
1366 } | |
1367 | |
1368 //get Checksum | |
1369 if(HAL_UART_Receive(&UartHandle, sBuffer, 4,5000)!= HAL_OK) // 580000 | |
1370 { | |
1371 return 0; | |
1372 } | |
1373 uint32_t checksum = 256 * 256 * 256 *(uint32_t)sBuffer[0] + 256 * 256 * (uint32_t)sBuffer[1] + 256 * (uint32_t)sBuffer[2] + sBuffer[3]; | |
1374 // uint32_t checksumCalc = crc32c_checksum(pBuffer, length,0,0); | |
1375 uint32_t checksumCalc = CRC_CalcBlockCRC((uint32_t*)pBuffer, length/4); | |
1376 | |
1377 if(checksum != checksumCalc) | |
1378 { | |
1379 return 0; | |
1380 } | |
1381 | |
1382 if(id != id_RTE) | |
1383 { | |
1384 strcpy(display_text,"wrong data."); | |
1385 display_text[255] = 32; | |
1386 return 0; | |
1387 } | |
1388 | |
1389 strcpy(display_text," RTE update."); | |
1390 display_text[255] = 32; | |
1391 | |
1392 return extCPU2bootloader(pBuffer,length,display_text); | |
1393 } | |
1394 #endif // BOOTLOADER_STANDALONE | |
1395 | |
1396 | |
1397 | |
1398 uint8_t receive_update_flex(uint8_t isRTEupdateALLOWED) | |
1399 { | |
1400 uint8_t answer; | |
1401 | |
1402 uint8_t* pBuffer1 = (uint8_t*)getFrame(20); | |
1403 uint8_t* pBuffer2 = (uint8_t*)getFrame(20); | |
1404 | |
1405 answer = receive_update_data_flex(pBuffer1, pBuffer2, isRTEupdateALLOWED); | |
1406 | |
1407 releaseFrame(20,(uint32_t)pBuffer1); | |
1408 releaseFrame(20,(uint32_t)pBuffer2); | |
1409 | |
1410 return answer; | |
1411 } | |
1412 | |
1413 uint8_t receive_update_data_mainCPU_firmware(void) | |
1414 { | |
1415 uint8_t answer; | |
1416 | |
1417 uint8_t* pBuffer1 = (uint8_t*)getFrame(20); | |
1418 | |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1419 answer = receive_update_data_mainCPU_firmware_subroutine(1, pBuffer1, 0); |
38 | 1420 |
1421 releaseFrame(20,(uint32_t)pBuffer1); | |
1422 | |
1423 return answer; | |
1424 } | |
1425 | |
1426 /* multi buffer (long data) not tested yet */ | |
1427 uint8_t receive_update_data_mainCPU_variable_firmware(void) | |
1428 { | |
1429 uint8_t answer; | |
1430 | |
1431 uint8_t* pBuffer1 = (uint8_t*)getFrame(20); | |
1432 uint8_t* pBuffer2 = (uint8_t*)getFrame(20); | |
1433 | |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1434 answer = receive_update_data_mainCPU_firmware_subroutine(2, pBuffer1, pBuffer2); |
38 | 1435 |
1436 releaseFrame(20,(uint32_t)pBuffer1); | |
1437 releaseFrame(20,(uint32_t)pBuffer2); | |
1438 | |
1439 return answer; | |
1440 } | |
1441 | |
1442 uint8_t receive_update_data_flex(uint8_t* pBuffer1, uint8_t* pBuffer2, uint8_t RTEupdateALLOWED) | |
1443 { | |
1444 uint8_t sBuffer[10]; | |
1445 uint8_t serialBuffer[10]; | |
1446 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
|
1447 uint32_t lengthTotal, offsetTotal; |
51
8f8ea3a32e82
Resolved warnings pointing to possible invalid memory access
Ideenmodellierer
parents:
38
diff
changeset
|
1448 uint32_t checksum, checksumCalc = 0; |
38 | 1449 uint8_t id; |
1450 const uint8_t id_Region1_firmware = 0xFF; | |
1451 const uint8_t id_RTE = 0xFE; | |
1452 uint8_t textpointer = 0; | |
1453 | |
1454 //Get length | |
1455 if(HAL_UART_Receive(&UartHandle, sBuffer, 4,5000)!= HAL_OK) // 58000 | |
1456 { | |
1457 return 0; | |
1458 } | |
1459 lengthTotal = 256 * 256 * 256 * (uint32_t)sBuffer[0] + 256 * 256 * (uint32_t)sBuffer[1] + 256 * (uint32_t)sBuffer[2] + sBuffer[3]; | |
1460 | |
1461 //Get offset and/or id (id is 0xFF for RTE, 0xFE for firmware and offset if var) | |
1462 if(HAL_UART_Receive(&UartHandle, sBuffer, 4,5000)!= HAL_OK) // 58000 | |
1463 { | |
1464 return 0; | |
1465 } | |
1466 id = sBuffer[0]; | |
1467 | |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1468 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
|
1469 checksumCalc += lengthTotal; |
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1470 //old, does no longer work because of the fonts: checksumCalc = lengthTotal + offsetTotal; |
38 | 1471 |
1472 if((id != id_Region1_firmware) && (id != id_RTE) && (id != id_FONT) && (id != id_FONT_OLD)) | |
1473 { | |
1474 return 0; | |
1475 } | |
1476 | |
1477 // neu 110212 | |
1478 if(id == id_FONT) | |
1479 offsetTotal = 256 * 256 * 256 * (uint32_t)sBuffer[1] + 256 * 256 * (uint32_t)sBuffer[2] + 256 * (uint32_t)sBuffer[3]; | |
1480 else | |
1481 offsetTotal = 256 * 256 * 256 * (uint32_t)sBuffer[0] + 256 * 256 * (uint32_t)sBuffer[1] + 256 * (uint32_t)sBuffer[2] + sBuffer[3]; | |
1482 | |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1483 // get checksum, bytes are in different order on Dev C++ code!!! |
38 | 1484 if(HAL_UART_Receive(&UartHandle, sBuffer, 4,5000)!= HAL_OK) // 58000 |
1485 { | |
1486 return 0; | |
1487 } | |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1488 checksum = 256 * 256 * 256 * (uint32_t)sBuffer[3] + 256 * 256 * (uint32_t)sBuffer[2] + 256 * (uint32_t)sBuffer[1] + sBuffer[0]; |
38 | 1489 |
1490 | |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1491 if(checksumCalc != checksum) |
38 | 1492 { |
1493 uint8_t ptr = 0; | |
1494 strcpy(&display_text[ptr]," checksum error"); | |
1495 ptr += 15; | |
1496 strcpy(&display_text[ptr],"\n\r"); | |
1497 ptr += 2; | |
1498 ptr += gfx_number_to_string(10,0,&display_text[ptr],checksumCalc); | |
1499 display_text[ptr] = 0; | |
1500 display_text[255] = ptr + 1; | |
1501 return 0xFF; | |
1502 } | |
1503 | |
1504 //Get serial (new since 160211) | |
1505 if(HAL_UART_Receive(&UartHandle, serialBuffer, 4,5000)!= HAL_OK) | |
1506 { | |
1507 return 0; | |
1508 } | |
1509 | |
1510 if(lengthTotal > 768000) | |
1511 { | |
1512 length1 = 768000; | |
1513 length2 = lengthTotal - length1; | |
1514 } | |
1515 else | |
1516 { | |
1517 length1 = lengthTotal; | |
1518 length2 = 0; | |
1519 } | |
1520 | |
1521 if((pBuffer2 == 0) && (length2 != 0)) | |
1522 return 0; | |
1523 | |
1524 //get Code | |
1525 if(receive_uart_large_size(&UartHandle, pBuffer1, length1)!= HAL_OK) | |
1526 return 0; | |
1527 | |
1528 if(length2) | |
1529 if(receive_uart_large_size(&UartHandle, pBuffer2, length2)!= HAL_OK) | |
1530 return 0; | |
1531 | |
1532 //get Checksum | |
1533 if(HAL_UART_Receive(&UartHandle, sBuffer, 4,5000)!= HAL_OK) // 58000 | |
1534 return 0; | |
1535 | |
1536 checksum = 256 * 256 * 256 *(uint32_t)sBuffer[0] + 256 * 256 * (uint32_t)sBuffer[1] + 256 * (uint32_t)sBuffer[2] + sBuffer[3]; | |
1537 // uint32_t checksumCalc = crc32c_checksum(pBuffer1, length1, pBuffer2, length2); | |
1538 if(length2) | |
1539 checksumCalc = CRC_CalcBlockCRC_moreThan768000((uint32_t*)pBuffer1, (uint32_t*)pBuffer2, lengthTotal/4); | |
1540 else | |
1541 checksumCalc = CRC_CalcBlockCRC((uint32_t*)pBuffer1, length1/4); | |
1542 | |
1543 /* check id now */ | |
1544 /* | |
1545 if(region == 2) | |
1546 { | |
1547 if((id == id_Region1_firmware) || (id == id_RTE)) | |
1548 { | |
1549 strcpy(display_text,"wrong data."); | |
1550 display_text[255] = 32; | |
1551 return 0; | |
1552 } | |
1553 } | |
1554 else | |
1555 { | |
1556 if(id != id_Region1_firmware) | |
1557 { | |
1558 strcpy(display_text,"wrong data."); | |
1559 display_text[255] = 32; | |
1560 return 0; | |
1561 } | |
1562 } | |
1563 */ | |
1564 /* test checksum */ | |
1565 if(checksum != checksumCalc) | |
1566 { | |
1567 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
|
1568 strcpy(&display_text[ptr]," checksum error"); |
38 | 1569 ptr += 15; |
1570 strcpy(&display_text[ptr],"\n\r"); | |
1571 display_text[ptr] = 0; | |
1572 display_text[255] = ptr + 1; | |
1573 return 0xFF; | |
1574 } | |
1575 | |
1576 if(id == id_Region1_firmware) | |
1577 { | |
1578 uint8_t ptr = 0; | |
1579 display_text[ptr++] = 'V'; | |
1580 ptr += gfx_number_to_string(2,0,&display_text[ptr],pBuffer1[0x10000] & 0x1F); | |
1581 display_text[ptr++] = '.'; | |
1582 ptr += gfx_number_to_string(2,0,&display_text[ptr],pBuffer1[0x10001] & 0x1F); | |
1583 display_text[ptr++] = '.'; | |
1584 ptr += gfx_number_to_string(2,0,&display_text[ptr],pBuffer1[0x10002] & 0x1F); | |
1585 display_text[ptr++] = ' '; | |
1586 if(pBuffer1[0x10003]) | |
1587 { | |
1588 strcpy(&display_text[ptr],"beta "); | |
1589 ptr +=5; | |
1590 } | |
1591 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
|
1592 ptr += 25; |
38 | 1593 display_text[255] = ptr + 1; |
1594 } | |
1595 else if(id == id_RTE) | |
1596 { | |
1597 if(RTEupdateALLOWED) | |
1598 { | |
1599 strcpy(display_text," RTE update.\n\r"); | |
1600 textpointer = 0; | |
1601 while((display_text[textpointer] != 0) && (textpointer < 50)) | |
1602 textpointer++; | |
1603 #ifndef BOOTLOADER_STANDALONE | |
1604 if(textpointer < 50) | |
1605 { | |
1606 // display_text[textpointer++] = | |
1607 display_text[textpointer++] = '\025'; | |
1608 display_text[textpointer++] = TXT_2BYTE; | |
1609 display_text[textpointer++] = TXT2BYTE_DecoDataLost; | |
1610 display_text[textpointer] = 0; | |
1611 } | |
1612 #endif | |
1613 display_text[255] = textpointer+1; | |
1614 return extCPU2bootloader(pBuffer1,length1,display_text); | |
1615 } | |
1616 else | |
1617 return 0xFF; | |
1618 } | |
1619 else | |
1620 //if(region == 2) | |
1621 { | |
1622 uint8_t ptr = 0; | |
1623 ptr += gfx_number_to_string(7,0,&display_text[ptr],lengthTotal); | |
1624 strcpy(&display_text[ptr]," bytes with "); | |
1625 ptr += 12; | |
1626 ptr += gfx_number_to_string(7,0,&display_text[ptr],offsetTotal); | |
1627 strcpy(&display_text[ptr]," offset"); | |
1628 ptr += 7; | |
1629 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
|
1630 ptr += 25; |
38 | 1631 display_text[255] = ptr + 1; |
1632 } | |
1633 | |
1634 | |
1635 // only non RTE !! | |
1636 uint8_t* pBufferCompare = (uint8_t*)getFrame(20); | |
1637 ByteCompareStatus = 0; | |
1638 | |
1639 if(id == id_Region1_firmware) | |
1640 { | |
1641 /* standard firmware limited to 768000 */ | |
1642 if(ext_flash_read_firmware(pBufferCompare,4,0) != 0xFFFFFFFF) | |
1643 ext_flash_erase_firmware(); | |
1644 ext_flash_write_firmware(pBuffer1, length1); | |
1645 lengthCompare = ext_flash_read_firmware(pBufferCompare,768000,0); | |
1646 | |
1647 if(lengthCompare != length1) | |
1648 ByteCompareStatus = 10000; | |
1649 for(int i = 0; i < length1; i++) | |
1650 { | |
1651 if(pBuffer1[0] != pBufferCompare[0]) | |
1652 ByteCompareStatus++; | |
1653 } | |
1654 } | |
1655 else | |
1656 //if(region == 2) | |
1657 { | |
1658 /* upper region firmware can be larger (1MB) */ | |
1659 if(ext_flash_read_firmware2(0, pBufferCompare,4, 0,0) != 0xFFFFFFFF) | |
1660 ext_flash_erase_firmware2(); | |
1661 ext_flash_write_firmware2(offsetTotal, pBuffer1, length1, pBuffer2, length2); | |
1662 lengthCompare = ext_flash_read_firmware2(&offsetCompare, pBufferCompare,768000, 0,768000); | |
1663 | |
1664 if(lengthCompare != length1 + length2) | |
1665 ByteCompareStatus = 10000; | |
1666 if(offsetTotal != offsetCompare) | |
1667 ByteCompareStatus += 20000; | |
1668 for(int i = 0; i < length1; i++) | |
1669 { | |
1670 if(pBuffer1[0] != pBufferCompare[0]) | |
1671 ByteCompareStatus++; | |
1672 } | |
1673 | |
1674 lengthCompare = ext_flash_read_firmware2(0, 0,768000, pBufferCompare,768000); | |
1675 for(int i = 0; i < length2; i++) | |
1676 { | |
1677 if(pBuffer2[0] != pBufferCompare[0]) | |
1678 ByteCompareStatus++; | |
1679 } | |
1680 } | |
1681 | |
1682 releaseFrame(20,(uint32_t)pBufferCompare); | |
1683 | |
1684 if(ByteCompareStatus != 0) | |
1685 { | |
1686 strcpy(&display_text[0],"\n\rcopy error."); | |
1687 display_text[255] = 21; | |
1688 return 0; | |
1689 } | |
1690 else | |
1691 { | |
1692 strcpy(&display_text[0],"\n\rready to install."); | |
1693 display_text[255] = 21; | |
1694 return 1; | |
1695 } | |
1696 } | |
1697 | |
1698 | |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1699 uint8_t receive_update_data_mainCPU_firmware_subroutine(uint8_t region, uint8_t* pBuffer1, uint8_t* pBuffer2) |
38 | 1700 { |
1701 uint8_t sBuffer[10]; | |
1702 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
|
1703 uint32_t lengthTotal, offsetTotal, checksum, checksumCalc = 0; |
38 | 1704 uint8_t id; |
1705 | |
1706 //Get length | |
1707 if(HAL_UART_Receive(&UartHandle, sBuffer, 4,5000)!= HAL_OK) // 58000 | |
1708 return 0; | |
1709 | |
1710 lengthTotal = 256 * 256 * 256 * (uint32_t)sBuffer[0] + 256 * 256 * (uint32_t)sBuffer[1] + 256 * (uint32_t)sBuffer[2] + sBuffer[3]; | |
1711 | |
1712 //Get offset and/or id (id is 0xFF for RTE, 0xFE for firmware and offset if var) | |
1713 if(HAL_UART_Receive(&UartHandle, sBuffer, 4,5000)!= HAL_OK) // 58000 | |
1714 return 0; | |
1715 | |
1716 id = sBuffer[0]; | |
1717 | |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1718 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
|
1719 checksumCalc += lengthTotal; |
38 | 1720 |
1721 if((id != id_Region1_firmware) && (id != id_RTE) && (id != id_FONT) && (id != id_FONT_OLD)) | |
1722 return 0; | |
1723 | |
1724 if(id == id_FONT) | |
1725 offsetTotal = 256 * 256 * 256 * (uint32_t)sBuffer[1] + 256 * 256 * (uint32_t)sBuffer[2] + 256 * (uint32_t)sBuffer[3]; | |
1726 // alt, prior to id for font | |
1727 else | |
1728 offsetTotal = 256 * 256 * 256 * (uint32_t)sBuffer[0] + 256 * 256 * (uint32_t)sBuffer[1] + 256 * (uint32_t)sBuffer[2] + sBuffer[3]; | |
1729 | |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1730 // get checksum, bytes are in different order on Dev C++ code!!! |
38 | 1731 if(HAL_UART_Receive(&UartHandle, sBuffer, 4,5000)!= HAL_OK) // 58000 |
1732 return 0; | |
1733 | |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1734 checksum = 256 * 256 * 256 * (uint32_t)sBuffer[3] + 256 * 256 * (uint32_t)sBuffer[2] + 256 * (uint32_t)sBuffer[1] + sBuffer[0]; |
38 | 1735 |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1736 //old: checksumCalc = lengthTotal + offsetTotal; |
38 | 1737 |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1738 if(checksumCalc != checksum) |
38 | 1739 { |
1740 uint8_t ptr = 0; | |
1741 strcpy(&display_text[ptr]," checksum error"); | |
1742 ptr += 15; | |
1743 strcpy(&display_text[ptr],"\n\r"); | |
1744 ptr += 2; | |
1745 ptr += gfx_number_to_string(10,0,&display_text[ptr],checksumCalc); | |
1746 display_text[ptr] = 0; | |
1747 display_text[255] = ptr + 1; | |
1748 return 0xFF; | |
1749 } | |
1750 | |
1751 if(lengthTotal > 768000) | |
1752 { | |
1753 length1 = 768000; | |
1754 length2 = lengthTotal - length1; | |
1755 } | |
1756 else | |
1757 { | |
1758 length1 = lengthTotal; | |
1759 length2 = 0; | |
1760 } | |
1761 | |
1762 if((pBuffer2 == 0) && (length2 != 0)) | |
1763 return 0; | |
1764 | |
1765 //get Code | |
1766 if(receive_uart_large_size(&UartHandle, pBuffer1, length1)!= HAL_OK) | |
1767 return 0; | |
1768 | |
1769 if(length2) | |
1770 if(receive_uart_large_size(&UartHandle, pBuffer2, length2)!= HAL_OK) | |
1771 return 0; | |
1772 | |
1773 //get Checksum | |
1774 if(HAL_UART_Receive(&UartHandle, sBuffer, 4,5000)!= HAL_OK) // 58000 | |
1775 return 0; | |
1776 | |
1777 checksum = 256 * 256 * 256 *(uint32_t)sBuffer[0] + 256 * 256 * (uint32_t)sBuffer[1] + 256 * (uint32_t)sBuffer[2] + sBuffer[3]; | |
1778 // uint32_t checksumCalc = crc32c_checksum(pBuffer1, length1, pBuffer2, length2); | |
1779 if(length2) | |
1780 checksumCalc = CRC_CalcBlockCRC_moreThan768000((uint32_t*)pBuffer1, (uint32_t*)pBuffer2, lengthTotal/4); | |
1781 else | |
1782 checksumCalc = CRC_CalcBlockCRC((uint32_t*)pBuffer1, length1/4); | |
1783 | |
1784 /* check id now */ | |
1785 if(region == 2) | |
1786 { | |
1787 if((id == id_Region1_firmware) || (id == id_RTE)) | |
1788 { | |
1789 strcpy(display_text,"wrong data."); | |
1790 display_text[255] = 32; | |
1791 return 0; | |
1792 } | |
1793 } | |
1794 else | |
1795 { | |
1796 if(id != id_Region1_firmware) | |
1797 { | |
1798 strcpy(display_text,"wrong data."); | |
1799 display_text[255] = 32; | |
1800 return 0; | |
1801 } | |
1802 } | |
1803 | |
1804 /* test checksum */ | |
1805 if(checksum != checksumCalc) | |
1806 { | |
1807 uint8_t ptr = 0; | |
1808 strcpy(&display_text[ptr]," pruefsummen error"); | |
1809 ptr += 15; | |
1810 strcpy(&display_text[ptr],"\n\r"); | |
1811 display_text[ptr] = 0; | |
1812 display_text[255] = ptr + 1; | |
1813 return 0xFF; | |
1814 } | |
1815 | |
1816 if(region == 2) | |
1817 { | |
1818 uint8_t ptr = 0; | |
1819 ptr += gfx_number_to_string(7,0,&display_text[ptr],lengthTotal); | |
1820 strcpy(&display_text[ptr]," bytes with "); | |
1821 ptr += 12; | |
1822 ptr += gfx_number_to_string(7,0,&display_text[ptr],offsetTotal); | |
1823 strcpy(&display_text[ptr]," offset"); | |
1824 ptr += 7; | |
1825 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
|
1826 ptr += 25; |
38 | 1827 display_text[255] = ptr + 1; |
1828 | |
1829 } | |
1830 else | |
1831 { | |
1832 uint8_t ptr = 0; | |
1833 display_text[ptr++] = 'V'; | |
1834 ptr += gfx_number_to_string(2,0,&display_text[ptr],pBuffer1[0x10000] & 0x1F); | |
1835 display_text[ptr++] = '.'; | |
1836 ptr += gfx_number_to_string(2,0,&display_text[ptr],pBuffer1[0x10001] & 0x1F); | |
1837 display_text[ptr++] = '.'; | |
1838 ptr += gfx_number_to_string(2,0,&display_text[ptr],pBuffer1[0x10002] & 0x1F); | |
1839 display_text[ptr++] = ' '; | |
1840 if(pBuffer1[0x10003]) | |
1841 { | |
1842 strcpy(&display_text[ptr],"beta "); | |
1843 ptr +=5; | |
1844 } | |
1845 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
|
1846 ptr += 25; |
38 | 1847 display_text[255] = ptr + 1; |
1848 } | |
1849 | |
1850 uint8_t* pBufferCompare = (uint8_t*)getFrame(20); | |
1851 ByteCompareStatus = 0; | |
1852 | |
1853 if(region == 2) | |
1854 { | |
1855 /* upper region firmware can be larger (1MB) */ | |
1856 if(ext_flash_read_firmware2(0, pBufferCompare,4, 0,0) != 0xFFFFFFFF) | |
1857 ext_flash_erase_firmware2(); | |
1858 ext_flash_write_firmware2(offsetTotal, pBuffer1, length1, pBuffer2, length2); | |
1859 lengthCompare = ext_flash_read_firmware2(&offsetCompare, pBufferCompare,768000, 0,768000); | |
1860 | |
1861 if(lengthCompare != length1 + length2) | |
1862 ByteCompareStatus = 10000; | |
1863 if(offsetTotal != offsetCompare) | |
1864 ByteCompareStatus += 20000; | |
1865 for(int i = 0; i < length1; i++) | |
1866 { | |
1867 if(pBuffer1[0] != pBufferCompare[0]) | |
1868 ByteCompareStatus++; | |
1869 } | |
1870 | |
1871 lengthCompare = ext_flash_read_firmware2(0, 0,768000, pBufferCompare,768000); | |
1872 for(int i = 0; i < length2; i++) | |
1873 { | |
1874 if(pBuffer2[0] != pBufferCompare[0]) | |
1875 ByteCompareStatus++; | |
1876 } | |
1877 } | |
1878 else | |
1879 { | |
1880 /* standard firmware limited to 768000 */ | |
1881 if(ext_flash_read_firmware(pBufferCompare,4,0) != 0xFFFFFFFF) | |
1882 ext_flash_erase_firmware(); | |
1883 ext_flash_write_firmware(pBuffer1, length1); | |
1884 lengthCompare = ext_flash_read_firmware(pBufferCompare,768000,0); | |
1885 | |
1886 if(lengthCompare != length1) | |
1887 ByteCompareStatus = 10000; | |
1888 for(int i = 0; i < length1; i++) | |
1889 { | |
1890 if(pBuffer1[0] != pBufferCompare[0]) | |
1891 ByteCompareStatus++; | |
1892 } | |
1893 } | |
1894 | |
1895 releaseFrame(20,(uint32_t)pBufferCompare); | |
1896 | |
1897 if(ByteCompareStatus != 0) | |
1898 { | |
1899 strcpy(&display_text[0],"\n\rcopy error."); | |
1900 display_text[255] = 21; | |
1901 return 0; | |
1902 } | |
1903 else | |
1904 { | |
1905 strcpy(&display_text[0],"\n\rready to install."); | |
1906 display_text[255] = 21; | |
1907 return 1; | |
1908 } | |
1909 } | |
1910 | |
396 | 1911 void tComm_RequestBluetoothStrength(void) |
1912 { | |
1913 EvaluateBluetoothSignalStrength = 1; | |
1914 } | |
1915 | |
1916 /* read, validate the modul answer and flush rx que if necessary */ | |
1917 uint8_t tComm_CheckAnswerOK() | |
1918 { | |
1919 char answerOkay[] = "\r\nOK\r\n"; | |
1920 char aRxBuffer[20]; | |
1921 uint8_t sizeAnswer = sizeof(answerOkay) -1; | |
1922 uint8_t result = HAL_OK; | |
1923 uint8_t index = 0; | |
1924 uint8_t answer; | |
1925 | |
1926 memset(aRxBuffer,0,20); | |
1927 if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, sizeAnswer, 1000) == HAL_OK) | |
1928 { | |
1929 do | |
1930 { | |
1931 if(answerOkay[index] != aRxBuffer[index]) | |
1932 { | |
1933 index = sizeAnswer; | |
1934 result = HAL_ERROR; /* unexpected answer => there might be characters left in RX que => read and discard all rx bytes */ | |
1935 do | |
1936 { | |
1937 answer = HAL_UART_Receive(&UartHandle, (uint8_t*)&aRxBuffer[index], 1, 10); | |
1938 if (index < 20) index++; | |
1939 }while(answer == HAL_OK); | |
1940 index = sizeAnswer; | |
1941 } | |
1942 else | |
1943 { | |
1944 index++; | |
1945 } | |
1946 }while(index < sizeAnswer); | |
1947 } | |
1948 else | |
1949 { | |
1950 result = HAL_ERROR; | |
1951 } | |
1952 return result; | |
1953 | |
1954 } | |
1955 char SignalStr[20]; | |
1956 | |
1957 void tComm_EvaluateBluetoothStrength(void) | |
1958 { | |
1959 char aTxBufferBarSSI[] = "AT+BARSSI\r"; | |
1960 char aTxBufferEscapeSequence[] = "+++"; | |
1961 char aTxBufferEnd[] = "ATO\r"; | |
1962 uint8_t sizeRequest = sizeof(aTxBufferBarSSI) -1; | |
1963 | |
1964 uint8_t answer = HAL_OK; | |
1965 char aRxBuffer[20]; | |
1966 | |
1967 uint8_t index = 0; | |
1968 uint8_t strindex = 0; | |
1969 int8_t sigqual = 0; | |
1970 | |
1971 HAL_Delay(200); | |
1972 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBufferEscapeSequence, 3, 2000)== HAL_OK) | |
1973 { | |
1974 if(tComm_CheckAnswerOK() == HAL_OK) | |
1975 { | |
1976 HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBufferBarSSI,sizeRequest , 2000); | |
1977 { | |
1978 index = 0; | |
1979 do /* Answer is not the common one. Instead the signal strength is received => read all available bytes one by one*/ | |
1980 { | |
1981 answer = HAL_UART_Receive(&UartHandle, (uint8_t*)&aRxBuffer[index], 1, 100); | |
1982 if(index < 20-1) index++; | |
1983 }while(answer == HAL_OK); | |
1984 | |
1985 if((aRxBuffer[index] != 'E') && (aRxBuffer[index] != 0)) /* E represents the first letter of the string ERROR */ | |
1986 { | |
1987 index = 0; | |
1988 strindex = 0; | |
1989 do | |
1990 { | |
1991 SignalStr[strindex++] = aRxBuffer[index++]; | |
1992 }while ((index < 20) && (aRxBuffer[index] != '\r')); | |
1993 SignalStr[strindex] = 0; /* terminate String */ | |
1994 sigqual = strtol(SignalStr,NULL,0); | |
1995 #if 0 | |
1996 if(sigqual & 0x80) /* high bit set? */ | |
1997 { | |
1998 sigqual = ~sigqual; /* calc complement of 2 */ | |
1999 sigqual++; | |
2000 } | |
2001 #endif | |
2002 /* Map db to abstract Bargraph */ | |
2003 if(sigqual > 0) | |
2004 { | |
2005 sprintf(SignalStr,"Bluetooth ||||||||||"); | |
2006 } | |
2007 else | |
2008 { | |
2009 sprintf(SignalStr,"Bluetooth |"); | |
2010 strindex = strlen(SignalStr); | |
2011 sigqual *=-1; | |
2012 sigqual = 100 - sigqual; /* invert because of negative db value */ | |
2013 while(sigqual / 10 > 0 ) | |
2014 { | |
2015 SignalStr[strindex++] = '|'; | |
2016 sigqual -= 10; | |
2017 } | |
2018 SignalStr[strindex] = 0; | |
2019 } | |
2020 strcpy(display_text,SignalStr); | |
2021 display_text[255] = strlen(SignalStr); | |
2022 EvaluateBluetoothSignalStrength = 0; | |
2023 } | |
2024 } | |
2025 HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBufferEnd, 4, 2000); /* exit terminal mode */ | |
2026 index = 0; | |
2027 do /* module will answer with current connection state */ | |
2028 { | |
2029 answer = HAL_UART_Receive(&UartHandle, (uint8_t*)&aRxBuffer[index], 1, 100); | |
2030 if(index < 20-1) index++; | |
2031 }while(answer == HAL_OK); | |
2032 } | |
2033 } | |
2034 } | |
2035 | |
2036 void tComm_StartBlueModConfig() | |
2037 { | |
2038 uint8_t answer = HAL_OK; | |
2039 uint8_t RxBuffer[20]; | |
2040 uint8_t index = 0; | |
2041 | |
2042 BmTmpConfig = BM_CONFIG_ECHO; | |
2043 do /* flush RX buffer */ | |
2044 { | |
2045 answer = HAL_UART_Receive(&UartHandle, (uint8_t*)&RxBuffer[index], 1, 10); | |
2046 if(index < 20-1) index++; | |
2047 }while(answer == HAL_OK); | |
2048 } | |
2049 | |
2050 uint8_t tComm_HandleBlueModConfig() | |
2051 { | |
2052 static uint8_t ConfigRetryCnt = 0; | |
2053 | |
2054 char TxBuffer[20]; | |
2055 uint8_t CmdSize = 0; | |
2056 | |
2057 uint8_t result = HAL_OK; | |
2058 | |
2059 switch (BmTmpConfig) | |
2060 { | |
2061 case BM_CONFIG_ECHO: sprintf(TxBuffer,"ATE0\r"); | |
2062 break; | |
2063 case BM_CONFIG_SILENCE: sprintf(TxBuffer,"ATS30=0\r"); | |
2064 break; | |
2065 case BM_CONFIG_ESCAPE_DELAY: sprintf(TxBuffer,"ATS12=10\r"); | |
2066 break; | |
2067 case BM_CONFIG_SIGNAL_POLL: sprintf(TxBuffer,"AT+BSTPOLL=100\r"); | |
2068 break; | |
2069 case BM_CONFIG_BAUD: sprintf(TxBuffer,"AT%%B22\r"); | |
2070 break; | |
2071 // case BM_CONFIG_DISABLE_EVENT: sprintf(TxBuffer,"AT+LECPEVENT=0\r"); | |
2072 // break; | |
2073 case BM_CONFIG_DONE: | |
2074 case BM_CONFIG_OFF: | |
2075 ConfigRetryCnt = 0; | |
2076 break; | |
2077 default: | |
2078 break; | |
2079 } | |
2080 if((BmTmpConfig != BM_CONFIG_OFF) && (BmTmpConfig != BM_CONFIG_DONE)) | |
2081 { | |
2082 CmdSize = strlen(TxBuffer); | |
2083 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)TxBuffer,CmdSize, 2000) == HAL_OK) | |
2084 { | |
2085 if(BmTmpConfig == BM_CONFIG_ECHO) /* echo is not yet turned off => read and discard echo */ | |
2086 { | |
2087 HAL_UART_Receive(&UartHandle, (uint8_t*)TxBuffer, CmdSize, 1000); | |
2088 } | |
2089 | |
2090 result = tComm_CheckAnswerOK(); | |
2091 | |
2092 | |
2093 if((BmTmpConfig == BM_CONFIG_BAUD) && (result == HAL_OK) && (UartHandle.Init.BaudRate != 460800)) /* is com already switched to fast speed? */ | |
2094 { | |
2095 HAL_UART_DeInit(&UartHandle); | |
2096 HAL_Delay(1); | |
2097 UartHandle.Init.BaudRate = 460800; | |
2098 HAL_UART_Init(&UartHandle); | |
2099 } | |
2100 if(result == HAL_OK) | |
2101 { | |
2102 BmTmpConfig++; | |
2103 } | |
2104 if(BmTmpConfig == BM_CONFIG_DONE) | |
2105 { | |
2106 ConfigRetryCnt = 0; | |
2107 } | |
2108 } | |
2109 } | |
2110 if(result != HAL_OK) | |
2111 { | |
2112 ConfigRetryCnt++; | |
2113 if(ConfigRetryCnt > 3) /* Configuration failed => switch off module */ | |
2114 { | |
2115 ConfigRetryCnt = 0; | |
2116 BmTmpConfig = BM_CONFIG_OFF; | |
2117 settingsGetPointer()->bluetoothActive = 0; | |
2118 MX_Bluetooth_PowerOff(); | |
2119 } | |
2120 } | |
2121 return result; | |
2122 } | |
2123 | |
38 | 2124 static void tComm_Error_Handler(void) |
2125 { | |
2126 while(1) | |
2127 {} | |
2128 } |