Browse Source

upgrade kniznice, nbus_slave: getData

Juraj Ďuďák 8 months ago
parent
commit
dab2132118

+ 5 - 0
Core/Inc/NbusSlave.h

@@ -24,6 +24,8 @@ private:
 	Nbus_pdu _pdu;
 	uint8_t _sensor_cache[16];
 
+	uint8_t _sensor_count;
+
 public:
 	NbusSlave(uint8_t, NbusCommunicator*);
 	virtual ~NbusSlave();
@@ -35,6 +37,9 @@ public:
 	DataFrame* nbus_module_info();
 	DataFrame* nbus_module_format();
 	DataFrame* nbus_sensor_parameter(uint8_t, uint8_t);
+	DataFrame* nbus_sensor_getData(uint8_t sensor_address);
+
+	uint8_t nbus_get_sensor_count(bool check_hw);
 
 };
 

+ 15 - 15
Core/Inc/nbus_structs.h

@@ -24,21 +24,21 @@
 
 typedef enum {
 	FC_FIND = 0,
-	FC_ECHO,
-	FC_STOP,
-	FC_START,
-	FC_PARAM,
-	FC_SENSOR_CNT,
-	FC_SLEEP,
-	FC_WAKEUP,
-	FC_CALIBRATE,
-	FC_RESET,
-	FC_STORE,
-	FC_DATA,
-	FC_SYNC,
-	FC_SENSOR_TYPE,
-	FC_INFO,
-	FC_SENSOR_FORMAT,
+	FC_ECHO,			/* 1 */
+	FC_STOP,			/* 2 */
+	FC_START,			/* 3 */
+	FC_PARAM,			/* 4 */
+	FC_SENSOR_CNT,		/* 5 */
+	FC_SLEEP,			/* 6 */
+	FC_WAKEUP,			/* 7 */
+	FC_CALIBRATE,		/* 8 */
+	FC_RESET,			/* 9 */
+	FC_STORE,			/* 0xA => 10 */
+	FC_DATA,			/* 0xB => 11 */
+	FC_SYNC,			/* 0xC => 12 */
+	FC_SENSOR_TYPE,		/* 0xD => 13 */
+	FC_INFO,			/* 0xE => 14 */
+	FC_SENSOR_FORMAT, 	/* 0xF => 15 */
 }Nbus_FC_e;
 
 

+ 1 - 1
Core/Inc/stm32l4xx_it.h

@@ -22,7 +22,7 @@
 #define __STM32L4xx_IT_H
 
 #ifdef __cplusplus
- extern "C" {
+extern "C" {
 #endif
 
 /* Private includes ----------------------------------------------------------*/

+ 72 - 19
Core/Src/AppBridge.cpp

@@ -12,38 +12,91 @@
 
 #include "NbusCommunicator.h"
 
+UART_HandleTypeDef *pUartMasterGlobal;
+volatile uint32_t flagUartMasterDataReady;
+uint8_t pMasterUartRx[64];
+uint8_t pMasterUartFrame[64];
+uint8_t vMasterUartIndex;
+uint8_t vMasterUartSize;
+
+static void init_app(){
+	HAL_UARTEx_ReceiveToIdle_DMA(pUartMasterGlobal, pMasterUartRx, 64);
+	flagUartMasterDataReady = 0;
+	vMasterUartIndex = 0;
+	vMasterUartSize = 0;
+}
+
 void app(UART_HandleTypeDef *uartNbus, UART_HandleTypeDef *uartMaster){
 
-	NbusCommunicator *nc = new NbusCommunicator(uartNbus, uartMaster);
+	pUartMasterGlobal = uartMaster;
+	init_app();
 
-//	NbusSlave *slave1 = new NbusSlave(5, nc);
-//	NbusSlave *slave2 = new NbusSlave(6, nc);
-//	bridge->addSlave(slave1);
-//	bridge->addSlave(slave2);
+	NbusCommunicator nc(uartNbus, uartMaster);
+
+	// boot long blink
+	for(uint32_t i = 0 ; i<64 ; i++){
+		HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_3);
+		HAL_Delay(25);
+	}
 
-	NbusBridge *bridge = new NbusBridge(nc);
-	bridge->scan();
+	NbusBridge bridge(&nc);
+	bridge.scan();
 
 	DataFrame *frameTX;
-	while(1){
 
-		for(int i=0; i<bridge->getNumSlaves() ; i++){
-			frameTX = bridge->getSlave(i)->nbus_echo();
-			bridge->sendResponseToMaster(frameTX);
+	for(uint32_t i=0; i<bridge.getNumSlaves() ; i++){
+		frameTX = bridge.getSlave(i)->nbus_echo();
+		bridge.sendResponseToMaster(frameTX);
 
-			frameTX = bridge->getSlave(i)->nbus_module_info();
-			bridge->sendResponseToMaster(frameTX);
+		frameTX = bridge.getSlave(i)->nbus_module_info();
+		bridge.sendResponseToMaster(frameTX);
+	}
 
+	while(1){
+/*
+		for(uint32_t i=0; i<bridge.getNumSlaves() ; i++){
+			for(uint32_t k = 0 ; k <= bridge.getSlave(i)->nbus_get_sensor_count(false) ; k++){
+				frameTX = bridge.getSlave(i)->nbus_sensor_getData(k);
+				bridge.sendResponseToMaster(frameTX);
+			}
+
+		}
+*/
+		if(flagUartMasterDataReady > 0){
+			HAL_UART_Transmit_DMA(uartMaster, pMasterUartRx, flagUartMasterDataReady);
+			flagUartMasterDataReady = 0;
 		}
 
-		HAL_Delay(1);
-		/*
-		for(int i=0;i<256;i++){
-			HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_3);
-			HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_3);
+		//HAL_Delay(1);
+	}
+}
+
+
+//  Application callbacks
+// TODO - toto je zatial nefunkcne
+void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size){
+	if(huart == pUartMasterGlobal) {
+		HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_3);
+		flagUartMasterDataReady = Size;
+		HAL_UARTEx_ReceiveToIdle_DMA(pUartMasterGlobal, pMasterUartRx, 64);
+		uint8_t copy_offset = 0;
+
+		if(vMasterUartIndex == 0){
+			vMasterUartSize = pMasterUartRx[0];
+			vMasterUartIndex++;
+			vMasterUartSize--;
+			copy_offset = 1;
 		}
-		*/
+		memcpy(&pMasterUartFrame[vMasterUartIndex], &pMasterUartRx[copy_offset], vMasterUartSize);
 
+		vMasterUartIndex += vMasterUartSize;
+		if(vMasterUartIndex >= vMasterUartSize){
+			flagUartMasterDataReady = vMasterUartSize;
+			vMasterUartSize = 0;
+			vMasterUartIndex = 0;
+			pMasterUartRx[0] = 0;
+//			uart_timeout = MAX_SYSTICK;
+		}
 
 	}
 }

+ 0 - 10
Core/Src/NbusCommunicator.cpp

@@ -101,13 +101,3 @@ void NbusCommunicator::sendToMaster(DataFrame *master_frame){
 }
 
 
-/*
-//  Application callbacks
-void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size){
-	if(huart == &huart1) {
-//		if(huart->RxEventType != HAL_UART_RXEVENT_HT){
-		flag_nbus_received = Size;
-//		}
-	}
-}
-*/

+ 17 - 1
Core/Src/NbusSlave.cpp

@@ -25,14 +25,24 @@ DataFrame* NbusSlave::nbus_echo() {
 	_sensor_cache[1] = 61 + _pdu.ma;
 	_sensor_cache[2] = 62 + _pdu.ma;
 	_sensor_cache[3] = 63 + _pdu.ma;
+	_sensor_count = 0;
 	return _communicator->send(&_pdu, _sensor_cache, 4);
 }
 
+uint8_t NbusSlave::nbus_get_sensor_count(bool check_hw) {
+	if (_sensor_count == 0 || check_hw == true) {
+		nbus_sensor_count();
+	}
+	return _sensor_count;
+}
 
 DataFrame* NbusSlave::nbus_sensor_count() {
 	_pdu.sa = SLAVE_ADDRESS_MODULE;
 	_pdu.fc = FC_SENSOR_CNT;
-	return _communicator->send(&_pdu, _sensor_cache, 0);
+	DataFrame* df = _communicator->send(&_pdu, _sensor_cache, 0);
+	_sensor_count = df->GetFrame()[3];
+	return df;
+
 }
 
 DataFrame* NbusSlave::nbus_sensor_type(uint8_t sensor_address) {
@@ -70,3 +80,9 @@ DataFrame* NbusSlave::nbus_sensor_parameter(uint8_t sensor_address, uint8_t  par
 	_sensor_cache[0] = parameter;
 	return _communicator->send(&_pdu, _sensor_cache, 1);
 }
+
+DataFrame* NbusSlave::nbus_sensor_getData(uint8_t sensor_address) {
+	_pdu.sa = sensor_address;
+	_pdu.fc = FC_DATA;
+	return _communicator->send(&_pdu, _sensor_cache, 0);
+}

+ 4 - 4
Core/Src/main.c

@@ -283,8 +283,8 @@ static void MX_DMA_Init(void)
 static void MX_GPIO_Init(void)
 {
   GPIO_InitTypeDef GPIO_InitStruct = {0};
-/* USER CODE BEGIN MX_GPIO_Init_1 */
-/* USER CODE END MX_GPIO_Init_1 */
+  /* USER CODE BEGIN MX_GPIO_Init_1 */
+  /* USER CODE END MX_GPIO_Init_1 */
 
   /* GPIO Ports Clock Enable */
   __HAL_RCC_GPIOC_CLK_ENABLE();
@@ -301,8 +301,8 @@ static void MX_GPIO_Init(void)
   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
   HAL_GPIO_Init(LD3_GPIO_Port, &GPIO_InitStruct);
 
-/* USER CODE BEGIN MX_GPIO_Init_2 */
-/* USER CODE END MX_GPIO_Init_2 */
+  /* USER CODE BEGIN MX_GPIO_Init_2 */
+  /* USER CODE END MX_GPIO_Init_2 */
 }
 
 /* USER CODE BEGIN 4 */

+ 26 - 26
Core/Src/stm32l4xx_hal_msp.c

@@ -85,20 +85,20 @@ void HAL_MspInit(void)
 }
 
 /**
-* @brief UART MSP Initialization
-* This function configures the hardware resources used in this example
-* @param huart: UART handle pointer
-* @retval None
-*/
+  * @brief UART MSP Initialization
+  * This function configures the hardware resources used in this example
+  * @param huart: UART handle pointer
+  * @retval None
+  */
 void HAL_UART_MspInit(UART_HandleTypeDef* huart)
 {
   GPIO_InitTypeDef GPIO_InitStruct = {0};
   RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
   if(huart->Instance==USART1)
   {
-  /* USER CODE BEGIN USART1_MspInit 0 */
+    /* USER CODE BEGIN USART1_MspInit 0 */
 
-  /* USER CODE END USART1_MspInit 0 */
+    /* USER CODE END USART1_MspInit 0 */
 
   /** Initializes the peripherals clock
   */
@@ -163,15 +163,15 @@ void HAL_UART_MspInit(UART_HandleTypeDef* huart)
     /* USART1 interrupt Init */
     HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
     HAL_NVIC_EnableIRQ(USART1_IRQn);
-  /* USER CODE BEGIN USART1_MspInit 1 */
+    /* USER CODE BEGIN USART1_MspInit 1 */
 
-  /* USER CODE END USART1_MspInit 1 */
+    /* USER CODE END USART1_MspInit 1 */
   }
   else if(huart->Instance==USART2)
   {
-  /* USER CODE BEGIN USART2_MspInit 0 */
+    /* USER CODE BEGIN USART2_MspInit 0 */
 
-  /* USER CODE END USART2_MspInit 0 */
+    /* USER CODE END USART2_MspInit 0 */
 
   /** Initializes the peripherals clock
   */
@@ -242,26 +242,26 @@ void HAL_UART_MspInit(UART_HandleTypeDef* huart)
     /* USART2 interrupt Init */
     HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
     HAL_NVIC_EnableIRQ(USART2_IRQn);
-  /* USER CODE BEGIN USART2_MspInit 1 */
+    /* USER CODE BEGIN USART2_MspInit 1 */
 
-  /* USER CODE END USART2_MspInit 1 */
+    /* USER CODE END USART2_MspInit 1 */
   }
 
 }
 
 /**
-* @brief UART MSP De-Initialization
-* This function freeze the hardware resources used in this example
-* @param huart: UART handle pointer
-* @retval None
-*/
+  * @brief UART MSP De-Initialization
+  * This function freeze the hardware resources used in this example
+  * @param huart: UART handle pointer
+  * @retval None
+  */
 void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
 {
   if(huart->Instance==USART1)
   {
-  /* USER CODE BEGIN USART1_MspDeInit 0 */
+    /* USER CODE BEGIN USART1_MspDeInit 0 */
 
-  /* USER CODE END USART1_MspDeInit 0 */
+    /* USER CODE END USART1_MspDeInit 0 */
     /* Peripheral clock disable */
     __HAL_RCC_USART1_CLK_DISABLE();
 
@@ -278,15 +278,15 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
 
     /* USART1 interrupt DeInit */
     HAL_NVIC_DisableIRQ(USART1_IRQn);
-  /* USER CODE BEGIN USART1_MspDeInit 1 */
+    /* USER CODE BEGIN USART1_MspDeInit 1 */
 
-  /* USER CODE END USART1_MspDeInit 1 */
+    /* USER CODE END USART1_MspDeInit 1 */
   }
   else if(huart->Instance==USART2)
   {
-  /* USER CODE BEGIN USART2_MspDeInit 0 */
+    /* USER CODE BEGIN USART2_MspDeInit 0 */
 
-  /* USER CODE END USART2_MspDeInit 0 */
+    /* USER CODE END USART2_MspDeInit 0 */
     /* Peripheral clock disable */
     __HAL_RCC_USART2_CLK_DISABLE();
 
@@ -302,9 +302,9 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
 
     /* USART2 interrupt DeInit */
     HAL_NVIC_DisableIRQ(USART2_IRQn);
-  /* USER CODE BEGIN USART2_MspDeInit 1 */
+    /* USER CODE BEGIN USART2_MspDeInit 1 */
 
-  /* USER CODE END USART2_MspDeInit 1 */
+    /* USER CODE END USART2_MspDeInit 1 */
   }
 
 }

+ 3 - 2
nbus-bridge.ioc

@@ -72,8 +72,8 @@ Mcu.PinsNb=12
 Mcu.ThirdPartyNb=0
 Mcu.UserConstants=
 Mcu.UserName=STM32L432KCUx
-MxCube.Version=6.13.0
-MxDb.Version=DB.6.0.130
+MxCube.Version=6.14.0
+MxDb.Version=DB.6.0.140
 NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
 NVIC.DMA1_Channel4_IRQn=true\:0\:0\:false\:false\:true\:false\:true\:true
 NVIC.DMA1_Channel5_IRQn=true\:0\:0\:false\:false\:true\:false\:true\:true
@@ -135,6 +135,7 @@ PC15-OSC32_OUT\ (PC15).Signal=RCC_OSC32_OUT
 PinOutPanel.RotationAngle=0
 ProjectManager.AskForMigrate=true
 ProjectManager.BackupPrevious=false
+ProjectManager.CompilerLinker=GCC
 ProjectManager.CompilerOptimize=6
 ProjectManager.ComputerToolchain=false
 ProjectManager.CoupleFile=false