author | Christian Taedcke |
Fri, 28 Jan 2011 14:51:18 +0100 | |
changeset 657 | c0e68a63f600 |
parent 631 | 08b6b903f84a |
permissions | -rwxr-xr-x |
364 | 1 |
/* |
2 |
This file is part of CanFestival, a library implementing CanOpen Stack. |
|
3 |
||
4 |
Copyright (C): Jorge BERZOSA |
|
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 |
* can4linux driver |
|
25 |
*/ |
|
26 |
||
27 |
#include <stdio.h> |
|
28 |
#include <string.h> |
|
29 |
#include <errno.h> |
|
30 |
#include <fcntl.h> |
|
31 |
||
32 |
#include "can4linux.h" |
|
33 |
#include "can_driver.h" |
|
34 |
||
384 | 35 |
//struct timeval init_time,current_time; |
364 | 36 |
|
37 |
/*********functions which permit to communicate with the board****************/ |
|
38 |
UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m) |
|
39 |
{ |
|
40 |
int res,i; |
|
41 |
canmsg_t canmsg; |
|
384 | 42 |
//long int time_period; |
364 | 43 |
|
44 |
canmsg.flags = 0; |
|
45 |
do{ |
|
46 |
res = read(fd0,&canmsg,1); |
|
47 |
if((res<0)&&(errno == -EAGAIN)) res = 0; |
|
48 |
}while(res==0); |
|
49 |
||
50 |
if(res !=1) // No new message |
|
51 |
return 1; |
|
52 |
||
53 |
if(canmsg.flags&MSG_EXT){ |
|
54 |
/* There is no mark for extended messages in CanFestival */; |
|
55 |
} |
|
56 |
||
365 | 57 |
m->cob_id = canmsg.id; |
364 | 58 |
m->len = canmsg.length; |
59 |
if(canmsg.flags&MSG_RTR){ |
|
60 |
m->rtr = 1; |
|
61 |
}else{ |
|
62 |
m->rtr = 0; |
|
63 |
memcpy(m->data,canmsg.data,8); |
|
64 |
} |
|
65 |
||
66 |
||
67 |
/*gettimeofday(¤t_time,NULL); |
|
68 |
time_period=(current_time.tv_sec - init_time.tv_sec)* 1000000 + current_time.tv_usec - init_time.tv_usec; |
|
365 | 69 |
printf("%3ld.%3ld.%3ld - Receive ID: %lx ->",time_period/1000000,(time_period%1000000)/1000,time_period%1000,m->cob_id); |
70 |
printf("Receive ID: %lx ->",m->cob_id); |
|
364 | 71 |
for(i=0; i<canmsg.length;i++)printf("%x, ", m->data[i]); |
72 |
printf("\n");*/ |
|
73 |
||
74 |
return 0; |
|
75 |
} |
|
76 |
||
77 |
/***************************************************************************/ |
|
631 | 78 |
UNS8 canSend_driver(CAN_HANDLE fd0, Message const *m) |
364 | 79 |
{ |
80 |
int res; |
|
81 |
canmsg_t canmsg; |
|
82 |
||
83 |
canmsg.flags = 0; |
|
365 | 84 |
canmsg.id = m->cob_id; |
364 | 85 |
canmsg.length = m->len; |
86 |
if(m->rtr){ |
|
87 |
canmsg.flags |= MSG_RTR; |
|
88 |
}else{ |
|
89 |
memcpy(canmsg.data,m->data,8); |
|
90 |
} |
|
91 |
||
92 |
/*printf("Send ID: %lx ->",canmsg.id); |
|
93 |
for(i=0; i<canmsg.length;i++)printf("%x, ", canmsg.data[i]); |
|
94 |
printf("\n");*/ |
|
95 |
||
96 |
if(canmsg.id >= 0x800){ |
|
97 |
canmsg.flags |= MSG_EXT; |
|
98 |
} |
|
99 |
||
100 |
res = write(fd0,&canmsg,1); |
|
101 |
if(res!=1) |
|
102 |
return 1; |
|
103 |
||
104 |
return 0; |
|
105 |
} |
|
106 |
||
107 |
||
108 |
/***************************************************************************/ |
|
384 | 109 |
int TranslateBaudRate(char* optarg){ |
364 | 110 |
if(!strcmp( optarg, "1M")) return (int)1000; |
111 |
if(!strcmp( optarg, "500K")) return (int)500; |
|
112 |
if(!strcmp( optarg, "250K")) return (int)250; |
|
113 |
if(!strcmp( optarg, "125K")) return (int)125; |
|
114 |
if(!strcmp( optarg, "100K")) return (int)100; |
|
115 |
if(!strcmp( optarg, "50K")) return (int)50; |
|
116 |
if(!strcmp( optarg, "20K")) return (int)20; |
|
117 |
if(!strcmp( optarg, "10K")) return (int)10; |
|
118 |
if(!strcmp( optarg, "5K")) return (int)5; |
|
384 | 119 |
return 0; |
120 |
} |
|
121 |
||
122 |
UNS8 _canChangeBaudRate( CAN_HANDLE fd, int baud) |
|
123 |
{ |
|
124 |
Config_par_t cfg; |
|
125 |
volatile Command_par_t cmd; |
|
126 |
||
127 |
cmd.cmd = CMD_STOP; |
|
128 |
ioctl(fd, COMMAND, &cmd); |
|
129 |
||
130 |
cfg.target = CONF_TIMING; |
|
131 |
cfg.val1 = baud; |
|
132 |
ioctl(fd, CONFIG, &cfg); |
|
133 |
||
134 |
cmd.cmd = CMD_START; |
|
135 |
ioctl(fd, COMMAND, &cmd); |
|
136 |
||
137 |
return 0; |
|
138 |
} |
|
139 |
||
140 |
UNS8 canChangeBaudRate_driver( CAN_HANDLE fd, char* baud) |
|
141 |
{ |
|
142 |
int temp=TranslateBaudRate(baud); |
|
143 |
||
144 |
if(temp==0)return 1; |
|
145 |
_canChangeBaudRate(fd, temp); |
|
146 |
printf("Baudrate changed to=>%s\n", baud); |
|
147 |
return 0; |
|
364 | 148 |
} |
149 |
||
150 |
/***************************************************************************/ |
|
151 |
static const char lnx_can_dev_prefix[] = "/dev/can"; |
|
152 |
||
153 |
CAN_HANDLE canOpen_driver(s_BOARD *board) |
|
154 |
{ |
|
155 |
int name_len = strlen(board->busname); |
|
156 |
int prefix_len = strlen(lnx_can_dev_prefix); |
|
157 |
char dev_name[prefix_len+name_len+1]; |
|
158 |
int o_flags = 0; |
|
384 | 159 |
//int baud = TranslateBaudeRate(board->baudrate); |
364 | 160 |
int fd0; |
384 | 161 |
int res; |
364 | 162 |
|
163 |
||
164 |
/*o_flags = O_NONBLOCK;*/ |
|
165 |
||
166 |
memcpy(dev_name,lnx_can_dev_prefix,prefix_len); |
|
167 |
memcpy(dev_name+prefix_len,board->busname,name_len); |
|
168 |
dev_name[prefix_len+name_len] = 0; |
|
169 |
||
170 |
fd0 = open(dev_name, O_RDWR|o_flags); |
|
471
bea45a091214
Applied James patch that fixes can_cn4linux.c file descriptor handling and make it compatible with can_serial_hub.
etisserant
parents:
384
diff
changeset
|
171 |
if(fd0 == -1){ |
364 | 172 |
fprintf(stderr,"!!! %s is unknown. See can4linux.c\n", dev_name); |
173 |
goto error_ret; |
|
174 |
} |
|
175 |
||
384 | 176 |
res=TranslateBaudRate(board->baudrate); |
177 |
if(res == 0){ |
|
178 |
fprintf(stderr,"!!! %s baudrate not supported. See can4linux.c\n", board->baudrate); |
|
179 |
goto error_ret; |
|
180 |
} |
|
181 |
||
182 |
_canChangeBaudRate( (CAN_HANDLE)fd0, res); |
|
364 | 183 |
|
184 |
printf("CAN device dev/can%s opened. Baudrate=>%s\n",board->busname, board->baudrate); |
|
185 |
||
186 |
return (CAN_HANDLE)fd0; |
|
187 |
||
188 |
error_ret: |
|
189 |
return NULL; |
|
190 |
} |
|
191 |
||
192 |
/***************************************************************************/ |
|
193 |
int canClose_driver(CAN_HANDLE fd0) |
|
194 |
{ |
|
471
bea45a091214
Applied James patch that fixes can_cn4linux.c file descriptor handling and make it compatible with can_serial_hub.
etisserant
parents:
384
diff
changeset
|
195 |
if((int)fd0 != -1) { |
bea45a091214
Applied James patch that fixes can_cn4linux.c file descriptor handling and make it compatible with can_serial_hub.
etisserant
parents:
384
diff
changeset
|
196 |
return close((int)fd0); |
bea45a091214
Applied James patch that fixes can_cn4linux.c file descriptor handling and make it compatible with can_serial_hub.
etisserant
parents:
384
diff
changeset
|
197 |
} |
bea45a091214
Applied James patch that fixes can_cn4linux.c file descriptor handling and make it compatible with can_serial_hub.
etisserant
parents:
384
diff
changeset
|
198 |
|
bea45a091214
Applied James patch that fixes can_cn4linux.c file descriptor handling and make it compatible with can_serial_hub.
etisserant
parents:
384
diff
changeset
|
199 |
return -1; |
bea45a091214
Applied James patch that fixes can_cn4linux.c file descriptor handling and make it compatible with can_serial_hub.
etisserant
parents:
384
diff
changeset
|
200 |
} |
bea45a091214
Applied James patch that fixes can_cn4linux.c file descriptor handling and make it compatible with can_serial_hub.
etisserant
parents:
384
diff
changeset
|
201 |
|
bea45a091214
Applied James patch that fixes can_cn4linux.c file descriptor handling and make it compatible with can_serial_hub.
etisserant
parents:
384
diff
changeset
|
202 |
int canfd_driver(CAN_HANDLE fd0) |
bea45a091214
Applied James patch that fixes can_cn4linux.c file descriptor handling and make it compatible with can_serial_hub.
etisserant
parents:
384
diff
changeset
|
203 |
{ |
bea45a091214
Applied James patch that fixes can_cn4linux.c file descriptor handling and make it compatible with can_serial_hub.
etisserant
parents:
384
diff
changeset
|
204 |
return ((int)fd0); |
bea45a091214
Applied James patch that fixes can_cn4linux.c file descriptor handling and make it compatible with can_serial_hub.
etisserant
parents:
384
diff
changeset
|
205 |
} |
bea45a091214
Applied James patch that fixes can_cn4linux.c file descriptor handling and make it compatible with can_serial_hub.
etisserant
parents:
384
diff
changeset
|
206 |
|
bea45a091214
Applied James patch that fixes can_cn4linux.c file descriptor handling and make it compatible with can_serial_hub.
etisserant
parents:
384
diff
changeset
|
207 |