diff Small_CPU/Src/uart.c @ 690:fca2bd25e6e2 Betatest

Added Sentinel protocoll support: Added function for evaluation of the Sentinel protocoll. At the moment only O2 sensor values are extracted.
author Ideenmodellierer
date Fri, 05 Aug 2022 15:22:26 +0200
parents 1b995079c045
children f1b40364b0af
line wrap: on
line diff
--- a/Small_CPU/Src/uart.c	Fri Aug 05 15:15:01 2022 +0200
+++ b/Small_CPU/Src/uart.c	Fri Aug 05 15:22:26 2022 +0200
@@ -94,13 +94,36 @@
   HAL_NVIC_EnableIRQ(DMA2_Stream5_IRQn);
 }
 
+void ConvertByteToHexString(uint8_t byte, char* str)
+{
+	uint8_t worker = 0;
+	uint8_t digit = 0;
+	uint8_t digitCnt = 1;
 
-uint32_t dataValue = 0;
+	worker = byte;
+	while((worker!=0) && (digitCnt != 255))
+	{
+		digit = worker % 16;
+		if( digit < 10)
+		{
+			digit += '0';
+		}
+		else
+		{
+			digit += 'A' - 10;
+		}
+		str[digitCnt--]= digit;
+		worker = worker / 16;
+	}
+}
 
-void HandleUARTData(void)
+
+#ifdef ENABLE_CO2_SUPPORT
+void HandleUARTCO2Data(void)
 {
 	uint8_t localRX = rxReadIndex;
 	uint8_t dataType = 0;
+	uint32_t dataValue = 0;
 	static receiveState_t rxState = RX_Ready;
 	static uint32_t lastReceiveTick = 0;
 
@@ -178,6 +201,160 @@
 		}
 	}
 }
+#endif
+
+#ifdef ENABLE_SENTINEL_MODE
+void HandleUARTSentinelData(void)
+{
+	uint8_t localRX = rxReadIndex;
+	static uint8_t dataType = 0;
+	static uint32_t dataValue[3];
+	static uint8_t dataValueIdx = 0;
+	static receiveState_t rxState = RX_Ready;
+	static uint32_t lastReceiveTick = 0;
+	static uint8_t lastAlive = 0;
+	static uint8_t curAlive = 0;
+	static uint8_t checksum = 0;
+	char checksum_str[]="00";
+
+	while(localRX != rxWriteIndex)
+	{
+		lastReceiveTick = HAL_GetTick();
+
+		switch(rxState)
+		{
+			case RX_Ready:	if((rxBuffer[localRX] >= 'a') && (rxBuffer[localRX] <= 'z'))
+							{
+								rxState = RX_DetectStart;
+								curAlive = rxBuffer[localRX];
+								checksum = 0;
+							}
+					break;
+
+			case RX_DetectStart: 	checksum += rxBuffer[localRX];
+									if(rxBuffer[localRX] == '1')
+								 	{
+								 		rxState = RX_SelectData;
+								 		dataType = 0xFF;
+
+								 	}
+									else
+									{
+										rxState = RX_Ready;
+									}
+					break;
+
+			case RX_SelectData:		checksum += rxBuffer[localRX];
+									switch(rxBuffer[localRX])
+									{
+										case 'T':	dataType = rxBuffer[localRX];
+											break;
+										case '0': 	if(dataType != 0xff)
+													{
+														rxState = RX_Data0;
+														dataValueIdx = 0;
+														dataValue[0] = 0;
+
+													}
+													else
+													{
+														rxState = RX_Ready;
+													}
+											break;
+										default:	rxState = RX_Ready;
+									}
+					break;
+
+			case RX_Data0:
+			case RX_Data1:
+			case RX_Data2:
+			case RX_Data4:
+			case RX_Data5:
+			case RX_Data6:
+			case RX_Data8:
+			case RX_Data9:
+			case RX_Data10: checksum += rxBuffer[localRX];
+							if((rxBuffer[localRX] >= '0') && (rxBuffer[localRX] <= '9'))
+							{
+								dataValue[dataValueIdx] = dataValue[dataValueIdx] * 10 + (rxBuffer[localRX] - '0');
+								rxState++;
+							}
+							else
+							{
+								rxState = RX_Ready;
+							}
+					break;
+
+			case RX_Data3:
+			case RX_Data7:	checksum += rxBuffer[localRX];
+							if(rxBuffer[localRX] == '0')
+							{
+								rxState++;
+								dataValueIdx++;
+								dataValue[dataValueIdx] = 0;
+							}
+							else
+							{
+								rxState = RX_Ready;
+							}
+					break;
+			case RX_Data11: rxState = RX_DataComplete;
+							ConvertByteToHexString(checksum,checksum_str);
+							if(rxBuffer[localRX] == checksum_str[0])
+							{
+								rxState = RX_DataComplete;
+							}
+							else
+							{
+								rxState = RX_Ready;
+							}
+
+				break;
+
+			case RX_DataComplete:	if(rxBuffer[localRX] == checksum_str[1])
+									{
+										setExternalInterfaceChannel(0,(float)(dataValue[0] / 10.0));
+										setExternalInterfaceChannel(1,(float)(dataValue[1] / 10.0));
+										setExternalInterfaceChannel(2,(float)(dataValue[2] / 10.0));
+									}
+									rxState = RX_Ready;
+				break;
+
+
+			default:				rxState = RX_Ready;
+				break;
+
+		}
+
+		localRX++;
+		rxReadIndex++;
+		if(rxReadIndex >= CHUNK_SIZE * CHUNKS_PER_BUFFER)
+		{
+			localRX = 0;
+			rxReadIndex = 0;
+		}
+	}
+
+	if(time_elapsed_ms(lastReceiveTick,HAL_GetTick()) > 4000)	/* check for communication timeout */
+	{
+		if(curAlive == lastAlive)
+		{
+			setExternalInterfaceChannel(0,0.0);
+			setExternalInterfaceChannel(1,0.0);
+			setExternalInterfaceChannel(2,0.0);
+		}
+		lastAlive = curAlive;
+	}
+
+	if((dmaActive == 0)	&& (externalInterface_isEnabledPower33()))	/* Should never happen in normal operation => restart in case of communication error */
+	{
+		if(HAL_OK == HAL_UART_Receive_DMA (&huart1, &rxBuffer[rxWriteIndex], CHUNK_SIZE))
+		{
+			dmaActive = 1;
+		}
+	}
+}
+#endif
 
 void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
 {