author | greg |
Fri, 04 May 2007 12:26:46 +0200 | |
changeset 178 | 01d81faa3350 |
parent 157 | 8b45ff4202c3 |
child 193 | 0487270d441c |
permissions | -rw-r--r-- |
0 | 1 |
/* |
2 |
This file is part of CanFestival, a library implementing CanOpen Stack. |
|
3 |
||
4 |
Copyright (C): Edouard TISSERANT and Francis DUPIN |
|
5 |
||
6 |
See COPYING file for copyrights details. |
|
7 |
||
8 |
This library is free software; you can redistribute it and/or |
|
9 |
modify it under the terms of the GNU Lesser General Public |
|
10 |
License as published by the Free Software Foundation; either |
|
11 |
version 2.1 of the License, or (at your option) any later version. |
|
12 |
||
13 |
This library is distributed in the hope that it will be useful, |
|
14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
16 |
Lesser General Public License for more details. |
|
17 |
||
18 |
You should have received a copy of the GNU Lesser General Public |
|
19 |
License along with this library; if not, write to the Free Software |
|
20 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
21 |
*/ |
|
22 |
||
23 |
/* |
|
24 |
Virtual CAN driver. |
|
25 |
*/ |
|
26 |
||
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
27 |
#include <stdio.h> |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
28 |
#include <unistd.h> |
0 | 29 |
|
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
30 |
#include "can_driver.h" |
157 | 31 |
#include "def.h" |
0 | 32 |
|
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
33 |
#define MAX_NB_CAN_PIPES 16 |
0 | 34 |
|
35 |
typedef struct { |
|
36 |
char used; |
|
37 |
int pipe[2]; |
|
38 |
} CANPipe; |
|
39 |
||
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
40 |
CANPipe canpipes[MAX_NB_CAN_PIPES] = {{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},}; |
0 | 41 |
|
42 |
/*********functions which permit to communicate with the board****************/ |
|
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
43 |
UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m) |
0 | 44 |
{ |
45 |
if(read(((CANPipe*)fd0)->pipe[0], m, sizeof(Message)) < sizeof(Message)) |
|
46 |
{ |
|
47 |
return 1; |
|
48 |
} |
|
49 |
return 0; |
|
50 |
} |
|
51 |
||
157 | 52 |
#define MyCase(fc) case fc: printf(#fc);break; |
53 |
void print_message(Message *m) |
|
54 |
{ |
|
55 |
int i; |
|
56 |
switch(m->cob_id.w >> 7) |
|
57 |
{ |
|
58 |
MyCase(SYNC) |
|
59 |
MyCase(TIME_STAMP) |
|
60 |
MyCase(PDO1tx) |
|
61 |
MyCase(PDO1rx) |
|
62 |
MyCase(PDO2tx) |
|
63 |
MyCase(PDO2rx) |
|
64 |
MyCase(PDO3tx) |
|
65 |
MyCase(PDO3rx) |
|
66 |
MyCase(PDO4tx) |
|
67 |
MyCase(PDO4rx) |
|
68 |
MyCase(SDOtx) |
|
69 |
MyCase(SDOrx) |
|
70 |
MyCase(NODE_GUARD) |
|
71 |
MyCase(NMT) |
|
72 |
} |
|
73 |
printf(" rtr:%d", m->rtr); |
|
74 |
printf(" len:%d", m->len); |
|
75 |
for (i = 0 ; i < m->len ; i++) |
|
76 |
printf(" %02x", m->data[i]); |
|
77 |
printf("\n"); |
|
78 |
} |
|
79 |
||
0 | 80 |
/***************************************************************************/ |
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
81 |
UNS8 canSend_driver(CAN_HANDLE fd0, Message *m) |
0 | 82 |
{ |
83 |
int i; |
|
157 | 84 |
|
85 |
printf("%x->[ ", (CANPipe*)fd0 - &canpipes[0]); |
|
86 |
for(i=0; i < MAX_NB_CAN_PIPES; i++) |
|
87 |
{ |
|
88 |
if(canpipes[i].used && &canpipes[i] != (CANPipe*)fd0) |
|
89 |
{ |
|
90 |
printf("%x ",i); |
|
91 |
} |
|
92 |
} |
|
93 |
printf(" ]"); |
|
94 |
print_message(m); |
|
95 |
||
0 | 96 |
// Send to all readers, except myself |
97 |
for(i=0; i < MAX_NB_CAN_PIPES; i++) |
|
98 |
{ |
|
99 |
if(canpipes[i].used && &canpipes[i] != (CANPipe*)fd0) |
|
100 |
{ |
|
101 |
write(canpipes[i].pipe[1], m, sizeof(Message)); |
|
102 |
} |
|
103 |
} |
|
104 |
return 0; |
|
105 |
} |
|
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
106 |
/* |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
107 |
int TranslateBaudeRate(char* optarg){ |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
108 |
if(!strcmp( optarg, "1M")) return 1000; |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
109 |
if(!strcmp( optarg, "500K")) return 500; |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
110 |
if(!strcmp( optarg, "250K")) return 250; |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
111 |
if(!strcmp( optarg, "125K")) return 125; |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
112 |
if(!strcmp( optarg, "100K")) return 100; |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
113 |
if(!strcmp( optarg, "50K")) return 50; |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
114 |
if(!strcmp( optarg, "20K")) return 20; |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
115 |
if(!strcmp( optarg, "10K")) return 10; |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
116 |
if(!strcmp( optarg, "5K")) return 5; |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
117 |
if(!strcmp( optarg, "none")) return 0; |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
118 |
return 0; |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
119 |
}*/ |
0 | 120 |
/***************************************************************************/ |
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
121 |
CAN_HANDLE canOpen_driver(s_BOARD *board) |
0 | 122 |
{ |
123 |
int i; |
|
124 |
for(i=0; i < MAX_NB_CAN_PIPES; i++) |
|
125 |
{ |
|
126 |
if(!canpipes[i].used) |
|
127 |
break; |
|
128 |
} |
|
129 |
||
130 |
/* Create the pipe. */ |
|
131 |
if (i==MAX_NB_CAN_PIPES || pipe(canpipes[i].pipe)) |
|
132 |
{ |
|
133 |
fprintf (stderr, "Open failed.\n"); |
|
134 |
return (CAN_HANDLE)NULL; |
|
135 |
} |
|
136 |
||
137 |
canpipes[i].used = 1; |
|
138 |
return (CAN_HANDLE) &canpipes[i]; |
|
139 |
} |
|
140 |
||
141 |
/***************************************************************************/ |
|
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
142 |
int canClose_driver(CAN_HANDLE fd0) |
0 | 143 |
{ |
144 |
close(((CANPipe*)fd0)->pipe[0]); |
|
145 |
close(((CANPipe*)fd0)->pipe[1]); |
|
146 |
((CANPipe*)fd0)->used = 0; |
|
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
0
diff
changeset
|
147 |
return 0; |
0 | 148 |
} |
149 |
||
150 |