can_peak_linux.c

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

Generated on Mon Jul 2 19:10:16 2007 for CanFestival by  doxygen 1.5.1