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