2 This file is part of CanFestival, a library implementing CanOpen Stack.
4 Copyright (C): Edouard TISSERANT and Francis DUPIN
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
28 /* driver pcan pci for Peak board */
29 //#include "libpcan.h"
32 #include "libpcan.h" // for CAN_HANDLE
34 #include "can_driver.h"
36 // Define for rtr CAN message
37 #define CAN_INIT_TYPE_ST_RTR MSGTYPE_STANDARD | MSGTYPE_RTR
39 /*********functions which permit to communicate with the board****************/
40 UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m)
44 if ((errno = CAN_Read(fd0, & peakMsg))) { // Blocks until no new message or error.
45 if(errno != -EIDRM && errno != -EPERM) // error is not "Can Port closed while reading"
47 perror("canReceive_driver (Peak_Linux) : error of reading.\n");
51 m->cob_id = peakMsg.ID;
52 if (peakMsg.MSGTYPE == CAN_INIT_TYPE_ST) /* bits of MSGTYPE_*/
56 m->len = peakMsg.LEN; /* count of data bytes (0..8) */
57 for(data = 0 ; data < peakMsg.LEN ; data++)
58 m->data[data] = peakMsg.DATA[data]; /* data bytes, up to 8 */
60 #if defined DEBUG_MSG_CONSOLE_ON
68 /***************************************************************************/
69 UNS8 canSend_driver(CAN_HANDLE fd0, Message *m)
73 peakMsg.ID=m -> cob_id; /* 11/29 bit code */
75 peakMsg.MSGTYPE = CAN_INIT_TYPE_ST; /* bits of MSGTYPE_*/
77 peakMsg.MSGTYPE = CAN_INIT_TYPE_ST_RTR; /* bits of MSGTYPE_*/
80 /* count of data bytes (0..8) */
81 for(data = 0 ; data < m->len; data ++)
82 peakMsg.DATA[data] = m->data[data]; /* data bytes, up to 8 */
84 #if defined DEBUG_MSG_CONSOLE_ON
88 if((errno = CAN_Write(fd0, & peakMsg))) {
89 perror("canSend_driver (Peak_Linux) : error of writing.\n");
97 /***************************************************************************/
98 int TranslateBaudeRate(char* optarg){
99 if(!strcmp( optarg, "1M")) return CAN_BAUD_1M;
100 if(!strcmp( optarg, "500K")) return CAN_BAUD_500K;
101 if(!strcmp( optarg, "250K")) return CAN_BAUD_250K;
102 if(!strcmp( optarg, "125K")) return CAN_BAUD_125K;
103 if(!strcmp( optarg, "100K")) return CAN_BAUD_100K;
104 if(!strcmp( optarg, "50K")) return CAN_BAUD_50K;
105 if(!strcmp( optarg, "20K")) return CAN_BAUD_20K;
106 if(!strcmp( optarg, "10K")) return CAN_BAUD_10K;
107 if(!strcmp( optarg, "5K")) return CAN_BAUD_5K;
108 if(!strcmp( optarg, "none")) return 0;
112 UNS8 canChangeBaudRate_driver( CAN_HANDLE fd, char* baud)
114 printf("canChangeBaudRate not yet supported by this driver\n");
118 /***************************************************************************/
119 CAN_HANDLE canOpen_driver(s_BOARD *board)
127 if(strtol(board->busname, &pEnd,0) >= 0)
129 sprintf(busname,"/dev/pcan%s",board->busname);
130 fd0 = LINUX_CAN_Open(busname, O_RDWR);
133 if(fd0 && (baudrate = TranslateBaudeRate(board->baudrate)))
135 CAN_Init(fd0, baudrate, CAN_INIT_TYPE_ST);
137 fprintf(stderr, "canOpen_driver (Peak_Linux) : error opening %s\n", busname);
140 return (CAN_HANDLE)fd0;
143 /***************************************************************************/
144 int canClose_driver(CAN_HANDLE fd0)