comparison 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
comparison
equal deleted inserted replaced
395:eb7696e0510f 396:effa6fb9eb89
82 #endif 82 #endif
83 83
84 #ifdef SPECIALPROGRAMM 84 #ifdef SPECIALPROGRAMM
85 # include "firmwareEraseProgram.h" 85 # include "firmwareEraseProgram.h"
86 #endif 86 #endif
87 87 #include <stdlib.h>
88 #include <string.h> 88 #include <string.h>
89 89
90 90
91 /* Private variables ---------------------------------------------------------*/ 91 /* Private variables ---------------------------------------------------------*/
92 GFX_DrawCfgScreen tCscreen; 92 GFX_DrawCfgScreen tCscreen;
112 const uint8_t id_Region1_firmware = 0xFF; 112 const uint8_t id_Region1_firmware = 0xFF;
113 const uint8_t id_RTE = 0xFE; 113 const uint8_t id_RTE = 0xFE;
114 const uint8_t id_FONT = 0x10; 114 const uint8_t id_FONT = 0x10;
115 const uint8_t id_FONT_OLD = 0x00; 115 const uint8_t id_FONT_OLD = 0x00;
116 116
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 */
117 /* Private function prototypes -----------------------------------------------*/ 120 /* Private function prototypes -----------------------------------------------*/
118 121 static void tComm_Disconnect(void);
119 static void tComm_Error_Handler(void); 122 static void tComm_Error_Handler(void);
120 static uint8_t select_mode(uint8_t aRxByte); 123 static uint8_t select_mode(uint8_t aRxByte);
124 static uint8_t tComm_CheckAnswerOK(void);
125 static uint8_t tComm_HandleBlueModConfig(void);
126 static void tComm_EvaluateBluetoothStrength(void);
121 uint8_t receive_update_flex(uint8_t isRTEupdateALLOWED); 127 uint8_t receive_update_flex(uint8_t isRTEupdateALLOWED);
122 uint8_t receive_update_data_flex(uint8_t* pBuffer1, uint8_t* pBuffer2, uint8_t RTEupdateALLOWED); 128 uint8_t receive_update_data_flex(uint8_t* pBuffer1, uint8_t* pBuffer2, uint8_t RTEupdateALLOWED);
123 uint8_t receive_update_data_mainCPU_firmware(void); 129 uint8_t receive_update_data_mainCPU_firmware(void);
124 uint8_t receive_update_data_mainCPU_variable_firmware(void); 130 uint8_t receive_update_data_mainCPU_variable_firmware(void);
125 uint8_t receive_update_data_mainCPU_firmware_subroutine(uint8_t region, uint8_t* pBuffer1, uint8_t* pBuffer2); 131 uint8_t receive_update_data_mainCPU_firmware_subroutine(uint8_t region, uint8_t* pBuffer1, uint8_t* pBuffer2);
160 tCwindow.WindowY0 = 479 - 390; 166 tCwindow.WindowY0 = 479 - 390;
161 tCwindow.WindowY1 = 479 - 25; 167 tCwindow.WindowY1 = 479 - 25;
162 } 168 }
163 169
164 StartListeningToUART = 1; 170 StartListeningToUART = 1;
165
166 /* WHY?
167 #ifdef BLE_NENABLE_PIN
168 HAL_GPIO_WritePin(BLE_NENABLE_GPIO_PORT,BLE_NENABLE_PIN,GPIO_PIN_RESET);
169 #endif
170 */
171 } 171 }
172 172
173 uint8_t tComm_control(void) 173 uint8_t tComm_control(void)
174 { 174 {
175 uint8_t answer = 0; 175 uint8_t answer = 0;
178 /* should do something like reset UART ... */ 178 /* should do something like reset UART ... */
179 if( settingsGetPointer()->bluetoothActive == 0) 179 if( settingsGetPointer()->bluetoothActive == 0)
180 { 180 {
181 if(bluetoothActiveLastTime) 181 if(bluetoothActiveLastTime)
182 { 182 {
183 HAL_UART_AbortReceive_IT(&UartHandle);
183 HAL_UART_DeInit(&UartHandle); 184 HAL_UART_DeInit(&UartHandle);
184 HAL_Delay(1); 185 HAL_Delay(1);
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 */
185 HAL_UART_Init(&UartHandle); 188 HAL_UART_Init(&UartHandle);
186 HAL_Delay(1); 189 HAL_Delay(1);
187 UartReady = RESET; 190 UartReady = RESET;
188 StartListeningToUART = 1; 191 StartListeningToUART = 1;
189 bluetoothActiveLastTime = 0; 192 bluetoothActiveLastTime = 0;
190 receiveStartByteUart = 0; 193 receiveStartByteUart = 0;
194 RequestDisconnection = 0;
191 } 195 }
192 return 0; 196 return 0;
193 } 197 }
194 else 198 else
195 { 199 {
196 bluetoothActiveLastTime = 1; 200 bluetoothActiveLastTime = 1;
201 if(RequestDisconnection)
202 {
203 RequestDisconnection = 0;
204 tComm_Disconnect();
205 }
197 } 206 }
198 207
199 #endif 208 #endif
200 209
210 if(BmTmpConfig != BM_CONFIG_DONE)
211 {
212 tComm_HandleBlueModConfig();
213 }
214 else
215 {
201 /*##-2- Put UART peripheral in reception process ###########################*/ 216 /*##-2- Put UART peripheral in reception process ###########################*/
202 217
203 if((UartReady == RESET) && StartListeningToUART) 218 if((UartReady == RESET) && StartListeningToUART)
204 { 219 {
205 StartListeningToUART = 0; 220 StartListeningToUART = 0;
206 if(HAL_UART_Receive_IT(&UartHandle, &receiveStartByteUart, 1) != HAL_OK) 221 if(HAL_UART_Receive_IT(&UartHandle, &receiveStartByteUart, 1) != HAL_OK)
207 tComm_Error_Handler(); 222 tComm_Error_Handler();
208 } 223 }
209 /* Reset transmission flag */ 224 /* Reset transmission flag */
210 if(UartReady == SET) 225 if(UartReady == SET)
211 { 226 {
212 UartReady = RESET; 227 UartReady = RESET;
213 if((receiveStartByteUart == BYTE_DOWNLOAD_MODE) || (receiveStartByteUart == BYTE_SERVICE_MODE)) 228 if((receiveStartByteUart == BYTE_DOWNLOAD_MODE) || (receiveStartByteUart == BYTE_SERVICE_MODE))
214 answer = openComm(receiveStartByteUart); 229 answer = openComm(receiveStartByteUart);
215 StartListeningToUART = 1; 230 StartListeningToUART = 1;
216 return answer; 231 return answer;
232 }
217 } 233 }
218 return 0; 234 return 0;
219 } 235 }
220 236
221 237
224 if(tCscreen.FBStartAdress == 0) 240 if(tCscreen.FBStartAdress == 0)
225 { 241 {
226 GFX_hwBackgroundOn(); 242 GFX_hwBackgroundOn();
227 tCscreen.FBStartAdress = getFrame(18); 243 tCscreen.FBStartAdress = getFrame(18);
228 write_content_simple(&tCscreen, 0, 800, 480-24, &FontT24,"Exit",CLUT_ButtonSurfaceScreen); 244 write_content_simple(&tCscreen, 0, 800, 480-24, &FontT24,"Exit",CLUT_ButtonSurfaceScreen);
245 write_content_simple(&tCscreen, 800 - 70, 800, 480-24, &FontT24,"Signal",CLUT_ButtonSurfaceScreen);
246
229 if(receiveStartByteUart == BYTE_SERVICE_MODE) 247 if(receiveStartByteUart == BYTE_SERVICE_MODE)
230 GFX_write_string(&FontT48, &tCwindow, "Service mode enabled",2); 248 GFX_write_string(&FontT48, &tCwindow, "Service mode enabled",2);
231 else 249 else
232 GFX_write_string(&FontT48, &tCwindow, "Download mode enabled",2); 250 GFX_write_string(&FontT48, &tCwindow, "Download mode enabled",2);
233 GFX_SetFramesTopBottom(tCscreen.FBStartAdress, 0,480); 251 GFX_SetFramesTopBottom(tCscreen.FBStartAdress, 0,480);
238 { 256 {
239 display_text[(uint8_t)display_text[255]] = 0; 257 display_text[(uint8_t)display_text[255]] = 0;
240 releaseFrame(18,tCscreen.FBStartAdress); 258 releaseFrame(18,tCscreen.FBStartAdress);
241 tCscreen.FBStartAdress = getFrame(18); 259 tCscreen.FBStartAdress = getFrame(18);
242 write_content_simple(&tCscreen, 0, 800, 480-24, &FontT24,"Exit",CLUT_ButtonSurfaceScreen); 260 write_content_simple(&tCscreen, 0, 800, 480-24, &FontT24,"Exit",CLUT_ButtonSurfaceScreen);
261 write_content_simple(&tCscreen, 800 - 70, 800, 480-24, &FontT24,"Signal",CLUT_ButtonSurfaceScreen);
243 GFX_write_string(&FontT48, &tCwindow, display_text,2); 262 GFX_write_string(&FontT48, &tCwindow, display_text,2);
244 GFX_SetFrameTop(tCscreen.FBStartAdress); 263 GFX_SetFrameTop(tCscreen.FBStartAdress);
245 display_text[0] = 0; 264 display_text[0] = 0;
246 display_text[255] = 0; 265 display_text[255] = 0;
247 } 266 }
291 setForcedBluetoothName = 0; 310 setForcedBluetoothName = 0;
292 MX_Bluetooth_PowerOff(); 311 MX_Bluetooth_PowerOff();
293 HAL_Delay(1000); 312 HAL_Delay(1000);
294 MX_Bluetooth_PowerOn(); 313 MX_Bluetooth_PowerOn();
295 tComm_Set_Bluetooth_Name(1); 314 tComm_Set_Bluetooth_Name(1);
315 tComm_StartBlueModConfig();
296 } 316 }
297 #ifndef BOOTLOADER_STANDALONE 317 #ifndef BOOTLOADER_STANDALONE
298 if(updateSettingsAndMenuOnExit) 318 if(updateSettingsAndMenuOnExit)
299 { 319 {
300 check_and_correct_settings(); 320 check_and_correct_settings();
351 371
352 char aTxBufferEscapeSequence[50] = "+++"; 372 char aTxBufferEscapeSequence[50] = "+++";
353 // limit is 19 chars, with 7 chars shown in BLE advertising mode 373 // limit is 19 chars, with 7 chars shown in BLE advertising mode
354 //________________________123456789012345678901 374 //________________________123456789012345678901
355 char aTxBufferName[50] = "AT+BNAME=OSTC4-12345\r"; 375 char aTxBufferName[50] = "AT+BNAME=OSTC4-12345\r";
356 char answerOkay[6] = "\r\nOKr\n"; 376 char answerOkay[6] = "\r\nOK\r\n";
357 377
358 gfx_number_to_string(5,1,&aTxBufferName[15],serial); 378 gfx_number_to_string(5,1,&aTxBufferName[15],serial);
359 379
360 // store active configuration in non-volatile memory 380 // store active configuration in non-volatile memory
361 char aTxBufferWrite[50] = "AT&W\r"; 381 char aTxBufferWrite[50] = "AT&W\r";
406 426
407 answer = HAL_OK; 427 answer = HAL_OK;
408 return answer; 428 return answer;
409 } 429 }
410 430
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
411 477
412 uint8_t openComm(uint8_t aRxByte) 478 uint8_t openComm(uint8_t aRxByte)
413 { 479 {
414 SStateList status; 480 SStateList status;
481 uint8_t localRx;
415 uint8_t timeoutCounter = 0; 482 uint8_t timeoutCounter = 0;
416 uint8_t answer = 0; 483 uint8_t answer = 0;
417 uint8_t service_mode_last_three_bytes[3]; 484 uint8_t service_mode_last_three_bytes[3];
418 uint8_t service_mode_response[5] = 485 uint8_t service_mode_response[5] =
419 { 486 {
436 503
437 /* service mode is four bytes 504 /* service mode is four bytes
438 0xAA 0xAB 0xCD 0xEF 505 0xAA 0xAB 0xCD 0xEF
439 answer is 506 answer is
440 */ 507 */
508 localRx = aRxByte;
441 509
442 if(aRxByte == BYTE_SERVICE_MODE) 510 if(aRxByte == BYTE_SERVICE_MODE)
443 { 511 {
444 if((HAL_UART_Receive(&UartHandle, (uint8_t*)service_mode_last_three_bytes, 3, 2000)!= HAL_OK)) 512 if((HAL_UART_Receive(&UartHandle, (uint8_t*)service_mode_last_three_bytes, 3, 2000)!= HAL_OK))
445 answer = 0x00; 513 answer = 0x00;
461 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)download_mode_response, 2, 2000)!= HAL_OK) 529 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)download_mode_response, 2, 2000)!= HAL_OK)
462 answer = 0x00; 530 answer = 0x00;
463 else 531 else
464 answer = prompt4D4C(receiveStartByteUart); 532 answer = prompt4D4C(receiveStartByteUart);
465 } 533 }
466 /* 534
467 uint8_t debug[256] = {0}; 535 while((answer == prompt4D4C(receiveStartByteUart)) && (timeoutCounter < UART_TIMEOUT_SECONDS)) /* try receive once a second */
468 uint8_t dbgptr = 0; 536 {
469 debug[dbgptr++] = aRxByte; 537 if(HAL_UART_Receive(&UartHandle, (uint8_t*)&aRxByte, 1, 1000)!= HAL_OK)
470 */
471 while((answer == prompt4D4C(receiveStartByteUart)) && (timeoutCounter < UART_TIMEOUT_SECONDS * 2)) /* Split 120 seconds timeout into 240 iterations a 500ms */
472 {
473 if(HAL_UART_Receive(&UartHandle, (uint8_t*)&aRxByte, 1, 500)!= HAL_OK) /* Timeout half a second */
474 { 538 {
475 timeoutCounter++; 539 timeoutCounter++;
476 get_globalStateList(&status); 540 get_globalStateList(&status);
477 if (status.base != BaseComm) 541 if (status.base != BaseComm)
478 { 542 {
479 timeoutCounter = UART_TIMEOUT_SECONDS * 2; /* Abort action triggered outside main loop => exit */ 543 timeoutCounter = UART_TIMEOUT_SECONDS; /* Abort action triggered outside main loop => exit */
544 }
545 if(EvaluateBluetoothSignalStrength)
546 {
547 tComm_EvaluateBluetoothStrength();
480 } 548 }
481 } 549 }
482 else 550 else
483 { 551 {
484 // debug[dbgptr++] = aRxByte; 552 answer = select_mode(localRx);
485 answer = select_mode(aRxByte); 553 timeoutCounter = 0;
486 } 554 }
487 } 555 }
488 set_returnFromComm(); 556 set_returnFromComm();
489 return 1; 557 return 1;
490 } 558 }
520 aTxBuffer[0] = type; 588 aTxBuffer[0] = type;
521 aTxBuffer[1] = prompt4D4C(receiveStartByteUart); 589 aTxBuffer[1] = prompt4D4C(receiveStartByteUart);
522 uint8_t tempHigh, tempLow; 590 uint8_t tempHigh, tempLow;
523 count = 0; 591 count = 0;
524 592
593 // Ignore communication on Text like RING, CONNECT,
594 if(type < 0x60) return prompt4D4C(receiveStartByteUart);
595
525 // service mode only commands 596 // service mode only commands
526 if(receiveStartByteUart == BYTE_SERVICE_MODE) 597 if(receiveStartByteUart == BYTE_SERVICE_MODE)
527 { 598 {
528 // first part 599 // first part
529 switch(type) 600 switch(type)
559 uint8_t logStepBackwards = 0; 630 uint8_t logStepBackwards = 0;
560 convert16_Type totalDiveCount; 631 convert16_Type totalDiveCount;
561 logCopyDataLength.u32bit = 0; 632 logCopyDataLength.u32bit = 0;
562 totalDiveCount.u16bit = 0; 633 totalDiveCount.u16bit = 0;
563 #endif 634 #endif
564 635 // Exit communication on 0xFF command
565 // Exit communication on Text like RING, CONNECT, ... or 0xFF command 636 if(type == 0xFF) return 0;
566 if((type < 0x60) || (type == 0xFF))
567 return 0;
568 637
569 // return of command for (almost) all commands 638 // return of command for (almost) all commands
570 switch(type) 639 switch(type)
571 { 640 {
572 // not supported yet case 0x20: // send hi:lo:temp1 bytes starting from ext_flash_address:3 641 // not supported yet case 0x20: // send hi:lo:temp1 bytes starting from ext_flash_address:3
858 case 0x63: // set custom text 927 case 0x63: // set custom text
859 case 0x66: // get dive profile 928 case 0x66: // get dive profile
860 case 0x69: // get serial, old version numbering, custom text 929 case 0x69: // get serial, old version numbering, custom text
861 case 0x6A: // get model 930 case 0x6A: // get model
862 case 0x6B: // get specific firmware version 931 case 0x6B: // get specific firmware version
932 case 0x6C: /* Display Bluetooth signal strength */
863 case 0x6D: // get all compact headers (16 byte) 933 case 0x6D: // get all compact headers (16 byte)
864 case 0x6E: // display text 934 case 0x6E: // display text
865 case 0x70: // read min, default, max setting 935 case 0x70: // read min, default, max setting
866 case 0x72: // read setting 936 case 0x72: // read setting
867 case 0x77: // write setting 937 case 0x77: // write setting
1002 */ 1072 */
1003 // prompt 1073 // prompt
1004 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); 1074 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart);
1005 break; 1075 break;
1006 1076
1077 /* Trigger Bluetooth signal strength evaluation */
1078 case 0x6C: tComm_EvaluateBluetoothStrength();
1079 aTxBuffer[count++] = prompt4D4C(receiveStartByteUart);
1080 break;
1007 // display text 1081 // display text
1008 case 0x6E: 1082 case 0x6E:
1009 for(int i=0;i<16;i++) 1083 for(int i=0;i<16;i++)
1010 display_text[i] = aRxBuffer[i]; 1084 display_text[i] = aRxBuffer[i];
1011 display_text[15] = 0; 1085 display_text[15] = 0;
1200 return prompt4D4C(receiveStartByteUart); 1274 return prompt4D4C(receiveStartByteUart);
1201 } 1275 }
1202 return 0; 1276 return 0;
1203 } 1277 }
1204 1278
1279 #define BLOCKSIZE 0x1000
1205 1280
1206 HAL_StatusTypeDef receive_uart_large_size(UART_HandleTypeDef *huart, uint8_t *pData, uint32_t Size) 1281 HAL_StatusTypeDef receive_uart_large_size(UART_HandleTypeDef *huart, uint8_t *pData, uint32_t Size)
1207 { 1282 {
1208 uint16_t length_16k_blocks; 1283 uint16_t length_4k_blocks;
1209 uint16_t length_16k_remainder; 1284 uint16_t length_4k_remainder;
1210 uint32_t temp; 1285 uint32_t temp;
1211 HAL_StatusTypeDef result = HAL_OK; 1286 HAL_StatusTypeDef result = HAL_OK;
1212 uint32_t pDataLocal; 1287 uint32_t pDataLocal;
1213 1288
1214 length_16k_blocks = (uint16_t) (Size / 0x3FFF); 1289 length_4k_blocks = (uint16_t) (Size / BLOCKSIZE);
1215 temp = length_16k_blocks; 1290 temp = length_4k_blocks;
1216 temp *= 0x3FFF; 1291 temp *= BLOCKSIZE;
1217 length_16k_remainder = (uint16_t) ( Size - temp); 1292 length_4k_remainder = (uint16_t) ( Size - temp);
1218 1293
1219 pDataLocal = (uint32_t)pData; 1294 pDataLocal = (uint32_t)pData;
1220 1295
1221 1296
1222 while((result == HAL_OK) && length_16k_blocks) 1297 while((result == HAL_OK) && length_4k_blocks)
1223 { 1298 {
1224 result = HAL_UART_Receive(&UartHandle, (uint8_t *)pDataLocal, 0x3FFF , UART_TIMEOUT_LARGE_BLOCK); 1299 result = HAL_UART_Receive(&UartHandle, (uint8_t *)pDataLocal, BLOCKSIZE , UART_TIMEOUT_LARGE_BLOCK);
1225 pDataLocal += 0x3FFF; 1300 pDataLocal += BLOCKSIZE;
1226 length_16k_blocks--; 1301 length_4k_blocks--;
1227 } 1302 }
1228 if((result == HAL_OK) && length_16k_remainder) 1303
1229 { 1304 if((result == HAL_OK) && length_4k_remainder)
1230 result = HAL_UART_Receive(&UartHandle, (uint8_t *)pDataLocal, length_16k_remainder , UART_TIMEOUT_LARGE_BLOCK); 1305 {
1306 result = HAL_UART_Receive(&UartHandle, (uint8_t *)pDataLocal, length_4k_remainder , UART_TIMEOUT_LARGE_BLOCK);
1231 } 1307 }
1232 return result; 1308 return result;
1233 } 1309 }
1234 1310
1235 1311
1831 display_text[255] = 21; 1907 display_text[255] = 21;
1832 return 1; 1908 return 1;
1833 } 1909 }
1834 } 1910 }
1835 1911
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
1836 static void tComm_Error_Handler(void) 2125 static void tComm_Error_Handler(void)
1837 { 2126 {
1838 while(1) 2127 while(1)
1839 {} 2128 {}
1840 } 2129 }