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