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. 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. /*
  37. for(uint32_t i=0; i<bridge.getNumSlaves() ; i++){
  38. for(uint32_t k = 0 ; k <= bridge.getSlave(i)->nbus_get_sensor_count(false) ; k++){
  39. frameTX = bridge.getSlave(i)->nbus_sensor_getData(k);
  40. bridge.sendResponseToMaster(frameTX);
  41. }
  42. }
  43. */
  44. if(flagUartMasterDataReady > 0){
  45. bridge.processRequest(pMasterUartFrame, flagUartMasterDataReady);
  46. flagUartMasterDataReady = 0;
  47. }
  48. }
  49. }
  50. // Application callbacks
  51. void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size){
  52. if(huart == pUartMasterGlobal) {
  53. HAL_UARTEx_ReceiveToIdle_DMA(pUartMasterGlobal, pMasterUartRx, 64);
  54. uint8_t copy_offset = 0;
  55. if(vMasterUartIndex == 0){
  56. vMasterUartSize = pMasterUartRx[0];
  57. vMasterUartIndex = 1;
  58. Size--;
  59. copy_offset = 1;
  60. }
  61. if (Size > 0) {
  62. memcpy(&pMasterUartFrame[vMasterUartIndex-1], &pMasterUartRx[copy_offset], Size);
  63. vMasterUartIndex += Size;
  64. }
  65. if(vMasterUartIndex > vMasterUartSize){
  66. flagUartMasterDataReady = vMasterUartSize;
  67. vMasterUartSize = 0;
  68. vMasterUartIndex = 0;
  69. pMasterUartRx[0] = 0;
  70. }
  71. }
  72. }