Files
templates/c/set-protocol/include/set_can.h

90 lines
2.3 KiB
C

#ifndef SET_CAN_H
#define SET_CAN_H
#include "set_protocol.h"
#ifdef __cplusplus
extern "C" {
#endif
#define SETP_CAN_ID_PREFIX 0x12U
#define SETP_CAN_ID_PREFIX_MASK 0x1F000000UL
#define SETP_CAN_ID_MASK 0x1FFFFFFFUL
#define SETP_CAN_PCI_FIRST 0x10U
#define SETP_CAN_PCI_CONSECUTIVE 0x20U
#define SETP_CAN_PCI_FLOW_CONTROL 0x30U
#define SETP_CAN_PCI_TYPE_MASK 0xF0U
#define SETP_CAN_PCI_VALUE_MASK 0x0FU
#define SETP_CAN_FC_CONTINUE 0x00U
#define SETP_CAN_FC_WAIT 0x01U
#define SETP_CAN_FC_OVERFLOW 0x02U
#define SETP_CAN_FIRST_DATA 5U
#define SETP_CAN_CONSECUTIVE_DATA 7U
#ifndef SETP_CAN_REASSEMBLY_TIMEOUT_MS
#define SETP_CAN_REASSEMBLY_TIMEOUT_MS 500U
#endif
typedef struct {
uint32_t id;
uint8_t length;
uint8_t data[8];
} setp_can_frame_t;
typedef struct {
uint8_t source;
uint8_t destination;
uint8_t priority;
uint8_t channel;
} setp_can_id_t;
typedef enum {
SETP_CAN_RX_NONE = 0,
SETP_CAN_RX_COMPLETE = 1,
SETP_CAN_RX_FORMAT_ERROR = 2,
SETP_CAN_RX_SEQUENCE_ERROR = 3,
SETP_CAN_RX_TIMEOUT = 4
} setp_can_rx_result_t;
typedef struct {
uint8_t buffer[SETP_FRAME_MAX];
uint16_t expected_length;
uint16_t received_length;
uint32_t can_id;
uint32_t deadline_ms;
uint8_t next_sequence;
uint8_t active;
} setp_can_rx_t;
typedef struct {
const uint8_t *data;
uint16_t length;
uint32_t can_id;
} setp_can_packet_t;
typedef bool (*setp_can_send_fn)(const setp_can_frame_t *frame, void *user);
uint32_t setp_can_id_pack(const setp_can_id_t *value);
bool setp_can_id_unpack(uint32_t raw, setp_can_id_t *out);
/** Segment one complete SETP frame. Flow-control pacing belongs to the port. */
bool setp_can_segment(const uint8_t *packet, uint16_t length, uint32_t can_id,
setp_can_send_fn send, void *user);
void setp_can_rx_init(setp_can_rx_t *state);
setp_can_rx_result_t setp_can_rx_feed(setp_can_rx_t *state,
const setp_can_frame_t *frame,
uint32_t now_ms, setp_can_packet_t *out);
bool setp_can_flow_control(setp_can_frame_t *out, uint32_t can_id,
uint8_t status, uint8_t block_size, uint8_t st_min_ms);
#ifdef __cplusplus
}
#endif
#endif /* SET_CAN_H */