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