AppBridge.cpp 3.1 KB

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