AppBridge.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * @file AppBridge.cpp
  3. * @brief Vysokoúrovňová logika formvéru nBusBridge
  4. * @date Mar 5, 2025
  5. * @author Juraj Dudak
  6. */
  7. #include <NbusSlave.h>
  8. #include "AppBridge.h"
  9. #include "dataframe.h"
  10. #include "NbusBridge.h"
  11. #include "NbusCommunicator.h"
  12. UART_HandleTypeDef *pUartMasterGlobal;
  13. volatile uint32_t flagUartMasterDataReady;
  14. uint8_t pMasterUartRx[64];
  15. uint8_t pMasterUartFrame[64];
  16. volatile uint8_t vMasterUartIndex;
  17. volatile uint8_t vMasterUartSize;
  18. static void init_app(){
  19. HAL_UARTEx_ReceiveToIdle_DMA(pUartMasterGlobal, pMasterUartRx, 64);
  20. flagUartMasterDataReady = 0;
  21. vMasterUartIndex = 0;
  22. vMasterUartSize = 0;
  23. // boot long blink
  24. for(uint32_t i = 0 ; i<64 ; i++){
  25. HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_3);
  26. HAL_Delay(25);
  27. }
  28. }
  29. void app(UART_HandleTypeDef *uartNbus, UART_HandleTypeDef *uartMaster){
  30. pUartMasterGlobal = uartMaster;
  31. init_app();
  32. NbusCommunicator nc(uartNbus, uartMaster);
  33. NbusBridge bridge(&nc);
  34. bridge.scan();
  35. while(1){
  36. bridge.processRunningState();
  37. if(flagUartMasterDataReady > 0){
  38. bridge.processRequest(pMasterUartFrame, flagUartMasterDataReady);
  39. flagUartMasterDataReady = 0;
  40. }
  41. }
  42. }
  43. // Application callbacks
  44. void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size){
  45. if(huart == pUartMasterGlobal) {
  46. HAL_UARTEx_ReceiveToIdle_DMA(pUartMasterGlobal, pMasterUartRx, 64);
  47. uint8_t copy_offset = 0;
  48. if(vMasterUartIndex == 0){
  49. vMasterUartSize = pMasterUartRx[0];
  50. vMasterUartIndex = 1;
  51. Size--;
  52. copy_offset = 1;
  53. }
  54. if (Size > 0) {
  55. memcpy(&pMasterUartFrame[vMasterUartIndex-1], &pMasterUartRx[copy_offset], Size);
  56. vMasterUartIndex += Size;
  57. }
  58. if(vMasterUartIndex > vMasterUartSize){
  59. flagUartMasterDataReady = vMasterUartSize;
  60. vMasterUartSize = 0;
  61. vMasterUartIndex = 0;
  62. pMasterUartRx[0] = 0;
  63. }
  64. }
  65. }