-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcan.c
More file actions
79 lines (65 loc) · 2.01 KB
/
Copy pathcan.c
File metadata and controls
79 lines (65 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "can.h"
#include <errno.h>
#include <string.h>
LOG_MODULE_REGISTER(can);
static void can_tx_done(const struct device *dev, int error, void *user_data)
{
ARG_UNUSED(dev);
ARG_UNUSED(user_data);
if (error != 0 && error != -EAGAIN && error != -ENETUNREACH && error != -ENETDOWN) {
LOG_WRN("CAN TX error: %d", error);
}
}
int can_send_(const struct device *can_dev, uint16_t id, uint8_t *data, uint8_t data_len)
{
struct can_frame frame = {0};
frame.id = id;
frame.dlc = data_len;
frame.flags = CAN_FRAME_IDE;
memcpy(frame.data, data, data_len);
int ret = can_send(can_dev, &frame, K_NO_WAIT, can_tx_done, NULL);
if (ret == -EAGAIN || ret == -ENETUNREACH || ret == -ENETDOWN) {
return 0;
}
return ret;
}
int can_send_float(const struct device *can_dev, uint16_t id, float value)
{
return can_send_(can_dev, id, (uint8_t *)&value, sizeof(float));
}
int can_add_rx_filter_(const struct device *can_dev, can_rx_callback_t can_rx_callback,
const struct can_filter *filter)
{
int filter_id = can_add_rx_filter(can_dev, can_rx_callback, NULL, filter);
if (filter_id < 0) {
LOG_ERR("Unable to add rx filter [%d]", filter_id);
} else {
LOG_INF("CAN rx filter [%d] has been added succesfully", filter_id);
}
return filter_id;
}
int can_init(const struct device *can_dev, uint32_t baudrate)
{
struct can_timing timing;
int ret;
ret = can_calc_timing(can_dev, &timing, baudrate, 875);
if (ret > 0) {
LOG_INF("Sample-Point error: %d", ret);
}
if (ret < 0) {
LOG_ERR("Failed to calc a valid timing");
return -1;
}
ret = can_set_timing(can_dev, &timing);
if (ret != 0) {
LOG_ERR("Failed to set timing");
}
ret = can_start(can_dev);
if (ret != 0) {
LOG_ERR("Failed to start CAN controller");
}
if (ret == 0) {
LOG_INF("CAN initialized correctly");
}
return ret;
}