diff Discovery/Src/tComm.c @ 696:cc542448fb28

Merge
author heinrichsweikamp
date Fri, 19 Aug 2022 11:30:24 +0200
parents 667093daa937
children bc6c90e20d9e
line wrap: on
line diff
--- a/Discovery/Src/tComm.c	Mon Nov 01 12:39:34 2021 +0100
+++ b/Discovery/Src/tComm.c	Fri Aug 19 11:30:24 2022 +0200
@@ -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 */