dronecan.h 2.4 KB

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