/** * @file AppBridge.cpp * @brief High-level logic of nBusBridge. This is the application entry point. * * @date Nov 28, 2025 * @author Juraj Dudak, Matus Necas */ #include "AppBridge.h" #include "NbusBridge.h" /** @brief UARTinterface between Master-Bridge. */ UART_HandleTypeDef *pUartMasterGlobal; /** @brief flag for data-ready from pUartMasterGlobal. */ volatile uint32_t flagUartMasterDataReady; /** @brief reading buffer from pUartMasterGlobal. */ uint8_t pMasterUartRx[NBUS_APP_UART_FRAME_SIZE]; /** @brief master buffer for pUartMasterGlobal. */ uint8_t pMasterUartFrame[NBUS_APP_UART_FRAME_SIZE]; /** @brief internal index for data acquisition from pUartMasterGlobal. */ volatile uint8_t vMasterUartIndex; /** @brief size of received request from pUartMasterGlobal. */ volatile uint8_t vMasterUartSize; /** Initialize resources and provide boot sequence. */ static void init_app() { HAL_UARTEx_ReceiveToIdle_DMA(pUartMasterGlobal, pMasterUartRx, NBUS_APP_UART_FRAME_SIZE); flagUartMasterDataReady = 0; vMasterUartIndex = 0; vMasterUartSize = 0; // boot long blink for (uint32_t i = 0; i < NBUS_APP_BLINK_COUNT; i++) { HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_3); HAL_Delay(NBUS_APP_BLINK_DELAY); } } /** * Entrypoint of nBusBridge applicattion. * @param uart_nbus: interface for internal bridge-slave * @param uart_master: nterface for external bridge-master */ void app(UART_HandleTypeDef *uart_nbus, UART_HandleTypeDef *uart_master) { pUartMasterGlobal = uart_master; init_app(); NbusCommunicator nc(uart_nbus, uart_master); NbusBridge bridge(&nc); bridge.scanNetwork(); while (1) { bridge.processRunningState(); if (flagUartMasterDataReady > 0) { bridge.processRequest(pMasterUartFrame, flagUartMasterDataReady); flagUartMasterDataReady = 0; } } } /** * @brief Application callback of STM32 HAL. */ void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) { if (huart == pUartMasterGlobal) { HAL_UARTEx_ReceiveToIdle_DMA(pUartMasterGlobal, pMasterUartRx, NBUS_APP_UART_FRAME_SIZE); uint8_t copy_offset = 0; if (vMasterUartIndex == 0) { vMasterUartSize = pMasterUartRx[0]; vMasterUartIndex = 1; Size--; copy_offset = 1; } 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; } } }