# HG changeset patch # User Ideenmodellierer # Date 1577608936 -3600 # Node ID 4151fddb1e34814c8c50d1c519d1429ee5c84e6f # Parent 7bcbbaa42cbf6a63ff8f3eb921f8bc259cee7d77# Parent cad6f8dfacabc7558320046cd226be7888521392 merged default into ImproveBluetooth diff -r cad6f8dfacab -r 4151fddb1e34 Common/CPU1-F429.ld --- a/Common/CPU1-F429.ld Mon Dec 02 07:56:47 2019 +0000 +++ b/Common/CPU1-F429.ld Sun Dec 29 09:42:16 2019 +0100 @@ -98,6 +98,7 @@ SECTIONS { /******************* NVIC reset and interupt vectors **********************/ + .isr_vector 0x08040000 : { . = ALIGN(4); @@ -106,30 +107,12 @@ } >ROM - /* PROVIDE( firmware_FirmwareData = . );*/ - .firmware_FirmwareData 0x08050000 : { - KEEP( *(.firmware_firmware_data) ) - } >ROM - - .font_firmware_data 0x08132000 : { - KEEP( *(.font_firmware_data) ) - - } >UPPER - - .text 0x08054000 : + .StdLibs 0x08040200: { - . = ALIGN(4); - *(.text) /* .text sections (code) */ - *(.text*) /* .text* sections (code) */ - *(.glue_7) /* glue arm to thumb code */ - *(.glue_7t) /* glue thumb to arm code */ - *(.eh_frame) - . = ALIGN(4); - _etext = .; /* define a global symbols at end of code */ + . = ALIGN(4); + *libm.a:*(*) } >ROM - - /********************** Constant data into ROM memory *********************/ .rodata : { . = ALIGN(4); @@ -138,6 +121,9 @@ . = ALIGN(4); } >ROM + /********************** Constant data into ROM memory *********************/ + + .ARM.extab : { . = ALIGN(4); *(.ARM.extab* .gnu.linkonce.armextab.*) @@ -184,6 +170,31 @@ /************* The program code and other data into ROM memory ************/ + .firmware_FirmwareData 0x08050000 : { + KEEP( *(.firmware_firmware_data) ) + } >ROM + + .font_firmware_data 0x08132000 : { + KEEP( *(.font_firmware_data) ) + + } >UPPER + + + .text 0x08054000 : + { + . = ALIGN(4); + *(.text) /* .text sections (code) */ + *(.text*) /* .text* sections (code) */ + *(.glue_7) /* glue arm to thumb code */ + *(.glue_7t) /* glue thumb to arm code */ + *(.eh_frame) + . = ALIGN(4); + _etext = .; /* define a global symbols at end of code */ + } >ROM + + + + /************* Data and Structures at absolute address in ROM *************/ diff -r cad6f8dfacab -r 4151fddb1e34 Discovery/Inc/tComm.h --- a/Discovery/Inc/tComm.h Mon Dec 02 07:56:47 2019 +0000 +++ b/Discovery/Inc/tComm.h Sun Dec 29 09:42:16 2019 +0100 @@ -31,6 +31,20 @@ /* Includes ------------------------------------------------------------------*/ #include + +/* types */ +typedef enum +{ + BM_CONFIG_OFF = 0, + BM_CONFIG_ECHO, + BM_CONFIG_ESCAPE_DELAY, + BM_CONFIG_SIGNAL_POLL, + BM_CONFIG_BAUD, + BM_CONFIG_SILENCE, + BM_CONFIG_DONE, + BM_CONFIG_RETRY +} BlueModTmpConfig_t; + /* Exported functions --------------------------------------------------------*/ void tComm_init(void); @@ -39,5 +53,7 @@ void tComm_exit(void); void tComm_verlauf(uint8_t percentage_complete); uint8_t tComm_Set_Bluetooth_Name(uint8_t force); +void tComm_StartBlueModConfig(void); +void tComm_RequestBluetoothStrength(void); #endif /* TCOMM_H */ diff -r cad6f8dfacab -r 4151fddb1e34 Discovery/Src/base.c --- a/Discovery/Src/base.c Mon Dec 02 07:56:47 2019 +0000 +++ b/Discovery/Src/base.c Sun Dec 29 09:42:16 2019 +0100 @@ -362,6 +362,7 @@ MX_GPIO_Init(); // MX_SmallCPU_NO_Reset_Helper(); //161116 hw + MX_Bluetooth_PowerOff(); /* disable module, needed in case of e.g. a reset event to make sure module is configured from scratch */ MX_SPI_Init(); MX_UART_Init(); SDRAM_Config(); @@ -386,12 +387,7 @@ // new 170508: bluetooth on at start settingsGetPointer()->bluetoothActive = 1; MX_Bluetooth_PowerOn(); - - // Haase Geburtstag: - // settingsGetPointer()->serialHigh = (3012 / 256); - // settingsGetPointer()->serialLow = (3012 & 0xFF); - - // settingsGetPointer()->showDebugInfo = 1; + tComm_StartBlueModConfig(); /* if( (hardwareDataGetPointer()->primarySerial == 20+18) @@ -483,7 +479,7 @@ } deco_loop(); TriggerButtonAction(); - if(DoDisplayRefresh) + if(DoDisplayRefresh) /* set every 100ms by timer interrupt */ { DoDisplayRefresh = 0; RefreshDisplay(); @@ -1017,10 +1013,14 @@ get_globalStateList(&status); if(status.base == BaseComm) /* main loop is not serviced in comm mode => react immediately */ { - if (action == ACTION_BUTTON_BACK) { - settingsGetPointer()->bluetoothActive = 0; - MX_Bluetooth_PowerOff(); - tComm_exit(); + switch(action) + { + case ACTION_BUTTON_BACK: tComm_exit(); + break; + case ACTION_BUTTON_NEXT: tComm_RequestBluetoothStrength(); + break; + default: + break; } } else diff -r cad6f8dfacab -r 4151fddb1e34 Discovery/Src/stm32f4xx_hal_msp_hw2.c --- a/Discovery/Src/stm32f4xx_hal_msp_hw2.c Mon Dec 02 07:56:47 2019 +0000 +++ b/Discovery/Src/stm32f4xx_hal_msp_hw2.c Sun Dec 29 09:42:16 2019 +0100 @@ -578,17 +578,19 @@ PA11 ------> USART1_CTS PA12 ------> USART1_RTS */ -#ifdef USARTx_CTS_PIN - GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12; -#else GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10; -#endif GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FAST;//GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF7_USART1; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); +#ifdef USARTx_CTS_PIN /* config control flow pins */ + GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); +#endif + HAL_NVIC_SetPriority(USART1_IRQn, 0, 1); HAL_NVIC_EnableIRQ(USART1_IRQn); } diff -r cad6f8dfacab -r 4151fddb1e34 Discovery/Src/t7.c --- a/Discovery/Src/t7.c Mon Dec 02 07:56:47 2019 +0000 +++ b/Discovery/Src/t7.c Sun Dec 29 09:42:16 2019 +0100 @@ -2123,7 +2123,7 @@ GFX_write_string(&FontT105,&t7l2,TextL2,1); /* ascent rate graph */ - if(stateUsed->lifeData.ascent_rate_meter_per_min > 0) + if(stateUsed->lifeData.ascent_rate_meter_per_min > 1) /* a value < 1 would cause a bar in negative direction brush rectangle of 12 and step width of 6 */ { if(!pSettings->FlipDisplay) { diff -r cad6f8dfacab -r 4151fddb1e34 Discovery/Src/tComm.c --- a/Discovery/Src/tComm.c Mon Dec 02 07:56:47 2019 +0000 +++ b/Discovery/Src/tComm.c Sun Dec 29 09:42:16 2019 +0100 @@ -84,7 +84,7 @@ #ifdef SPECIALPROGRAMM # include "firmwareEraseProgram.h" #endif - +#include #include @@ -107,17 +107,25 @@ #define BYTE_SERVICE_MODE (0xAA) #define UART_TIMEOUT_SECONDS (120u) /* Timeout for keeping connection open and waiting for data */ -#define UART_TIMEOUT_LARGE_BLOCK (6000u) /* Timeout (ms) for receiption of an 16K data block (typical RX time ~4,5seconds) */ +#define UART_TIMEOUT_LARGE_BLOCK (6000u) /* Timeout (ms) for reception of an 16K data block (typical RX time ~4,5seconds) */ + +#define UART_CMD_BUF_SIZE (20u) /* size of buffer for command exchange */ const uint8_t id_Region1_firmware = 0xFF; const uint8_t id_RTE = 0xFE; const uint8_t id_FONT = 0x10; const uint8_t id_FONT_OLD = 0x00; +static BlueModTmpConfig_t BmTmpConfig = BM_CONFIG_OFF; /* Config BlueMod without storing the changes */ +static uint8_t EvaluateBluetoothSignalStrength = 0; +static uint8_t RequestDisconnection = 0; /* Disconnection from remote device requested */ /* Private function prototypes -----------------------------------------------*/ - +static void tComm_Disconnect(void); static void tComm_Error_Handler(void); static uint8_t select_mode(uint8_t aRxByte); +static uint8_t tComm_CheckAnswerOK(void); +static uint8_t tComm_HandleBlueModConfig(void); +static void tComm_EvaluateBluetoothStrength(void); uint8_t receive_update_flex(uint8_t isRTEupdateALLOWED); uint8_t receive_update_data_flex(uint8_t* pBuffer1, uint8_t* pBuffer2, uint8_t RTEupdateALLOWED); uint8_t receive_update_data_mainCPU_firmware(void); @@ -162,12 +170,6 @@ } StartListeningToUART = 1; - -/* WHY? -#ifdef BLE_NENABLE_PIN - HAL_GPIO_WritePin(BLE_NENABLE_GPIO_PORT,BLE_NENABLE_PIN,GPIO_PIN_RESET); -#endif -*/ } uint8_t tComm_control(void) @@ -180,40 +182,56 @@ { if(bluetoothActiveLastTime) { + HAL_UART_AbortReceive_IT(&UartHandle); HAL_UART_DeInit(&UartHandle); HAL_Delay(1); + UartHandle.Init.BaudRate = 115200; /* Module will be operating at default baud rate if powered again */ + BmTmpConfig = BM_CONFIG_OFF; /* Restart configuration if powered again */ HAL_UART_Init(&UartHandle); HAL_Delay(1); UartReady = RESET; StartListeningToUART = 1; bluetoothActiveLastTime = 0; receiveStartByteUart = 0; + RequestDisconnection = 0; } return 0; } else { bluetoothActiveLastTime = 1; + if(RequestDisconnection) + { + RequestDisconnection = 0; + tComm_Disconnect(); + } } #endif + if(BmTmpConfig != BM_CONFIG_DONE) + { + tComm_HandleBlueModConfig(); + } + else + { /*##-2- Put UART peripheral in reception process ###########################*/ - if((UartReady == RESET) && StartListeningToUART) - { - StartListeningToUART = 0; - if(HAL_UART_Receive_IT(&UartHandle, &receiveStartByteUart, 1) != HAL_OK) - tComm_Error_Handler(); - } - /* Reset transmission flag */ - if(UartReady == SET) - { - UartReady = RESET; - if((receiveStartByteUart == BYTE_DOWNLOAD_MODE) || (receiveStartByteUart == BYTE_SERVICE_MODE)) - answer = openComm(receiveStartByteUart); - StartListeningToUART = 1; - return answer; + if((UartReady == RESET) && StartListeningToUART) + { + StartListeningToUART = 0; + if(HAL_UART_Receive_IT(&UartHandle, &receiveStartByteUart, 1) != HAL_OK) + tComm_Error_Handler(); + } + /* Reset transmission flag */ + if(UartReady == SET) + { + UartReady = RESET; + if((receiveStartByteUart == BYTE_DOWNLOAD_MODE) || (receiveStartByteUart == BYTE_SERVICE_MODE)) + answer = openComm(receiveStartByteUart); + StartListeningToUART = 1; + return answer; + } } return 0; } @@ -226,6 +244,8 @@ GFX_hwBackgroundOn(); tCscreen.FBStartAdress = getFrame(18); write_content_simple(&tCscreen, 0, 800, 480-24, &FontT24,"Exit",CLUT_ButtonSurfaceScreen); + write_content_simple(&tCscreen, 800 - 70, 800, 480-24, &FontT24,"Signal",CLUT_ButtonSurfaceScreen); + if(receiveStartByteUart == BYTE_SERVICE_MODE) GFX_write_string(&FontT48, &tCwindow, "Service mode enabled",2); else @@ -240,6 +260,7 @@ releaseFrame(18,tCscreen.FBStartAdress); tCscreen.FBStartAdress = getFrame(18); write_content_simple(&tCscreen, 0, 800, 480-24, &FontT24,"Exit",CLUT_ButtonSurfaceScreen); + write_content_simple(&tCscreen, 800 - 70, 800, 480-24, &FontT24,"Signal",CLUT_ButtonSurfaceScreen); GFX_write_string(&FontT48, &tCwindow, display_text,2); GFX_SetFrameTop(tCscreen.FBStartAdress); display_text[0] = 0; @@ -293,6 +314,7 @@ HAL_Delay(1000); MX_Bluetooth_PowerOn(); tComm_Set_Bluetooth_Name(1); + tComm_StartBlueModConfig(); } #ifndef BOOTLOADER_STANDALONE if(updateSettingsAndMenuOnExit) @@ -353,7 +375,7 @@ // limit is 19 chars, with 7 chars shown in BLE advertising mode //________________________123456789012345678901 char aTxBufferName[50] = "AT+BNAME=OSTC4-12345\r"; - char answerOkay[6] = "\r\nOKr\n"; + char answerOkay[6] = "\r\nOK\r\n"; gfx_number_to_string(5,1,&aTxBufferName[15],serial); @@ -408,10 +430,57 @@ return answer; } +void tComm_Disconnect() +{ + uint8_t answer; + uint8_t retrycnt = 3; + char aTxDisconnect[] ="ATH\r"; + char aTxBufferEnd[] = "ATO\r"; + char aTxBufferEscapeSequence[] = "+++"; + + uint8_t sizeDisconnect = sizeof(aTxDisconnect) -1; + + HAL_UART_AbortReceive_IT(&UartHandle); + do + { + HAL_Delay(200); + if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBufferEscapeSequence, 3, 1000)== HAL_OK) + { + answer = tComm_CheckAnswerOK(); + } + retrycnt--; + } + while((answer != HAL_OK) && (retrycnt > 0)); + + if(answer == HAL_OK) + { + answer = HAL_ERROR; + if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxDisconnect,sizeDisconnect , 1000)== HAL_OK) + { + answer = HAL_ERROR; + if(tComm_CheckAnswerOK() == HAL_OK) + { + answer = HAL_ERROR; + if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBufferEnd, 4, 1000) == HAL_OK) /* exit terminal mode */ + { + answer = tComm_CheckAnswerOK(); + } + } + } + } + + if(answer != HAL_OK) /* we are somehow not able to do a clean disconnect => fallback to "previous" power off implementation" */ + { + settingsGetPointer()->bluetoothActive = 0; + MX_Bluetooth_PowerOff(); + } +} + uint8_t openComm(uint8_t aRxByte) { SStateList status; + uint8_t localRx; uint8_t timeoutCounter = 0; uint8_t answer = 0; uint8_t service_mode_last_three_bytes[3]; @@ -438,6 +507,7 @@ 0xAA 0xAB 0xCD 0xEF answer is */ + localRx = aRxByte; if(aRxByte == BYTE_SERVICE_MODE) { @@ -463,26 +533,26 @@ else answer = prompt4D4C(receiveStartByteUart); } -/* -uint8_t debug[256] = {0}; -uint8_t dbgptr = 0; -debug[dbgptr++] = aRxByte; -*/ - while((answer == prompt4D4C(receiveStartByteUart)) && (timeoutCounter < UART_TIMEOUT_SECONDS * 2)) /* Split 120 seconds timeout into 240 iterations a 500ms */ + + while((answer == prompt4D4C(receiveStartByteUart)) && (timeoutCounter < UART_TIMEOUT_SECONDS)) /* try receive once a second */ { - if(HAL_UART_Receive(&UartHandle, (uint8_t*)&aRxByte, 1, 500)!= HAL_OK) /* Timeout half a second */ + if(HAL_UART_Receive(&UartHandle, (uint8_t*)&localRx, 1, 1000)!= HAL_OK) { timeoutCounter++; get_globalStateList(&status); if (status.base != BaseComm) { - timeoutCounter = UART_TIMEOUT_SECONDS * 2; /* Abort action triggered outside main loop => exit */ + timeoutCounter = UART_TIMEOUT_SECONDS; /* Abort action triggered outside main loop => exit */ + } + if(EvaluateBluetoothSignalStrength) + { + tComm_EvaluateBluetoothStrength(); } } else { -// debug[dbgptr++] = aRxByte; - answer = select_mode(aRxByte); + answer = select_mode(localRx); + timeoutCounter = 0; } } set_returnFromComm(); @@ -860,6 +930,7 @@ case 0x69: // get serial, old version numbering, custom text case 0x6A: // get model case 0x6B: // get specific firmware version + case 0x6C: /* Display Bluetooth signal strength */ case 0x6D: // get all compact headers (16 byte) case 0x6E: // display text case 0x70: // read min, default, max setting @@ -1004,6 +1075,10 @@ aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); break; + /* Trigger Bluetooth signal strength evaluation */ + case 0x6C: tComm_EvaluateBluetoothStrength(); + aTxBuffer[count++] = prompt4D4C(receiveStartByteUart); + break; // display text case 0x6E: for(int i=0;i<16;i++) @@ -1202,32 +1277,34 @@ return 0; } +#define BLOCKSIZE 0x1000 HAL_StatusTypeDef receive_uart_large_size(UART_HandleTypeDef *huart, uint8_t *pData, uint32_t Size) { - uint16_t length_16k_blocks; - uint16_t length_16k_remainder; + uint16_t length_4k_blocks; + uint16_t length_4k_remainder; uint32_t temp; HAL_StatusTypeDef result = HAL_OK; uint32_t pDataLocal; - length_16k_blocks = (uint16_t) (Size / 0x3FFF); - temp = length_16k_blocks; - temp *= 0x3FFF; - length_16k_remainder = (uint16_t) ( Size - temp); + length_4k_blocks = (uint16_t) (Size / BLOCKSIZE); + temp = length_4k_blocks; + temp *= BLOCKSIZE; + length_4k_remainder = (uint16_t) ( Size - temp); pDataLocal = (uint32_t)pData; - while((result == HAL_OK) && length_16k_blocks) + while((result == HAL_OK) && length_4k_blocks) { - result = HAL_UART_Receive(&UartHandle, (uint8_t *)pDataLocal, 0x3FFF , UART_TIMEOUT_LARGE_BLOCK); - pDataLocal += 0x3FFF; - length_16k_blocks--; + result = HAL_UART_Receive(&UartHandle, (uint8_t *)pDataLocal, BLOCKSIZE , UART_TIMEOUT_LARGE_BLOCK); + pDataLocal += BLOCKSIZE; + length_4k_blocks--; } - if((result == HAL_OK) && length_16k_remainder) + + if((result == HAL_OK) && length_4k_remainder) { - result = HAL_UART_Receive(&UartHandle, (uint8_t *)pDataLocal, length_16k_remainder , UART_TIMEOUT_LARGE_BLOCK); + result = HAL_UART_Receive(&UartHandle, (uint8_t *)pDataLocal, length_4k_remainder , UART_TIMEOUT_LARGE_BLOCK); } return result; } @@ -1833,6 +1910,240 @@ } } +void tComm_RequestBluetoothStrength(void) +{ + EvaluateBluetoothSignalStrength = 1; +} + +/* read, validate the modul answer and flush rx que if necessary */ +uint8_t tComm_CheckAnswerOK() +{ + char answerOkay[] = "\r\nOK\r\n"; + char aRxBuffer[UART_CMD_BUF_SIZE]; + uint8_t sizeAnswer = sizeof(answerOkay) -1; + uint8_t result = HAL_OK; + uint8_t index = 0; + uint8_t answer; + + memset(aRxBuffer,0,UART_CMD_BUF_SIZE); + if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, sizeAnswer, 1000) == HAL_OK) + { + do + { + if(answerOkay[index] != aRxBuffer[index]) + { + index = sizeAnswer; + result = HAL_ERROR; /* unexpected answer => there might be characters left in RX que => read and discard all rx bytes */ + do + { + answer = HAL_UART_Receive(&UartHandle, (uint8_t*)&aRxBuffer[index], 1, 10); + if (index < UART_CMD_BUF_SIZE) + { + index++; + } + }while(answer == HAL_OK); + index = sizeAnswer; + } + else + { + index++; + } + }while(index < sizeAnswer); + } + else + { + result = HAL_ERROR; + } + return result; + +} + + +void tComm_EvaluateBluetoothStrength(void) +{ + char aTxBufferBarSSI[] = "AT+BARSSI\r"; + char aTxBufferEscapeSequence[] = "+++"; + char aTxBufferEnd[] = "ATO\r"; + uint8_t sizeRequest = sizeof(aTxBufferBarSSI) -1; + + uint8_t answer = HAL_OK; + char aRxBuffer[UART_CMD_BUF_SIZE]; + char SignalStr[UART_CMD_BUF_SIZE]; + uint8_t index = 0; + uint8_t strindex = 0; + int8_t sigqual = 0; + + HAL_Delay(200); + if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBufferEscapeSequence, 3, 2000)== HAL_OK) + { + if(tComm_CheckAnswerOK() == HAL_OK) + { + HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBufferBarSSI,sizeRequest , 2000); + { + index = 0; + do /* Answer is not the common one. Instead the signal strength is received => read all available bytes one by one*/ + { + answer = HAL_UART_Receive(&UartHandle, (uint8_t*)&aRxBuffer[index], 1, 100); + if(index < UART_CMD_BUF_SIZE) + { + index++; + } + }while(answer == HAL_OK); + + if((aRxBuffer[index] != 'E') && (aRxBuffer[index] != 0)) /* E represents the first letter of the string ERROR */ + { + index = 0; + strindex = 0; + do + { + SignalStr[strindex++] = aRxBuffer[index++]; + }while ((index < UART_CMD_BUF_SIZE - 1) && (aRxBuffer[index] != '\r')); + SignalStr[strindex] = 0; /* terminate String */ + sigqual = strtol(SignalStr,NULL,0); + /* Map db to abstract Bargraph */ + if(sigqual > 0) + { + sprintf(SignalStr,"Bluetooth ||||||||"); + } + else + { + sprintf(SignalStr,"Bluetooth |"); + strindex = strlen(SignalStr); + sigqual *=-1; + sigqual = 100 - sigqual; /* invert because of negative db value */ + while(sigqual / 10 > 0 ) + { + SignalStr[strindex++] = '|'; + sigqual -= 10; + } + SignalStr[strindex] = 0; + } + strcpy(display_text,SignalStr); + display_text[255] = strlen(SignalStr); + EvaluateBluetoothSignalStrength = 0; + } + } + HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBufferEnd, 4, 2000); /* exit terminal mode */ + index = 0; + do /* module will answer with current connection state */ + { + answer = HAL_UART_Receive(&UartHandle, (uint8_t*)&aRxBuffer[index], 1, 100); + if(index < UART_CMD_BUF_SIZE) + { + index++; + } + }while(answer == HAL_OK); + } + } +} + +void tComm_StartBlueModConfig() +{ + uint8_t answer = HAL_OK; + uint8_t RxBuffer[UART_CMD_BUF_SIZE]; + uint8_t index = 0; + + BmTmpConfig = BM_CONFIG_ECHO; + do /* flush RX buffer */ + { + answer = HAL_UART_Receive(&UartHandle, (uint8_t*)&RxBuffer[index], 1, 10); + if(index < UART_CMD_BUF_SIZE) index++; + }while(answer == HAL_OK); +} + +uint8_t tComm_HandleBlueModConfig() +{ + static uint8_t RestartModule = 1; /* used to do power off / on cycle */ + static uint8_t ConfigRetryCnt = 0; /* Retry count without power cycle */ + + char TxBuffer[UART_CMD_BUF_SIZE]; + uint8_t CmdSize = 0; + + uint8_t result = HAL_OK; + + TxBuffer[0] = 0; + + switch (BmTmpConfig) + { + case BM_CONFIG_ECHO: sprintf(TxBuffer,"ATE0\r"); + break; + case BM_CONFIG_SILENCE: sprintf(TxBuffer,"ATS30=0\r"); + break; + case BM_CONFIG_ESCAPE_DELAY: sprintf(TxBuffer,"ATS12=10\r"); + break; + case BM_CONFIG_SIGNAL_POLL: sprintf(TxBuffer,"AT+BSTPOLL=100\r"); + break; + case BM_CONFIG_BAUD: sprintf(TxBuffer,"AT%%B22\r"); + break; + case BM_CONFIG_RETRY: ConfigRetryCnt--; + HAL_Delay(1); + if(ConfigRetryCnt == 0) + { + MX_Bluetooth_PowerOn(); + tComm_StartBlueModConfig(); + } + break; + case BM_CONFIG_DONE: + case BM_CONFIG_OFF: + ConfigRetryCnt = 0; + RestartModule = 1; + break; + default: + break; + } + if(TxBuffer[0] != 0) /* forward command to module */ + { + CmdSize = strlen(TxBuffer); + if(HAL_UART_Transmit(&UartHandle, (uint8_t*)TxBuffer,CmdSize, 2000) == HAL_OK) + { + if(BmTmpConfig == BM_CONFIG_ECHO) /* echo is not yet turned off => read and discard echo */ + { + HAL_UART_Receive(&UartHandle, (uint8_t*)TxBuffer, CmdSize, 1000); + } + + result = tComm_CheckAnswerOK(); + + + if((BmTmpConfig == BM_CONFIG_BAUD) && (result == HAL_OK) && (UartHandle.Init.BaudRate != 460800)) /* is com already switched to fast speed? */ + { + HAL_UART_DeInit(&UartHandle); + HAL_Delay(1); + UartHandle.Init.BaudRate = 460800; + HAL_UART_Init(&UartHandle); + } + if(result == HAL_OK) + { + BmTmpConfig++; + } + if(BmTmpConfig == BM_CONFIG_DONE) + { + ConfigRetryCnt = 0; + } + } + } + if(result != HAL_OK) + { + ConfigRetryCnt++; + if(ConfigRetryCnt > 3) /* Configuration failed => switch off module */ + { + MX_Bluetooth_PowerOff(); + if(RestartModule) + { + RestartModule = 0; /* only one try */ + ConfigRetryCnt = 200; /* used for delay to startup module again */ + BmTmpConfig = BM_CONFIG_RETRY; + } + else /* even restarting module failed => switch bluetooth off */ + { + ConfigRetryCnt = 0; + BmTmpConfig = BM_CONFIG_OFF; + settingsGetPointer()->bluetoothActive = 0; + } + } + } + return result; +} + static void tComm_Error_Handler(void) { while(1) diff -r cad6f8dfacab -r 4151fddb1e34 Discovery/Src/tDebug.c --- a/Discovery/Src/tDebug.c Mon Dec 02 07:56:47 2019 +0000 +++ b/Discovery/Src/tDebug.c Sun Dec 29 09:42:16 2019 +0100 @@ -34,6 +34,7 @@ #include "gfx_fonts.h" #include "ostc.h" #include "tInfo.h" +#include "tComm.h" #include "stm32f4xx_hal.h" @@ -78,6 +79,7 @@ void tDebug_start(void) { MX_Bluetooth_PowerOn(); + tComm_StartBlueModConfig(); tD_debugModeActive = 1; tD_status = 0; @@ -102,6 +104,7 @@ case ACTION_MODE_CHANGE: case ACTION_BUTTON_BACK: exitInfo(); + break; default: break; case ACTION_IDLE_TICK: diff -r cad6f8dfacab -r 4151fddb1e34 Discovery/Src/tMenuEditHardware.c --- a/Discovery/Src/tMenuEditHardware.c Mon Dec 02 07:56:47 2019 +0000 +++ b/Discovery/Src/tMenuEditHardware.c Sun Dec 29 09:42:16 2019 +0100 @@ -37,6 +37,7 @@ #include "tHome.h" #include "tInfo.h" #include "tInfoLog.h" +#include "tComm.h" extern void tM_build_pages(void); @@ -104,6 +105,7 @@ { pSettings->bluetoothActive = 1; MX_Bluetooth_PowerOn(); + tComm_StartBlueModConfig(); } else {