49 lines
1.0 KiB
C
49 lines
1.0 KiB
C
#include "can_device.h"
|
|
#include "can_if.h"
|
|
|
|
int can_device_init(can_device_t *dev, int ch)
|
|
{
|
|
if (!dev) return -1;
|
|
|
|
dev->ch = ch;
|
|
dev->initialized = 0;
|
|
|
|
int ret = can_if_init(ch);
|
|
if (ret == 0) {
|
|
dev->initialized = 1;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
int can_device_send(can_device_t *dev, uint32_t id, const uint8_t *data, uint8_t len)
|
|
{
|
|
if (!dev || !data || len > 8) return -1;
|
|
if (!dev->initialized) return -1;
|
|
|
|
can_message_t msg;
|
|
msg.id = id;
|
|
msg.length = len;
|
|
for (int i = 0; i < len; i++) {
|
|
msg.data[i] = data[i];
|
|
}
|
|
|
|
return can_if_send(dev->ch, &msg);
|
|
}
|
|
|
|
int can_device_recv(can_device_t *dev, can_message_t *msg)
|
|
{
|
|
if (!dev || !msg) return -1;
|
|
if (!dev->initialized) return -1;
|
|
|
|
return can_if_recv(dev->ch, msg);
|
|
}
|
|
|
|
int can_device_set_filter(can_device_t *dev, uint8_t filter_id, uint32_t id, uint32_t mask)
|
|
{
|
|
if (!dev || filter_id > 13) return -1;
|
|
if (!dev->initialized) return -1;
|
|
|
|
return can_if_filter_config(dev->ch, filter_id, id, mask);
|
|
}
|