| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- #pragma once
- #include "dronecan_id_alloc.h"
- #include "canard.h"
- #include <stdint.h>
- #include "soft_can.h"
- #ifdef __cplusplus
- extern "C" {
- #endif
- #define LOCK 1
- #define UNLOCK 0
- /* The default local node id. */
- #define DRONECAN_CANARD_DEFAULT_LOCAL_NODE_ID DRONECAN_FMU_NODE_ID
- /* The size of the memory pool for the Canard library. */
- #define DRONECAN_CANARD_MEMORY_POOL_SIZE 2048
- #define DRONECAN_CANARD_IFACE1_MASK (1 << 0)
- #define DRONECAN_CANARD_IFACE2_MASK (1 << 1)
- #define DRONECAN_CANARD_IFACE_MAX_NUM 2
- /** The dronecan instance. */
- struct dronecan {
- /* canard 对象 */
- CanardInstance *_canard_ins;
- /* canard 库内存池 */
- uint8_t *_canard_memory_pool;
- /* can 接口 */
- //rt_device_t _can_iface[DRONECAN_CANARD_IFACE_MAX_NUM];
- //rt_mutex_t _lock;
- /* 定时器, 用于定时广播 node status */
- //rt_timer_t _timer;
- /* 时间戳, 调用 canardCleanupStaleTransfers */
- uint32_t _cleanup_timestamp;
- /* 发送消息更新回调钩子函数 */
- void (*_tx_update_hook)(struct dronecan *dcan);
- uint32_t _re_tx_cnt; /**< redo tx count */
- uint32_t _tx_succ_cnt; /**< tx success count */
- uint32_t _tx_fail_cnt; /**< tx fail count */
- };
- /**
- * @brief init dronecan instance
- *
- * @param can_dev can device
- * @return int 0 success, else failed
- */
- void dronecan_init(void);
- /**
- * @brief dronecan 接收回调, 当 can 接口收到 can 消息后调用此函数
- *
- * @param msg can message
- * @param iface_id interface id, if can1 then 0, if can2 then 1, etc
- * @return int 0-not handled, 1-handled
- */
- int dronecan_rx_callback(CAN_RxHeaderTypeDef Rxhead,uint8_t data[]);
- void dronecan_tx_processing(void);
- /**
- * @brief dronecan 发送广播消息
- *
- * @param dcan
- * @param tx_transfer
- * @return int
- */
- int dronecan_broadcast(struct dronecan *dcan, CanardTxTransfer *tx_transfer);
- /**
- * @brief dronecan 发送请求或应答消息
- *
- * @param dcan
- * @param dest_id 目标节点 id
- * @param tx_transfer
- * @return int
- */
- int dronecan_request_or_respond(uint8_t dest_id,
- CanardTxTransfer *tx_transfer);
- /**
- * @brief dronecan 注册 tx_hook 回调函数
- * 当有数据需要通过dronecan 发送时, tx_hook 回调函数会被调用
- *
- * @param fun 回调函数
- * @return int RT_EOK 为成功, 其它为失败
- */
- int dronecan_tx_hook_register(void (*fun)(struct dronecan *dcan));
- void dronecan_lock();
- void dronecan_unlock();
- #ifdef __cplusplus
- }
- #endif
|