AppBridge.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * @file AppBridge.cpp
  3. * @brief Vysokoúrovňová logika formvéru nBusBridge. Toto je entrypoint aplikácie.
  4. *
  5. * @date Mar 5, 2025
  6. * @author Juraj Dudak
  7. */
  8. #include "AppBridge.h"
  9. #include "NbusBridge.h"
  10. /**
  11. * @brief UART rozhranie medzi Master-Bridge.
  12. */
  13. UART_HandleTypeDef *pUartMasterGlobal;
  14. /** @brief príznak pre prijatie dát z pUartMasterGlobal */
  15. volatile uint32_t flagUartMasterDataReady;
  16. /** @brief čítací buffer rozhrania pUartMasterGlobal */
  17. uint8_t pMasterUartRx[UART_FRAME_SIZE];
  18. /** @brief buffer pre odoslanie ovpovede rozhrania pUartMasterGlobal */
  19. uint8_t pMasterUartFrame[UART_FRAME_SIZE];
  20. /** @brief interný index pri príjme bajtov z pUartMasterGlobal*/
  21. volatile uint8_t vMasterUartIndex;
  22. /** @brief veľkosť prijatej požiadavky z rozhrania pUartMasterGlobal */
  23. volatile uint8_t vMasterUartSize;
  24. /** @brief Objekt reprezentujúci funkcionality nBusBridge. */
  25. NbusBridge bridge;
  26. static void init_app()
  27. {
  28. HAL_UARTEx_ReceiveToIdle_DMA(pUartMasterGlobal, pMasterUartRx, UART_FRAME_SIZE);
  29. flagUartMasterDataReady = 0;
  30. vMasterUartIndex = 0;
  31. vMasterUartSize = 0;
  32. // boot long blink
  33. for (uint32_t i = 0; i < 10; i++)
  34. {
  35. HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_3);
  36. HAL_Delay(50);
  37. }
  38. }
  39. /**
  40. * @brief Aplikácia nBusBridge.
  41. * Zabezpežuje príjem a odoslanie odpovedí medzi Bridge a Master.
  42. * @param uartNbus rozhranie pre internú zbernicu nBus Slave
  43. * @param uartMaster rozhranie pre externú zbernicu nBus Master
  44. */
  45. void app(UART_HandleTypeDef *uartNbus, UART_HandleTypeDef *uartMaster)
  46. {
  47. pUartMasterGlobal = uartMaster;
  48. init_app();
  49. NbusCommunicator nc(uartNbus, uartMaster);
  50. bridge.setCommunicator(&nc);
  51. bridge.scan();
  52. while (1)
  53. {
  54. bridge.processRunningState();
  55. if (flagUartMasterDataReady > 0)
  56. {
  57. bridge.processRequest(pMasterUartFrame, flagUartMasterDataReady);
  58. flagUartMasterDataReady = 0;
  59. }
  60. }
  61. }
  62. /**
  63. * @brief Aplikačný callback vrstvy STM32 HAL.
  64. */
  65. void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
  66. {
  67. if (huart == pUartMasterGlobal)
  68. {
  69. HAL_UARTEx_ReceiveToIdle_DMA(pUartMasterGlobal, pMasterUartRx, UART_FRAME_SIZE);
  70. uint8_t copy_offset = 0;
  71. if (vMasterUartIndex == 0)
  72. {
  73. vMasterUartSize = pMasterUartRx[0];
  74. vMasterUartIndex = 1;
  75. Size--;
  76. copy_offset = 1;
  77. }
  78. if (Size > 0)
  79. {
  80. memcpy(&pMasterUartFrame[vMasterUartIndex - 1], &pMasterUartRx[copy_offset], Size);
  81. vMasterUartIndex += Size;
  82. }
  83. if (vMasterUartIndex > vMasterUartSize)
  84. {
  85. flagUartMasterDataReady = vMasterUartSize;
  86. vMasterUartSize = 0;
  87. vMasterUartIndex = 0;
  88. pMasterUartRx[0] = 0;
  89. if (MODULE_ADDRESS(pMasterUartFrame) == 0)
  90. {
  91. bridge.process_broadcast(pMasterUartFrame);
  92. // TODO tu este mozno deaktivovat spracovanie broadcast paketu
  93. // flagUartMasterDataReady = 0;
  94. }
  95. }
  96. }
  97. }