Browse Source

remove dynamic allocation, master communication

Juraj Ďuďák 8 months ago
parent
commit
91f22d4c35

+ 5 - 4
Core/Inc/AppBridge.h

@@ -1,10 +1,11 @@
 /*
- * AppBridge.h
- *
- *  Created on: Mar 3, 2025
- *      Author: juraj
+ * @file AppBridge.h
+ * @brief Deklarácia vysokoúrovňovej logiky firmvéru nBusBridge
+ * @date Mar 5, 2025
+ * @author Juraj Dudak
  */
 
+
 #ifndef SRC_APPBRIDGE_H_
 #define SRC_APPBRIDGE_H_
 

+ 15 - 6
Core/Inc/NbusBridge.h

@@ -1,8 +1,8 @@
 /*
- * NbusBridge.h
- *
- *  Created on: Mar 7, 2025
- *      Author: juraj
+ * @file NbusBridge.h
+ * @brief Deklarácia modulu nBus Brige
+ * @date Mar 7, 2025
+ * @author Juraj Dudak
  */
 
 #ifndef SRC_NBUSBRIDGE_H_
@@ -10,21 +10,29 @@
 
 #include "NbusSlave.h"
 
+/* DEFINES BEGIN */
+
 #define MAX_SLAVES 16
 
+#define MODULE_ADDRESS(packet)  packet[0]
+#define SENSOR_ADDRESS(packet)  packet[1]
+#define FUNCTION_CODE(packet)   packet[2]
+
+/* DEFINES END */
+
 class NbusBridge {
 
 private:
 	NbusCommunicator *_communicator;
-	NbusSlave *_slaves[MAX_SLAVES];
+	NbusSlave _slaves[MAX_SLAVES];
 	uint8_t _num_slaves;
 
 
 	void transmit(DataFrame *packet);
+	DataFrame *_frame_nbus_internal;
 
 public:
 	NbusBridge(NbusCommunicator *);
-	bool addSlave(NbusSlave *slave);
 	virtual ~NbusBridge();
 
 	void scan();
@@ -32,6 +40,7 @@ public:
 	bool call_echo(uint8_t slave);
 	NbusSlave * getSlave(uint8_t index);
 	void sendResponseToMaster(DataFrame *response_frame);
+	void processRequest(uint8_t *rxFrame);
 };
 
 #endif /* SRC_NBUSBRIDGE_H_ */

+ 8 - 4
Core/Inc/NbusCommunicator.h

@@ -1,8 +1,8 @@
 /*
- * NbusCommunicator.h
- *
- *  Created on: Mar 7, 2025
- *      Author: juraj
+ * @file NbusCommunicator.h
+ * @brief Deklarácia komunikačnej vrstvy: 1) internej 2) externej
+ * @date Mar 7, 2025
+ * @author Juraj Dudak
  */
 
 #ifndef SRC_NBUSCOMMUNICATOR_H_
@@ -11,11 +11,15 @@
 #include "inttypes.h"
 #include "dataframe.h"
 #include "nbus_structs.h"
+
 #include "stm32l4xx_hal.h"
 
 
+/* DEFINES BEGIN */
+
 #define NBUS_MAX_FRAME_SIZE 64
 
+/* DEFINES END */
 
 class NbusCommunicator {
 

+ 9 - 7
Core/Inc/NbusSlave.h

@@ -1,10 +1,9 @@
 /*
- * AppBridge.h
- *
- *  Created on: Mar 3, 2025
- *      Author: juraj
+ * @file AppBridge.h
+ * @brief Deklarácia modulu nBus Slave z pohľadu nBus Bridge
+ * @date Mar 3, 2025
+ * @author Juraj Dudak
  */
-
 #ifndef SRC_APPSLAVE_H_
 #define SRC_APPSLAVE_H_
 
@@ -28,18 +27,21 @@ private:
 
 public:
 	NbusSlave(uint8_t, NbusCommunicator*);
+	NbusSlave();
 	virtual ~NbusSlave();
+	void setCommunicator(NbusCommunicator*);
+	void setAddress(uint8_t);
+	bool isActive();
 	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);
 	DataFrame* nbus_sensor_getData(uint8_t sensor_address);
 
-	uint8_t nbus_get_sensor_count(bool check_hw);
+	uint8_t nbus_get_sensor_count(bool check_hw = false);
 
 };
 

+ 22 - 35
Core/Src/AppBridge.cpp

@@ -1,8 +1,8 @@
 /*
- * @filename AppBridge.cpp
- *
- *  @date Mar 05, 2025
- *  @author: juraj
+ * @file AppBridge.cpp
+ * @brief Vysokoúrovňová logika formvéru nBusBridge
+ * @date Mar 5, 2025
+ * @author Juraj Dudak
  */
 
 #include <NbusSlave.h>
@@ -16,14 +16,20 @@ UART_HandleTypeDef *pUartMasterGlobal;
 volatile uint32_t flagUartMasterDataReady;
 uint8_t pMasterUartRx[64];
 uint8_t pMasterUartFrame[64];
-uint8_t vMasterUartIndex;
-uint8_t vMasterUartSize;
+volatile uint8_t vMasterUartIndex;
+volatile uint8_t vMasterUartSize;
 
 static void init_app(){
 	HAL_UARTEx_ReceiveToIdle_DMA(pUartMasterGlobal, pMasterUartRx, 64);
 	flagUartMasterDataReady = 0;
 	vMasterUartIndex = 0;
 	vMasterUartSize = 0;
+
+	// boot long blink
+	for(uint32_t i = 0 ; i<64 ; i++){
+		HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_3);
+		HAL_Delay(25);
+	}
 }
 
 void app(UART_HandleTypeDef *uartNbus, UART_HandleTypeDef *uartMaster){
@@ -32,25 +38,9 @@ void app(UART_HandleTypeDef *uartNbus, UART_HandleTypeDef *uartMaster){
 	init_app();
 
 	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(&nc);
 	bridge.scan();
 
-	DataFrame *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);
-	}
 
 	while(1){
 /*
@@ -63,40 +53,37 @@ void app(UART_HandleTypeDef *uartNbus, UART_HandleTypeDef *uartMaster){
 		}
 */
 		if(flagUartMasterDataReady > 0){
-			HAL_UART_Transmit_DMA(uartMaster, pMasterUartRx, flagUartMasterDataReady);
 			flagUartMasterDataReady = 0;
+
+			bridge.processRequest(pMasterUartFrame);
 		}
 
-		//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--;
+			vMasterUartIndex = 1;
+			Size--;
 			copy_offset = 1;
 		}
-		memcpy(&pMasterUartFrame[vMasterUartIndex], &pMasterUartRx[copy_offset], vMasterUartSize);
 
-		vMasterUartIndex += vMasterUartSize;
-		if(vMasterUartIndex >= vMasterUartSize){
+		if (Size > 0) {
+			memcpy(&pMasterUartFrame[vMasterUartIndex-1], &pMasterUartRx[copy_offset], Size);
+			vMasterUartIndex += Size;
+		}
+
+		if(vMasterUartIndex > vMasterUartSize){
 			flagUartMasterDataReady = vMasterUartSize;
 			vMasterUartSize = 0;
 			vMasterUartIndex = 0;
 			pMasterUartRx[0] = 0;
-//			uart_timeout = MAX_SYSTICK;
 		}
-
 	}
 }

+ 89 - 30
Core/Src/NbusBridge.cpp

@@ -1,8 +1,8 @@
 /*
- * NbusBridge.cpp
- *
- *  Created on: Mar 7, 2025
- *      Author: juraj
+ * @file NbusBridge.cpp
+ * @brief Implemetnácia modulu nBus Brige
+ * @date Mar 7, 2025
+ * @author Juraj Dudak
  */
 
 #include "NbusBridge.h"
@@ -17,23 +17,12 @@ NbusBridge::NbusBridge(NbusCommunicator *nc) {
 	}
 	_communicator = nc;
 	_num_slaves = 0;
-	for(uint32_t i = 0 ; i<MAX_SLAVES ; i++){
-		_slaves[i] = NULL;
-	}
-
 }
 
 NbusBridge::~NbusBridge() {
-	// TODO Auto-generated destructor stub
+	// empty
 }
 
-bool NbusBridge::addSlave(NbusSlave *slave){
-	if(_num_slaves >= MAX_SLAVES){
-		return false;
-	}
-	_slaves[_num_slaves++] = slave;
-	return true;
-}
 
 void NbusBridge::scan(){
 	_num_slaves = 0;
@@ -44,7 +33,6 @@ void NbusBridge::scan(){
 	pdu.fc = FC_ECHO;
 	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++){
@@ -53,24 +41,19 @@ void NbusBridge::scan(){
 		if (!frame->IsEmpty()){
 			response = frame->GetFrame();
 			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++;
+				_slaves[i].setAddress(i);
+				_slaves[i].setCommunicator(_communicator);
 			}
 		}
 	}
-
-	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];
+	if(index >= 0 && index < MAX_SLAVES){
+		if (_slaves[index].isActive()) {
+			return &_slaves[index];
+		}
 	}
 	return NULL;
 }
@@ -85,11 +68,11 @@ bool NbusBridge::call_echo(uint8_t slave){
 	}
 	if(slave == 0){
 		for(uint32_t i = 0 ; i<_num_slaves ; i++){
-			_slaves[i]->nbus_echo();
+			getSlave(i)->nbus_echo();
 		}
 		return true;
 	}
-	_slaves[slave]->nbus_echo();
+	getSlave(slave)->nbus_echo();
 	return true;
 }
 
@@ -98,3 +81,79 @@ void NbusBridge::sendResponseToMaster(DataFrame *response_frame){
 		_communicator->sendToMaster(response_frame);
 	}
 }
+
+void NbusBridge::processRequest(uint8_t *rxFrame){
+	uint8_t send_reponse = 1;
+	NbusSlave *selected_slave = getSlave(MODULE_ADDRESS(rxFrame));
+	if (selected_slave == NULL){
+		return;
+	}
+
+	if (!selected_slave->isActive()){
+		return;
+	}
+
+	switch(FUNCTION_CODE(rxFrame)) {
+
+	case FC_FIND:			/* 0 */
+		// Not implemented yet
+	break;
+
+	case FC_ECHO:			/* 1 */
+		_frame_nbus_internal = selected_slave->nbus_echo();
+		break;
+
+	case FC_STOP:			/* 2 */
+		break;
+
+	case FC_START:			/* 3 */
+		break;
+
+	case FC_PARAM:			/* 4 */
+		break;
+
+	case FC_SENSOR_CNT:		/* 5 */
+		_frame_nbus_internal = selected_slave->nbus_sensor_count();
+		break;
+
+	case FC_SLEEP:			/* 6 */
+		break;
+
+	case FC_WAKEUP:			/* 7 */
+		break;
+
+	case FC_CALIBRATE:		/* 8 */
+		break;
+
+	case FC_RESET:			/* 9 */
+		break;
+
+	case FC_STORE:			/* 0xA => 10 */
+		break;
+
+	case FC_DATA:			/* 0xB => 11 */
+		break;
+
+	case FC_SYNC:			/* 0xC => 12 */
+		break;
+
+	case FC_SENSOR_TYPE:	/* 0xD => 13 */
+		_frame_nbus_internal = selected_slave->nbus_sensor_type(SENSOR_ADDRESS(rxFrame));
+		break;
+
+	case FC_INFO:			/* 0xE => 14 */
+		_frame_nbus_internal = selected_slave->nbus_module_info();
+		break;
+
+	case FC_SENSOR_FORMAT: 	/* 0xF => 15 */
+		_frame_nbus_internal = selected_slave->nbus_sensor_parameters(SENSOR_ADDRESS(rxFrame));
+		break;
+	default:
+		send_reponse = 0;
+	}
+
+	if (send_reponse == 1){
+		sendResponseToMaster(_frame_nbus_internal);
+	}
+
+}

+ 4 - 5
Core/Src/NbusCommunicator.cpp

@@ -1,13 +1,12 @@
 /*
  * @file NbusCommunicator.cpp
- *
- * @date: Mar 7, 2025
- * @author: juraj
+ * @brief Implemetnácia komunikačnej vrstvy: 1) internej 2) externej
+ * @date Mar 7, 2025
+ * @author Juraj Dudak
  */
 
 #include "NbusCommunicator.h"
 
-
 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)
@@ -44,7 +43,7 @@ NbusCommunicator::NbusCommunicator(UART_HandleTypeDef* uartUbus, UART_HandleType
 }
 
 NbusCommunicator::~NbusCommunicator() {
-	// TODO Auto-generated destructor stub
+	// empty
 }
 
 

+ 24 - 11
Core/Src/NbusSlave.cpp

@@ -1,12 +1,18 @@
 /*
- * AppBridge.cpp
- *
- *  Created on: Mar 3, 2025
- *      Author: juraj
+ * @file AppBridge.cpp
+ * @brief Implemetnácia modulu nBus Slave z pohľadu nBus Bridge
+ * @date Mar 2, 2025
+ * @author Juraj Dudak
  */
 
+
 #include <NbusSlave.h>
 
+NbusSlave::NbusSlave() {
+	_address = 0;
+	_communicator = NULL;
+	_pdu.ma = 0;
+}
 
 NbusSlave::NbusSlave(uint8_t address, NbusCommunicator *comm) {
 	_address = address;
@@ -15,7 +21,20 @@ NbusSlave::NbusSlave(uint8_t address, NbusCommunicator *comm) {
 }
 
 NbusSlave::~NbusSlave() {
-	// TODO Auto-generated destructor stub
+	// empty
+}
+
+void NbusSlave::setCommunicator(NbusCommunicator* comm){
+	_communicator = comm;
+}
+
+void NbusSlave::setAddress(uint8_t address){
+	_address = address;
+	_pdu.ma = _address;
+}
+
+bool NbusSlave::isActive(){
+	return _communicator != NULL && _address != 0;
 }
 
 DataFrame* NbusSlave::nbus_echo() {
@@ -58,12 +77,6 @@ DataFrame* NbusSlave::nbus_module_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);
 }