Browse Source

prve prikazy

Juraj Ďuďák 9 months ago
parent
commit
6cac437dda

+ 1 - 0
Core/Inc/NbusBridge.h

@@ -31,6 +31,7 @@ public:
 	uint8_t getNumSlaves();
 	bool call_echo(uint8_t slave);
 	NbusSlave * getSlave(uint8_t index);
+	void sendResponseToMaster(DataFrame *response_frame);
 };
 
 #endif /* SRC_NBUSBRIDGE_H_ */

+ 4 - 2
Core/Inc/NbusCommunicator.h

@@ -14,7 +14,7 @@
 #include "stm32l4xx_hal.h"
 
 
-#define NBUS_MAX_FRAME_SIZE 32
+#define NBUS_MAX_FRAME_SIZE 64
 
 
 class NbusCommunicator {
@@ -26,10 +26,12 @@ private:
 	DataFrame *_packet_tx;
 	DataFrame *_packet_rx;
 	UART_HandleTypeDef *_uart_nbus;
+	UART_HandleTypeDef *_uart_master;
 	void _receive();
 public:
-	NbusCommunicator(UART_HandleTypeDef*);
+	NbusCommunicator(UART_HandleTypeDef*, UART_HandleTypeDef*);
 	void transmit(uint8_t *, uint8_t);
+	void sendToMaster(DataFrame *master_frame);
 	virtual ~NbusCommunicator();
 
 	DataFrame* send(Nbus_pdu *pdu, uint8_t *data, uint8_t data_len);

+ 9 - 4
Core/Inc/NbusSlave.h

@@ -22,14 +22,19 @@ private:
 	uint8_t _address;
 	NbusCommunicator* _communicator;
 	Nbus_pdu _pdu;
-	uint8_t sensor_cache[16];
-
+	uint8_t _sensor_cache[16];
 
 public:
 	NbusSlave(uint8_t, NbusCommunicator*);
-	DataFrame* nbus_echo();
 	virtual ~NbusSlave();
-
+	DataFrame* nbus_echo();
+	DataFrame* nbus_sensor_count();
+	DataFrame* nbus_sensor_type(uint8_t);
+	DataFrame* nbus_sensor_parameters(uint8_t);
+	DataFrame* nbus_sensors_parameters();
+	DataFrame* nbus_module_info();
+	DataFrame* nbus_module_format();
+	DataFrame* nbus_sensor_parameter(uint8_t, uint8_t);
 
 };
 

+ 8 - 4
Core/Src/AppBridge.cpp

@@ -14,7 +14,7 @@
 
 void app(UART_HandleTypeDef *uartNbus, UART_HandleTypeDef *uartMaster){
 
-	NbusCommunicator *nc = new NbusCommunicator(uartNbus);
+	NbusCommunicator *nc = new NbusCommunicator(uartNbus, uartMaster);
 
 //	NbusSlave *slave1 = new NbusSlave(5, nc);
 //	NbusSlave *slave2 = new NbusSlave(6, nc);
@@ -29,9 +29,13 @@ void app(UART_HandleTypeDef *uartNbus, UART_HandleTypeDef *uartMaster){
 
 		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());
-			}
+			bridge->sendResponseToMaster(frameTX);
+
+			frameTX = bridge->getSlave(i)->nbus_module_info();
+			bridge->sendResponseToMaster(frameTX);
+//			if(!frameTX->IsEmpty()) {
+//				HAL_UART_Transmit_DMA(uartMaster, frameTX->GetFrame(), frameTX->GetLength());
+//			}
 		}
 
 		HAL_Delay(1);

+ 8 - 1
Core/Src/NbusBridge.cpp

@@ -45,13 +45,14 @@ void NbusBridge::scan(){
 	pdu.sa = SLAVE_ADDRESS_MODULE;
 	uint8_t data[4] = {110, 66, 117, 115};	// nBus
 	uint8_t detected_slaves[MAX_SLAVES];
+	const uint8_t data_offset = 4;
 
 	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) {
+			if(response[0+data_offset] == 110 && response[1+data_offset] == 66 && response[2+data_offset] == 117 && response[3+data_offset] == 115) {
 				detected_slaves[_num_slaves] = i;
 				_num_slaves++;
 			}
@@ -91,3 +92,9 @@ bool NbusBridge::call_echo(uint8_t slave){
 	_slaves[slave]->nbus_echo();
 	return true;
 }
+
+void NbusBridge::sendResponseToMaster(DataFrame *response_frame){
+	if(response_frame !=NULL && response_frame->IsEmpty() == false) {
+		_communicator->sendToMaster(response_frame);
+	}
+}

+ 24 - 10
Core/Src/NbusCommunicator.cpp

@@ -9,8 +9,8 @@
 
 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);
+NbusCommunicator::NbusCommunicator(UART_HandleTypeDef* uartUbus, UART_HandleTypeDef *uartMaster) {
+	_packet_tx = new DataFrame(_data_packet_tx, sizeof(_data_packet_tx), TYPE_PLAIN, CRC_ON);
 	if(_packet_tx == NULL)
 	{
 		while(1){
@@ -18,7 +18,7 @@ NbusCommunicator::NbusCommunicator(UART_HandleTypeDef* uartUbus) {
 		}
 	}
 
-	_packet_rx = new DataFrame (_data_packet_rx, sizeof(_data_packet_rx), TYPE_PLAIN, CRC_ON);\
+	_packet_rx = new DataFrame(_data_packet_rx, sizeof(_data_packet_rx), TYPE_PLAIN, CRC_OFF);\
 	if(_packet_rx == NULL)
 	{
 		while(1){
@@ -27,14 +27,21 @@ NbusCommunicator::NbusCommunicator(UART_HandleTypeDef* uartUbus) {
 	}
 
 	if(uartUbus == NULL)
-		{
-			while(1){
-				__NOP();
-			}
+	{
+		while(1){
+			__NOP();
 		}
-
+	}
 	_uart_nbus = uartUbus;
 
+	if(uartMaster == NULL)
+	{
+		while(1){
+			__NOP();
+		}
+	}
+	_uart_master = uartMaster;
+
 }
 
 NbusCommunicator::~NbusCommunicator() {
@@ -50,9 +57,11 @@ void NbusCommunicator::_receive(){
 
 	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->AddArray(_data_packet_comm, flag_data_received);
+		_packet_rx->FromArray(_data_packet_comm, flag_data_received);
 	}
 	_packet_rx->Commit();
 
@@ -76,7 +85,7 @@ DataFrame* NbusCommunicator::send(Nbus_pdu *pdu, uint8_t *data, uint8_t data_len
 	int length = _packet_tx->Commit();
 
 	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_SET);
-	HAL_UART_Transmit(_uart_nbus, _packet_tx->GetFrame(), length, 100);
+	HAL_UART_Transmit_DMA(_uart_nbus, _packet_tx->GetFrame(), length);
 	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_RESET);
 
 	this->_receive();
@@ -84,6 +93,11 @@ DataFrame* NbusCommunicator::send(Nbus_pdu *pdu, uint8_t *data, uint8_t data_len
 
 }
 
+void NbusCommunicator::sendToMaster(DataFrame *master_frame){
+	HAL_UART_Transmit_DMA(_uart_master, master_frame->GetFrame(), master_frame->GetLength());
+}
+
+
 //  Application callbacks
 void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size){
 	flag_data_received = Size;

+ 45 - 5
Core/Src/NbusSlave.cpp

@@ -21,12 +21,52 @@ NbusSlave::~NbusSlave() {
 DataFrame* NbusSlave::nbus_echo() {
 	_pdu.sa = SLAVE_ADDRESS_MODULE;
 	_pdu.fc = FC_ECHO;
-	sensor_cache[0] = 60 + _pdu.ma;
-	sensor_cache[1] = 61 + _pdu.ma;
-	sensor_cache[2] = 62 + _pdu.ma;
-	sensor_cache[3] = 63 + _pdu.ma;
-	return _communicator->send(&_pdu, sensor_cache, 4);
+	_sensor_cache[0] = 60 + _pdu.ma;
+	_sensor_cache[1] = 61 + _pdu.ma;
+	_sensor_cache[2] = 62 + _pdu.ma;
+	_sensor_cache[3] = 63 + _pdu.ma;
+	return _communicator->send(&_pdu, _sensor_cache, 4);
+}
+
+
+DataFrame* NbusSlave::nbus_sensor_count() {
+	_pdu.sa = SLAVE_ADDRESS_MODULE;
+	_pdu.fc = FC_SENSOR_CNT;
+	return _communicator->send(&_pdu, _sensor_cache, 0);
+}
+
+DataFrame* NbusSlave::nbus_sensor_type(uint8_t sensor_address) {
+	_pdu.sa = sensor_address;
+	_pdu.fc = FC_SENSOR_TYPE;
+	return _communicator->send(&_pdu, _sensor_cache, 0);
+}
+
 
+DataFrame* NbusSlave::nbus_module_info() {
+	_pdu.sa = SLAVE_ADDRESS_MODULE;
+	_pdu.fc = FC_INFO;
+	return _communicator->send(&_pdu, _sensor_cache, 0);
+}
+
+DataFrame* NbusSlave::nbus_module_format() {
+	_pdu.sa = SLAVE_ADDRESS_MODULE;
+	_pdu.fc = FC_SENSOR_FORMAT;
+	return _communicator->send(&_pdu, _sensor_cache, 0);
+}
+
+DataFrame* NbusSlave::nbus_sensors_parameters() {
+	return nbus_sensor_parameters(0);
+}
 
+DataFrame* NbusSlave::nbus_sensor_parameters(uint8_t sensor_address) {
+	_pdu.sa = sensor_address;
+	_pdu.fc = FC_SENSOR_FORMAT;
+	return _communicator->send(&_pdu, _sensor_cache, 0);
+}
 
+DataFrame* NbusSlave::nbus_sensor_parameter(uint8_t sensor_address, uint8_t  parameter) {
+	_pdu.sa = sensor_address;
+	_pdu.fc = FC_SENSOR_FORMAT;
+	_sensor_cache[0] = parameter;
+	return _communicator->send(&_pdu, _sensor_cache, 1);
 }

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

@@ -145,6 +145,7 @@ DataFrame::DataFrame(uint8_t *frame, uint8_t size, DataframeType_t type,
 
 void DataFrame::Init(void) {
   _error = ERROR_NONE;
+  _raw_data = false;
 
   if (_type == TYPE_HEADER_1B) {
     _frame[0] = HEADER_CHAR1;
@@ -165,6 +166,13 @@ void DataFrame::Init(void) {
   }
 }
 
+void DataFrame::FromArray(uint8_t *data, uint8_t size){
+	memcpy(_frame, data, size);
+//	_frame = data;
+	_length = size;
+	_raw_data = true;
+}
+
 bool DataFrame::AddUint8(uint8_t d) {
   if ((_length + 1) >= _capacity) {
     _error = ERROR_OVERFLOW;
@@ -239,6 +247,10 @@ bool DataFrame::AddFloat(float f) {
 }
 
 uint8_t DataFrame::Commit(void) {
+  if (_raw_data == true){
+	  return _length;
+  }
+
   if (_type == TYPE_HEADER_1B) {
     // compute _length
     if (_crcUse == CRC_ON) {
@@ -279,12 +291,28 @@ bool DataFrame::IsEmpty(void){
 	     return true;
 	  }
 
-	  if (_type == TYPE_HEADER_2B&& _length == 3) {
+	  if (_type == TYPE_HEADER_2B && _length == 3) {
 		  return true;
 	  }
 
-	  if (_type == TYPE_PLAIN && _frame[0] == 1) {
-		  return true;
+	  if (_type == TYPE_PLAIN )
+	  {
+		  if (_crcUse == CRC_ON && _frame[0] == 1)
+		  {
+			  return true;
+		  }
+		  if (_crcUse == CRC_OFF && _frame[0] == 0)
+		  {
+			  return true;
+		  }
+//		  if (_raw_data == true && _frame[0] == 0)
+//		  {
+//			  return true;
+//		  }
+//		  if (_raw_data == false && _frame[0] == 1)
+//		  {
+//			  return true;
+//		  }
 	  }
 	  return false;
 }

+ 5 - 1
Modules/dataframe/src/dataframe.h

@@ -16,7 +16,8 @@
 /** Character for preamble - 2nd byte, if is needed */
 #define HEADER_CHAR2 0xDD
 
-#include <locale.h>
+#include  <locale.h>
+#include  <string.h>
 /**
  * Definition of DatFrame type.
  * Can be simple frame, or frame with some preamble.
@@ -62,6 +63,7 @@ private:
   DataframeType_t _type;
   DataframeCrc_t _crcUse;
   DataframeError_t _error;
+  bool _raw_data;
 
 public:
   /**
@@ -153,6 +155,8 @@ public:
 
   bool IsEmpty(void);
 
+  void FromArray(uint8_t *data, uint8_t size);
+
 };
 
 #ifdef __cplusplus