| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- /*
- * @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;
- 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);
- NbusBridge bridge(&nc);
- bridge.scan();
- while(1){
- /*
- for(uint32_t i=0; i<bridge.getNumSlaves() ; i++){
- for(uint32_t k = 0 ; k <= bridge.getSlave(i)->nbus_get_sensor_count(false) ; k++){
- frameTX = bridge.getSlave(i)->nbus_sensor_getData(k);
- bridge.sendResponseToMaster(frameTX);
- }
- }
- */
- if(flagUartMasterDataReady > 0){
- flagUartMasterDataReady = 0;
- bridge.processRequest(pMasterUartFrame);
- }
- }
- }
- // 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;
- }
- }
- }
|