Browse Source

scan siete

Juraj Ďuďák 9 months ago
parent
commit
f13646189a

+ 6 - 7
Core/Inc/NbusBridge.h

@@ -10,14 +10,12 @@
 
 #include "NbusSlave.h"
 
-#define MAX_SLAVES 8
+#define MAX_SLAVES 16
 
 class NbusBridge {
 
 private:
-	UART_HandleTypeDef *_uartUbus;
-	uint8_t _data_packet_rx[32];
-
+	NbusCommunicator *_communicator;
 	NbusSlave *_slaves[MAX_SLAVES];
 	uint8_t _num_slaves;
 
@@ -25,13 +23,14 @@ private:
 	void transmit(DataFrame *packet);
 
 public:
-	NbusBridge(UART_HandleTypeDef *uartUbus);
+	NbusBridge(NbusCommunicator *);
 	bool addSlave(NbusSlave *slave);
 	virtual ~NbusBridge();
 
-	uint8_t send(DataFrame *);
-
+	void scan();
+	uint8_t getNumSlaves();
 	bool call_echo(uint8_t slave);
+	NbusSlave * getSlave(uint8_t index);
 };
 
 #endif /* SRC_NBUSBRIDGE_H_ */

+ 8 - 4
Core/Inc/NbusCommunicator.h

@@ -14,18 +14,22 @@
 #include "stm32l4xx_hal.h"
 
 
+#define NBUS_MAX_FRAME_SIZE 32
+
+
 class NbusCommunicator {
 
 private:
-	uint8_t _data_packet_tx[32];
-	uint8_t _data_packet_comm[32];
-	uint8_t _data_packet_rx[32];
+	uint8_t _data_packet_tx[NBUS_MAX_FRAME_SIZE];
+	uint8_t _data_packet_comm[NBUS_MAX_FRAME_SIZE];
+	uint8_t _data_packet_rx[NBUS_MAX_FRAME_SIZE];
 	DataFrame *_packet_tx;
 	DataFrame *_packet_rx;
 	UART_HandleTypeDef *_uart_nbus;
-
+	void _receive();
 public:
 	NbusCommunicator(UART_HandleTypeDef*);
+	void transmit(uint8_t *, uint8_t);
 	virtual ~NbusCommunicator();
 
 	DataFrame* send(Nbus_pdu *pdu, uint8_t *data, uint8_t data_len);

+ 2 - 2
Core/Inc/NbusSlave.h

@@ -11,10 +11,10 @@
 
 #include "inttypes.h"
 #include "dataframe.h"
-
-
 #include "NbusCommunicator.h"
 
+/** Adresa slave modulu, bez špecifikácie adresy konktrétneho senzora. */
+#define SLAVE_ADDRESS_MODULE	0
 
 class NbusSlave {
 

+ 3 - 3
Core/Inc/nbus_structs.h

@@ -1,8 +1,8 @@
 /*
- * nbus_structs.h
+ * @file nbus_structs.h
  *
- *  Created on: Mar 7, 2025
- *      Author: juraj
+ *  @date: Mar 7, 2025
+ *  @author: Juraj Dudak
  */
 
 #ifndef INC_NBUS_STRUCTS_H_

+ 14 - 10
Core/Src/AppBridge.cpp

@@ -12,23 +12,27 @@
 
 #include "NbusCommunicator.h"
 
-void app(UART_HandleTypeDef *uartUbus, UART_HandleTypeDef *uartMaster){
+void app(UART_HandleTypeDef *uartNbus, UART_HandleTypeDef *uartMaster){
 
-	NbusCommunicator *nc = new NbusCommunicator(uartUbus);
+	NbusCommunicator *nc = new NbusCommunicator(uartNbus);
 
-	NbusSlave *slave1 = new NbusSlave(5, nc);
+//	NbusSlave *slave1 = new NbusSlave(5, nc);
+//	NbusSlave *slave2 = new NbusSlave(6, nc);
+//	bridge->addSlave(slave1);
+//	bridge->addSlave(slave2);
 
-	NbusBridge *bridge = new NbusBridge(uartUbus);
-	bridge->addSlave(slave1);
-	bridge->call_echo(0);
+	NbusBridge *bridge = new NbusBridge(nc);
+	bridge->scan();
 
 	DataFrame *frameTX;
 	while(1){
 
-
-		frameTX = slave1->nbus_echo();
-		HAL_UART_Transmit_DMA(uartMaster, frameTX->GetFrame(), frameTX->GetLength());
-
+		for(int i=0; i<bridge->getNumSlaves() ; i++){
+			frameTX = bridge->getSlave(i)->nbus_echo();
+			if(!frameTX->IsEmpty()) {
+				HAL_UART_Transmit_DMA(uartMaster, frameTX->GetFrame(), frameTX->GetLength());
+			}
+		}
 
 		HAL_Delay(1);
 		/*

+ 54 - 16
Core/Src/NbusBridge.cpp

@@ -8,10 +8,18 @@
 #include "NbusBridge.h"
 #include "dataframe.h"
 
-NbusBridge::NbusBridge(UART_HandleTypeDef *uartUbus) {
-	_uartUbus = uartUbus;
+NbusBridge::NbusBridge(NbusCommunicator *nc) {
+	if(nc == NULL)
+	{
+		while(1){
+			__NOP();
+		}
+	}
+	_communicator = nc;
 	_num_slaves = 0;
-	//_packet_rx = new DataFrame (_data_packet_rx, sizeof(_data_packet_rx), TYPE_PLAIN, CRC_ON);
+	for(uint32_t i = 0 ; i<MAX_SLAVES ; i++){
+		_slaves[i] = NULL;
+	}
 
 }
 
@@ -19,19 +27,6 @@ NbusBridge::~NbusBridge() {
 	// TODO Auto-generated destructor stub
 }
 
-void NbusBridge::transmit(DataFrame *packet) {
-	HAL_UART_Transmit(_uartUbus, packet->GetFrame(), packet->GetLength(), 100);
-
-	HAL_UARTEx_ReceiveToIdle_DMA(_uartUbus, _data_packet_rx, 64);
-}
-
-uint8_t NbusBridge::send(DataFrame *frame){
-	frame->Commit();
-	transmit(frame);
-	return 0;
-}
-
-
 bool NbusBridge::addSlave(NbusSlave *slave){
 	if(_num_slaves >= MAX_SLAVES){
 		return false;
@@ -40,6 +35,49 @@ bool NbusBridge::addSlave(NbusSlave *slave){
 	return true;
 }
 
+void NbusBridge::scan(){
+	_num_slaves = 0;
+
+	DataFrame *frame;
+	uint8_t *response;
+	Nbus_pdu pdu;
+	pdu.fc = FC_ECHO;
+	pdu.sa = SLAVE_ADDRESS_MODULE;
+	uint8_t data[4] = {110, 66, 117, 115};	// nBus
+	uint8_t detected_slaves[MAX_SLAVES];
+
+	for(uint32_t i = 1 ; i < MAX_SLAVES; i++){
+		pdu.ma = i;
+		frame = _communicator->send(&pdu, data, 4);
+		if (!frame->IsEmpty()){
+			response = frame->GetFrame();
+			if(response[5] == 110 && response[6] == 66 && response[7] == 117 && response[8] == 115) {
+				detected_slaves[_num_slaves] = i;
+				_num_slaves++;
+			}
+		}
+	}
+
+	NbusSlave *slave;
+	for(uint32_t i = 0 ; i < _num_slaves; i++){
+		slave = new NbusSlave(detected_slaves[i], _communicator);
+		if (slave != NULL){
+			_slaves[i] = slave;
+		}
+	}
+}
+
+NbusSlave * NbusBridge::getSlave(uint8_t index){
+	if(index >= 0 && index<_num_slaves){
+		return _slaves[index];
+	}
+	return NULL;
+}
+
+uint8_t NbusBridge::getNumSlaves(){
+	return _num_slaves;
+}
+
 bool NbusBridge::call_echo(uint8_t slave){
 	if(slave >= _num_slaves){
 			return false;

+ 46 - 12
Core/Src/NbusCommunicator.cpp

@@ -11,7 +11,28 @@ volatile int32_t flag_data_received = -1;
 
 NbusCommunicator::NbusCommunicator(UART_HandleTypeDef* uartUbus) {
 	_packet_tx = new DataFrame (_data_packet_tx, sizeof(_data_packet_tx), TYPE_PLAIN, CRC_ON);
-	_packet_rx = new DataFrame (_data_packet_rx, sizeof(_data_packet_rx), TYPE_PLAIN, CRC_ON);
+	if(_packet_tx == NULL)
+	{
+		while(1){
+			__NOP();
+		}
+	}
+
+	_packet_rx = new DataFrame (_data_packet_rx, sizeof(_data_packet_rx), TYPE_PLAIN, CRC_ON);\
+	if(_packet_rx == NULL)
+	{
+		while(1){
+			__NOP();
+		}
+	}
+
+	if(uartUbus == NULL)
+		{
+			while(1){
+				__NOP();
+			}
+		}
+
 	_uart_nbus = uartUbus;
 
 }
@@ -21,6 +42,29 @@ NbusCommunicator::~NbusCommunicator() {
 }
 
 
+void NbusCommunicator::_receive(){
+	flag_data_received = -1;
+	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_SET);
+	HAL_UARTEx_ReceiveToIdle_DMA(_uart_nbus, _data_packet_comm, NBUS_MAX_FRAME_SIZE);
+	uint32_t stop_receive = HAL_GetTick() + 2;
+
+	while(flag_data_received < 0 && HAL_GetTick() < stop_receive);
+	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_RESET);
+	_packet_rx->Init();
+	if(flag_data_received > 0){
+		_packet_rx->AddArray(_data_packet_comm, flag_data_received);
+	}
+	_packet_rx->Commit();
+
+	flag_data_received = 0;
+}
+
+void NbusCommunicator::transmit(uint8_t *frame, uint8_t length){
+	HAL_UART_Transmit(_uart_nbus, frame, length, 100);
+
+	HAL_UARTEx_ReceiveToIdle_DMA(_uart_nbus, _data_packet_rx, 64);
+}
+
 DataFrame* NbusCommunicator::send(Nbus_pdu *pdu, uint8_t *data, uint8_t data_len){
 	_packet_tx->Init();
 	_packet_tx->AddInt8(pdu->ma);	// Module address
@@ -35,17 +79,7 @@ DataFrame* NbusCommunicator::send(Nbus_pdu *pdu, uint8_t *data, uint8_t data_len
 	HAL_UART_Transmit(_uart_nbus, _packet_tx->GetFrame(), length, 100);
 	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_RESET);
 
-	flag_data_received = -1;
-	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_SET);
-	HAL_UARTEx_ReceiveToIdle_DMA(_uart_nbus, _data_packet_comm, 32);
-	while(flag_data_received < 0);
-	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_RESET);
-	_packet_rx->Init();
-	_packet_rx->AddArray(_data_packet_comm, flag_data_received);
-	length = _packet_rx->Commit();
-
-	flag_data_received = 0;
-
+	this->_receive();
 	return _packet_rx;
 
 }

+ 1 - 1
Core/Src/NbusSlave.cpp

@@ -19,7 +19,7 @@ NbusSlave::~NbusSlave() {
 }
 
 DataFrame* NbusSlave::nbus_echo() {
-	_pdu.sa = 0;
+	_pdu.sa = SLAVE_ADDRESS_MODULE;
 	_pdu.fc = FC_ECHO;
 	sensor_cache[0] = 60 + _pdu.ma;
 	sensor_cache[1] = 61 + _pdu.ma;

+ 22 - 3
Modules/dataframe/src/dataframe.cpp

@@ -160,8 +160,8 @@ void DataFrame::Init(void) {
   }
 
   if (_type == TYPE_PLAIN) {
-    _frame[1] = 0; // length of packet
-    _length = 1;
+    _frame[0] = 0; // length of packet
+    _length = 1; //(uint8_t)_crcUse;
   }
 }
 
@@ -265,11 +265,30 @@ uint8_t DataFrame::Commit(void) {
 //	  _frame[_length - 1] = crc8x_fast(_length - 1, (uint8_t*)&(_frame[1]));
 	  _frame[_length - 1] = crc8x_fast((uint8_t*)&(_frame[1]), _length - 2 );
 	}
-    _frame[0] = _length - 1;
+	if (_length == 0){
+		_frame[0] = 0;
+	} else {
+		_frame[0] = _length - 1;
+	}
   }
   return _length;
 }
 
+bool DataFrame::IsEmpty(void){
+	  if (_type == TYPE_HEADER_1B && _length == 2) {
+	     return true;
+	  }
+
+	  if (_type == TYPE_HEADER_2B&& _length == 3) {
+		  return true;
+	  }
+
+	  if (_type == TYPE_PLAIN && _frame[0] == 1) {
+		  return true;
+	  }
+	  return false;
+}
+
 uint8_t DataFrame::GetLength(void){
 	return _length;
 }

+ 3 - 0
Modules/dataframe/src/dataframe.h

@@ -150,6 +150,9 @@ public:
   DataframeError_t getError(void);
 
   uint8_t GetLength(void);
+
+  bool IsEmpty(void);
+
 };
 
 #ifdef __cplusplus