changeset 664:667093daa937 Betatest

Stability improvment bluetooth startup: The previous implementation expected a default setup of the Bluetooth module. Deviations from the default expectation caused the init function to stop. The new implementation is able to fix wrong baud rate setting (reset baudrate to default 115200). In addition the function evaluating the answers of the module is not able to derive the status out of a data stream.
author Ideenmodellierer
date Tue, 21 Dec 2021 19:36:41 +0100
parents 16833cd3a2f5
children b52b5a3f61f4
files Discovery/Src/tComm.c
diffstat 1 files changed, 59 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/Discovery/Src/tComm.c	Tue Dec 21 19:32:29 2021 +0100
+++ b/Discovery/Src/tComm.c	Tue Dec 21 19:36:41 2021 +0100
@@ -2001,7 +2001,8 @@
     char aRxBuffer[UART_CMD_BUF_SIZE];
     uint8_t sizeAnswer = sizeof(answerOkay) -1;
 	uint8_t result = HAL_OK;
-	uint8_t index = 0;
+	uint8_t indexRef = 0;
+	uint8_t indexBuf = 0;
 	uint8_t answer;
 
 	memset(aRxBuffer,0,UART_CMD_BUF_SIZE);
@@ -2009,25 +2010,46 @@
 	{
 		do
 		{
-			if(answerOkay[index] != aRxBuffer[index])
+			if(answerOkay[indexRef] == aRxBuffer[indexBuf])
 			{
-				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;
+				indexRef++;
 			}
 			else
 			{
-				index++;
+				if(indexRef != 0)
+				{
+					indexRef = 0;
+				}
 			}
-		}while(index < sizeAnswer);
+			indexBuf++;
+		}while(indexBuf < sizeAnswer);
+
+		if(indexRef != sizeAnswer)		/* unexpected answer => there might be characters left in RX que => read and check all rx bytes */
+		{
+			do
+			{
+				answer = HAL_UART_Receive(&UartHandle, (uint8_t*)&aRxBuffer[indexBuf], 1, 10);
+				if (indexBuf < UART_CMD_BUF_SIZE)
+				{
+					if(answerOkay[indexRef] == aRxBuffer[indexBuf])
+					{
+						indexRef++;
+					}
+					else
+					{
+						if(indexRef != 0)
+						{
+							indexRef = 0;
+						}
+					}
+					indexBuf++;
+				}
+			}while(answer == HAL_OK);
+			if(indexRef != sizeAnswer)
+			{
+				result = HAL_ERROR;
+			}
+		}
 	}
 	else
 	{
@@ -2175,14 +2197,8 @@
 		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, UART_OPERATION_TIMEOUT);
-			}
-
 			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);
@@ -2190,6 +2206,19 @@
 				UartHandle.Init.BaudRate   = 460800;
 				HAL_UART_Init(&UartHandle);
 			}
+			if((BmTmpConfig == BM_CONFIG_BAUD) && (result == HAL_OK) && (UartHandle.Init.BaudRate == 460800)) /* This shut not happen because default speed is 115200 => update module configuration */
+			{
+				sprintf(TxBuffer,"AT%%B8\r");	/* set default baudrate */
+				CmdSize = strlen(TxBuffer);
+				HAL_UART_Transmit(&UartHandle, (uint8_t*)TxBuffer,CmdSize, 2000);
+				HAL_UART_DeInit(&UartHandle);
+				HAL_Delay(10);
+				UartHandle.Init.BaudRate   = 115200;
+				HAL_UART_Init(&UartHandle);
+				sprintf(TxBuffer,"AT&W\r");		/* write configuration */
+				CmdSize = strlen(TxBuffer);
+				HAL_UART_Transmit(&UartHandle, (uint8_t*)TxBuffer,CmdSize, 2000);
+			}
 			if(result == HAL_OK)
 			{
 				BmTmpConfig++;
@@ -2197,6 +2226,7 @@
 			if(BmTmpConfig == BM_CONFIG_DONE)
 			{
 				ConfigRetryCnt = 0;
+				RestartModule = 1;
 			}
 		}
 	}
@@ -2210,6 +2240,14 @@
 			{
 				RestartModule = 0;      /* only one try */
 				ConfigRetryCnt = 200;	/* used for delay to startup module again */
+
+				if(BmTmpConfig == BM_CONFIG_ECHO)	/* the module did not answer even once => try again with alternative baud rate */
+				{
+					HAL_UART_DeInit(&UartHandle);
+					HAL_Delay(1);
+					UartHandle.Init.BaudRate   = 460800;
+					HAL_UART_Init(&UartHandle);
+				}
 				BmTmpConfig = BM_CONFIG_RETRY;
 			}
 			else						/* even restarting module failed => switch bluetooth off */