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