Prechádzať zdrojové kódy

Minimized dependencies in NbusCommunicator

xnecas 6 dní pred
rodič
commit
c484a980e3

+ 0 - 64
Inc/NbusCommunicator.h

@@ -1,64 +0,0 @@
-/**
- * @file NbusCommunicator.h
- * @brief Declaration of hardware communication interface.
- * @date Jun 19, 2026
- * @author Juraj Dudak, Matus Necas
- */
-
-#ifndef SRC_NBUSCOMMUNICATOR_H_
-#define SRC_NBUSCOMMUNICATOR_H_
-
-#include "inttypes.h"
-#include "dataframe.h"
-#include "nbus_config.h"
-#include "nbus_defines.h"
-#include "nbus_platform.h"
-
-/** @brief Class for NBus Communicator handle.
- */
-class NbusCommunicator
-{
-public:
-
-	/** Constructor.
-	 * @param uart_nbus: pointer to UART nBus instance
-	 * @param uart_master: pointer to UART master instance
-	 */
-	NbusCommunicator(UART_HandleTypeDef *uart_nbus, UART_HandleTypeDef *uart_master);
-
-	/** Send data to master.
-	 *  @param master_frame: data frame to send
-	 */
-	void sendToMaster(DataFrame *master_frame);
-
-	/** Send data to slave.
-	 *  @param slave_frame: data frame to send
-	 */
-	void sendToSlave(DataFrame *slave_frame);
-
-	/** Send data to slave and receive response.
-	 *  @param slave_frame: data frame to send
-	 *  @return: response data frame
-	 */
-	DataFrame* sendAndReceiveSlave(DataFrame *slave_frame);
-
-	void setLedOn();
-	void setLedOff();
-	void toggleLed();
-
-private:
-
-	/** Receive data from slave.
-	 */
-	void _receiveFromSlave();
-
-	uint8_t _data_packet_tx[NBUS_COMM_MAX_FRAME_SIZE]{0};									///< raw TX data packet
-	uint8_t _data_packet_comm[NBUS_COMM_MAX_FRAME_SIZE]{0};									///< raw common data packet
-	uint8_t _data_packet_rx[NBUS_COMM_MAX_FRAME_SIZE]{0};									///< raw RX data packet
-	DataFrame _packet_tx{_data_packet_tx, NBUS_COMM_MAX_FRAME_SIZE, TYPE_PLAIN, CRC_ON};	///< TX data frame
-	DataFrame _packet_rx{_data_packet_rx, NBUS_COMM_MAX_FRAME_SIZE, TYPE_PLAIN, CRC_OFF};   ///< RX data frame
-	UART_HandleTypeDef *_uart_nbus{nullptr};												///< pointer to UART nBus
-	UART_HandleTypeDef *_uart_master{nullptr};												///< pointer to UART master
-};
-
-#endif /* SRC_NBUSCOMMUNICATOR_H_ */

+ 1 - 1
Src/NbusBridge.cpp

@@ -354,7 +354,7 @@ DataFrame* NbusBridge::_forwardPacket(uint8_t *data, uint8_t size)
 
 void NbusBridge::_addDataPayload(DataFrame * data_frame)
 {
-	data_frame->AddUint32(HAL_GetTick());
+	data_frame->AddUint32(_communicator->getTime());
 
 	for (uint32_t i = 0; i < _num_slaves; i++)
 	{

+ 0 - 203
Src/NbusCommunicator.cpp

@@ -1,203 +0,0 @@
-/**
- * @file NbusCommunicator.cpp
- * @brief Implementation of hardware communication interface.
- * @date Jun 19, 2026
- * @author Juraj Dudak, Matus Necas
- */
-
-#include "NbusCommunicator.h"
-
-#if !defined(ESP32)
-
-NbusCommunicator::NbusCommunicator(UART_HandleTypeDef *uartNbus, UART_HandleTypeDef *uartMaster)
-: _uart_nbus{uartNbus}, _uart_master{uartMaster}
-{
-
-}
-
-void NbusCommunicator::sendToMaster(DataFrame *master_frame)
-{
-	if (master_frame == nullptr || master_frame->IsEmpty())
-		return;
-
-	while (_uart_master->gState != HAL_UART_STATE_READY)
-    {
-        __NOP(); // cakanie na ukoncenie prebiehajuceho odosielania
-                 // tu to moze byt vcelku tesno odoslany dalsi paket. je tam pauza o dlzke 1.2bit
-    }
-
-    HAL_UART_Transmit_DMA(_uart_master, master_frame->GetFrame(), master_frame->GetLength());
-}
-
-void NbusCommunicator::sendToSlave(DataFrame *slaveFrame)
-{
-    setLedOn();
-    HAL_UART_Transmit(_uart_nbus, slaveFrame->GetFrame(), slaveFrame->GetLength(), NBUS_COMM_UART_TX_TIMEOUT);
-    setLedOff();
-}
-
-DataFrame* NbusCommunicator::sendAndReceiveSlave(DataFrame *slave_frame)
-{
-    this->sendToSlave(slave_frame);
-    this->_receiveFromSlave();
-    return &_packet_rx;
-}
-
-void NbusCommunicator::_receiveFromSlave()
-{
-    uint16_t received_size;
-    setLedOn();
-
-    memset(_data_packet_comm, 0, NBUS_COMM_MAX_FRAME_SIZE);
-    // DMA communication must be in blocking mode for less overhead
-    HAL_StatusTypeDef status = HAL_UARTEx_ReceiveToIdle(_uart_nbus, _data_packet_comm, NBUS_COMM_MAX_FRAME_SIZE,
-                                                        &received_size, NBUS_COMM_UART_RX_TIMEOUT);
-
-    _packet_rx.Init();
-
-    if (status == HAL_OK)
-    {
-        if (received_size > 0)
-        {
-            _packet_rx.FromArray(_data_packet_comm, received_size);
-        }
-    }
-
-    setLedOff();
-
-    _packet_rx.Commit();
-}
-
-void NbusCommunicator::setLedOn()
-{
-#if NBUS_USE_LED
-    #if NBUS_LED_POLARITY_REVERSED
-        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_RESET);
-    #else
-        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_SET);
-    #endif // POLARITY
-#endif // NBUS_USE_LED
-}
-
-void NbusCommunicator::setLedOff()
-{
-#if NBUS_USE_LED
-    #if NBUS_LED_POLARITY_REVERSED
-        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_SET);
-    #else
-        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_RESET);
-    #endif // POLARITY
-#endif // NBUS_USE_LED
-}
-
-void NbusCommunicator::toggleLed()
-{
-#if NBUS_USE_LED
-    HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_3);
-#endif // NBUS_USE_LED
-}
-
-#else // ESP32
-
-NbusCommunicator::NbusCommunicator(UART_HandleTypeDef *uartNbus, UART_HandleTypeDef *uartMaster)
-: _uart_nbus{uartNbus}, _uart_master{uartMaster}
-{
-
-}
-
-void NbusCommunicator::sendToMaster(DataFrame *master_frame)
-{
-	if (master_frame == nullptr || master_frame->IsEmpty())
-		return;
-
-    _uart_master->write(master_frame->GetFrame(), master_frame->GetLength());
-}
-
-void NbusCommunicator::sendToSlave(DataFrame *slaveFrame)
-{
-    setLedOn();
-    _uart_nbus->write(slaveFrame->GetFrame(), slaveFrame->GetLength());
-    setLedOff();
-}
-
-DataFrame* NbusCommunicator::sendAndReceiveSlave(DataFrame *slave_frame)
-{
-    this->sendToSlave(slave_frame);
-    this->_receiveFromSlave();
-    return &_packet_rx;
-}
-
-void NbusCommunicator::_receiveFromSlave()
-{
-    int16_t packet_size = 0;
-    int16_t received_size = 0;
-    int16_t in_bytes = 0;
-    //HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_SET);
-    
-    // reference time
-    time_t time0 = millis();
-
-    // read packet length
-    while (packet_size == 0 && millis() - time0 <= NBUS_COMM_UART_RX_TIMEOUT)
-    {
-        if (_uart_nbus->available())
-        {
-            packet_size = _uart_nbus->read();
-            _data_packet_comm[received_size++] = packet_size;
-        }
-    }
-
-    // read rest of the packet
-    while (received_size < packet_size && millis() - time0 <= NBUS_COMM_UART_RX_TIMEOUT)
-    {
-        in_bytes = _uart_nbus->available();
-        
-        if (in_bytes > 0)
-        {
-            _uart_nbus->readBytes(_data_packet_comm + received_size, in_bytes);
-            received_size += in_bytes;
-        }
-    }
-
-    _packet_rx.Init();
-
-    if (received_size > 0)
-    {
-        _packet_rx.FromArray(_data_packet_comm, received_size);
-    }
-
-    //HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_RESET);
-
-    _packet_rx.Commit();
-}
-
-void NbusCommunicator::setLedOn()
-{
-#if NBUS_USE_LED
-    #if NBUS_LED_POLARITY_REVERSED
-        digitalWrite(NBUS_LED_PIN, LOW);
-    #else
-        digitalWrite(NBUS_LED_PIN, HIGH);
-    #endif // POLARITY    
-#endif // NBUS_USE_LED
-}
-
-void NbusCommunicator::setLedOff()
-{
-#if NBUS_USE_LED
-    #if NBUS_LED_POLARITY_REVERSED
-        digitalWrite(NBUS_LED_PIN, HIGH);
-    #else
-        digitalWrite(NBUS_LED_PIN, LOW);
-    #endif // POLARITY    
-#endif // NBUS_USE_LED
-}
-
-void NbusCommunicator::toggleLed()
-{
-#if NBUS_USE_LED
-    digitalWrite(NBUS_LED_PIN, !digitalRead(NBUS_LED_PIN));
-#endif // NBUS_USE_LED
-}
-
-#endif // platform

+ 0 - 0
Inc/NbusBridge.h → include/NbusBridge.h


+ 65 - 0
include/NbusCommunicator.h

@@ -0,0 +1,65 @@
+/**
+ * @file NbusCommunicator.h
+ * @brief Declaration of hardware communication interface.
+ * @date Jun 23, 2026
+ * @author Juraj Dudak, Matus Necas
+ */
+
+#ifndef SRC_NBUSCOMMUNICATOR_H_
+#define SRC_NBUSCOMMUNICATOR_H_
+
+#include "inttypes.h"
+#include "dataframe.h"
+#include "nbus_config.h"
+#include "nbus_defines.h"
+
+/** @brief Interface for NBus Communicator handle.
+ */
+class NbusCommunicator
+{
+public:
+	/** Send data to master.
+	 *  @param master_frame: data frame to send
+	 */
+	virtual void sendToMaster(DataFrame *master_frame) = 0;
+
+	/** Receive packet from master
+	 * @return received data frame
+	 */
+	virtual DataFrame* receiveFromMaster() = 0;
+
+	/** Send data to slave.
+	 *  @param slave_frame: data frame to send
+	 */
+	virtual void sendToSlave(DataFrame *slave_frame) = 0;
+
+	/** Receive data from slave.
+	 * @return received data frame
+	 */
+	virtual DataFrame* receiveFromSlave() = 0;
+
+	/** Set onboard LED to ON-state.
+	 */
+	virtual void setLedOn() = 0;
+	
+	/** Set onboard LED to OFF-state.
+	 */
+	virtual void setLedOff() = 0;
+	
+	/** Change onboard LED state.
+	 */
+	virtual void toggleLed() = 0;
+	
+	/** Get actual elapsed time in ms
+	 * @return time in ms
+	 */
+	virtual uint32_t getTime() = 0;
+
+	/** Send data to slave and receive response.
+	 *  @param slave_frame: data frame to send
+	 *  @return: response data frame
+	 */
+	virtual DataFrame* sendAndReceiveSlave(DataFrame *slave_frame) = 0;
+};
+
+#endif /* SRC_NBUSCOMMUNICATOR_H_ */

+ 0 - 0
Inc/NbusSlave.h → include/NbusSlave.h


+ 1 - 1
Inc/nbus_config.h → include/nbus_config.h

@@ -12,7 +12,7 @@
 
 #define NBUS_USE_LED                    1
 #define NBUS_LED_PORT                   GPIOB
-#define NBUS_LED_PIN                    8// GPIO_PIN_3
+#define NBUS_LED_PIN                    8 // GPIO_PIN_3
 #define NBUS_LED_POLARITY_REVERSED      1 // 0-normal, 1-reversed
 
 /* END NBUS LED DEFINES */

+ 0 - 0
Inc/nbus_defines.h → include/nbus_defines.h


+ 0 - 0
Inc/nbus_platform.h → include/nbus_platform.h