AppBridge.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. static void init_app()
  26. {
  27. HAL_UARTEx_ReceiveToIdle_DMA(pUartMasterGlobal, pMasterUartRx, UART_FRAME_SIZE);
  28. flagUartMasterDataReady = 0;
  29. vMasterUartIndex = 0;
  30. vMasterUartSize = 0;
  31. // boot long blink
  32. for (uint32_t i = 0; i < 10; i++)
  33. {
  34. HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_3);
  35. HAL_Delay(50);
  36. }
  37. }
  38. /**
  39. * @brief Aplikácia nBusBridge.
  40. * Zabezpežuje príjem a odoslanie odpovedí medzi Bridge a Master.
  41. * @param uartNbus rozhranie pre internú zbernicu nBus Slave
  42. * @param uartMaster rozhranie pre externú zbernicu nBus Master
  43. */
  44. void app(UART_HandleTypeDef *uartNbus, UART_HandleTypeDef *uartMaster)
  45. {
  46. pUartMasterGlobal = uartMaster;
  47. init_app();
  48. NbusCommunicator nc(uartNbus, uartMaster);
  49. NbusBridge bridge(&nc);
  50. bridge.scanNetwork();
  51. while (1)
  52. {
  53. bridge.processRunningState();
  54. if (flagUartMasterDataReady > 0)
  55. {
  56. bridge.processRequest(pMasterUartFrame, flagUartMasterDataReady);
  57. flagUartMasterDataReady = 0;
  58. }
  59. }
  60. }
  61. /**
  62. * @brief Aplikačný callback vrstvy STM32 HAL.
  63. */
  64. void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
  65. {
  66. if (huart == pUartMasterGlobal)
  67. {
  68. HAL_UARTEx_ReceiveToIdle_DMA(pUartMasterGlobal, pMasterUartRx, UART_FRAME_SIZE);
  69. uint8_t copy_offset = 0;
  70. if (vMasterUartIndex == 0)
  71. {
  72. vMasterUartSize = pMasterUartRx[0];
  73. vMasterUartIndex = 1;
  74. Size--;
  75. copy_offset = 1;
  76. }
  77. if (Size > 0)
  78. {
  79. memcpy(&pMasterUartFrame[vMasterUartIndex - 1], &pMasterUartRx[copy_offset], Size);
  80. vMasterUartIndex += Size;
  81. }
  82. if (vMasterUartIndex > vMasterUartSize)
  83. {
  84. flagUartMasterDataReady = vMasterUartSize;
  85. vMasterUartSize = 0;
  86. vMasterUartIndex = 0;
  87. pMasterUartRx[0] = 0;
  88. }
  89. }
  90. }