Mercurial > public > ostc4
comparison Discovery/Src/tComm.c @ 696:cc542448fb28
Merge
author | heinrichsweikamp |
---|---|
date | Fri, 19 Aug 2022 11:30:24 +0200 |
parents | 667093daa937 |
children | bc6c90e20d9e |
comparison
equal
deleted
inserted
replaced
661:87bee7cc77b3 | 696:cc542448fb28 |
---|---|
1999 { | 1999 { |
2000 char answerOkay[] = "\r\nOK\r\n"; | 2000 char answerOkay[] = "\r\nOK\r\n"; |
2001 char aRxBuffer[UART_CMD_BUF_SIZE]; | 2001 char aRxBuffer[UART_CMD_BUF_SIZE]; |
2002 uint8_t sizeAnswer = sizeof(answerOkay) -1; | 2002 uint8_t sizeAnswer = sizeof(answerOkay) -1; |
2003 uint8_t result = HAL_OK; | 2003 uint8_t result = HAL_OK; |
2004 uint8_t index = 0; | 2004 uint8_t indexRef = 0; |
2005 uint8_t indexBuf = 0; | |
2005 uint8_t answer; | 2006 uint8_t answer; |
2006 | 2007 |
2007 memset(aRxBuffer,0,UART_CMD_BUF_SIZE); | 2008 memset(aRxBuffer,0,UART_CMD_BUF_SIZE); |
2008 if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, sizeAnswer, UART_OPERATION_TIMEOUT) == HAL_OK) | 2009 if(HAL_UART_Receive(&UartHandle, (uint8_t*)aRxBuffer, sizeAnswer, UART_OPERATION_TIMEOUT) == HAL_OK) |
2009 { | 2010 { |
2010 do | 2011 do |
2011 { | 2012 { |
2012 if(answerOkay[index] != aRxBuffer[index]) | 2013 if(answerOkay[indexRef] == aRxBuffer[indexBuf]) |
2013 { | 2014 { |
2014 index = sizeAnswer; | 2015 indexRef++; |
2015 result = HAL_ERROR; /* unexpected answer => there might be characters left in RX que => read and discard all rx bytes */ | |
2016 do | |
2017 { | |
2018 answer = HAL_UART_Receive(&UartHandle, (uint8_t*)&aRxBuffer[index], 1, 10); | |
2019 if (index < UART_CMD_BUF_SIZE) | |
2020 { | |
2021 index++; | |
2022 } | |
2023 }while(answer == HAL_OK); | |
2024 index = sizeAnswer; | |
2025 } | 2016 } |
2026 else | 2017 else |
2027 { | 2018 { |
2028 index++; | 2019 if(indexRef != 0) |
2020 { | |
2021 indexRef = 0; | |
2022 } | |
2029 } | 2023 } |
2030 }while(index < sizeAnswer); | 2024 indexBuf++; |
2025 }while(indexBuf < sizeAnswer); | |
2026 | |
2027 if(indexRef != sizeAnswer) /* unexpected answer => there might be characters left in RX que => read and check all rx bytes */ | |
2028 { | |
2029 do | |
2030 { | |
2031 answer = HAL_UART_Receive(&UartHandle, (uint8_t*)&aRxBuffer[indexBuf], 1, 10); | |
2032 if (indexBuf < UART_CMD_BUF_SIZE) | |
2033 { | |
2034 if(answerOkay[indexRef] == aRxBuffer[indexBuf]) | |
2035 { | |
2036 indexRef++; | |
2037 } | |
2038 else | |
2039 { | |
2040 if(indexRef != 0) | |
2041 { | |
2042 indexRef = 0; | |
2043 } | |
2044 } | |
2045 indexBuf++; | |
2046 } | |
2047 }while(answer == HAL_OK); | |
2048 if(indexRef != sizeAnswer) | |
2049 { | |
2050 result = HAL_ERROR; | |
2051 } | |
2052 } | |
2031 } | 2053 } |
2032 else | 2054 else |
2033 { | 2055 { |
2034 result = HAL_ERROR; | 2056 result = HAL_ERROR; |
2035 } | 2057 } |
2173 if(TxBuffer[0] != 0) /* forward command to module */ | 2195 if(TxBuffer[0] != 0) /* forward command to module */ |
2174 { | 2196 { |
2175 CmdSize = strlen(TxBuffer); | 2197 CmdSize = strlen(TxBuffer); |
2176 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)TxBuffer,CmdSize, 2000) == HAL_OK) | 2198 if(HAL_UART_Transmit(&UartHandle, (uint8_t*)TxBuffer,CmdSize, 2000) == HAL_OK) |
2177 { | 2199 { |
2178 if(BmTmpConfig == BM_CONFIG_ECHO) /* echo is not yet turned off => read and discard echo */ | |
2179 { | |
2180 HAL_UART_Receive(&UartHandle, (uint8_t*)TxBuffer, CmdSize, UART_OPERATION_TIMEOUT); | |
2181 } | |
2182 | |
2183 result = tComm_CheckAnswerOK(); | 2200 result = tComm_CheckAnswerOK(); |
2184 | |
2185 | 2201 |
2186 if((BmTmpConfig == BM_CONFIG_BAUD) && (result == HAL_OK) && (UartHandle.Init.BaudRate != 460800)) /* is com already switched to fast speed? */ | 2202 if((BmTmpConfig == BM_CONFIG_BAUD) && (result == HAL_OK) && (UartHandle.Init.BaudRate != 460800)) /* is com already switched to fast speed? */ |
2187 { | 2203 { |
2188 HAL_UART_DeInit(&UartHandle); | 2204 HAL_UART_DeInit(&UartHandle); |
2189 HAL_Delay(1); | 2205 HAL_Delay(1); |
2190 UartHandle.Init.BaudRate = 460800; | 2206 UartHandle.Init.BaudRate = 460800; |
2191 HAL_UART_Init(&UartHandle); | 2207 HAL_UART_Init(&UartHandle); |
2208 } | |
2209 if((BmTmpConfig == BM_CONFIG_BAUD) && (result == HAL_OK) && (UartHandle.Init.BaudRate == 460800)) /* This shut not happen because default speed is 115200 => update module configuration */ | |
2210 { | |
2211 sprintf(TxBuffer,"AT%%B8\r"); /* set default baudrate */ | |
2212 CmdSize = strlen(TxBuffer); | |
2213 HAL_UART_Transmit(&UartHandle, (uint8_t*)TxBuffer,CmdSize, 2000); | |
2214 HAL_UART_DeInit(&UartHandle); | |
2215 HAL_Delay(10); | |
2216 UartHandle.Init.BaudRate = 115200; | |
2217 HAL_UART_Init(&UartHandle); | |
2218 sprintf(TxBuffer,"AT&W\r"); /* write configuration */ | |
2219 CmdSize = strlen(TxBuffer); | |
2220 HAL_UART_Transmit(&UartHandle, (uint8_t*)TxBuffer,CmdSize, 2000); | |
2192 } | 2221 } |
2193 if(result == HAL_OK) | 2222 if(result == HAL_OK) |
2194 { | 2223 { |
2195 BmTmpConfig++; | 2224 BmTmpConfig++; |
2196 } | 2225 } |
2197 if(BmTmpConfig == BM_CONFIG_DONE) | 2226 if(BmTmpConfig == BM_CONFIG_DONE) |
2198 { | 2227 { |
2199 ConfigRetryCnt = 0; | 2228 ConfigRetryCnt = 0; |
2229 RestartModule = 1; | |
2200 } | 2230 } |
2201 } | 2231 } |
2202 } | 2232 } |
2203 if(result != HAL_OK) | 2233 if(result != HAL_OK) |
2204 { | 2234 { |
2208 MX_Bluetooth_PowerOff(); | 2238 MX_Bluetooth_PowerOff(); |
2209 if(RestartModule) | 2239 if(RestartModule) |
2210 { | 2240 { |
2211 RestartModule = 0; /* only one try */ | 2241 RestartModule = 0; /* only one try */ |
2212 ConfigRetryCnt = 200; /* used for delay to startup module again */ | 2242 ConfigRetryCnt = 200; /* used for delay to startup module again */ |
2243 | |
2244 if(BmTmpConfig == BM_CONFIG_ECHO) /* the module did not answer even once => try again with alternative baud rate */ | |
2245 { | |
2246 HAL_UART_DeInit(&UartHandle); | |
2247 HAL_Delay(1); | |
2248 UartHandle.Init.BaudRate = 460800; | |
2249 HAL_UART_Init(&UartHandle); | |
2250 } | |
2213 BmTmpConfig = BM_CONFIG_RETRY; | 2251 BmTmpConfig = BM_CONFIG_RETRY; |
2214 } | 2252 } |
2215 else /* even restarting module failed => switch bluetooth off */ | 2253 else /* even restarting module failed => switch bluetooth off */ |
2216 { | 2254 { |
2217 ConfigRetryCnt = 0; | 2255 ConfigRetryCnt = 0; |