| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- /**
- * @file AppBridge.cpp
- * @brief Vysokoúrovňová logika formvéru nBusBridge. Toto je entrypoint aplikácie.
- *
- * @date Mar 5, 2025
- * @author Juraj Dudak
- */
- #include "AppBridge.h"
- #include "NbusBridge.h"
- /**
- * @brief UART rozhranie medzi Master-Bridge.
- */
- UART_HandleTypeDef *pUartMasterGlobal;
- /** @brief príznak pre prijatie dát z pUartMasterGlobal */
- volatile uint32_t flagUartMasterDataReady;
- /** @brief čítací buffer rozhrania pUartMasterGlobal */
- uint8_t pMasterUartRx[UART_FRAME_SIZE];
- /** @brief buffer pre odoslanie ovpovede rozhrania pUartMasterGlobal */
- uint8_t pMasterUartFrame[UART_FRAME_SIZE];
- /** @brief interný index pri príjme bajtov z pUartMasterGlobal*/
- volatile uint8_t vMasterUartIndex;
- /** @brief veľkosť prijatej požiadavky z rozhrania pUartMasterGlobal */
- volatile uint8_t vMasterUartSize;
- /** @brief Objekt reprezentujúci funkcionality nBusBridge. */
- NbusBridge bridge;
- static void init_app()
- {
- HAL_UARTEx_ReceiveToIdle_DMA(pUartMasterGlobal, pMasterUartRx, UART_FRAME_SIZE);
- 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);
- }
- }
- /**
- * @brief Aplikácia nBusBridge.
- * Zabezpežuje príjem a odoslanie odpovedí medzi Bridge a Master.
- * @param uartNbus rozhranie pre internú zbernicu nBus Slave
- * @param uartMaster rozhranie pre externú zbernicu nBus Master
- */
- void app(UART_HandleTypeDef *uartNbus, UART_HandleTypeDef *uartMaster)
- {
- pUartMasterGlobal = uartMaster;
- init_app();
- NbusCommunicator nc(uartNbus, uartMaster);
- bridge.setCommunicator(&nc);
- bridge.scan();
- while (1)
- {
- bridge.processRunningState();
- if (flagUartMasterDataReady > 0)
- {
- bridge.processRequest(pMasterUartFrame, flagUartMasterDataReady);
- flagUartMasterDataReady = 0;
- }
- }
- }
- /**
- * @brief Aplikačný callback vrstvy STM32 HAL.
- */
- void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
- {
- if (huart == pUartMasterGlobal)
- {
- HAL_UARTEx_ReceiveToIdle_DMA(pUartMasterGlobal, pMasterUartRx, 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;
- if (MODULE_ADDRESS(pMasterUartFrame) == 0)
- {
- bridge.process_broadcast(pMasterUartFrame);
- // TODO tu este mozno deaktivovat spracovanie broadcast paketu
- // flagUartMasterDataReady = 0;
- }
- }
- }
- }
|