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