diff Discovery/Src/ostc.c @ 1032:33b91584d827 Puls_Integration

New CV Pulse: The basic infrastructure for external puls measurement via Bluetooth has been added. Precondition is an OSTC with an activated central role. The OSTC will then search for a BLE device with puls measurement service. Reading data and visualization is not implemented yet.
author Ideenmodellierer
date Mon, 28 Jul 2025 18:34:45 +0200
parents 8d3f3a635397
children 5865f0aeb438
line wrap: on
line diff
--- a/Discovery/Src/ostc.c	Mon Jul 28 18:32:23 2025 +0200
+++ b/Discovery/Src/ostc.c	Mon Jul 28 18:34:45 2025 +0200
@@ -27,8 +27,10 @@
 //////////////////////////////////////////////////////////////////////////////
 
 /* Includes ------------------------------------------------------------------*/
+#include "configuration.h"
 #include "ostc.h"
 #include "stm32f4xx_hal.h"
+#include "cv_heartbeat.h"
 
 #ifndef BOOTLOADER_STANDALONE
 #include "tCCR.h"
@@ -54,6 +56,11 @@
 
 /* Private variables with external access via get_xxx() function -------------*/
 static uint8_t	hardwareDisplay = 0;		//< either OSTC4 LCD (=0) or new Screen (=1)
+
+static uint16_t rxBufRead = 0;
+static uint16_t rxBufWrite = 0;
+static uint8_t rxBufferUart[CHUNK_SIZE * CHUNKS_PER_BUFFER];		/* The complete buffer has a X * chunk size to allow variations in buffer read time */
+
 /* Private function prototypes -----------------------------------------------*/
 
 /* Exported functions --------------------------------------------------------*/
@@ -380,6 +387,64 @@
 #endif
 }
 
+static DMA_HandleTypeDef  hdma_uart_BT_rx;
+
+void MX_UART_BT_Init_DMA()
+{
+
+	__DMA2_CLK_ENABLE();
+	 __HAL_RCC_DMA2_CLK_ENABLE();
+
+	hdma_uart_BT_rx.Instance = DMA2_Stream2;
+	hdma_uart_BT_rx.Init.Channel = DMA_CHANNEL_4;
+	hdma_uart_BT_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
+	hdma_uart_BT_rx.Init.PeriphInc = DMA_PINC_DISABLE;
+	hdma_uart_BT_rx.Init.MemInc = DMA_MINC_ENABLE;
+	hdma_uart_BT_rx.Init.PeriphDataAlignment = DMA_MDATAALIGN_BYTE;
+	hdma_uart_BT_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
+	hdma_uart_BT_rx.Init.Mode = DMA_NORMAL;
+	hdma_uart_BT_rx.Init.Priority = DMA_PRIORITY_LOW;
+	hdma_uart_BT_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
+	HAL_DMA_Init(&hdma_uart_BT_rx);
+
+	__HAL_LINKDMA(&UartHandle, hdmarx, hdma_uart_BT_rx);
+
+	HAL_NVIC_SetPriority(DMA2_Stream2_IRQn, 0, 0);
+	HAL_NVIC_EnableIRQ(DMA2_Stream2_IRQn);
+}
+
+
+uint8_t UART_getChar()
+{
+	uint8_t retChar = 0;
+
+	if((rxBufRead != rxBufWrite) && (rxBufferUart[rxBufRead] != 0))
+	{
+		retChar = rxBufferUart[rxBufRead];
+		rxBufferUart[rxBufRead++] = 0;
+		if(rxBufRead == CHUNK_SIZE * CHUNKS_PER_BUFFER)
+		{
+			rxBufRead = 0;
+		}
+	}
+	return retChar;
+}
+
+void UART_StartDMARx()
+{
+	HAL_UART_Receive_DMA (&UartHandle, &rxBufferUart[rxBufWrite], CHUNK_SIZE);
+	rxBufWrite += CHUNK_SIZE;
+	if(rxBufWrite >= CHUNK_SIZE * CHUNKS_PER_BUFFER)
+	{
+		rxBufWrite = 0;
+	}
+}
+
+void DMA2_Stream2_IRQHandler(void)
+{
+  HAL_DMA_IRQHandler(&hdma_uart_BT_rx);
+}
+
 void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
 {
     if(huart == &UartHandle)
@@ -390,7 +455,20 @@
 void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
 {
     if(huart == &UartHandle)
-        UartReady = SET;
+    {
+#ifdef ENABLE_PULSE_SENSOR_BT
+    	if(cv_heartbeat_getState() != SENSOR_HB_OFFLINE)
+    	{
+    		UART_StartDMARx();
+    	}
+    	else
+    	{
+    		UartReady = SET;
+    	}
+#else
+    	UartReady = SET;
+#endif
+    }
     else
     if(huart == &UartIR_HUD_Handle)
     {