forked from collin80/GEVCU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CanHandler.cpp
277 lines (230 loc) · 7.94 KB
/
CanHandler.cpp
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* CanHandler.cpp
*
* Devices may register to this handler in order to receive CAN frames (publish/subscribe)
* and they can also use this class to send messages.
*
Copyright (c) 2013 Collin Kidder, Michael Neuweiler, Charles Galpin
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "CanHandler.h"
CanHandler *CanHandler::canHandlerEV = NULL;
CanHandler *CanHandler::canHandlerCar = NULL;
/*
* Private constructor of the can handler
*/
CanHandler::CanHandler(CanBusNode canBusNode) {
this->canBusNode = canBusNode;
// assign the correct bus instance to the pointer
if (canBusNode == CAN_BUS_CAR)
bus = &CAN2;
else
bus = &CAN;
for (int i=0; i < CFG_CAN_NUM_OBSERVERS; i++)
observerData[i].observer = NULL;
}
/*
* Retrieve the singleton instance of the CanHandler which is responsible
* for the EV can bus (CAN0)
*
* \retval the CanHandler instance for the EV can bus
*/
CanHandler* CanHandler::getInstanceEV()
{
if (canHandlerEV == NULL)
canHandlerEV = new CanHandler(CAN_BUS_EV);
return canHandlerEV;
}
/*
* Retrieve the singleton instance of the CanHandler which is responsible
* for the car can bus (CAN1)
*
* \retval the CanHandler instance for the car can bus
*/
CanHandler* CanHandler::getInstanceCar()
{
if (canHandlerCar == NULL)
canHandlerCar = new CanHandler(CAN_BUS_CAR);
return canHandlerCar;
}
/*
* Initialization of the CAN bus
*/
void CanHandler::initialize() {
// Initialize the canbus at the specified baudrate
bus->begin(canBusNode == CAN_BUS_EV ? CFG_CAN0_SPEED : CFG_CAN1_SPEED, 255);
//Mailboxes are default set up initialized with one MB for TX and the rest for RX
//That's OK with us so no need to initialize those things there.
Logger::info("CAN%d init ok", (canBusNode == CAN_BUS_EV ? 0 : 1));
}
/*
* Attach a CanObserver. Can frames which match the id/mask will be forwarded to the observer
* via the method handleCanFrame(RX_CAN_FRAME).
* Sets up a can bus mailbox if necessary.
*
* \param observer - the observer object to register (must implement CanObserver class)
* \param id - the id of the can frame to listen to
* \param mask - the mask to be applied to the frames
* \param extended - set if extended frames must be supported
*/
void CanHandler::attach(CanObserver* observer, uint32_t id, uint32_t mask, bool extended) {
int8_t pos = findFreeObserverData();
if (pos == -1) {
Logger::error("no free space in CanHandler::observerData, increase its size via CFG_CAN_NUM_OBSERVERS");
return;
}
int mailbox = bus->findFreeRXMailbox();
if (mailbox == -1) {
Logger::error("no free CAN mailbox on bus %d", canBusNode);
return;
}
observerData[pos].id = id;
observerData[pos].mask = mask;
observerData[pos].extended = extended;
observerData[pos].mailbox = mailbox;
observerData[pos].observer = observer;
bus->setRXFilter((uint8_t)mailbox, id, mask, extended);
Logger::debug("attached CanObserver (%X) for id=%X, mask=%X, mailbox=%d", observer, id, mask, mailbox);
}
/*
* Detaches a previously attached observer from this handler.
*
* \param observer - observer object to detach
* \param id - id of the observer to detach (required as one CanObserver may register itself several times)
* \param mask - mask of the observer to detach (dito)
*/
void CanHandler::detach(CanObserver* observer, uint32_t id, uint32_t mask) {
for (int i = 0; i < CFG_CAN_NUM_OBSERVERS; i++) {
if (observerData[i].observer == observer &&
observerData[i].id == id &&
observerData[i].mask == mask) {
observerData[i].observer = NULL;
//TODO: if no more observers on same mailbox, disable its interrupt, reset mailbox
}
}
}
/*
* Logs the content of a received can frame
*
* \param frame - the received can frame to log
*/
void CanHandler::logFrame(CAN_FRAME& frame) {
if (Logger::isDebug()) {
Logger::debug("CAN: dlc=%X fid=%X id=%X ide=%X rtr=%X data=%X,%X,%X,%X,%X,%X,%X,%X",
frame.length, frame.fid, frame.id, frame.extended, frame.rtr,
frame.data.bytes[0], frame.data.bytes[1], frame.data.bytes[2], frame.data.bytes[3],
frame.data.bytes[4], frame.data.bytes[5], frame.data.bytes[6], frame.data.bytes[7]);
}
}
/*
* Find a observerData entry which is not in use.
*
* \retval array index of the next unused entry in observerData[]
*/
int8_t CanHandler::findFreeObserverData() {
for (int i = 0; i < CFG_CAN_NUM_OBSERVERS; i++) {
if (observerData[i].observer == NULL)
return i;
}
return -1;
}
/*
* Find a unused can mailbox according to entries in observerData[].
*
* \retval the mailbox index of the next unused mailbox
*/
int8_t CanHandler::findFreeMailbox() {
uint8_t numRxMailboxes = (canBusNode == CAN_BUS_EV ? CFG_CAN0_NUM_RX_MAILBOXES : CFG_CAN1_NUM_RX_MAILBOXES);
for (uint8_t i = 0; i < numRxMailboxes; i++) {
bool used = false;
for (uint8_t j = 0; j < CFG_CAN_NUM_OBSERVERS; j++) {
if (observerData[j].observer != NULL && observerData[j].mailbox == i)
used = true;
}
if (!used)
return i;
}
return -1;
}
/*
* If a message is available, read it and forward it to registered observers.
*/
void CanHandler::process() {
static CAN_FRAME frame;
if (bus->rx_avail()) {
bus->get_rx_buff(frame);
//logFrame(frame);
for (int i = 0; i < CFG_CAN_NUM_OBSERVERS; i++) {
if (observerData[i].observer != NULL) {
// Apply mask to frame.id and observer.id. If they match, forward the frame to the observer
if ((frame.id & observerData[i].mask) == (observerData[i].id & observerData[i].mask))
observerData[i].observer->handleCanFrame(&frame);
}
if(frame.id==CAN_SWITCH)CANIO(frame);
}
}
}
//Allow the canbus driver to figure out the proper mailbox to use
//(whatever happens to be open) or queue it to send (if nothing is open)
void CanHandler::sendFrame(CAN_FRAME& frame) {
bus->sendFrame(frame);
// logFrame(frame);
}
/*
* Default implementation of the CanObserver method. Must be overwritten
* by every sub-class.
*/
void CanObserver::handleCanFrame(CAN_FRAME *frame) {
Logger::error("CanObserver does not implement handleCanFrame(), frame.id=%d", frame->id);
}
void CanHandler::CANIO(CAN_FRAME& frame) {
static CAN_FRAME CANioFrame;
CANioFrame.id = CAN_OUTPUTS;
CANioFrame.length = 8;
CANioFrame.extended = 0; //standard frame
CANioFrame.rtr = 0;
for(int i=0;i==8;i++)
{
if(frame.data.bytes[i]==0x88)setOutput(i,true);
if(frame.data.bytes[i]==0xFF)setOutput(i,false);
}
for(int i=0;i==8;i++)
{
if(getOutput(i))CANioFrame.data.bytes[i]=0x88;
else CANioFrame.data.bytes[i]=0xFF;
}
sendFrame(CANioFrame);
CANioFrame.id = CAN_ANALOG_INPUTS;
int i=0;
uint16_t anaVal;
for(int j=0;j>6;j+=2)
{
anaVal=getAnalog(i++);
CANioFrame.data.bytes[j]=highByte (anaVal);
CANioFrame.data.bytes[j+1]=lowByte(anaVal);
}
sendFrame(CANioFrame);
CANioFrame.id = CAN_DIGITAL_INPUTS;
CANioFrame.length = 4;
for(int i=0;i==4;i++)
{
if (getDigital(i))CANioFrame.data.bytes[i]=0x88;
else CANioFrame.data.bytes[i]=0xff;
}
sendFrame(CANioFrame);
}