CAN driver support for hardware abstraction layer.
// init
rt_dev_t can = can(1);
can_open(can);
can_setBitTiming(can, 1000000, 1, 4, 2);
can_enable(can);
can_setMode(can, CAN_MODE_NORMAL);
// send and receive data
CAN_MSG_HEADER can_header;
char data[8] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF};
can_header.size = 8;
can_header.id = 0x01AB45CD;
can_header.flags = CAN_VERS2BA;
can_send(can, 0, &can_header, data);
char rdata[8];
int r = can_rec(can, 0, &can_header, rdata);
if (r == 1)
{
printf("%#08X\n", rdata);
}
rt_dev_t can_getFreeDevice();
Gives a free can device number and opens it
int can_open(rt_dev_t device);
Opens a can bus device
int can_close(rt_dev_t device);
Closes a can bus device
bool can_isOpened(rt_dev_t device)
Gives the SDK device status
int can_enable(rt_dev_t device);
Enables the specified can bus device
int can_disable(rt_dev_t device);
Disables the specified can bus device
bool can_isEnabled(rt_dev_t device)
Gives the SDK enabled state
int can_setBitTiming(rt_dev_t device, uint32_t bitRate, uint8_t propagSeg, uint8_t s1Seg, uint8_t s2Seg);
Sets bit rate and segments timing. Sum of all segments (propagSeg + s1Seg +, s2Seg) + 1 must be contained in the range of 8 to 25 quantums.
CAN Bit Timing (8-25 Tq) segments computation :
Sync | Propag seg | Phase seg 1 | Phase seg 2 | |
---|---|---|---|---|
1 Tq | 1-8 Tq | 1-8 Tq | sample point | 1-8 Tq |
uint32_t can_bitRate(rt_dev_t device);
Gets the true bit rate
uint32_t can_effectiveBitRate(rt_dev_t device);
Gets the effective bit rate
uint8_t can_propagSeg(rt_dev_t device);
Gets the current propagation segment length
uint8_t can_s1Seg(rt_dev_t device);
Gets the current S1 segment length
uint8_t can_s2Seg(rt_dev_t device);
Gets the current S2 segment length
int can_send(rt_dev_t device, uint8_t fifo, CAN_MSG_HEADER *header, char *data);
Puts a message in fifo fifo
int can_rec(rt_dev_t device, uint8_t fifo, CAN_MSG_HEADER *header, char *data);
Gets a message from fifo fifo
, return 1 if a message is read
Device assignation, configuration, send and read data on fifo 0 only
- multi fifo send / rec
- fifo status
- implementation on dsPIC33E/F
- interrupts
Header file : can.h