Browse Source

prvá verzia Bridge - dokáže echovať

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

+ 1 - 1
.cproject

@@ -92,9 +92,9 @@
 						</toolChain>
 					</folderInfo>
 					<sourceEntries>
+						<entry flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="Modules/dataframe/src"/>
 						<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="Core"/>
 						<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="Drivers"/>
-						<entry flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="Modules/dataframe/src"/>
 					</sourceEntries>
 				</configuration>
 			</storageModule>

+ 37 - 0
Core/Inc/NbusBridge.h

@@ -0,0 +1,37 @@
+/*
+ * NbusBridge.h
+ *
+ *  Created on: Mar 7, 2025
+ *      Author: juraj
+ */
+
+#ifndef SRC_NBUSBRIDGE_H_
+#define SRC_NBUSBRIDGE_H_
+
+#include "NbusSlave.h"
+
+#define MAX_SLAVES 8
+
+class NbusBridge {
+
+private:
+	UART_HandleTypeDef *_uartUbus;
+	uint8_t _data_packet_rx[32];
+
+	NbusSlave *_slaves[MAX_SLAVES];
+	uint8_t _num_slaves;
+
+
+	void transmit(DataFrame *packet);
+
+public:
+	NbusBridge(UART_HandleTypeDef *uartUbus);
+	bool addSlave(NbusSlave *slave);
+	virtual ~NbusBridge();
+
+	uint8_t send(DataFrame *);
+
+	bool call_echo(uint8_t slave);
+};
+
+#endif /* SRC_NBUSBRIDGE_H_ */

+ 35 - 0
Core/Inc/NbusCommunicator.h

@@ -0,0 +1,35 @@
+/*
+ * NbusCommunicator.h
+ *
+ *  Created on: Mar 7, 2025
+ *      Author: juraj
+ */
+
+#ifndef SRC_NBUSCOMMUNICATOR_H_
+#define SRC_NBUSCOMMUNICATOR_H_
+
+#include "inttypes.h"
+#include "dataframe.h"
+#include "nbus_structs.h"
+#include "stm32l4xx_hal.h"
+
+
+class NbusCommunicator {
+
+private:
+	uint8_t _data_packet_tx[32];
+	uint8_t _data_packet_comm[32];
+	uint8_t _data_packet_rx[32];
+	DataFrame *_packet_tx;
+	DataFrame *_packet_rx;
+	UART_HandleTypeDef *_uart_nbus;
+
+public:
+	NbusCommunicator(UART_HandleTypeDef*);
+	virtual ~NbusCommunicator();
+
+	DataFrame* send(Nbus_pdu *pdu, uint8_t *data, uint8_t data_len);
+};
+
+
+#endif /* SRC_NBUSCOMMUNICATOR_H_ */

+ 47 - 0
Core/Inc/NbusSlave.h

@@ -0,0 +1,47 @@
+/*
+ * AppBridge.h
+ *
+ *  Created on: Mar 3, 2025
+ *      Author: juraj
+ */
+
+#ifndef SRC_APPSLAVE_H_
+#define SRC_APPSLAVE_H_
+
+
+#include "inttypes.h"
+#include "dataframe.h"
+
+
+#include "NbusCommunicator.h"
+
+
+class NbusSlave {
+
+private:
+	uint8_t _address;
+	NbusCommunicator* _communicator;
+	Nbus_pdu _pdu;
+	uint8_t sensor_cache[16];
+
+
+public:
+	NbusSlave(uint8_t, NbusCommunicator*);
+	DataFrame* nbus_echo();
+	virtual ~NbusSlave();
+
+
+};
+
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* SRC_APPSLAVE_H_ */

+ 0 - 54
Core/Inc/UbusSlave.h

@@ -1,54 +0,0 @@
-/*
- * AppBridge.h
- *
- *  Created on: Mar 3, 2025
- *      Author: juraj
- */
-
-#include "inttypes.h"
-#include "dataframe.h"
-
-#if defined(STM32F401xC) || defined(STM32F401xE)
-#include "stm32f4xx_hal.h"
-#endif
-
-#if defined (STM32L432xx)
-#include "stm32l4xx_hal.h"
-#endif
-
-#if defined (STM32L031xx) || defined (STM32L082xx)
-#include "stm32l0xx_hal.h"
-#endif
-
-#ifndef SRC_APPSLAVE_H_
-#define SRC_APPSLAVE_H_
-
-class UbusSlave {
-
-private:
-	uint8_t _address;
-	uint8_t _data_packet[32];
-	DataFrame *_packet;
-	UART_HandleTypeDef *_uartUbus;
-
-	void transmit(uint8_t);
-public:
-	UbusSlave(uint8_t, UART_HandleTypeDef *);
-	void ubus_echo();
-	virtual ~UbusSlave();
-
-
-};
-
-
-#ifdef __cplusplus
-extern "C"
-{
-#endif
-
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* SRC_APPSLAVE_H_ */

+ 53 - 0
Core/Inc/nbus_structs.h

@@ -0,0 +1,53 @@
+/*
+ * nbus_structs.h
+ *
+ *  Created on: Mar 7, 2025
+ *      Author: juraj
+ */
+
+#ifndef INC_NBUS_STRUCTS_H_
+#define INC_NBUS_STRUCTS_H_
+
+
+#if defined(STM32F401xC) || defined(STM32F401xE)
+#include "stm32f4xx_hal.h"
+#endif
+
+#if defined (STM32L432xx)
+#include "stm32l4xx_hal.h"
+#endif
+
+#if defined (STM32L031xx) || defined (STM32L082xx)
+#include "stm32l0xx_hal.h"
+#endif
+
+
+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,
+}Nbus_FC_e;
+
+
+typedef struct {
+	uint8_t ma;
+	uint8_t sa;
+	Nbus_FC_e fc;
+}Nbus_pdu;
+
+
+
+#endif /* INC_NBUS_STRUCTS_H_ */

+ 2 - 2
Core/Inc/stm32l4xx_hal_conf.h

@@ -236,8 +236,8 @@
 #define USE_HAL_SWPMI_REGISTER_CALLBACKS      0U
 #define USE_HAL_TIM_REGISTER_CALLBACKS        0U
 #define USE_HAL_TSC_REGISTER_CALLBACKS        0U
-#define USE_HAL_UART_REGISTER_CALLBACKS       0U
-#define USE_HAL_USART_REGISTER_CALLBACKS      0U
+#define USE_HAL_UART_REGISTER_CALLBACKS       1U
+#define USE_HAL_USART_REGISTER_CALLBACKS      1U
 #define USE_HAL_WWDG_REGISTER_CALLBACKS       0U
 
 /* ################## SPI peripheral configuration ########################## */

+ 2 - 0
Core/Inc/stm32l4xx_it.h

@@ -59,6 +59,8 @@ void DMA1_Channel4_IRQHandler(void);
 void DMA1_Channel5_IRQHandler(void);
 void DMA1_Channel6_IRQHandler(void);
 void DMA1_Channel7_IRQHandler(void);
+void USART1_IRQHandler(void);
+void USART2_IRQHandler(void);
 /* USER CODE BEGIN EFP */
 
 /* USER CODE END EFP */

+ 28 - 8
Core/Src/AppBridge.cpp

@@ -1,23 +1,43 @@
 /*
- * AppBridge.cpp
+ * @filename AppBridge.cpp
  *
- *  Created on: Nov 23, 2023
- *      Author: juraj
+ *  @date Mar 05, 2025
+ *  @author: juraj
  */
 
+#include <NbusSlave.h>
 #include "AppBridge.h"
-#include "UbusSlave.h"
 #include "dataframe.h"
+#include "NbusBridge.h"
+
+#include "NbusCommunicator.h"
 
 void app(UART_HandleTypeDef *uartUbus, UART_HandleTypeDef *uartMaster){
-	UbusSlave *slave1 = new UbusSlave(5, uartUbus);
 
+	NbusCommunicator *nc = new NbusCommunicator(uartUbus);
+
+	NbusSlave *slave1 = new NbusSlave(5, nc);
+
+	NbusBridge *bridge = new NbusBridge(uartUbus);
+	bridge->addSlave(slave1);
+	bridge->call_echo(0);
+
+	DataFrame *frameTX;
 	while(1){
 
-		slave1->ubus_echo();
 
-		HAL_Delay(2);
-		HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_3);
+		frameTX = slave1->nbus_echo();
+		HAL_UART_Transmit_DMA(uartMaster, frameTX->GetFrame(), frameTX->GetLength());
+
+
+		HAL_Delay(1);
+		/*
+		for(int i=0;i<256;i++){
+			HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_3);
+			HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_3);
+		}
+		*/
+
 
 	}
 }

+ 55 - 0
Core/Src/NbusBridge.cpp

@@ -0,0 +1,55 @@
+/*
+ * NbusBridge.cpp
+ *
+ *  Created on: Mar 7, 2025
+ *      Author: juraj
+ */
+
+#include "NbusBridge.h"
+#include "dataframe.h"
+
+NbusBridge::NbusBridge(UART_HandleTypeDef *uartUbus) {
+	_uartUbus = uartUbus;
+	_num_slaves = 0;
+	//_packet_rx = new DataFrame (_data_packet_rx, sizeof(_data_packet_rx), TYPE_PLAIN, CRC_ON);
+
+}
+
+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;
+	}
+	_slaves[_num_slaves++] = slave;
+	return true;
+}
+
+bool NbusBridge::call_echo(uint8_t slave){
+	if(slave >= _num_slaves){
+			return false;
+	}
+	if(slave == 0){
+		for(uint32_t i = 0 ; i<_num_slaves ; i++){
+			_slaves[i]->nbus_echo();
+		}
+		return true;
+	}
+	_slaves[slave]->nbus_echo();
+	return true;
+}

+ 56 - 0
Core/Src/NbusCommunicator.cpp

@@ -0,0 +1,56 @@
+/*
+ * @file NbusCommunicator.cpp
+ *
+ * @date: Mar 7, 2025
+ * @author: juraj
+ */
+
+#include "NbusCommunicator.h"
+
+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);
+	_uart_nbus = uartUbus;
+
+}
+
+NbusCommunicator::~NbusCommunicator() {
+	// TODO Auto-generated destructor stub
+}
+
+
+DataFrame* NbusCommunicator::send(Nbus_pdu *pdu, uint8_t *data, uint8_t data_len){
+	_packet_tx->Init();
+	_packet_tx->AddInt8(pdu->ma);	// Module address
+	_packet_tx->AddInt8(pdu->sa);	// Slave address
+	_packet_tx->AddInt8(pdu->fc);	// ECHO
+	for(int i = 0 ; i< data_len ; i++){
+		_packet_tx->AddInt8(data[i]);
+	}
+	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_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;
+
+	return _packet_rx;
+
+}
+
+//  Application callbacks
+void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size){
+	flag_data_received = Size;
+}

+ 32 - 0
Core/Src/NbusSlave.cpp

@@ -0,0 +1,32 @@
+/*
+ * AppBridge.cpp
+ *
+ *  Created on: Mar 3, 2025
+ *      Author: juraj
+ */
+
+#include <NbusSlave.h>
+
+
+NbusSlave::NbusSlave(uint8_t address, NbusCommunicator *comm) {
+	_address = address;
+	_communicator = comm;
+	_pdu.ma = address;
+}
+
+NbusSlave::~NbusSlave() {
+	// TODO Auto-generated destructor stub
+}
+
+DataFrame* NbusSlave::nbus_echo() {
+	_pdu.sa = 0;
+	_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);
+
+
+
+}

+ 0 - 39
Core/Src/UbusSlave.cpp

@@ -1,39 +0,0 @@
-/*
- * AppBridge.cpp
- *
- *  Created on: Mar 3, 2025
- *      Author: juraj
- */
-
-#include <UbusSlave.h>
-
-
-
-UbusSlave::UbusSlave(uint8_t address, UART_HandleTypeDef *uartUbus) {
-	_address = address;
-	_uartUbus = uartUbus;
-	_packet = new DataFrame (_data_packet, sizeof(_data_packet), TYPE_PLAIN, CRC_ON);
-}
-
-UbusSlave::~UbusSlave() {
-	// TODO Auto-generated destructor stub
-}
-
-
-void UbusSlave::transmit(uint8_t size) {
-	HAL_UART_Transmit(_uartUbus, _packet->GetFrame(), size, 100);
-
-}
-
-
-void UbusSlave::ubus_echo() {
-	_packet->Init();
-	_packet->AddInt8(_address);
-	_packet->AddInt8(0);	// toMODULE
-	_packet->AddInt8(1);	// ECHO
-	_packet->AddInt32(0x64636261);
-	uint8_t length = _packet->Commit();
-
-	transmit(length);
-
-}

+ 12 - 0
Core/Src/stm32l4xx_hal_msp.c

@@ -160,6 +160,9 @@ void HAL_UART_MspInit(UART_HandleTypeDef* huart)
 
     __HAL_LINKDMA(huart,hdmatx,hdma_usart1_tx);
 
+    /* USART1 interrupt Init */
+    HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
+    HAL_NVIC_EnableIRQ(USART1_IRQn);
   /* USER CODE BEGIN USART1_MspInit 1 */
 
   /* USER CODE END USART1_MspInit 1 */
@@ -236,6 +239,9 @@ void HAL_UART_MspInit(UART_HandleTypeDef* huart)
 
     __HAL_LINKDMA(huart,hdmatx,hdma_usart2_tx);
 
+    /* USART2 interrupt Init */
+    HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
+    HAL_NVIC_EnableIRQ(USART2_IRQn);
   /* USER CODE BEGIN USART2_MspInit 1 */
 
   /* USER CODE END USART2_MspInit 1 */
@@ -269,6 +275,9 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
     /* USART1 DMA DeInit */
     HAL_DMA_DeInit(huart->hdmarx);
     HAL_DMA_DeInit(huart->hdmatx);
+
+    /* USART1 interrupt DeInit */
+    HAL_NVIC_DisableIRQ(USART1_IRQn);
   /* USER CODE BEGIN USART1_MspDeInit 1 */
 
   /* USER CODE END USART1_MspDeInit 1 */
@@ -290,6 +299,9 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
     /* USART2 DMA DeInit */
     HAL_DMA_DeInit(huart->hdmarx);
     HAL_DMA_DeInit(huart->hdmatx);
+
+    /* USART2 interrupt DeInit */
+    HAL_NVIC_DisableIRQ(USART2_IRQn);
   /* USER CODE BEGIN USART2_MspDeInit 1 */
 
   /* USER CODE END USART2_MspDeInit 1 */

+ 30 - 0
Core/Src/stm32l4xx_it.c

@@ -59,6 +59,8 @@ extern DMA_HandleTypeDef hdma_usart1_rx;
 extern DMA_HandleTypeDef hdma_usart1_tx;
 extern DMA_HandleTypeDef hdma_usart2_rx;
 extern DMA_HandleTypeDef hdma_usart2_tx;
+extern UART_HandleTypeDef huart1;
+extern UART_HandleTypeDef huart2;
 /* USER CODE BEGIN EV */
 
 /* USER CODE END EV */
@@ -257,6 +259,34 @@ void DMA1_Channel7_IRQHandler(void)
   /* USER CODE END DMA1_Channel7_IRQn 1 */
 }
 
+/**
+  * @brief This function handles USART1 global interrupt.
+  */
+void USART1_IRQHandler(void)
+{
+  /* USER CODE BEGIN USART1_IRQn 0 */
+
+  /* USER CODE END USART1_IRQn 0 */
+  HAL_UART_IRQHandler(&huart1);
+  /* USER CODE BEGIN USART1_IRQn 1 */
+
+  /* USER CODE END USART1_IRQn 1 */
+}
+
+/**
+  * @brief This function handles USART2 global interrupt.
+  */
+void USART2_IRQHandler(void)
+{
+  /* USER CODE BEGIN USART2_IRQn 0 */
+
+  /* USER CODE END USART2_IRQn 0 */
+  HAL_UART_IRQHandler(&huart2);
+  /* USER CODE BEGIN USART2_IRQn 1 */
+
+  /* USER CODE END USART2_IRQn 1 */
+}
+
 /* USER CODE BEGIN 1 */
 
 /* USER CODE END 1 */

+ 17 - 0
Modules/dataframe/src/dataframe.cpp

@@ -202,6 +202,19 @@ bool DataFrame::AddUint32(uint32_t d) {
   return true;
 }
 
+bool DataFrame::AddArray(uint8_t *data, uint8_t length) {
+  if ((_length + length) >= _capacity) {
+    _error = ERROR_OVERFLOW;
+    return false;
+  }
+
+  for(uint32_t i=0 ; i<length ; i++){
+	  _frame[_length++] = data[i];
+  }
+
+  return true;
+}
+
 bool DataFrame::AddInt8(int8_t d) { return this->AddUint8((uint8_t)d); }
 
 bool DataFrame::AddInt16(int16_t d) { return this->AddUint16((uint16_t)d); }
@@ -257,6 +270,10 @@ uint8_t DataFrame::Commit(void) {
   return _length;
 }
 
+uint8_t DataFrame::GetLength(void){
+	return _length;
+}
+
 DataframeError_t DataFrame::getError(void) { return _error; }
 
 uint8_t *DataFrame::GetFrame(void) { return _frame; }

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

@@ -97,6 +97,8 @@ public:
    */
   bool AddUint32(uint32_t);
 
+  bool AddArray(uint8_t *data, uint8_t length);
+
   /**
    * @brief add one byte signed variable to data frame
    * @param d variable added to data frame
@@ -146,6 +148,8 @@ public:
    * increased in user code.
    */
   DataframeError_t getError(void);
+
+  uint8_t GetLength(void);
 };
 
 #ifdef __cplusplus

+ 5 - 2
nbus-bridge.ioc

@@ -88,6 +88,8 @@ NVIC.PendSV_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
 NVIC.PriorityGroup=NVIC_PRIORITYGROUP_4
 NVIC.SVCall_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
 NVIC.SysTick_IRQn=true\:0\:0\:true\:false\:true\:true\:true\:false
+NVIC.USART1_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true
+NVIC.USART2_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true
 NVIC.UsageFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
 PA0.GPIOParameters=GPIO_Label
 PA0.GPIO_Label=MCO [High speed clock in]
@@ -154,7 +156,7 @@ ProjectManager.ProjectBuild=false
 ProjectManager.ProjectFileName=nbus-bridge.ioc
 ProjectManager.ProjectName=nbus-bridge
 ProjectManager.ProjectStructure=
-ProjectManager.RegisterCallBack=
+ProjectManager.RegisterCallBack=UART,USART
 ProjectManager.StackSize=0x400
 ProjectManager.TargetToolchain=STM32CubeIDE
 ProjectManager.ToolChainLocation=
@@ -217,7 +219,8 @@ RCC.VCOOutputFreq_Value=160000000
 RCC.VCOSAI1OutputFreq_Value=32000000
 RCC.WatchDogFreq_Value=32000
 USART1.BaudRate=921600
-USART1.IPParameters=VirtualMode-Asynchronous,VirtualMode-Hardware Flow Control (RS485),BaudRate
+USART1.IPParameters=VirtualMode-Asynchronous,VirtualMode-Hardware Flow Control (RS485),BaudRate,OverSampling
+USART1.OverSampling=UART_OVERSAMPLING_16
 USART1.VirtualMode-Asynchronous=VM_ASYNC
 USART1.VirtualMode-Hardware\ Flow\ Control\ (RS485)=VM_ASYNC
 USART2.BaudRate=921600