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)
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);
107 /***************************************************************************/
108 int set_bitrate( CAN_HANDLE fd, int baud)
111 volatile Command_par_t cmd;
114 ioctl(fd, COMMAND, &cmd);
116 cfg.target = CONF_TIMING;
118 ioctl(fd, CONFIG, &cfg);
121 ioctl(fd, COMMAND, &cmd);
125 /***************************************************************************/
126 int TranslateBaudeRate(char* optarg){
127 if(!strcmp( optarg, "1M")) return (int)1000;
128 if(!strcmp( optarg, "500K")) return (int)500;
129 if(!strcmp( optarg, "250K")) return (int)250;
130 if(!strcmp( optarg, "125K")) return (int)125;
131 if(!strcmp( optarg, "100K")) return (int)100;
132 if(!strcmp( optarg, "50K")) return (int)50;
133 if(!strcmp( optarg, "20K")) return (int)20;
134 if(!strcmp( optarg, "10K")) return (int)10;
135 if(!strcmp( optarg, "5K")) return (int)5;
136 if(!strcmp( optarg, "none")) return 0;
140 /***************************************************************************/
141 static const char lnx_can_dev_prefix[] = "/dev/can";
143 CAN_HANDLE canOpen_driver(s_BOARD *board)
145 int name_len = strlen(board->busname);
146 int prefix_len = strlen(lnx_can_dev_prefix);
147 char dev_name[prefix_len+name_len+1];
149 int baud = TranslateBaudeRate(board->baudrate);
153 /*o_flags = O_NONBLOCK;*/
155 memcpy(dev_name,lnx_can_dev_prefix,prefix_len);
156 memcpy(dev_name+prefix_len,board->busname,name_len);
157 dev_name[prefix_len+name_len] = 0;
159 fd0 = open(dev_name, O_RDWR|o_flags);
161 fprintf(stderr,"!!! %s is unknown. See can4linux.c\n", dev_name);
165 set_bitrate((CAN_HANDLE)fd0, baud);
167 printf("CAN device dev/can%s opened. Baudrate=>%s\n",board->busname, board->baudrate);
169 return (CAN_HANDLE)fd0;
175 /***************************************************************************/
176 int canClose_driver(CAN_HANDLE fd0)