AppBridge.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. NbusBridge bridge;
  19. static void init_app(){
  20. HAL_UARTEx_ReceiveToIdle_DMA(pUartMasterGlobal, pMasterUartRx, 64);
  21. flagUartMasterDataReady = 0;
  22. vMasterUartIndex = 0;
  23. vMasterUartSize = 0;
  24. // boot long blink
  25. for(uint32_t i = 0 ; i<64 ; i++){
  26. HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_3);
  27. HAL_Delay(25);
  28. }
  29. }
  30. void app(UART_HandleTypeDef *uartNbus, UART_HandleTypeDef *uartMaster){
  31. pUartMasterGlobal = uartMaster;
  32. init_app();
  33. NbusCommunicator nc(uartNbus, uartMaster);
  34. bridge.setCommunicator(&nc);
  35. bridge.scan();
  36. while(1){
  37. bridge.processRunningState();
  38. if(flagUartMasterDataReady > 0){
  39. bridge.processRequest(pMasterUartFrame, flagUartMasterDataReady);
  40. flagUartMasterDataReady = 0;
  41. }
  42. }
  43. }
  44. // Application callbacks
  45. void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size){
  46. if(huart == pUartMasterGlobal) {
  47. HAL_UARTEx_ReceiveToIdle_DMA(pUartMasterGlobal, pMasterUartRx, 64);
  48. uint8_t copy_offset = 0;
  49. if(vMasterUartIndex == 0){
  50. vMasterUartSize = pMasterUartRx[0];
  51. vMasterUartIndex = 1;
  52. Size--;
  53. copy_offset = 1;
  54. }
  55. if (Size > 0) {
  56. memcpy(&pMasterUartFrame[vMasterUartIndex-1], &pMasterUartRx[copy_offset], Size);
  57. vMasterUartIndex += Size;
  58. }
  59. if(vMasterUartIndex > vMasterUartSize){
  60. flagUartMasterDataReady = vMasterUartSize;
  61. vMasterUartSize = 0;
  62. vMasterUartIndex = 0;
  63. pMasterUartRx[0] = 0;
  64. if (MODULE_ADDRESS(pMasterUartFrame) == 0){
  65. bridge.process_broadcast(pMasterUartFrame);
  66. // TODO tu este mozno deaktivovat spracovanie nroadcast paketu
  67. // flagUartMasterDataReady = 0;
  68. }
  69. }
  70. }
  71. }