|
|
@@ -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;
|
|
|
+}
|