Mercurial > public > ostc4
annotate Discovery/Src/tComm.c @ 457:4bd01f48c285 minor_improvments
Eliminated some minor warnings
author | ideenmodellierer |
---|---|
date | Tue, 24 Mar 2020 22:32:48 +0100 |
parents | 5e38b09d2c45 |
children | 5dd996ed8611 |
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; | |
456
5e38b09d2c45
Transfer a dummy profile in case of missing sample information:
ideenmodellierer
parents:
434
diff
changeset
|
1185 |
5e38b09d2c45
Transfer a dummy profile in case of missing sample information:
ideenmodellierer
parents:
434
diff
changeset
|
1186 if(logbookHeader.pBeginProfileData[0]==0) /* no sample information */ |
38 | 1187 { |
456
5e38b09d2c45
Transfer a dummy profile in case of missing sample information:
ideenmodellierer
parents:
434
diff
changeset
|
1188 sampleTotalLength = logbook_fillDummySampleBuffer(logbookHeader.diveTimeMinutes, logbookHeader.diveTimeSeconds, logbookHeader.maxDepth, logbookHeader.lastDecostop_m, logbookHeader.minTemp); |
5e38b09d2c45
Transfer a dummy profile in case of missing sample information:
ideenmodellierer
parents:
434
diff
changeset
|
1189 while(sampleTotalLength >= 128) |
5e38b09d2c45
Transfer a dummy profile in case of missing sample information:
ideenmodellierer
parents:
434
diff
changeset
|
1190 { |
5e38b09d2c45
Transfer a dummy profile in case of missing sample information:
ideenmodellierer
parents:
434
diff
changeset
|
1191 logbook_readDummySamples(aTxBuffer,128); |
5e38b09d2c45
Transfer a dummy profile in case of missing sample information:
ideenmodellierer
parents:
434
diff
changeset
|
1192 sampleTotalLength -= 128; |
5e38b09d2c45
Transfer a dummy profile in case of missing sample information:
ideenmodellierer
parents:
434
diff
changeset
|
1193 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
|
1194 return 0; |
5e38b09d2c45
Transfer a dummy profile in case of missing sample information:
ideenmodellierer
parents:
434
diff
changeset
|
1195 } |
5e38b09d2c45
Transfer a dummy profile in case of missing sample information:
ideenmodellierer
parents:
434
diff
changeset
|
1196 if(sampleTotalLength) |
5e38b09d2c45
Transfer a dummy profile in case of missing sample information:
ideenmodellierer
parents:
434
diff
changeset
|
1197 { |
5e38b09d2c45
Transfer a dummy profile in case of missing sample information:
ideenmodellierer
parents:
434
diff
changeset
|
1198 logbook_readDummySamples(aTxBuffer,sampleTotalLength); |
5e38b09d2c45
Transfer a dummy profile in case of missing sample information:
ideenmodellierer
parents:
434
diff
changeset
|
1199 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
|
1200 return 0; |
5e38b09d2c45
Transfer a dummy profile in case of missing sample information:
ideenmodellierer
parents:
434
diff
changeset
|
1201 } |
5e38b09d2c45
Transfer a dummy profile in case of missing sample information:
ideenmodellierer
parents:
434
diff
changeset
|
1202 |
38 | 1203 } |
456
5e38b09d2c45
Transfer a dummy profile in case of missing sample information:
ideenmodellierer
parents:
434
diff
changeset
|
1204 else |
38 | 1205 { |
456
5e38b09d2c45
Transfer a dummy profile in case of missing sample information:
ideenmodellierer
parents:
434
diff
changeset
|
1206 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
|
1207 while(sampleTotalLength >= 128) |
5e38b09d2c45
Transfer a dummy profile in case of missing sample information:
ideenmodellierer
parents:
434
diff
changeset
|
1208 { |
5e38b09d2c45
Transfer a dummy profile in case of missing sample information:
ideenmodellierer
parents:
434
diff
changeset
|
1209 ext_flash_read_next_sample_part(aTxBuffer,128); |
5e38b09d2c45
Transfer a dummy profile in case of missing sample information:
ideenmodellierer
parents:
434
diff
changeset
|
1210 sampleTotalLength -= 128; |
5e38b09d2c45
Transfer a dummy profile in case of missing sample information:
ideenmodellierer
parents:
434
diff
changeset
|
1211 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
|
1212 return 0; |
5e38b09d2c45
Transfer a dummy profile in case of missing sample information:
ideenmodellierer
parents:
434
diff
changeset
|
1213 } |
5e38b09d2c45
Transfer a dummy profile in case of missing sample information:
ideenmodellierer
parents:
434
diff
changeset
|
1214 if(sampleTotalLength) |
5e38b09d2c45
Transfer a dummy profile in case of missing sample information:
ideenmodellierer
parents:
434
diff
changeset
|
1215 { |
5e38b09d2c45
Transfer a dummy profile in case of missing sample information:
ideenmodellierer
parents:
434
diff
changeset
|
1216 ext_flash_read_next_sample_part(aTxBuffer,sampleTotalLength); |
5e38b09d2c45
Transfer a dummy profile in case of missing sample information:
ideenmodellierer
parents:
434
diff
changeset
|
1217 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
|
1218 return 0; |
5e38b09d2c45
Transfer a dummy profile in case of missing sample information:
ideenmodellierer
parents:
434
diff
changeset
|
1219 } |
38 | 1220 } |
456
5e38b09d2c45
Transfer a dummy profile in case of missing sample information:
ideenmodellierer
parents:
434
diff
changeset
|
1221 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); |
38 | 1222 break; |
1223 | |
1224 // read min,default,max setting | |
1225 case 0x70: | |
1226 count += readDataLimits__8and16BitValues_4and7BytesOutput(aRxBuffer[0],&aTxBuffer[count]); | |
1227 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1228 break; | |
1229 | |
1230 // read setting | |
1231 case 0x72: | |
1232 readData(aRxBuffer[0],&aTxBuffer[count]); | |
1233 count += 4; | |
1234 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1235 break; | |
1236 | |
1237 // write setting | |
1238 case 0x77: | |
1239 writeData(aRxBuffer); | |
1240 updateSettingsAndMenuOnExit = 1; | |
1241 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1242 break; | |
1243 #else | |
1244 /* bootloader dummies */ | |
1245 // full headers (256 byte) | |
1246 case 0x61: | |
1247 for(int StepBackwards = 0;StepBackwards<256;StepBackwards++) | |
1248 { | |
1249 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)dummyForBootloader, 256,5000)!= HAL_OK) | |
1250 return 0; | |
1251 } | |
1252 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1253 break; | |
1254 // compact headers (16 byte) | |
1255 case 0x6D: | |
1256 for(int StepBackwards = 0;StepBackwards<256;StepBackwards++) | |
1257 { | |
1258 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)dummyForBootloader, 16,5000)!= HAL_OK) | |
1259 return 0; | |
1260 } | |
1261 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1262 break; | |
1263 // set clock & date | |
1264 case 0x62: | |
1265 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1266 break; | |
1267 // set custom text | |
1268 case 0x63: | |
1269 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1270 break; | |
1271 // get dive profile | |
1272 case 0x66: | |
1273 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)dummyForBootloader, 256,5000)!= HAL_OK) | |
1274 return 0; | |
1275 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1276 break; | |
1277 // read min,default,max setting | |
1278 // read settings | |
1279 | |
1280 | |
1281 case 0x72: | |
1282 memcpy(&aTxBuffer[count], dummyForBootloader, 4); | |
1283 count += 4; | |
1284 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1285 break; | |
1286 // write settings | |
1287 case 0x77: | |
1288 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); | |
1289 break; | |
1290 #endif | |
1291 } | |
1292 | |
1293 if(count) | |
1294 { | |
1295 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, count,10000)!= HAL_OK) | |
1296 return 0; | |
1297 else | |
1298 return prompt4D4C(receiveStartByteUart); | |
1299 } | |
1300 return 0; | |
1301 } | |
1302 | |
396 | 1303 #define BLOCKSIZE 0x1000 |
38 | 1304 |
1305 HAL_StatusTypeDef receive_uart_large_size(UART_HandleTypeDef *huart, uint8_t *pData, uint32_t Size) | |
1306 { | |
396 | 1307 uint16_t length_4k_blocks; |
1308 uint16_t length_4k_remainder; | |
38 | 1309 uint32_t temp; |
1310 HAL_StatusTypeDef result = HAL_OK; | |
1311 uint32_t pDataLocal; | |
1312 | |
396 | 1313 length_4k_blocks = (uint16_t) (Size / BLOCKSIZE); |
1314 temp = length_4k_blocks; | |
1315 temp *= BLOCKSIZE; | |
1316 length_4k_remainder = (uint16_t) ( Size - temp); | |
38 | 1317 |
1318 pDataLocal = (uint32_t)pData; | |
1319 | |
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
|
1320 |
396 | 1321 while((result == HAL_OK) && length_4k_blocks) |
38 | 1322 { |
396 | 1323 result = HAL_UART_Receive(&UartHandle, (uint8_t *)pDataLocal, BLOCKSIZE , UART_TIMEOUT_LARGE_BLOCK); |
1324 pDataLocal += BLOCKSIZE; | |
1325 length_4k_blocks--; | |
38 | 1326 } |
396 | 1327 |
1328 if((result == HAL_OK) && length_4k_remainder) | |
38 | 1329 { |
396 | 1330 result = HAL_UART_Receive(&UartHandle, (uint8_t *)pDataLocal, length_4k_remainder , UART_TIMEOUT_LARGE_BLOCK); |
38 | 1331 } |
1332 return result; | |
1333 } | |
1334 | |
1335 | |
1336 /* for safety reason (memory blocking this code is main and sub */ | |
1337 | |
1338 #ifdef BOOTLOADER_STANDALONE | |
1339 | |
1340 uint8_t receive_update_data_cpu2(void) | |
1341 { | |
1342 uint8_t answer; | |
1343 | |
1344 uint8_t* pBuffer = (uint8_t*)getFrame(20); | |
1345 answer = receive_update_data_cpu2_sub(pBuffer); | |
1346 releaseFrame(20,(uint32_t)pBuffer); | |
1347 return answer; | |
1348 } | |
1349 | |
1350 | |
1351 uint8_t receive_update_data_cpu2_sub(uint8_t* pBuffer) | |
1352 { | |
1353 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
|
1354 uint32_t length, offsetTotal, checksum, checksumCalc; |
38 | 1355 uint8_t id; |
1356 const uint8_t id_RTE = 0xFE; | |
1357 | |
1358 //Get length | |
1359 if(HAL_UART_Receive(&UartHandle, pBuffer, 4,5000)!= HAL_OK) // 58000 | |
1360 { | |
1361 return 0; | |
1362 } | |
1363 length = 256 * 256 * 256 * (uint32_t)pBuffer[0] + 256 * 256 * (uint32_t)pBuffer[1] + 256 * (uint32_t)pBuffer[2] + pBuffer[3]; | |
1364 | |
1365 //Get id | |
1366 if(HAL_UART_Receive(&UartHandle, pBuffer, 4,5000)!= HAL_OK) // 58000 | |
1367 { | |
1368 return 0; | |
1369 } | |
1370 id = pBuffer[0]; | |
1371 offsetTotal = 256 * 256 * 256 * (uint32_t)pBuffer[0] + 256 * 256 * (uint32_t)pBuffer[1] + 256 * (uint32_t)pBuffer[2] + pBuffer[3]; | |
1372 | |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1373 // get checksum, bytes are in different order on Dev C++ code!!! |
38 | 1374 if(HAL_UART_Receive(&UartHandle, sBuffer, 4,5000)!= HAL_OK) // 58000 |
1375 { | |
1376 return 0; | |
1377 } | |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1378 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
|
1379 checksumCalc = length + offsetTotal; |
38 | 1380 |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1381 // 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
|
1382 if(checksumCalc != checksum) |
38 | 1383 { |
1384 return 0; | |
1385 } | |
1386 | |
1387 //get Code | |
1388 if(receive_uart_large_size(&UartHandle, pBuffer, length)!= HAL_OK) | |
1389 { | |
1390 return 0; | |
1391 } | |
1392 | |
1393 //get Checksum | |
1394 if(HAL_UART_Receive(&UartHandle, sBuffer, 4,5000)!= HAL_OK) // 580000 | |
1395 { | |
1396 return 0; | |
1397 } | |
1398 uint32_t checksum = 256 * 256 * 256 *(uint32_t)sBuffer[0] + 256 * 256 * (uint32_t)sBuffer[1] + 256 * (uint32_t)sBuffer[2] + sBuffer[3]; | |
1399 // uint32_t checksumCalc = crc32c_checksum(pBuffer, length,0,0); | |
1400 uint32_t checksumCalc = CRC_CalcBlockCRC((uint32_t*)pBuffer, length/4); | |
1401 | |
1402 if(checksum != checksumCalc) | |
1403 { | |
1404 return 0; | |
1405 } | |
1406 | |
1407 if(id != id_RTE) | |
1408 { | |
1409 strcpy(display_text,"wrong data."); | |
1410 display_text[255] = 32; | |
1411 return 0; | |
1412 } | |
1413 | |
1414 strcpy(display_text," RTE update."); | |
1415 display_text[255] = 32; | |
1416 | |
1417 return extCPU2bootloader(pBuffer,length,display_text); | |
1418 } | |
1419 #endif // BOOTLOADER_STANDALONE | |
1420 | |
1421 | |
1422 | |
1423 uint8_t receive_update_flex(uint8_t isRTEupdateALLOWED) | |
1424 { | |
1425 uint8_t answer; | |
1426 | |
1427 uint8_t* pBuffer1 = (uint8_t*)getFrame(20); | |
1428 uint8_t* pBuffer2 = (uint8_t*)getFrame(20); | |
1429 | |
1430 answer = receive_update_data_flex(pBuffer1, pBuffer2, isRTEupdateALLOWED); | |
1431 | |
1432 releaseFrame(20,(uint32_t)pBuffer1); | |
1433 releaseFrame(20,(uint32_t)pBuffer2); | |
1434 | |
1435 return answer; | |
1436 } | |
1437 | |
1438 uint8_t receive_update_data_mainCPU_firmware(void) | |
1439 { | |
1440 uint8_t answer; | |
1441 | |
1442 uint8_t* pBuffer1 = (uint8_t*)getFrame(20); | |
1443 | |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1444 answer = receive_update_data_mainCPU_firmware_subroutine(1, pBuffer1, 0); |
38 | 1445 |
1446 releaseFrame(20,(uint32_t)pBuffer1); | |
1447 | |
1448 return answer; | |
1449 } | |
1450 | |
1451 /* multi buffer (long data) not tested yet */ | |
1452 uint8_t receive_update_data_mainCPU_variable_firmware(void) | |
1453 { | |
1454 uint8_t answer; | |
1455 | |
1456 uint8_t* pBuffer1 = (uint8_t*)getFrame(20); | |
1457 uint8_t* pBuffer2 = (uint8_t*)getFrame(20); | |
1458 | |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1459 answer = receive_update_data_mainCPU_firmware_subroutine(2, pBuffer1, pBuffer2); |
38 | 1460 |
1461 releaseFrame(20,(uint32_t)pBuffer1); | |
1462 releaseFrame(20,(uint32_t)pBuffer2); | |
1463 | |
1464 return answer; | |
1465 } | |
1466 | |
1467 uint8_t receive_update_data_flex(uint8_t* pBuffer1, uint8_t* pBuffer2, uint8_t RTEupdateALLOWED) | |
1468 { | |
1469 uint8_t sBuffer[10]; | |
1470 uint8_t serialBuffer[10]; | |
1471 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
|
1472 uint32_t lengthTotal, offsetTotal; |
51
8f8ea3a32e82
Resolved warnings pointing to possible invalid memory access
Ideenmodellierer
parents:
38
diff
changeset
|
1473 uint32_t checksum, checksumCalc = 0; |
38 | 1474 uint8_t id; |
1475 const uint8_t id_Region1_firmware = 0xFF; | |
1476 const uint8_t id_RTE = 0xFE; | |
1477 uint8_t textpointer = 0; | |
1478 | |
1479 //Get length | |
1480 if(HAL_UART_Receive(&UartHandle, sBuffer, 4,5000)!= HAL_OK) // 58000 | |
1481 { | |
1482 return 0; | |
1483 } | |
1484 lengthTotal = 256 * 256 * 256 * (uint32_t)sBuffer[0] + 256 * 256 * (uint32_t)sBuffer[1] + 256 * (uint32_t)sBuffer[2] + sBuffer[3]; | |
1485 | |
1486 //Get offset and/or id (id is 0xFF for RTE, 0xFE for firmware and offset if var) | |
1487 if(HAL_UART_Receive(&UartHandle, sBuffer, 4,5000)!= HAL_OK) // 58000 | |
1488 { | |
1489 return 0; | |
1490 } | |
1491 id = sBuffer[0]; | |
1492 | |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1493 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
|
1494 checksumCalc += lengthTotal; |
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1495 //old, does no longer work because of the fonts: checksumCalc = lengthTotal + offsetTotal; |
38 | 1496 |
1497 if((id != id_Region1_firmware) && (id != id_RTE) && (id != id_FONT) && (id != id_FONT_OLD)) | |
1498 { | |
1499 return 0; | |
1500 } | |
1501 | |
1502 // neu 110212 | |
1503 if(id == id_FONT) | |
1504 offsetTotal = 256 * 256 * 256 * (uint32_t)sBuffer[1] + 256 * 256 * (uint32_t)sBuffer[2] + 256 * (uint32_t)sBuffer[3]; | |
1505 else | |
1506 offsetTotal = 256 * 256 * 256 * (uint32_t)sBuffer[0] + 256 * 256 * (uint32_t)sBuffer[1] + 256 * (uint32_t)sBuffer[2] + sBuffer[3]; | |
1507 | |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1508 // get checksum, bytes are in different order on Dev C++ code!!! |
38 | 1509 if(HAL_UART_Receive(&UartHandle, sBuffer, 4,5000)!= HAL_OK) // 58000 |
1510 { | |
1511 return 0; | |
1512 } | |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1513 checksum = 256 * 256 * 256 * (uint32_t)sBuffer[3] + 256 * 256 * (uint32_t)sBuffer[2] + 256 * (uint32_t)sBuffer[1] + sBuffer[0]; |
38 | 1514 |
1515 | |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1516 if(checksumCalc != checksum) |
38 | 1517 { |
1518 uint8_t ptr = 0; | |
1519 strcpy(&display_text[ptr]," checksum error"); | |
1520 ptr += 15; | |
1521 strcpy(&display_text[ptr],"\n\r"); | |
1522 ptr += 2; | |
1523 ptr += gfx_number_to_string(10,0,&display_text[ptr],checksumCalc); | |
1524 display_text[ptr] = 0; | |
1525 display_text[255] = ptr + 1; | |
1526 return 0xFF; | |
1527 } | |
1528 | |
1529 //Get serial (new since 160211) | |
1530 if(HAL_UART_Receive(&UartHandle, serialBuffer, 4,5000)!= HAL_OK) | |
1531 { | |
1532 return 0; | |
1533 } | |
1534 | |
1535 if(lengthTotal > 768000) | |
1536 { | |
1537 length1 = 768000; | |
1538 length2 = lengthTotal - length1; | |
1539 } | |
1540 else | |
1541 { | |
1542 length1 = lengthTotal; | |
1543 length2 = 0; | |
1544 } | |
1545 | |
1546 if((pBuffer2 == 0) && (length2 != 0)) | |
1547 return 0; | |
1548 | |
1549 //get Code | |
1550 if(receive_uart_large_size(&UartHandle, pBuffer1, length1)!= HAL_OK) | |
1551 return 0; | |
1552 | |
1553 if(length2) | |
1554 if(receive_uart_large_size(&UartHandle, pBuffer2, length2)!= HAL_OK) | |
1555 return 0; | |
1556 | |
1557 //get Checksum | |
1558 if(HAL_UART_Receive(&UartHandle, sBuffer, 4,5000)!= HAL_OK) // 58000 | |
1559 return 0; | |
1560 | |
1561 checksum = 256 * 256 * 256 *(uint32_t)sBuffer[0] + 256 * 256 * (uint32_t)sBuffer[1] + 256 * (uint32_t)sBuffer[2] + sBuffer[3]; | |
1562 // uint32_t checksumCalc = crc32c_checksum(pBuffer1, length1, pBuffer2, length2); | |
1563 if(length2) | |
1564 checksumCalc = CRC_CalcBlockCRC_moreThan768000((uint32_t*)pBuffer1, (uint32_t*)pBuffer2, lengthTotal/4); | |
1565 else | |
1566 checksumCalc = CRC_CalcBlockCRC((uint32_t*)pBuffer1, length1/4); | |
1567 | |
1568 /* check id now */ | |
1569 /* | |
1570 if(region == 2) | |
1571 { | |
1572 if((id == id_Region1_firmware) || (id == id_RTE)) | |
1573 { | |
1574 strcpy(display_text,"wrong data."); | |
1575 display_text[255] = 32; | |
1576 return 0; | |
1577 } | |
1578 } | |
1579 else | |
1580 { | |
1581 if(id != id_Region1_firmware) | |
1582 { | |
1583 strcpy(display_text,"wrong data."); | |
1584 display_text[255] = 32; | |
1585 return 0; | |
1586 } | |
1587 } | |
1588 */ | |
1589 /* test checksum */ | |
1590 if(checksum != checksumCalc) | |
1591 { | |
1592 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
|
1593 strcpy(&display_text[ptr]," checksum error"); |
38 | 1594 ptr += 15; |
1595 strcpy(&display_text[ptr],"\n\r"); | |
1596 display_text[ptr] = 0; | |
1597 display_text[255] = ptr + 1; | |
1598 return 0xFF; | |
1599 } | |
1600 | |
1601 if(id == id_Region1_firmware) | |
1602 { | |
1603 uint8_t ptr = 0; | |
1604 display_text[ptr++] = 'V'; | |
1605 ptr += gfx_number_to_string(2,0,&display_text[ptr],pBuffer1[0x10000] & 0x1F); | |
1606 display_text[ptr++] = '.'; | |
1607 ptr += gfx_number_to_string(2,0,&display_text[ptr],pBuffer1[0x10001] & 0x1F); | |
1608 display_text[ptr++] = '.'; | |
1609 ptr += gfx_number_to_string(2,0,&display_text[ptr],pBuffer1[0x10002] & 0x1F); | |
1610 display_text[ptr++] = ' '; | |
1611 if(pBuffer1[0x10003]) | |
1612 { | |
1613 strcpy(&display_text[ptr],"beta "); | |
1614 ptr +=5; | |
1615 } | |
1616 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
|
1617 ptr += 25; |
38 | 1618 display_text[255] = ptr + 1; |
1619 } | |
1620 else if(id == id_RTE) | |
1621 { | |
1622 if(RTEupdateALLOWED) | |
1623 { | |
1624 strcpy(display_text," RTE update.\n\r"); | |
1625 textpointer = 0; | |
1626 while((display_text[textpointer] != 0) && (textpointer < 50)) | |
1627 textpointer++; | |
1628 #ifndef BOOTLOADER_STANDALONE | |
1629 if(textpointer < 50) | |
1630 { | |
1631 // display_text[textpointer++] = | |
1632 display_text[textpointer++] = '\025'; | |
1633 display_text[textpointer++] = TXT_2BYTE; | |
1634 display_text[textpointer++] = TXT2BYTE_DecoDataLost; | |
1635 display_text[textpointer] = 0; | |
1636 } | |
1637 #endif | |
1638 display_text[255] = textpointer+1; | |
1639 return extCPU2bootloader(pBuffer1,length1,display_text); | |
1640 } | |
1641 else | |
1642 return 0xFF; | |
1643 } | |
1644 else | |
1645 //if(region == 2) | |
1646 { | |
1647 uint8_t ptr = 0; | |
1648 ptr += gfx_number_to_string(7,0,&display_text[ptr],lengthTotal); | |
1649 strcpy(&display_text[ptr]," bytes with "); | |
1650 ptr += 12; | |
1651 ptr += gfx_number_to_string(7,0,&display_text[ptr],offsetTotal); | |
1652 strcpy(&display_text[ptr]," offset"); | |
1653 ptr += 7; | |
1654 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
|
1655 ptr += 25; |
38 | 1656 display_text[255] = ptr + 1; |
1657 } | |
1658 | |
1659 | |
1660 // only non RTE !! | |
1661 uint8_t* pBufferCompare = (uint8_t*)getFrame(20); | |
1662 ByteCompareStatus = 0; | |
1663 | |
1664 if(id == id_Region1_firmware) | |
1665 { | |
1666 /* standard firmware limited to 768000 */ | |
1667 if(ext_flash_read_firmware(pBufferCompare,4,0) != 0xFFFFFFFF) | |
1668 ext_flash_erase_firmware(); | |
1669 ext_flash_write_firmware(pBuffer1, length1); | |
1670 lengthCompare = ext_flash_read_firmware(pBufferCompare,768000,0); | |
1671 | |
1672 if(lengthCompare != length1) | |
1673 ByteCompareStatus = 10000; | |
1674 for(int i = 0; i < length1; i++) | |
1675 { | |
1676 if(pBuffer1[0] != pBufferCompare[0]) | |
1677 ByteCompareStatus++; | |
1678 } | |
1679 } | |
1680 else | |
1681 //if(region == 2) | |
1682 { | |
1683 /* upper region firmware can be larger (1MB) */ | |
1684 if(ext_flash_read_firmware2(0, pBufferCompare,4, 0,0) != 0xFFFFFFFF) | |
1685 ext_flash_erase_firmware2(); | |
1686 ext_flash_write_firmware2(offsetTotal, pBuffer1, length1, pBuffer2, length2); | |
1687 lengthCompare = ext_flash_read_firmware2(&offsetCompare, pBufferCompare,768000, 0,768000); | |
1688 | |
1689 if(lengthCompare != length1 + length2) | |
1690 ByteCompareStatus = 10000; | |
1691 if(offsetTotal != offsetCompare) | |
1692 ByteCompareStatus += 20000; | |
1693 for(int i = 0; i < length1; i++) | |
1694 { | |
1695 if(pBuffer1[0] != pBufferCompare[0]) | |
1696 ByteCompareStatus++; | |
1697 } | |
1698 | |
1699 lengthCompare = ext_flash_read_firmware2(0, 0,768000, pBufferCompare,768000); | |
1700 for(int i = 0; i < length2; i++) | |
1701 { | |
1702 if(pBuffer2[0] != pBufferCompare[0]) | |
1703 ByteCompareStatus++; | |
1704 } | |
1705 } | |
1706 | |
1707 releaseFrame(20,(uint32_t)pBufferCompare); | |
1708 | |
1709 if(ByteCompareStatus != 0) | |
1710 { | |
1711 strcpy(&display_text[0],"\n\rcopy error."); | |
1712 display_text[255] = 21; | |
1713 return 0; | |
1714 } | |
1715 else | |
1716 { | |
1717 strcpy(&display_text[0],"\n\rready to install."); | |
1718 display_text[255] = 21; | |
1719 return 1; | |
1720 } | |
1721 } | |
1722 | |
1723 | |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1724 uint8_t receive_update_data_mainCPU_firmware_subroutine(uint8_t region, uint8_t* pBuffer1, uint8_t* pBuffer2) |
38 | 1725 { |
1726 uint8_t sBuffer[10]; | |
1727 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
|
1728 uint32_t lengthTotal, offsetTotal, checksum, checksumCalc = 0; |
38 | 1729 uint8_t id; |
1730 | |
1731 //Get length | |
1732 if(HAL_UART_Receive(&UartHandle, sBuffer, 4,5000)!= HAL_OK) // 58000 | |
1733 return 0; | |
1734 | |
1735 lengthTotal = 256 * 256 * 256 * (uint32_t)sBuffer[0] + 256 * 256 * (uint32_t)sBuffer[1] + 256 * (uint32_t)sBuffer[2] + sBuffer[3]; | |
1736 | |
1737 //Get offset and/or id (id is 0xFF for RTE, 0xFE for firmware and offset if var) | |
1738 if(HAL_UART_Receive(&UartHandle, sBuffer, 4,5000)!= HAL_OK) // 58000 | |
1739 return 0; | |
1740 | |
1741 id = sBuffer[0]; | |
1742 | |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1743 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
|
1744 checksumCalc += lengthTotal; |
38 | 1745 |
1746 if((id != id_Region1_firmware) && (id != id_RTE) && (id != id_FONT) && (id != id_FONT_OLD)) | |
1747 return 0; | |
1748 | |
1749 if(id == id_FONT) | |
1750 offsetTotal = 256 * 256 * 256 * (uint32_t)sBuffer[1] + 256 * 256 * (uint32_t)sBuffer[2] + 256 * (uint32_t)sBuffer[3]; | |
1751 // alt, prior to id for font | |
1752 else | |
1753 offsetTotal = 256 * 256 * 256 * (uint32_t)sBuffer[0] + 256 * 256 * (uint32_t)sBuffer[1] + 256 * (uint32_t)sBuffer[2] + sBuffer[3]; | |
1754 | |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1755 // get checksum, bytes are in different order on Dev C++ code!!! |
38 | 1756 if(HAL_UART_Receive(&UartHandle, sBuffer, 4,5000)!= HAL_OK) // 58000 |
1757 return 0; | |
1758 | |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1759 checksum = 256 * 256 * 256 * (uint32_t)sBuffer[3] + 256 * 256 * (uint32_t)sBuffer[2] + 256 * (uint32_t)sBuffer[1] + sBuffer[0]; |
38 | 1760 |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1761 //old: checksumCalc = lengthTotal + offsetTotal; |
38 | 1762 |
236
ad6ddc4aabcd
Minor: translating some comments and variable names into english, some cleanup in tComm.c
heinrichsweikamp
parents:
229
diff
changeset
|
1763 if(checksumCalc != checksum) |
38 | 1764 { |
1765 uint8_t ptr = 0; | |
1766 strcpy(&display_text[ptr]," checksum error"); | |
1767 ptr += 15; | |
1768 strcpy(&display_text[ptr],"\n\r"); | |
1769 ptr += 2; | |
1770 ptr += gfx_number_to_string(10,0,&display_text[ptr],checksumCalc); | |
1771 display_text[ptr] = 0; | |
1772 display_text[255] = ptr + 1; | |
1773 return 0xFF; | |
1774 } | |
1775 | |
1776 if(lengthTotal > 768000) | |
1777 { | |
1778 length1 = 768000; | |
1779 length2 = lengthTotal - length1; | |
1780 } | |
1781 else | |
1782 { | |
1783 length1 = lengthTotal; | |
1784 length2 = 0; | |
1785 } | |
1786 | |
1787 if((pBuffer2 == 0) && (length2 != 0)) | |
1788 return 0; | |
1789 | |
1790 //get Code | |
1791 if(receive_uart_large_size(&UartHandle, pBuffer1, length1)!= HAL_OK) | |
1792 return 0; | |
1793 | |
1794 if(length2) | |
1795 if(receive_uart_large_size(&UartHandle, pBuffer2, length2)!= HAL_OK) | |
1796 return 0; | |
1797 | |
1798 //get Checksum | |
1799 if(HAL_UART_Receive(&UartHandle, sBuffer, 4,5000)!= HAL_OK) // 58000 | |
1800 return 0; | |
1801 | |
1802 checksum = 256 * 256 * 256 *(uint32_t)sBuffer[0] + 256 * 256 * (uint32_t)sBuffer[1] + 256 * (uint32_t)sBuffer[2] + sBuffer[3]; | |
1803 // uint32_t checksumCalc = crc32c_checksum(pBuffer1, length1, pBuffer2, length2); | |
1804 if(length2) | |
1805 checksumCalc = CRC_CalcBlockCRC_moreThan768000((uint32_t*)pBuffer1, (uint32_t*)pBuffer2, lengthTotal/4); | |
1806 else | |
1807 checksumCalc = CRC_CalcBlockCRC((uint32_t*)pBuffer1, length1/4); | |
1808 | |
1809 /* check id now */ | |
1810 if(region == 2) | |
1811 { | |
1812 if((id == id_Region1_firmware) || (id == id_RTE)) | |
1813 { | |
1814 strcpy(display_text,"wrong data."); | |
1815 display_text[255] = 32; | |
1816 return 0; | |
1817 } | |
1818 } | |
1819 else | |
1820 { | |
1821 if(id != id_Region1_firmware) | |
1822 { | |
1823 strcpy(display_text,"wrong data."); | |
1824 display_text[255] = 32; | |
1825 return 0; | |
1826 } | |
1827 } | |
1828 | |
1829 /* test checksum */ | |
1830 if(checksum != checksumCalc) | |
1831 { | |
1832 uint8_t ptr = 0; | |
1833 strcpy(&display_text[ptr]," pruefsummen error"); | |
1834 ptr += 15; | |
1835 strcpy(&display_text[ptr],"\n\r"); | |
1836 display_text[ptr] = 0; | |
1837 display_text[255] = ptr + 1; | |
1838 return 0xFF; | |
1839 } | |
1840 | |
1841 if(region == 2) | |
1842 { | |
1843 uint8_t ptr = 0; | |
1844 ptr += gfx_number_to_string(7,0,&display_text[ptr],lengthTotal); | |
1845 strcpy(&display_text[ptr]," bytes with "); | |
1846 ptr += 12; | |
1847 ptr += gfx_number_to_string(7,0,&display_text[ptr],offsetTotal); | |
1848 strcpy(&display_text[ptr]," offset"); | |
1849 ptr += 7; | |
1850 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
|
1851 ptr += 25; |
38 | 1852 display_text[255] = ptr + 1; |
1853 | |
1854 } | |
1855 else | |
1856 { | |
1857 uint8_t ptr = 0; | |
1858 display_text[ptr++] = 'V'; | |
1859 ptr += gfx_number_to_string(2,0,&display_text[ptr],pBuffer1[0x10000] & 0x1F); | |
1860 display_text[ptr++] = '.'; | |
1861 ptr += gfx_number_to_string(2,0,&display_text[ptr],pBuffer1[0x10001] & 0x1F); | |
1862 display_text[ptr++] = '.'; | |
1863 ptr += gfx_number_to_string(2,0,&display_text[ptr],pBuffer1[0x10002] & 0x1F); | |
1864 display_text[ptr++] = ' '; | |
1865 if(pBuffer1[0x10003]) | |
1866 { | |
1867 strcpy(&display_text[ptr],"beta "); | |
1868 ptr +=5; | |
1869 } | |
1870 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
|
1871 ptr += 25; |
38 | 1872 display_text[255] = ptr + 1; |
1873 } | |
1874 | |
1875 uint8_t* pBufferCompare = (uint8_t*)getFrame(20); | |
1876 ByteCompareStatus = 0; | |
1877 | |
1878 if(region == 2) | |
1879 { | |
1880 /* upper region firmware can be larger (1MB) */ | |
1881 if(ext_flash_read_firmware2(0, pBufferCompare,4, 0,0) != 0xFFFFFFFF) | |
1882 ext_flash_erase_firmware2(); | |
1883 ext_flash_write_firmware2(offsetTotal, pBuffer1, length1, pBuffer2, length2); | |
1884 lengthCompare = ext_flash_read_firmware2(&offsetCompare, pBufferCompare,768000, 0,768000); | |
1885 | |
1886 if(lengthCompare != length1 + length2) | |
1887 ByteCompareStatus = 10000; | |
1888 if(offsetTotal != offsetCompare) | |
1889 ByteCompareStatus += 20000; | |
1890 for(int i = 0; i < length1; i++) | |
1891 { | |
1892 if(pBuffer1[0] != pBufferCompare[0]) | |
1893 ByteCompareStatus++; | |
1894 } | |
1895 | |
1896 lengthCompare = ext_flash_read_firmware2(0, 0,768000, pBufferCompare,768000); | |
1897 for(int i = 0; i < length2; i++) | |
1898 { | |
1899 if(pBuffer2[0] != pBufferCompare[0]) | |
1900 ByteCompareStatus++; | |
1901 } | |
1902 } | |
1903 else | |
1904 { | |
1905 /* standard firmware limited to 768000 */ | |
1906 if(ext_flash_read_firmware(pBufferCompare,4,0) != 0xFFFFFFFF) | |
1907 ext_flash_erase_firmware(); | |
1908 ext_flash_write_firmware(pBuffer1, length1); | |
1909 lengthCompare = ext_flash_read_firmware(pBufferCompare,768000,0); | |
1910 | |
1911 if(lengthCompare != length1) | |
1912 ByteCompareStatus = 10000; | |
1913 for(int i = 0; i < length1; i++) | |
1914 { | |
1915 if(pBuffer1[0] != pBufferCompare[0]) | |
1916 ByteCompareStatus++; | |
1917 } | |
1918 } | |
1919 | |
1920 releaseFrame(20,(uint32_t)pBufferCompare); | |
1921 | |
1922 if(ByteCompareStatus != 0) | |
1923 { | |
1924 strcpy(&display_text[0],"\n\rcopy error."); | |
1925 display_text[255] = 21; | |
1926 return 0; | |
1927 } | |
1928 else | |
1929 { | |
1930 strcpy(&display_text[0],"\n\rready to install."); | |
1931 display_text[255] = 21; | |
1932 return 1; | |
1933 } | |
1934 } | |
1935 | |
396 | 1936 void tComm_RequestBluetoothStrength(void) |
1937 { | |
1938 EvaluateBluetoothSignalStrength = 1; | |
1939 } | |
1940 | |
1941 /* read, validate the modul answer and flush rx que if necessary */ | |
1942 uint8_t tComm_CheckAnswerOK() | |
1943 { | |
1944 char answerOkay[] = "\r\nOK\r\n"; | |
400 | 1945 char aRxBuffer[UART_CMD_BUF_SIZE]; |
396 | 1946 uint8_t sizeAnswer = sizeof(answerOkay) -1; |
1947 uint8_t result = HAL_OK; | |
1948 uint8_t index = 0; | |
1949 uint8_t answer; | |
1950 | |
400 | 1951 memset(aRxBuffer,0,UART_CMD_BUF_SIZE); |
434
f68f2c4d71c7
Reduced timeout for common UART communication:
ideenmodellierer
parents:
400
diff
changeset
|
1952 if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, sizeAnswer, UART_OPERATION_TIMEOUT) == HAL_OK) |
396 | 1953 { |
1954 do | |
1955 { | |
1956 if(answerOkay[index] != aRxBuffer[index]) | |
1957 { | |
1958 index = sizeAnswer; | |
1959 result = HAL_ERROR; /* unexpected answer => there might be characters left in RX que => read and discard all rx bytes */ | |
1960 do | |
1961 { | |
1962 answer = HAL_UART_Receive(&UartHandle, (uint8_t*)&aRxBuffer[index], 1, 10); | |
400 | 1963 if (index < UART_CMD_BUF_SIZE) |
1964 { | |
1965 index++; | |
1966 } | |
396 | 1967 }while(answer == HAL_OK); |
1968 index = sizeAnswer; | |
1969 } | |
1970 else | |
1971 { | |
1972 index++; | |
1973 } | |
1974 }while(index < sizeAnswer); | |
1975 } | |
1976 else | |
1977 { | |
1978 result = HAL_ERROR; | |
1979 } | |
1980 return result; | |
1981 | |
1982 } | |
400 | 1983 |
396 | 1984 |
1985 void tComm_EvaluateBluetoothStrength(void) | |
1986 { | |
1987 char aTxBufferBarSSI[] = "AT+BARSSI\r"; | |
1988 char aTxBufferEscapeSequence[] = "+++"; | |
1989 char aTxBufferEnd[] = "ATO\r"; | |
1990 uint8_t sizeRequest = sizeof(aTxBufferBarSSI) -1; | |
1991 | |
1992 uint8_t answer = HAL_OK; | |
400 | 1993 char aRxBuffer[UART_CMD_BUF_SIZE]; |
1994 char SignalStr[UART_CMD_BUF_SIZE]; | |
396 | 1995 uint8_t index = 0; |
1996 uint8_t strindex = 0; | |
1997 int8_t sigqual = 0; | |
1998 | |
1999 HAL_Delay(200); | |
2000 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBufferEscapeSequence, 3, 2000)== HAL_OK) | |
2001 { | |
2002 if(tComm_CheckAnswerOK() == HAL_OK) | |
2003 { | |
2004 HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBufferBarSSI,sizeRequest , 2000); | |
2005 { | |
2006 index = 0; | |
2007 do /* Answer is not the common one. Instead the signal strength is received => read all available bytes one by one*/ | |
2008 { | |
2009 answer = HAL_UART_Receive(&UartHandle, (uint8_t*)&aRxBuffer[index], 1, 100); | |
400 | 2010 if(index < UART_CMD_BUF_SIZE) |
2011 { | |
2012 index++; | |
2013 } | |
396 | 2014 }while(answer == HAL_OK); |
2015 | |
2016 if((aRxBuffer[index] != 'E') && (aRxBuffer[index] != 0)) /* E represents the first letter of the string ERROR */ | |
2017 { | |
2018 index = 0; | |
2019 strindex = 0; | |
2020 do | |
2021 { | |
2022 SignalStr[strindex++] = aRxBuffer[index++]; | |
400 | 2023 }while ((index < UART_CMD_BUF_SIZE - 1) && (aRxBuffer[index] != '\r')); |
396 | 2024 SignalStr[strindex] = 0; /* terminate String */ |
2025 sigqual = strtol(SignalStr,NULL,0); | |
2026 /* Map db to abstract Bargraph */ | |
2027 if(sigqual > 0) | |
2028 { | |
400 | 2029 sprintf(SignalStr,"Bluetooth ||||||||"); |
396 | 2030 } |
2031 else | |
2032 { | |
2033 sprintf(SignalStr,"Bluetooth |"); | |
2034 strindex = strlen(SignalStr); | |
2035 sigqual *=-1; | |
2036 sigqual = 100 - sigqual; /* invert because of negative db value */ | |
2037 while(sigqual / 10 > 0 ) | |
2038 { | |
2039 SignalStr[strindex++] = '|'; | |
2040 sigqual -= 10; | |
2041 } | |
2042 SignalStr[strindex] = 0; | |
2043 } | |
2044 strcpy(display_text,SignalStr); | |
2045 display_text[255] = strlen(SignalStr); | |
2046 EvaluateBluetoothSignalStrength = 0; | |
2047 } | |
2048 } | |
2049 HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBufferEnd, 4, 2000); /* exit terminal mode */ | |
2050 index = 0; | |
2051 do /* module will answer with current connection state */ | |
2052 { | |
2053 answer = HAL_UART_Receive(&UartHandle, (uint8_t*)&aRxBuffer[index], 1, 100); | |
400 | 2054 if(index < UART_CMD_BUF_SIZE) |
2055 { | |
2056 index++; | |
2057 } | |
396 | 2058 }while(answer == HAL_OK); |
2059 } | |
2060 } | |
2061 } | |
2062 | |
2063 void tComm_StartBlueModConfig() | |
2064 { | |
2065 uint8_t answer = HAL_OK; | |
400 | 2066 uint8_t RxBuffer[UART_CMD_BUF_SIZE]; |
396 | 2067 uint8_t index = 0; |
2068 | |
2069 BmTmpConfig = BM_CONFIG_ECHO; | |
2070 do /* flush RX buffer */ | |
2071 { | |
2072 answer = HAL_UART_Receive(&UartHandle, (uint8_t*)&RxBuffer[index], 1, 10); | |
400 | 2073 if(index < UART_CMD_BUF_SIZE) index++; |
396 | 2074 }while(answer == HAL_OK); |
2075 } | |
2076 | |
2077 uint8_t tComm_HandleBlueModConfig() | |
2078 { | |
400 | 2079 static uint8_t RestartModule = 1; /* used to do power off / on cycle */ |
2080 static uint8_t ConfigRetryCnt = 0; /* Retry count without power cycle */ | |
396 | 2081 |
400 | 2082 char TxBuffer[UART_CMD_BUF_SIZE]; |
396 | 2083 uint8_t CmdSize = 0; |
2084 | |
2085 uint8_t result = HAL_OK; | |
2086 | |
400 | 2087 TxBuffer[0] = 0; |
2088 | |
396 | 2089 switch (BmTmpConfig) |
2090 { | |
2091 case BM_CONFIG_ECHO: sprintf(TxBuffer,"ATE0\r"); | |
2092 break; | |
2093 case BM_CONFIG_SILENCE: sprintf(TxBuffer,"ATS30=0\r"); | |
2094 break; | |
2095 case BM_CONFIG_ESCAPE_DELAY: sprintf(TxBuffer,"ATS12=10\r"); | |
2096 break; | |
2097 case BM_CONFIG_SIGNAL_POLL: sprintf(TxBuffer,"AT+BSTPOLL=100\r"); | |
2098 break; | |
2099 case BM_CONFIG_BAUD: sprintf(TxBuffer,"AT%%B22\r"); | |
2100 break; | |
400 | 2101 case BM_CONFIG_RETRY: ConfigRetryCnt--; |
2102 HAL_Delay(1); | |
2103 if(ConfigRetryCnt == 0) | |
2104 { | |
2105 MX_Bluetooth_PowerOn(); | |
2106 tComm_StartBlueModConfig(); | |
2107 } | |
2108 break; | |
396 | 2109 case BM_CONFIG_DONE: |
2110 case BM_CONFIG_OFF: | |
2111 ConfigRetryCnt = 0; | |
400 | 2112 RestartModule = 1; |
396 | 2113 break; |
2114 default: | |
2115 break; | |
2116 } | |
400 | 2117 if(TxBuffer[0] != 0) /* forward command to module */ |
396 | 2118 { |
2119 CmdSize = strlen(TxBuffer); | |
2120 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)TxBuffer,CmdSize, 2000) == HAL_OK) | |
2121 { | |
2122 if(BmTmpConfig == BM_CONFIG_ECHO) /* echo is not yet turned off => read and discard echo */ | |
2123 { | |
434
f68f2c4d71c7
Reduced timeout for common UART communication:
ideenmodellierer
parents:
400
diff
changeset
|
2124 HAL_UART_Receive(&UartHandle, (uint8_t*)TxBuffer, CmdSize, UART_OPERATION_TIMEOUT); |
396 | 2125 } |
2126 | |
2127 result = tComm_CheckAnswerOK(); | |
2128 | |
2129 | |
2130 if((BmTmpConfig == BM_CONFIG_BAUD) && (result == HAL_OK) && (UartHandle.Init.BaudRate != 460800)) /* is com already switched to fast speed? */ | |
2131 { | |
2132 HAL_UART_DeInit(&UartHandle); | |
2133 HAL_Delay(1); | |
2134 UartHandle.Init.BaudRate = 460800; | |
2135 HAL_UART_Init(&UartHandle); | |
2136 } | |
2137 if(result == HAL_OK) | |
2138 { | |
2139 BmTmpConfig++; | |
2140 } | |
2141 if(BmTmpConfig == BM_CONFIG_DONE) | |
2142 { | |
2143 ConfigRetryCnt = 0; | |
2144 } | |
2145 } | |
2146 } | |
2147 if(result != HAL_OK) | |
2148 { | |
2149 ConfigRetryCnt++; | |
2150 if(ConfigRetryCnt > 3) /* Configuration failed => switch off module */ | |
2151 { | |
2152 MX_Bluetooth_PowerOff(); | |
400 | 2153 if(RestartModule) |
2154 { | |
2155 RestartModule = 0; /* only one try */ | |
2156 ConfigRetryCnt = 200; /* used for delay to startup module again */ | |
2157 BmTmpConfig = BM_CONFIG_RETRY; | |
2158 } | |
2159 else /* even restarting module failed => switch bluetooth off */ | |
2160 { | |
2161 ConfigRetryCnt = 0; | |
2162 BmTmpConfig = BM_CONFIG_OFF; | |
2163 settingsGetPointer()->bluetoothActive = 0; | |
2164 } | |
396 | 2165 } |
2166 } | |
2167 return result; | |
2168 } | |
2169 | |
38 | 2170 static void tComm_Error_Handler(void) |
2171 { | |
2172 while(1) | |
2173 {} | |
2174 } |