comparison Discovery/Src/tComm.c @ 400:7bcbbaa42cbf ImproveBluetooth

Added retry through power off/on cycle: During tests sometime (<10%) the initialization of the module failed which was visible due to a deactivated Bluetooth option. Manual activation was always successfull. To improve the startup behavior a retry by using a module power off / on cycle has been added
author ideenmodellierer
date Sat, 28 Dec 2019 19:42:04 +0100
parents 523892f62ce0
children f68f2c4d71c7
comparison
equal deleted inserted replaced
399:523892f62ce0 400:7bcbbaa42cbf
105 /* Private types -------------------------------------------------------------*/ 105 /* Private types -------------------------------------------------------------*/
106 #define BYTE_DOWNLOAD_MODE (0xBB) 106 #define BYTE_DOWNLOAD_MODE (0xBB)
107 #define BYTE_SERVICE_MODE (0xAA) 107 #define BYTE_SERVICE_MODE (0xAA)
108 108
109 #define UART_TIMEOUT_SECONDS (120u) /* Timeout for keeping connection open and waiting for data */ 109 #define UART_TIMEOUT_SECONDS (120u) /* Timeout for keeping connection open and waiting for data */
110 #define UART_TIMEOUT_LARGE_BLOCK (6000u) /* Timeout (ms) for receiption of an 16K data block (typical RX time ~4,5seconds) */ 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 */
111 113
112 const uint8_t id_Region1_firmware = 0xFF; 114 const uint8_t id_Region1_firmware = 0xFF;
113 const uint8_t id_RTE = 0xFE; 115 const uint8_t id_RTE = 0xFE;
114 const uint8_t id_FONT = 0x10; 116 const uint8_t id_FONT = 0x10;
115 const uint8_t id_FONT_OLD = 0x00; 117 const uint8_t id_FONT_OLD = 0x00;
1915 1917
1916 /* read, validate the modul answer and flush rx que if necessary */ 1918 /* read, validate the modul answer and flush rx que if necessary */
1917 uint8_t tComm_CheckAnswerOK() 1919 uint8_t tComm_CheckAnswerOK()
1918 { 1920 {
1919 char answerOkay[] = "\r\nOK\r\n"; 1921 char answerOkay[] = "\r\nOK\r\n";
1920 char aRxBuffer[20]; 1922 char aRxBuffer[UART_CMD_BUF_SIZE];
1921 uint8_t sizeAnswer = sizeof(answerOkay) -1; 1923 uint8_t sizeAnswer = sizeof(answerOkay) -1;
1922 uint8_t result = HAL_OK; 1924 uint8_t result = HAL_OK;
1923 uint8_t index = 0; 1925 uint8_t index = 0;
1924 uint8_t answer; 1926 uint8_t answer;
1925 1927
1926 memset(aRxBuffer,0,20); 1928 memset(aRxBuffer,0,UART_CMD_BUF_SIZE);
1927 if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, sizeAnswer, 1000) == HAL_OK) 1929 if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, sizeAnswer, 1000) == HAL_OK)
1928 { 1930 {
1929 do 1931 do
1930 { 1932 {
1931 if(answerOkay[index] != aRxBuffer[index]) 1933 if(answerOkay[index] != aRxBuffer[index])
1933 index = sizeAnswer; 1935 index = sizeAnswer;
1934 result = HAL_ERROR; /* unexpected answer => there might be characters left in RX que => read and discard all rx bytes */ 1936 result = HAL_ERROR; /* unexpected answer => there might be characters left in RX que => read and discard all rx bytes */
1935 do 1937 do
1936 { 1938 {
1937 answer = HAL_UART_Receive(&UartHandle, (uint8_t*)&aRxBuffer[index], 1, 10); 1939 answer = HAL_UART_Receive(&UartHandle, (uint8_t*)&aRxBuffer[index], 1, 10);
1938 if (index < 20) index++; 1940 if (index < UART_CMD_BUF_SIZE)
1941 {
1942 index++;
1943 }
1939 }while(answer == HAL_OK); 1944 }while(answer == HAL_OK);
1940 index = sizeAnswer; 1945 index = sizeAnswer;
1941 } 1946 }
1942 else 1947 else
1943 { 1948 {
1950 result = HAL_ERROR; 1955 result = HAL_ERROR;
1951 } 1956 }
1952 return result; 1957 return result;
1953 1958
1954 } 1959 }
1955 char SignalStr[20]; 1960
1956 1961
1957 void tComm_EvaluateBluetoothStrength(void) 1962 void tComm_EvaluateBluetoothStrength(void)
1958 { 1963 {
1959 char aTxBufferBarSSI[] = "AT+BARSSI\r"; 1964 char aTxBufferBarSSI[] = "AT+BARSSI\r";
1960 char aTxBufferEscapeSequence[] = "+++"; 1965 char aTxBufferEscapeSequence[] = "+++";
1961 char aTxBufferEnd[] = "ATO\r"; 1966 char aTxBufferEnd[] = "ATO\r";
1962 uint8_t sizeRequest = sizeof(aTxBufferBarSSI) -1; 1967 uint8_t sizeRequest = sizeof(aTxBufferBarSSI) -1;
1963 1968
1964 uint8_t answer = HAL_OK; 1969 uint8_t answer = HAL_OK;
1965 char aRxBuffer[20]; 1970 char aRxBuffer[UART_CMD_BUF_SIZE];
1966 1971 char SignalStr[UART_CMD_BUF_SIZE];
1967 uint8_t index = 0; 1972 uint8_t index = 0;
1968 uint8_t strindex = 0; 1973 uint8_t strindex = 0;
1969 int8_t sigqual = 0; 1974 int8_t sigqual = 0;
1970 1975
1971 HAL_Delay(200); 1976 HAL_Delay(200);
1977 { 1982 {
1978 index = 0; 1983 index = 0;
1979 do /* Answer is not the common one. Instead the signal strength is received => read all available bytes one by one*/ 1984 do /* Answer is not the common one. Instead the signal strength is received => read all available bytes one by one*/
1980 { 1985 {
1981 answer = HAL_UART_Receive(&UartHandle, (uint8_t*)&aRxBuffer[index], 1, 100); 1986 answer = HAL_UART_Receive(&UartHandle, (uint8_t*)&aRxBuffer[index], 1, 100);
1982 if(index < 20-1) index++; 1987 if(index < UART_CMD_BUF_SIZE)
1988 {
1989 index++;
1990 }
1983 }while(answer == HAL_OK); 1991 }while(answer == HAL_OK);
1984 1992
1985 if((aRxBuffer[index] != 'E') && (aRxBuffer[index] != 0)) /* E represents the first letter of the string ERROR */ 1993 if((aRxBuffer[index] != 'E') && (aRxBuffer[index] != 0)) /* E represents the first letter of the string ERROR */
1986 { 1994 {
1987 index = 0; 1995 index = 0;
1988 strindex = 0; 1996 strindex = 0;
1989 do 1997 do
1990 { 1998 {
1991 SignalStr[strindex++] = aRxBuffer[index++]; 1999 SignalStr[strindex++] = aRxBuffer[index++];
1992 }while ((index < 20) && (aRxBuffer[index] != '\r')); 2000 }while ((index < UART_CMD_BUF_SIZE - 1) && (aRxBuffer[index] != '\r'));
1993 SignalStr[strindex] = 0; /* terminate String */ 2001 SignalStr[strindex] = 0; /* terminate String */
1994 sigqual = strtol(SignalStr,NULL,0); 2002 sigqual = strtol(SignalStr,NULL,0);
1995 #if 0
1996 if(sigqual & 0x80) /* high bit set? */
1997 {
1998 sigqual = ~sigqual; /* calc complement of 2 */
1999 sigqual++;
2000 }
2001 #endif
2002 /* Map db to abstract Bargraph */ 2003 /* Map db to abstract Bargraph */
2003 if(sigqual > 0) 2004 if(sigqual > 0)
2004 { 2005 {
2005 sprintf(SignalStr,"Bluetooth ||||||||||"); 2006 sprintf(SignalStr,"Bluetooth ||||||||");
2006 } 2007 }
2007 else 2008 else
2008 { 2009 {
2009 sprintf(SignalStr,"Bluetooth |"); 2010 sprintf(SignalStr,"Bluetooth |");
2010 strindex = strlen(SignalStr); 2011 strindex = strlen(SignalStr);
2025 HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBufferEnd, 4, 2000); /* exit terminal mode */ 2026 HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBufferEnd, 4, 2000); /* exit terminal mode */
2026 index = 0; 2027 index = 0;
2027 do /* module will answer with current connection state */ 2028 do /* module will answer with current connection state */
2028 { 2029 {
2029 answer = HAL_UART_Receive(&UartHandle, (uint8_t*)&aRxBuffer[index], 1, 100); 2030 answer = HAL_UART_Receive(&UartHandle, (uint8_t*)&aRxBuffer[index], 1, 100);
2030 if(index < 20-1) index++; 2031 if(index < UART_CMD_BUF_SIZE)
2032 {
2033 index++;
2034 }
2031 }while(answer == HAL_OK); 2035 }while(answer == HAL_OK);
2032 } 2036 }
2033 } 2037 }
2034 } 2038 }
2035 2039
2036 void tComm_StartBlueModConfig() 2040 void tComm_StartBlueModConfig()
2037 { 2041 {
2038 uint8_t answer = HAL_OK; 2042 uint8_t answer = HAL_OK;
2039 uint8_t RxBuffer[20]; 2043 uint8_t RxBuffer[UART_CMD_BUF_SIZE];
2040 uint8_t index = 0; 2044 uint8_t index = 0;
2041 2045
2042 BmTmpConfig = BM_CONFIG_ECHO; 2046 BmTmpConfig = BM_CONFIG_ECHO;
2043 do /* flush RX buffer */ 2047 do /* flush RX buffer */
2044 { 2048 {
2045 answer = HAL_UART_Receive(&UartHandle, (uint8_t*)&RxBuffer[index], 1, 10); 2049 answer = HAL_UART_Receive(&UartHandle, (uint8_t*)&RxBuffer[index], 1, 10);
2046 if(index < 20-1) index++; 2050 if(index < UART_CMD_BUF_SIZE) index++;
2047 }while(answer == HAL_OK); 2051 }while(answer == HAL_OK);
2048 } 2052 }
2049 2053
2050 uint8_t tComm_HandleBlueModConfig() 2054 uint8_t tComm_HandleBlueModConfig()
2051 { 2055 {
2052 static uint8_t ConfigRetryCnt = 0; 2056 static uint8_t RestartModule = 1; /* used to do power off / on cycle */
2053 2057 static uint8_t ConfigRetryCnt = 0; /* Retry count without power cycle */
2054 char TxBuffer[20]; 2058
2059 char TxBuffer[UART_CMD_BUF_SIZE];
2055 uint8_t CmdSize = 0; 2060 uint8_t CmdSize = 0;
2056 2061
2057 uint8_t result = HAL_OK; 2062 uint8_t result = HAL_OK;
2063
2064 TxBuffer[0] = 0;
2058 2065
2059 switch (BmTmpConfig) 2066 switch (BmTmpConfig)
2060 { 2067 {
2061 case BM_CONFIG_ECHO: sprintf(TxBuffer,"ATE0\r"); 2068 case BM_CONFIG_ECHO: sprintf(TxBuffer,"ATE0\r");
2062 break; 2069 break;
2066 break; 2073 break;
2067 case BM_CONFIG_SIGNAL_POLL: sprintf(TxBuffer,"AT+BSTPOLL=100\r"); 2074 case BM_CONFIG_SIGNAL_POLL: sprintf(TxBuffer,"AT+BSTPOLL=100\r");
2068 break; 2075 break;
2069 case BM_CONFIG_BAUD: sprintf(TxBuffer,"AT%%B22\r"); 2076 case BM_CONFIG_BAUD: sprintf(TxBuffer,"AT%%B22\r");
2070 break; 2077 break;
2071 // case BM_CONFIG_DISABLE_EVENT: sprintf(TxBuffer,"AT+LECPEVENT=0\r"); 2078 case BM_CONFIG_RETRY: ConfigRetryCnt--;
2072 // break; 2079 HAL_Delay(1);
2080 if(ConfigRetryCnt == 0)
2081 {
2082 MX_Bluetooth_PowerOn();
2083 tComm_StartBlueModConfig();
2084 }
2085 break;
2073 case BM_CONFIG_DONE: 2086 case BM_CONFIG_DONE:
2074 case BM_CONFIG_OFF: 2087 case BM_CONFIG_OFF:
2075 ConfigRetryCnt = 0; 2088 ConfigRetryCnt = 0;
2089 RestartModule = 1;
2076 break; 2090 break;
2077 default: 2091 default:
2078 break; 2092 break;
2079 } 2093 }
2080 if((BmTmpConfig != BM_CONFIG_OFF) && (BmTmpConfig != BM_CONFIG_DONE)) 2094 if(TxBuffer[0] != 0) /* forward command to module */
2081 { 2095 {
2082 CmdSize = strlen(TxBuffer); 2096 CmdSize = strlen(TxBuffer);
2083 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)TxBuffer,CmdSize, 2000) == HAL_OK) 2097 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)TxBuffer,CmdSize, 2000) == HAL_OK)
2084 { 2098 {
2085 if(BmTmpConfig == BM_CONFIG_ECHO) /* echo is not yet turned off => read and discard echo */ 2099 if(BmTmpConfig == BM_CONFIG_ECHO) /* echo is not yet turned off => read and discard echo */
2110 if(result != HAL_OK) 2124 if(result != HAL_OK)
2111 { 2125 {
2112 ConfigRetryCnt++; 2126 ConfigRetryCnt++;
2113 if(ConfigRetryCnt > 3) /* Configuration failed => switch off module */ 2127 if(ConfigRetryCnt > 3) /* Configuration failed => switch off module */
2114 { 2128 {
2115 ConfigRetryCnt = 0;
2116 BmTmpConfig = BM_CONFIG_OFF;
2117 settingsGetPointer()->bluetoothActive = 0;
2118 MX_Bluetooth_PowerOff(); 2129 MX_Bluetooth_PowerOff();
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 }
2119 } 2142 }
2120 } 2143 }
2121 return result; 2144 return result;
2122 } 2145 }
2123 2146