2 This file is part of CanFestival, a library implementing CanOpen Stack.
4 Copyright (C): Jorge BERZOSA
6 See COPYING file for copyrights details.
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.
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.
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
32 #include "can4linux.h"
33 #include "can_driver.h"
35 //struct timeval init_time,current_time;
37 /*********functions which permit to communicate with the board****************/
38 UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m)
42 //long int time_period;
46 res = read(fd0,&canmsg,1);
47 if((res<0)&&(errno == -EAGAIN)) res = 0;
50 if(res !=1) // No new message
53 if(canmsg.flags&MSG_EXT){
54 /* There is no mark for extended messages in CanFestival */;
57 m->cob_id = canmsg.id;
58 m->len = canmsg.length;
59 if(canmsg.flags&MSG_RTR){
63 memcpy(m->data,canmsg.data,8);
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;
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);
71 for(i=0; i<canmsg.length;i++)printf("%x, ", m->data[i]);
77 /***************************************************************************/
78 UNS8 canSend_driver(CAN_HANDLE fd0, Message *m)
84 canmsg.id = m->cob_id;
85 canmsg.length = m->len;
87 canmsg.flags |= MSG_RTR;
89 memcpy(canmsg.data,m->data,8);
92 /*printf("Send ID: %lx ->",canmsg.id);
93 for(i=0; i<canmsg.length;i++)printf("%x, ", canmsg.data[i]);
96 if(canmsg.id >= 0x800){
97 canmsg.flags |= MSG_EXT;
100 res = write(fd0,&canmsg,1);
108 /***************************************************************************/
109 int TranslateBaudRate(char* optarg){
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;
122 UNS8 _canChangeBaudRate( CAN_HANDLE fd, int baud)
125 volatile Command_par_t cmd;
128 ioctl(fd, COMMAND, &cmd);
130 cfg.target = CONF_TIMING;
132 ioctl(fd, CONFIG, &cfg);
135 ioctl(fd, COMMAND, &cmd);
140 UNS8 canChangeBaudRate_driver( CAN_HANDLE fd, char* baud)
142 int temp=TranslateBaudRate(baud);
145 _canChangeBaudRate(fd, temp);
146 printf("Baudrate changed to=>%s\n", baud);
150 /***************************************************************************/
151 static const char lnx_can_dev_prefix[] = "/dev/can";
153 CAN_HANDLE canOpen_driver(s_BOARD *board)
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];
159 //int baud = TranslateBaudeRate(board->baudrate);
164 /*o_flags = O_NONBLOCK;*/
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;
170 fd0 = open(dev_name, O_RDWR|o_flags);
172 fprintf(stderr,"!!! %s is unknown. See can4linux.c\n", dev_name);
176 res=TranslateBaudRate(board->baudrate);
178 fprintf(stderr,"!!! %s baudrate not supported. See can4linux.c\n", board->baudrate);
182 _canChangeBaudRate( (CAN_HANDLE)fd0, res);
184 printf("CAN device dev/can%s opened. Baudrate=>%s\n",board->busname, board->baudrate);
186 return (CAN_HANDLE)fd0;
192 /***************************************************************************/
193 int canClose_driver(CAN_HANDLE fd0)
196 return close((int)fd0);
202 int canfd_driver(CAN_HANDLE fd0)