-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanopenlib_linux_hw.cpp
More file actions
374 lines (320 loc) · 11.3 KB
/
Copy pathcanopenlib_linux_hw.cpp
File metadata and controls
374 lines (320 loc) · 11.3 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// The functions contained in this file are pretty dummy
// and are included only as a placeholder. Nevertheless,
// they *will* get included in the shared library if you
// don't remove them :)
//
// Obviously, you 'll have to write yourself the super-duper
// functions to include in the resulting library...
// Also, it's not necessary to write every function in this file.
// Feel free to add more files in this project. They will be
// included in the resulting library.
#include <stdio.h>
#include <string.h>
//#include <pthread.h>
#include <time.h>
#include <fcntl.h>
#include "libpcan.h"
#include "canopenlib_linux_hw.h"
//portato nell header
//extern "C"
//{
#define MAX_CAN_DEVICES 4
#define RX_QUEUE_SIZE 100
typedef struct
{
canPortHandle handle;
bool opened;
bool in_use;
int echo_enabled;
int bitrate;
pthread_mutex_t rx_queue_cs;
TPCANMsg rx_queue[RX_QUEUE_SIZE];
int rx_queue_put_pos;
int rx_queue_get_pos;
bool can_bus_on;
} CanPortDataStruct;
static CanPortDataStruct can_port_data_devices[ MAX_CAN_DEVICES ];
canOpenStatus putCanMessageInQueue (CanPortDataStruct *p,
TPCANMsg *can_frame)
{
//printf("putCanMessageInQueue\n");
canOpenStatus res = CANOPEN_OK;
//EnterCriticalSection( &p->rx_queue_cs ); // We need to imlplement a "faked" echo mechanism.
pthread_mutex_lock( &p->rx_queue_cs );
int put_pos = p->rx_queue_put_pos;
if ( ++put_pos >= RX_QUEUE_SIZE )
put_pos = 0;
if (put_pos != p->rx_queue_get_pos) {
memcpy( &(p->rx_queue[ p->rx_queue_put_pos ]), can_frame, sizeof(TPCANMsg) );
p->rx_queue_put_pos = put_pos;
res = CANOPEN_OK;
} else {
res = CANOPEN_ERROR_CAN_LAYER_OVRFLOW;
}
//LeaveCriticalSection( &p->rx_queue_cs );
pthread_mutex_unlock (&p->rx_queue_cs);
return res;
}
//------------------------------------------------------------------------
//
//------------------------------------------------------------------------
canOpenStatus getCanMessageFromQueue (CanPortDataStruct *p,
TPCANMsg *can_frame)
{
//printf("getCanMessageFromQueue\n");
//aggiunto default
canOpenStatus res = CANOPEN_ERROR_NO_MESSAGE;
int get_pos = p->rx_queue_get_pos;
if (get_pos != p->rx_queue_put_pos ) {
memcpy( can_frame, &(p->rx_queue[ p->rx_queue_get_pos ]), sizeof(TPCANMsg) );
if ( ++get_pos >= RX_QUEUE_SIZE )
get_pos = 0;
p->rx_queue_get_pos = get_pos;
res = CANOPEN_OK;
} else {
res = CANOPEN_ERROR_NO_MESSAGE;
}
return res;
}
//------------------------------------------------------------------------
//
//------------------------------------------------------------------------
void canSinglePortReset(int port)
{
can_port_data_devices[port].opened = false;
can_port_data_devices[port].in_use = false;
can_port_data_devices[port].echo_enabled = false;
can_port_data_devices[port].can_bus_on = false;
can_port_data_devices[port].handle = NULL;
//InitializeCriticalSection( &can_port_data_devices[i].rx_queue_cs );
//create mutex attribute variable
pthread_mutexattr_t mAttr;
// setup recursive mutex for mutex attribute
pthread_mutexattr_settype(&mAttr, PTHREAD_MUTEX_RECURSIVE_NP);
// Use the mutex attribute to create the mutex
pthread_mutex_init(&can_port_data_devices[port].rx_queue_cs, &mAttr);
// Mutex attribute can be destroy after initializing the mutex variable
pthread_mutexattr_destroy(&mAttr);
}
//------------------------------------------------------------------------
//
//------------------------------------------------------------------------
canOpenStatus canPortLibraryInit(void)
{
//printf("canPortLibraryInit\n");
canOpenStatus canopen_res = CANOPEN_OK;
// Make sure that all CAN ports are available.
for ( int i = 0; i < MAX_CAN_DEVICES; i++ ) {
canSinglePortReset(i);
}
return canopen_res;
}
//------------------------------------------------------------------------
//
//------------------------------------------------------------------------
canOpenStatus canPortOpen(int port)
{
char buf[12];
sprintf(buf, "/dev/pcan%d", port);
const char *devName = buf;
return canPortOpenByName(port,devName);
}
canOpenStatus canPortOpenByName(int port, const char *devName)
{
//printf("canPortOpen\n");
if ( port >= 0 && port < MAX_CAN_DEVICES ) {
can_port_data_devices[port].handle = LINUX_CAN_Open(devName, O_RDWR);
can_port_data_devices[port].opened = true;
//Removing existent filters if any
CAN_ResetFilter(can_port_data_devices[port].handle);
return CANOPEN_OK;
} else {
return CANOPEN_ERROR_HW_NOT_CONNECTED;
}
}
//------------------------------------------------------------------------
//
//------------------------------------------------------------------------
canOpenStatus canPortClose(int port)
{
//printf("canPortClose\n");
if (can_port_data_devices[ port ].can_bus_on)
canPortGoBusOff(port);
canOpenStatus ret = CANOPEN_ERROR_CAN_LAYER ;
canSinglePortReset(port);
ret = CANOPEN_OK;
return ret;
}
//------------------------------------------------------------------------
//
//------------------------------------------------------------------------
canOpenStatus canPortBitrateSet(int port, int bitrate)
{
//printf("canPortBitrateSet\n");
canOpenStatus canopen_res = CANOPEN_ERROR;
switch ( bitrate )
{
case CAN_BAUD_125K:
can_port_data_devices[port].bitrate = CAN_BAUD_125K;
canopen_res = CANOPEN_OK;
break;
case CAN_BAUD_250K:
can_port_data_devices[port].bitrate = CAN_BAUD_250K;
canopen_res = CANOPEN_OK;
break;
case CAN_BAUD_500K:
can_port_data_devices[port].bitrate = CAN_BAUD_500K;
canopen_res = CANOPEN_OK;
break;
case CAN_BAUD_1M:
can_port_data_devices[port].bitrate = CAN_BAUD_1M;
canopen_res = CANOPEN_OK;
break;
default:
canopen_res = CANOPEN_ERROR;
break;
}
return canopen_res;
}
//------------------------------------------------------------------------
//
//------------------------------------------------------------------------
canOpenStatus canPortEcho( int port, bool enabled )
{
//printf("canPortEcho\n");
canOpenStatus canopen_res = CANOPEN_OK;
can_port_data_devices[port].echo_enabled = enabled;
return canopen_res;
}
//------------------------------------------------------------------------
//
//------------------------------------------------------------------------
canOpenStatus canPortGoBusOn(int port)
{
//printf("canPortGoBusOn\n");
canOpenStatus canopen_res = CANOPEN_ERROR;
if (!can_port_data_devices[ port ].opened)
return CANOPEN_ERROR;
DWORD res = CAN_Init(can_port_data_devices[ port ].handle, can_port_data_devices[ port ].bitrate, CAN_INIT_TYPE_EX);
if ( res == CAN_ERR_OK ) {
can_port_data_devices[ port ].can_bus_on = true;
canopen_res = CANOPEN_OK;
} else {
printf("Error on bus %i",res);
}
return canopen_res;
}
//------------------------------------------------------------------------
//
//------------------------------------------------------------------------
canOpenStatus canPortGoBusOff(int port)
{
//printf("canPortGoBusOff");
canOpenStatus canopen_res = CANOPEN_ERROR;
DWORD res = CAN_Close(can_port_data_devices[ port ].handle);
if ( res == CAN_ERR_OK ) {
can_port_data_devices[ port ].opened = false;
can_port_data_devices[ port ].can_bus_on = false;
canopen_res = CANOPEN_OK;
}
return canopen_res;
}
//------------------------------------------------------------------------
//
//------------------------------------------------------------------------
canOpenStatus canPortWrite( int port,
long id,
void *msg,
unsigned int dlc,
unsigned int flags)
{
canOpenStatus canopen_res = CANOPEN_CAN_LAYER_FAILED;
TPCANMsg frame;
frame.ID = id;
frame.LEN = dlc;
frame.MSGTYPE = 0; // Clear all flags.
if ( flags & CAN_MSG_EXT )
frame.MSGTYPE = MSGTYPE_EXTENDED;
if ( flags & CAN_MSG_RTR )
frame.MSGTYPE = MSGTYPE_RTR;
else
memcpy( frame.DATA, msg, dlc);
//DWORD res = CAN_Write( &handle, &frame );
if (can_port_data_devices[ port ].opened && can_port_data_devices[ port ].can_bus_on) {
DWORD res = CAN_Write( can_port_data_devices[ port ].handle, &frame );
if ( res == CAN_ERR_OK ) {
if ( can_port_data_devices[ port ].echo_enabled ) {
(void)putCanMessageInQueue( &can_port_data_devices[ port ], &frame);
}
canopen_res = CANOPEN_OK;
}
//printf("\nRes of write %d %d \n",res, can_port_data_devices[ port ].echo_enabled );
return canopen_res;
} else {
return CANOPEN_CAN_LAYER_FAILED;
}
}
//------------------------------------------------------------------------
// non blocking
// to be cleaned more
// blocking again...
//------------------------------------------------------------------------
canOpenStatus canPortRead( int port,
long *id,
void *msg,
unsigned int *dlc,
unsigned int *flags)
{
//printf("\ncanPortRead\n");
canOpenStatus canopen_res = CANOPEN_CAN_LAYER_FAILED;
TPCANMsg frame;
//TPCANRdMsg frame;
DWORD res;
*flags = 0;
//printf("\n1\n");
//First check if you are connected and on
if (can_port_data_devices[ port ].opened && can_port_data_devices[ port ].can_bus_on) {
// First check if there are any echos avilable.
//printf("\n2\n");
canopen_res = getCanMessageFromQueue( &can_port_data_devices[ port ], &frame );
//printf("\n--> %d \n",canopen_res);
if ( canopen_res == CANOPEN_OK ) {
/*
TPCANRdMsg tmp_frame;
//printf("MSG in QUEUE!!!");
//with 0 it becomes non blocking
res = LINUX_CAN_Read_Timeout(can_port_data_devices[port].handle, &tmp_frame,0);
if ( res == CAN_ERR_OK && !( frame.MSGTYPE & MSGTYPE_STATUS ) ) {
(void)putCanMessageInQueue( &can_port_data_devices[ port ], &tmp_frame.Msg);
} else {
res = CANOPEN_ERROR_NO_MESSAGE;
}*/
} else {
TPCANRdMsg tmp_frame;
res = LINUX_CAN_Read_Timeout(can_port_data_devices[port].handle, &tmp_frame,-1);
//res = CAN_Read(can_port_data_devices[port].handle, &frame);
if ( res == CAN_ERR_OK ) {
//printf("FRAME RECEIVED!!!\n");
frame = tmp_frame.Msg;
if ( frame.MSGTYPE & MSGTYPE_STATUS ) {
res = CANOPEN_ERROR_NO_MESSAGE;
} else {
canopen_res = CANOPEN_OK;
}
}
}
if ( canopen_res == CANOPEN_OK ) {
*id = (long)frame.ID;
*dlc = frame.LEN;
if ( frame.MSGTYPE & MSGTYPE_RTR )
*flags = CAN_MSG_RTR;
if ( frame.MSGTYPE & MSGTYPE_EXTENDED )
*flags = MSGTYPE_EXTENDED;
memcpy( msg, frame.DATA, *dlc);
canopen_res = CANOPEN_OK;
}
return canopen_res;
} else {
return CANOPEN_CAN_LAYER_FAILED;
}
}