nico@215: nico@215:
nico@215:00001 /* nico@215: 00002 This file is part of CanFestival, a library implementing CanOpen Stack. nico@215: 00003 nico@215: 00004 Copyright (C): Edouard TISSERANT and Francis DUPIN nico@215: 00005 nico@215: 00006 See COPYING file for copyrights details. nico@215: 00007 nico@215: 00008 This library is free software; you can redistribute it and/or nico@215: 00009 modify it under the terms of the GNU Lesser General Public nico@215: 00010 License as published by the Free Software Foundation; either nico@215: 00011 version 2.1 of the License, or (at your option) any later version. nico@215: 00012 nico@215: 00013 This library is distributed in the hope that it will be useful, nico@215: 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of nico@215: 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU nico@215: 00016 Lesser General Public License for more details. nico@215: 00017 nico@215: 00018 You should have received a copy of the GNU Lesser General Public nico@215: 00019 License along with this library; if not, write to the Free Software nico@215: 00020 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA nico@215: 00021 */ nico@215: 00022 nico@215: 00023 #include <stdio.h> nico@215: 00024 #include <string.h> nico@215: 00025 #include <errno.h> nico@215: 00026 #include <fcntl.h> nico@215: 00027 nico@215: 00028 /* driver pcan pci for Peak board */ nico@215: 00029 //#include "libpcan.h" nico@215: 00030 //#include "pcan.h" nico@215: 00031 nico@215: 00032 #include "libpcan.h" // for CAN_HANDLE nico@215: 00033 nico@215: 00034 #include "can_driver.h" nico@215: 00035 nico@215: 00036 // Define for rtr CAN message etisserant@240: 00037 #define CAN_INIT_TYPE_ST_RTR MSGTYPE_STANDARD | MSGTYPE_RTR nico@215: 00038 nico@215: 00039 /*********functions which permit to communicate with the board****************/ etisserant@240: 00040 UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m) nico@215: 00041 { etisserant@240: 00042 UNS8 data; nico@215: 00043 TPCANMsg peakMsg; nico@215: 00044 if ((errno = CAN_Read(fd0, & peakMsg))) { // Blocks until no new message or error. nico@215: 00045 if(errno != -EIDRM && errno != -EPERM) // error is not "Can Port closed while reading" nico@215: 00046 { nico@215: 00047 perror("canReceive_driver (Peak_Linux) : error of reading.\n"); nico@215: 00048 } nico@215: 00049 return 1; nico@215: 00050 } etisserant@240: 00051 m->cob_id.w = peakMsg.ID; nico@215: 00052 if (peakMsg.MSGTYPE == CAN_INIT_TYPE_ST) /* bits of MSGTYPE_*/ etisserant@240: 00053 m->rtr = 0; nico@215: 00054 else etisserant@240: 00055 m->rtr = 1; etisserant@240: 00056 m->len = peakMsg.LEN; /* count of data bytes (0..8) */ nico@215: 00057 for(data = 0 ; data < peakMsg.LEN ; data++) etisserant@240: 00058 m->data[data] = peakMsg.DATA[data]; /* data bytes, up to 8 */ nico@215: 00059 nico@215: 00060 return 0; nico@215: 00061 } nico@215: 00062 nico@215: 00063 /***************************************************************************/ etisserant@240: 00064 UNS8 canSend_driver(CAN_HANDLE fd0, Message *m) nico@215: 00065 { etisserant@240: 00066 UNS8 data; nico@215: 00067 TPCANMsg peakMsg; nico@215: 00068 peakMsg.ID=m -> cob_id.w; /* 11/29 bit code */ etisserant@240: 00069 if(m->rtr == 0) nico@215: 00070 peakMsg.MSGTYPE = CAN_INIT_TYPE_ST; /* bits of MSGTYPE_*/ nico@215: 00071 else { etisserant@240: 00072 peakMsg.MSGTYPE = CAN_INIT_TYPE_ST_RTR; /* bits of MSGTYPE_*/ nico@215: 00073 } etisserant@240: 00074 peakMsg.LEN = m->len; nico@215: 00075 /* count of data bytes (0..8) */ etisserant@240: 00076 for(data = 0 ; data < m->len; data ++) etisserant@240: 00077 peakMsg.DATA[data] = m->data[data]; /* data bytes, up to 8 */ nico@215: 00078 nico@215: 00079 if((errno = CAN_Write(fd0, & peakMsg))) { nico@215: 00080 perror("canSend_driver (Peak_Linux) : error of writing.\n"); nico@215: 00081 return 1; nico@215: 00082 } nico@215: 00083 return 0; nico@215: 00084 nico@215: 00085 } nico@215: 00086 nico@215: 00087 nico@215: 00088 /***************************************************************************/ etisserant@240: 00089 int TranslateBaudeRate(char* optarg){ nico@215: 00090 if(!strcmp( optarg, "1M")) return CAN_BAUD_1M; nico@215: 00091 if(!strcmp( optarg, "500K")) return CAN_BAUD_500K; nico@215: 00092 if(!strcmp( optarg, "250K")) return CAN_BAUD_250K; nico@215: 00093 if(!strcmp( optarg, "125K")) return CAN_BAUD_125K; nico@215: 00094 if(!strcmp( optarg, "100K")) return CAN_BAUD_100K; nico@215: 00095 if(!strcmp( optarg, "50K")) return CAN_BAUD_50K; nico@215: 00096 if(!strcmp( optarg, "20K")) return CAN_BAUD_20K; nico@215: 00097 if(!strcmp( optarg, "10K")) return CAN_BAUD_10K; nico@215: 00098 if(!strcmp( optarg, "5K")) return CAN_BAUD_5K; nico@215: 00099 if(!strcmp( optarg, "none")) return 0; nico@215: 00100 return 0x0000; nico@215: 00101 } nico@215: 00102 nico@215: 00103 /***************************************************************************/ etisserant@240: 00104 CAN_HANDLE canOpen_driver(s_BOARD *board) nico@215: 00105 { nico@215: 00106 HANDLE fd0 = NULL; nico@215: 00107 char busname[64]; nico@215: 00108 char* pEnd; nico@215: 00109 int i; nico@215: 00110 int baudrate; nico@215: 00111 etisserant@240: 00112 if(strtol(board->busname, &pEnd,0) >= 0) nico@215: 00113 { etisserant@240: 00114 sprintf(busname,"/dev/pcan%s",board->busname); nico@215: 00115 fd0 = LINUX_CAN_Open(busname, O_RDWR); nico@215: 00116 } nico@215: 00117 etisserant@240: 00118 if(fd0 && (baudrate = TranslateBaudeRate(board->baudrate))) nico@215: 00119 { nico@215: 00120 CAN_Init(fd0, baudrate, CAN_INIT_TYPE_ST); nico@215: 00121 }else{ nico@215: 00122 fprintf(stderr, "canOpen_driver (Peak_Linux) : error opening %s\n", busname); nico@215: 00123 } nico@215: 00124 etisserant@240: 00125 return (CAN_HANDLE)fd0; nico@215: 00126 } nico@215: 00127 nico@215: 00128 /***************************************************************************/ etisserant@240: 00129 int canClose_driver(CAN_HANDLE fd0) nico@215: 00130 { nico@215: 00131 CAN_Close(fd0); nico@215: 00132 return 0; nico@215: 00133 } etisserant@240: