| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- /*
- * @file AppBridge.cpp
- * @brief Vysokoúrovňová logika formvéru nBusBridge
- * @date Mar 5, 2025
- * @author Juraj Dudak
- */
- #include <NbusSlave.h>
- #include "AppBridge.h"
- #include "dataframe.h"
- #include "NbusBridge.h"
- #include "NbusCommunicator.h"
- UART_HandleTypeDef *pUartMasterGlobal;
- volatile uint32_t flagUartMasterDataReady;
- uint8_t pMasterUartRx[64];
- uint8_t pMasterUartFrame[64];
- volatile uint8_t vMasterUartIndex;
- volatile uint8_t vMasterUartSize;
- NbusBridge bridge;
- static void init_app(){
- HAL_UARTEx_ReceiveToIdle_DMA(pUartMasterGlobal, pMasterUartRx, 64);
- 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);
- }
- }
- 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;
- }
- }
- }
- // Application callbacks
- void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size){
- if(huart == pUartMasterGlobal) {
- HAL_UARTEx_ReceiveToIdle_DMA(pUartMasterGlobal, pMasterUartRx, 64);
- 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 nroadcast paketu
- // flagUartMasterDataReady = 0;
- }
- }
- }
- }
|