author | Christian Taedcke <Christian.Taedcke@ica-traffic.de> |
Fri, 19 Feb 2010 08:18:40 +0100 | |
changeset 639 | 27c0a025acf3 |
parent 631 | 08b6b903f84a |
permissions | -rw-r--r-- |
26 | 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 |
#include <stdio.h> |
|
24 |
#include <string.h> |
|
25 |
#include <errno.h> |
|
26 |
#include <fcntl.h> |
|
27 |
||
28 |
#include "canmsg.h" |
|
29 |
#include "lincan.h" |
|
30 |
||
31 |
#include "can_driver.h" |
|
32 |
||
33 |
/*********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:
26
diff
changeset
|
34 |
UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m) |
26 | 35 |
{ |
36 |
int res; |
|
37 |
struct canmsg_t canmsg; |
|
38 |
||
39 |
canmsg.flags = 0; /* Ensure standard receive, not required for LinCAN>=0.3.1 */ |
|
40 |
do{ |
|
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
26
diff
changeset
|
41 |
res = read(fd0,&canmsg,sizeof(canmsg_t)); |
26 | 42 |
if((res<0)&&(errno == -EAGAIN)) res = 0; |
43 |
}while(res==0); |
|
44 |
||
45 |
if(res != sizeof(canmsg_t)) // No new message |
|
46 |
return 1; |
|
47 |
||
48 |
if(canmsg.flags&MSG_EXT){ |
|
49 |
/* There is no mark for extended messages in CanFestival */; |
|
50 |
} |
|
51 |
||
365 | 52 |
m->cob_id = canmsg.id; |
26 | 53 |
m->len = canmsg.length; |
54 |
if(canmsg.flags&MSG_RTR){ |
|
55 |
m->rtr = 1; |
|
56 |
}else{ |
|
57 |
m->rtr = 0; |
|
58 |
memcpy(m->data,canmsg.data,8); |
|
59 |
} |
|
60 |
||
61 |
return 0; |
|
62 |
} |
|
63 |
||
64 |
/***************************************************************************/ |
|
631 | 65 |
UNS8 canSend_driver(CAN_HANDLE fd0, Message const *m) |
26 | 66 |
{ |
67 |
int res; |
|
68 |
struct canmsg_t canmsg; |
|
69 |
||
70 |
||
71 |
canmsg.flags = 0; |
|
365 | 72 |
canmsg.id = m->cob_id; |
26 | 73 |
canmsg.length = m->len; |
74 |
if(m->rtr){ |
|
75 |
canmsg.flags |= MSG_RTR; |
|
76 |
}else{ |
|
77 |
memcpy(canmsg.data,m->data,8); |
|
78 |
} |
|
79 |
||
80 |
if(canmsg.id >= 0x800){ |
|
81 |
canmsg.flags |= MSG_EXT; |
|
82 |
} |
|
83 |
||
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
26
diff
changeset
|
84 |
res = write(fd0,&canmsg,sizeof(canmsg_t)); |
26 | 85 |
if(res!=sizeof(canmsg_t)) |
86 |
return 1; |
|
87 |
||
88 |
return 0; |
|
89 |
} |
|
90 |
||
91 |
/***************************************************************************/ |
|
477 | 92 |
int TranslateBaudeRate(char* optarg){ |
93 |
if(!strcmp( optarg, "1M")) return 1000000; |
|
94 |
if(!strcmp( optarg, "500K")) return 500000; |
|
95 |
if(!strcmp( optarg, "250K")) return 250000; |
|
96 |
if(!strcmp( optarg, "125K")) return 125000; |
|
97 |
if(!strcmp( optarg, "100K")) return 100000; |
|
98 |
if(!strcmp( optarg, "50K")) return 50000; |
|
99 |
if(!strcmp( optarg, "20K")) return 20000; |
|
100 |
if(!strcmp( optarg, "none")) return 0; |
|
101 |
return 0x0000; |
|
102 |
} |
|
103 |
||
104 |
/***************************************************************************/ |
|
105 |
UNS8 canChangeBaudRate_driver( CAN_HANDLE fd0, char* baud) |
|
384 | 106 |
{ |
477 | 107 |
struct can_baudparams_t params; |
108 |
||
109 |
params.baudrate = TranslateBaudeRate(baud); |
|
110 |
if(params.baudrate == 0) |
|
111 |
return 0; |
|
112 |
params.flags = -1; // use driver defaults |
|
113 |
params.sjw = -1; // use driver defaults |
|
114 |
params.sample_pt = -1; // use driver defaults |
|
115 |
if(ioctl((int)fd0, CONF_BAUDPARAMS, ¶ms) < 0) |
|
116 |
{ |
|
117 |
fprintf(stderr, "canOpen_driver (lincan): IOCTL set speed failed\n"); |
|
118 |
return 0; |
|
119 |
} |
|
120 |
return 1; |
|
384 | 121 |
} |
122 |
||
123 |
/***************************************************************************/ |
|
26 | 124 |
static const char lnx_can_dev_prefix[] = "/dev/can"; |
125 |
||
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
26
diff
changeset
|
126 |
CAN_HANDLE canOpen_driver(s_BOARD *board) |
26 | 127 |
{ |
477 | 128 |
int name_len = strlen(board->busname); |
129 |
int prefix_len = strlen(lnx_can_dev_prefix); |
|
130 |
char dev_name[prefix_len+name_len+1]; |
|
131 |
struct can_baudparams_t params; |
|
132 |
int o_flags = 0; |
|
133 |
int fd; |
|
26 | 134 |
|
477 | 135 |
/*o_flags = O_NONBLOCK;*/ |
26 | 136 |
|
477 | 137 |
memcpy(dev_name,lnx_can_dev_prefix,prefix_len); |
138 |
memcpy(dev_name+prefix_len,board->busname,name_len); |
|
139 |
dev_name[prefix_len+name_len] = 0; |
|
140 |
printf("dev_name %s\n", dev_name); |
|
26 | 141 |
|
477 | 142 |
fd = open(dev_name, O_RDWR|o_flags); |
143 |
if(fd < 0) |
|
144 |
{ |
|
145 |
fprintf(stderr,"!!! Board %s is unknown. See can_lincan.c\n", board->busname); |
|
146 |
goto error_ret; |
|
147 |
} |
|
148 |
printf("fd = %d\n", fd); |
|
26 | 149 |
|
477 | 150 |
// set baudrate |
151 |
params.baudrate = TranslateBaudeRate(board->baudrate); |
|
152 |
if(params.baudrate == 0) |
|
153 |
goto error_ret; |
|
154 |
params.flags = -1; // use driver defaults |
|
155 |
params.sjw = -1; // use driver defaults |
|
156 |
params.sample_pt = -1; // use driver defaults |
|
157 |
if(ioctl(fd, CONF_BAUDPARAMS, ¶ms) < 0) |
|
158 |
{ |
|
159 |
fprintf(stderr, "canOpen_driver (lincan): IOCTL set speed failed\n"); |
|
160 |
goto error_ret; |
|
161 |
} |
|
26 | 162 |
|
477 | 163 |
return (CAN_HANDLE)fd; |
26 | 164 |
|
477 | 165 |
error_ret: |
166 |
return NULL; |
|
26 | 167 |
} |
168 |
||
169 |
/***************************************************************************/ |
|
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
26
diff
changeset
|
170 |
int canClose_driver(CAN_HANDLE fd0) |
26 | 171 |
{ |
172 |
if(!fd0) |
|
173 |
return 0; |
|
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
26
diff
changeset
|
174 |
close(fd0); |
26 | 175 |
return 0; |
176 |
} |