#include "canard.h" #include CanardInstance canard; CanardTxQueue txQueue; void* canard_memAllocate(CanardInstance* const canard, const size_t amount) { (void) canard; return x_malloc(amount); } void canard_memFree(CanardInstance* const canard, void* const pointer) { (void) canard; free(pointer); } void slave_comm_init() { canard = canardInit(&canard_memAllocate, &canard_memFree); canard.node_id = savePara.id; txQueue = canardTxInit(1536, CANARD_MTU_CAN_CLASSIC); // Set MTU = 64 bytes. There is also CANARD_MTU_CAN_CLASSIC. } void slave_comm_tx_process() { for (const CanardTxQueueItem* ti = NULL; (ti = canardTxPeek(&txQueue)) != NULL;) // Peek at the top of the queue. { if ((0U == ti->tx_deadline_usec) || (ti->tx_deadline_usec > rt_tick_get_millisecond()*1000)) // Check the deadline. { if (!slave_send_ext(ti->frame.extended_can_id,(void *)ti->frame.payload,ti->frame.payload_size)) // Send the frame over this redundant CAN iface. { break; // If the driver is busy, break and retry later. } } // After the frame is transmitted or if it has timed out while waiting, pop it from the queue and deallocate: canard.memory_free(&canard, canardTxPop(&txQueue, ti)); } } rt_inline uint8_t slave_send_ext(uint32_t id,uint8_t *sendBuf,uint8_t len) { struct rt_can_msg txMsg = {0}; txMsg.id = id; txMsg.ide = RT_CAN_EXTID; txMsg.rtr = RT_CAN_DTR; txMsg.len = len; for(rt_uint8_t i=0;imetadata.remote_node_id,transfer->payload_size); if(transfer->metadata.remote_node_id == canard.node_id) { slavePackDef *p = (slavePackDef *)transfer->payload; recCmd = p->packCmd; } }