dronecan.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #pragma once
  2. #include "dronecan_id_alloc.h"
  3. #include "canard.h"
  4. #include <stdint.h>
  5. #include "soft_can.h"
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. #define LOCK 1
  10. #define UNLOCK 0
  11. /* The default local node id. */
  12. #define DRONECAN_CANARD_DEFAULT_LOCAL_NODE_ID DRONECAN_FMU_NODE_ID
  13. /* The size of the memory pool for the Canard library. */
  14. #define DRONECAN_CANARD_MEMORY_POOL_SIZE 2048
  15. #define DRONECAN_CANARD_IFACE1_MASK (1 << 0)
  16. #define DRONECAN_CANARD_IFACE2_MASK (1 << 1)
  17. #define DRONECAN_CANARD_IFACE_MAX_NUM 2
  18. #define REMOTEID_NODEID 100
  19. /** The dronecan instance. */
  20. struct dronecan {
  21. /* canard 对象 */
  22. CanardInstance *_canard_ins;
  23. /* canard 库内存池 */
  24. uint8_t *_canard_memory_pool;
  25. /* can 接口 */
  26. //rt_device_t _can_iface[DRONECAN_CANARD_IFACE_MAX_NUM];
  27. //rt_mutex_t _lock;
  28. /* 定时器, 用于定时广播 node status */
  29. //rt_timer_t _timer;
  30. /* 时间戳, 调用 canardCleanupStaleTransfers */
  31. uint32_t _cleanup_timestamp;
  32. /* 发送消息更新回调钩子函数 */
  33. void (*_tx_update_hook)(struct dronecan *dcan);
  34. uint32_t _re_tx_cnt; /**< redo tx count */
  35. uint32_t _tx_succ_cnt; /**< tx success count */
  36. uint32_t _tx_fail_cnt; /**< tx fail count */
  37. };
  38. /**
  39. * @brief init dronecan instance
  40. *
  41. * @param can_dev can device
  42. * @return int 0 success, else failed
  43. */
  44. void dronecan_init(void);
  45. /**
  46. * @brief dronecan 接收回调, 当 can 接口收到 can 消息后调用此函数
  47. *
  48. * @param msg can message
  49. * @param iface_id interface id, if can1 then 0, if can2 then 1, etc
  50. * @return int 0-not handled, 1-handled
  51. */
  52. int dronecan_rx_callback(CAN_RxHeaderTypeDef Rxhead,uint8_t data[]);
  53. void dronecan_tx_processing(void);
  54. /**
  55. * @brief dronecan 发送广播消息
  56. *
  57. * @param dcan
  58. * @param tx_transfer
  59. * @return int
  60. */
  61. int dronecan_broadcast(CanardTxTransfer *tx_transfer);
  62. /**
  63. * @brief dronecan 发送请求或应答消息
  64. *
  65. * @param dcan
  66. * @param dest_id 目标节点 id
  67. * @param tx_transfer
  68. * @return int
  69. */
  70. int dronecan_request_or_respond(uint8_t dest_id,
  71. CanardTxTransfer *tx_transfer);
  72. /**
  73. * @brief dronecan 注册 tx_hook 回调函数
  74. * 当有数据需要通过dronecan 发送时, tx_hook 回调函数会被调用
  75. *
  76. * @param fun 回调函数
  77. * @return int RT_EOK 为成功, 其它为失败
  78. */
  79. int dronecan_tx_hook_register(void (*fun)(struct dronecan *dcan));
  80. void dronecan_lock();
  81. void dronecan_unlock();
  82. #ifdef __cplusplus
  83. }
  84. #endif