AppBridge.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * @file AppBridge.cpp
  3. * @brief High-level logic of nBusBridge. This is the application entry point.
  4. *
  5. * @date Nov 28, 2025
  6. * @author Juraj Dudak, Matus Necas
  7. */
  8. #include "AppBridge.h"
  9. #include "NbusBridge.h"
  10. /** @brief UARTinterface between Master-Bridge. */
  11. UART_HandleTypeDef *pUartMasterGlobal;
  12. /** @brief flag for data-ready from pUartMasterGlobal. */
  13. volatile uint32_t flagUartMasterDataReady;
  14. /** @brief reading buffer from pUartMasterGlobal. */
  15. uint8_t pMasterUartRx[NBUS_APP_UART_FRAME_SIZE];
  16. /** @brief master buffer for pUartMasterGlobal. */
  17. uint8_t pMasterUartFrame[NBUS_APP_UART_FRAME_SIZE];
  18. /** @brief internal index for data acquisition from pUartMasterGlobal. */
  19. volatile uint8_t vMasterUartIndex;
  20. /** @brief size of received request from pUartMasterGlobal. */
  21. volatile uint8_t vMasterUartSize;
  22. /** Initialize resources and provide boot sequence. */
  23. static void init_app()
  24. {
  25. HAL_UARTEx_ReceiveToIdle_DMA(pUartMasterGlobal, pMasterUartRx, NBUS_APP_UART_FRAME_SIZE);
  26. flagUartMasterDataReady = 0;
  27. vMasterUartIndex = 0;
  28. vMasterUartSize = 0;
  29. // boot long blink
  30. for (uint32_t i = 0; i < NBUS_APP_BLINK_COUNT; i++)
  31. {
  32. HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_3);
  33. HAL_Delay(NBUS_APP_BLINK_DELAY);
  34. }
  35. }
  36. /**
  37. * Entrypoint of nBusBridge applicattion.
  38. * @param uart_nbus: interface for internal bridge-slave
  39. * @param uart_master: nterface for external bridge-master
  40. */
  41. void app(UART_HandleTypeDef *uart_nbus, UART_HandleTypeDef *uart_master)
  42. {
  43. pUartMasterGlobal = uart_master;
  44. init_app();
  45. NbusCommunicator nc(uart_nbus, uart_master);
  46. NbusBridge bridge(&nc);
  47. bridge.scanNetwork();
  48. while (1)
  49. {
  50. bridge.processRunningState();
  51. if (flagUartMasterDataReady > 0)
  52. {
  53. bridge.processRequest(pMasterUartFrame, flagUartMasterDataReady);
  54. flagUartMasterDataReady = 0;
  55. }
  56. }
  57. }
  58. /**
  59. * @brief Application callback of STM32 HAL.
  60. */
  61. void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
  62. {
  63. if (huart == pUartMasterGlobal)
  64. {
  65. HAL_UARTEx_ReceiveToIdle_DMA(pUartMasterGlobal, pMasterUartRx, NBUS_APP_UART_FRAME_SIZE);
  66. uint8_t copy_offset = 0;
  67. if (vMasterUartIndex == 0)
  68. {
  69. vMasterUartSize = pMasterUartRx[0];
  70. vMasterUartIndex = 1;
  71. Size--;
  72. copy_offset = 1;
  73. }
  74. if (Size > 0)
  75. {
  76. memcpy(&pMasterUartFrame[vMasterUartIndex - 1], &pMasterUartRx[copy_offset], Size);
  77. vMasterUartIndex += Size;
  78. }
  79. if (vMasterUartIndex > vMasterUartSize)
  80. {
  81. flagUartMasterDataReady = vMasterUartSize;
  82. vMasterUartSize = 0;
  83. vMasterUartIndex = 0;
  84. pMasterUartRx[0] = 0;
  85. }
  86. }
  87. }