AppBridge.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #define NBUS_UART_FRAME_TIMEOUT 5
  11. #define MAX_SYSTICK 0xFFFFFFFF
  12. /** @brief UARTinterface between Master-Bridge. */
  13. UART_HandleTypeDef *pUartMasterGlobal;
  14. /** @brief flag for data-ready from pUartMasterGlobal. */
  15. volatile uint32_t flagUartMasterDataReady;
  16. /** @brief reading buffer from pUartMasterGlobal. */
  17. uint8_t pMasterUartRx[NBUS_APP_UART_FRAME_SIZE];
  18. /** @brief master buffer for pUartMasterGlobal. */
  19. uint8_t pMasterUartFrame[NBUS_APP_UART_FRAME_SIZE];
  20. /** @brief internal index for data acquisition from pUartMasterGlobal. */
  21. //volatile uint8_t vMasterUartIndex;
  22. /** @brief size of received request from pUartMasterGlobal. */
  23. //volatile uint8_t vMasterUartSize;
  24. ProtocolState state = STATE_WAIT_LEN;
  25. volatile uint32_t uart_timeout = MAX_SYSTICK;
  26. volatile uint16_t rx_read_pos = 0; // DAM UART CIRCULAR - tail
  27. volatile uint8_t msg_len = 0;
  28. volatile uint8_t msg_idx = 0;
  29. uint8_t rx_ring_buffer[BUFF_SIZE];
  30. void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart){
  31. HAL_UART_Receive_DMA(pUartMasterGlobal, rx_ring_buffer, BUFF_SIZE);
  32. }
  33. /** Initialize resources and provide boot sequence. */
  34. static void init_app()
  35. {
  36. uart_timeout = HAL_GetTick();
  37. rx_ring_buffer[0] = 0;
  38. //HAL_UARTEx_ReceiveToIdle_DMA(pUartMasterGlobal, pMasterUartRx, NBUS_APP_UART_FRAME_SIZE);
  39. HAL_UART_Receive_DMA(pUartMasterGlobal, rx_ring_buffer, BUFF_SIZE);
  40. flagUartMasterDataReady = 0;
  41. // vMasterUartIndex = 0;
  42. // vMasterUartSize = 0;
  43. // boot long blink
  44. for (uint32_t i = 0; i < NBUS_APP_BLINK_COUNT; i++)
  45. {
  46. HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_3);
  47. HAL_Delay(NBUS_APP_BLINK_DELAY);
  48. }
  49. }
  50. static uint8_t Parse_Protocol_Byte(uint8_t b) {
  51. uint8_t packet_finished = 0;
  52. // Timeout reset logic
  53. if (state != STATE_WAIT_LEN) {
  54. if ((HAL_GetTick() - uart_timeout) > NBUS_UART_FRAME_TIMEOUT){
  55. state = STATE_WAIT_LEN;
  56. msg_len = 0;
  57. msg_idx = 0;
  58. }
  59. }
  60. switch (state) {
  61. case STATE_WAIT_LEN:
  62. // Validácia dĺžky (max 128 bajtov, min 4 bajty)
  63. if (b > 3 && b < PAYLOAD_SIZE) {
  64. msg_len = b;
  65. msg_idx = 0;
  66. state = STATE_PAYLOAD;
  67. uart_timeout = HAL_GetTick(); // Reset timeoutu
  68. }
  69. break;
  70. case STATE_PAYLOAD:
  71. pMasterUartRx[msg_idx++] = b;
  72. uart_timeout = HAL_GetTick(); // Aktualizácia timeoutu pri každom bajte
  73. if (msg_idx >= msg_len) { // Koniec paketu (podľa definície dĺžky)
  74. //nbus_cb_UART_RX(msg_idx); // callback to nBus, notify message length
  75. state = STATE_WAIT_LEN;
  76. packet_finished = 1;
  77. flagUartMasterDataReady = 1;
  78. }
  79. break;
  80. }
  81. return packet_finished;
  82. }
  83. static uint8_t process_UART_RingBuffer(void) {
  84. // Zistíme, kde sa aktuálne nachádza DMA (Head)
  85. // CNDTR register obsahuje počet ZOSTÁVAJÚCICH bajtov do konca buffra
  86. uint16_t rx_dma_pos = BUFF_SIZE - __HAL_DMA_GET_COUNTER(pUartMasterGlobal->hdmarx);
  87. while (rx_read_pos != rx_dma_pos) {
  88. uint8_t byte = rx_ring_buffer[rx_read_pos];
  89. rx_read_pos++;
  90. if (rx_read_pos >= BUFF_SIZE) {
  91. rx_read_pos = 0;
  92. }
  93. if( Parse_Protocol_Byte(byte) != 0){
  94. return 1;
  95. }
  96. }
  97. return 0;
  98. }
  99. /**
  100. * Entrypoint of nBusBridge applicattion.
  101. * @param uart_nbus: interface for internal bridge-slave
  102. * @param uart_master: nterface for external bridge-master
  103. */
  104. void app(UART_HandleTypeDef *uart_nbus, UART_HandleTypeDef *uart_master)
  105. {
  106. pUartMasterGlobal = uart_master;
  107. init_app();
  108. NbusCommunicator nc(uart_nbus, uart_master);
  109. NbusBridge bridge(&nc);
  110. bridge.scanNetwork();
  111. while (1)
  112. {
  113. if (process_UART_RingBuffer() != 0){
  114. bridge.processRequest(pMasterUartRx, msg_len);
  115. flagUartMasterDataReady = 0;
  116. }
  117. bridge.processRunningState();
  118. }
  119. }
  120. /**
  121. * @brief Application callback of STM32 HAL.
  122. */
  123. /*
  124. void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
  125. {
  126. if (huart == pUartMasterGlobal)
  127. {
  128. HAL_UARTEx_ReceiveToIdle_DMA(pUartMasterGlobal, pMasterUartRx, NBUS_APP_UART_FRAME_SIZE);
  129. uint8_t copy_offset = 0;
  130. if (vMasterUartIndex == 0)
  131. {
  132. vMasterUartSize = pMasterUartRx[0];
  133. vMasterUartIndex = 1;
  134. Size--;
  135. copy_offset = 1;
  136. }
  137. if (Size > 0)
  138. {
  139. memcpy(&pMasterUartFrame[vMasterUartIndex - 1], &pMasterUartRx[copy_offset], Size);
  140. vMasterUartIndex += Size;
  141. }
  142. if (vMasterUartIndex > vMasterUartSize)
  143. {
  144. flagUartMasterDataReady = vMasterUartSize;
  145. vMasterUartSize = 0;
  146. vMasterUartIndex = 0;
  147. pMasterUartRx[0] = 0;
  148. }
  149. }
  150. }
  151. */