-
Notifications
You must be signed in to change notification settings - Fork 40
/
coal_can.c
154 lines (122 loc) · 3.42 KB
/
coal_can.c
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*********************************************************************
* _ _ _
* _ __ | |_ _ | | __ _ | |__ ___
* | '__|| __|(_)| | / _` || '_ \ / __|
* | | | |_ _ | || (_| || |_) |\__ \
* |_| \__|(_)|_| \__,_||_.__/ |___/
*
* www.rt-labs.com
* Copyright 2017 rt-labs AB, Sweden.
*
* This software is dual-licensed under GPLv3 and a commercial
* license. See the file LICENSE.md distributed with this software for
* full license information.
********************************************************************/
#include "coal_can.h"
#include "osal.h"
#include "options.h"
#include "osal_log.h"
#include "co_log.h"
#include "co_main.h"
#include "co_rtk.h"
#include <drivers/can/can.h>
#include <fcntl.h>
#include <string.h>
static void os_can_callback (void * arg, can_event_t event)
{
os_channel_t * channel = arg;
if (channel->callback)
channel->callback (channel->arg);
}
os_channel_t * os_channel_open (const char * name, void * callback, void * arg)
{
os_channel_t * channel = malloc (sizeof (*channel));
can_filter_t filter = {
.id = 0xFFFFFFFF,
.mask = 0,
};
/* Create file descriptor for CAN bus */
channel->handle = open (name, O_RDWR);
if (channel->handle < 0)
{
free (channel);
return NULL;
}
channel->callback = callback;
channel->arg = arg;
can_set_callback (
channel->handle,
os_can_callback,
CAN_EVENT_MSG_RECEIVED,
channel);
can_filter (channel->handle, &filter);
return channel;
}
int os_channel_send (os_channel_t * channel, uint32_t id, const void * data, size_t dlc)
{
can_frame_t frame;
co_msg_log ("Tx", id, data, dlc);
frame.id = id & CO_ID_MASK;
frame.id |= (id & CO_RTR_MASK) ? CAN_ID_RTR : 0;
frame.id |= (id & CO_EXT_MASK) ? CAN_ID_EXT : 0;
frame.id = id;
frame.dlc = dlc;
memcpy (frame.data, data, dlc);
can_transmit (channel->handle, &frame);
return 0;
}
int os_channel_receive (
os_channel_t * channel,
uint32_t * id,
void * data,
size_t * dlc)
{
can_frame_t frame;
int result;
result = can_receive (channel->handle, &frame);
if (result != 0)
return -1;
*id = frame.id & CAN_ID_MASK;
*id |= (frame.id & CAN_ID_RTR) ? CO_RTR_MASK : 0;
*id |= (frame.id & CAN_ID_EXT) ? CO_EXT_MASK : 0;
*dlc = frame.dlc;
memcpy (data, frame.data, frame.dlc);
co_msg_log ("Rx", *id, data, *dlc);
return 0;
}
CC_ATTRIBUTE_WEAK void co_can_get_cfg (int bitrate, can_cfg_t * cfg)
{
ASSERT (0);
}
int os_channel_set_bitrate (os_channel_t * channel, int bitrate)
{
can_cfg_t cfg;
/* Get can parameters from application */
co_can_get_cfg (bitrate, &cfg);
/* Set can parameters */
can_set_cfg (channel->handle, &cfg);
return 0;
}
int os_channel_set_filter (os_channel_t * channel, uint8_t * filter, size_t size)
{
return 0;
}
int os_channel_bus_on (os_channel_t * channel)
{
can_bus_on (channel->handle);
return 0;
}
int os_channel_bus_off (os_channel_t * channel)
{
can_bus_off (channel->handle);
return 0;
}
int os_channel_get_state (os_channel_t * channel, os_channel_state_t * state)
{
can_status_t can_status;
can_get_status (channel->handle, &can_status);
state->overrun = can_status.flags & CAN_STATUS_BUFFER_OVERFLOW;
state->error_passive = can_status.flags & CAN_STATUS_ERROR_PASSIVE;
state->bus_off = can_status.flags & CAN_STATUS_BUS_OFF;
return 0;
}