Added can4linux driver support.
authorgroke6
Tue, 22 Jan 2008 10:27:13 +0100
changeset 364 17edd8a028fa
parent 363 40b351ab5a93
child 365 9b76e0881beb
Added can4linux driver support.
configure
drivers/can_can4linux/.cvsignore
drivers/can_can4linux/Makefile.in
drivers/can_can4linux/can4linux.h
drivers/can_can4linux/can_can4linux.c
--- a/configure	Fri Jan 18 09:43:55 2008 +0100
+++ b/configure	Tue Jan 22 10:27:13 2008 +0100
@@ -150,6 +150,8 @@
 		echo	"                 see http://developer.berlios.de/projects/socketcan/"
 		echo	"               \"lincan\" lincan driver"
 		echo	"                 see http://www.ocera.org/download/components/WP7/lincan-0.3.3.html"
+		echo	"               \"can4linux\" can4linux driver"
+		echo	"                 see http://www.port.de/engl/canprod/hw_can4linux.html"
 		echo 	" --timers=foo  Use 'foo' as TIMERS driver (can be either 'unix' or 'xeno')"
 		echo 	" --disable-dll Disable run-time dynamic linking of can, led and nvram drivers"
 		echo 	" --enable-lss  Enable the LSS services"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/drivers/can_can4linux/.cvsignore	Tue Jan 22 10:27:13 2008 +0100
@@ -0,0 +1,2 @@
+libcanfestival_can_can4linux.so
+Makefile
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/drivers/can_can4linux/Makefile.in	Tue Jan 22 10:27:13 2008 +0100
@@ -0,0 +1,67 @@
+#! gmake
+
+#
+# Copyright (C) 2006 Laurent Bessard
+# 
+# This file is part of canfestival, a library implementing the canopen
+# stack
+# 
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+# 
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+# 
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+# 
+
+CC = SUB_CC
+OPT_CFLAGS = -O2
+CFLAGS = SUB_OPT_CFLAGS
+PROG_CFLAGS = SUB_PROG_CFLAGS
+PREFIX = SUB_PREFIX
+TARGET = SUB_TARGET
+CAN_DRIVER = SUB_CAN_DRIVER
+TIMERS_DRIVER = SUB_TIMERS_DRIVER
+ENABLE_DLL_DRIVERS=SUB_ENABLE_DLL_DRIVERS
+
+INCLUDES = -I../../include -I../../include/$(TARGET) -I../../include/$(CAN_DRIVER)
+
+OBJS = $(CAN_DRIVER).o
+
+ifeq ($(ENABLE_DLL_DRIVERS),1)
+CFLAGS += -fPIC
+DRIVER = libcanfestival_$(CAN_DRIVER).so
+else
+DRIVER = $(OBJS)
+endif
+
+TARGET_SOFILES = $(PREFIX)/lib/$(DRIVER)
+
+all: driver
+
+driver: $(DRIVER)
+
+%o: %c
+	$(CC) $(CFLAGS) $(PROG_CFLAGS) ${PROGDEFINES} $(INCLUDES) -o $@ -c $<
+
+libcanfestival_$(CAN_DRIVER).so: $(OBJS)
+	$(CC) -shared -Wl,-soname,libcanfestival_$(CAN_DRIVER).so $(CAN_DLL_CFLAGS) -o $@ $<
+
+install: libcanfestival_$(CAN_DRIVER).so
+	mkdir -p $(PREFIX)/lib/
+	cp $< $(PREFIX)/lib/
+	
+uninstall:
+	rm -f $(TARGET_HFILES)
+
+clean:
+	rm -f $(OBJS)
+
+mrproper: clean
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/drivers/can_can4linux/can4linux.h	Tue Jan 22 10:27:13 2008 +0100
@@ -0,0 +1,224 @@
+/*
+ * can4linux.h - can4linux CAN driver module
+ *
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License.  See the file "COPYING" in the main directory of this archive
+ * for more details.
+ *
+ * Copyright (c) 2001 port GmbH Halle/Saale
+ *------------------------------------------------------------------
+ * $Header$
+ *
+ *--------------------------------------------------------------------------
+ *
+ *
+ * modification history
+ * --------------------
+ * $Log$
+ * Revision 1.1  2008-01-22 09:27:13  groke6
+ * Added can4linux driver support.
+ *
+ * Revision 1.1  2004/08/12 10:14:31  jschoew
+ * add can4linux examples
+ *
+ * Revision 1.5  2004/05/14 10:02:54  oe
+ * - started supporting CPC-Card
+ * - version number in can4linux.h available
+ * - only one structure type for Config_par_t Command_par_t
+ * - new ioctl command CMD_CLEARBUFFERS
+ *
+ * Revision 1.4  2003/08/27 17:49:04  oe
+ * - New CanStatusPar structure
+ *
+ * Revision 1.3  2002/08/20 05:57:22  oe
+ * - new write() handling, now not ovrwriting buffer content if buffer fill
+ * - ioctl() get status returns buffer information
+ *
+ * Revision 1.2  2002/08/08 17:50:46  oe
+ * - MSG_ERR_MASK extended
+ *
+ * Revision 1.1  2002/01/10 19:13:19  oe
+ * - application header file changed name can.h -> can4linux.h
+ *
+ * Revision 1.2  2001/09/14 14:58:09  oe
+ * first free release
+ *
+ * Revision 1.1.1.1  2001/06/11 18:30:54  oe
+ * minimal version can4linux embedded, compile time Konfigurierbar
+ *
+ *
+ *
+ *
+ *--------------------------------------------------------------------------
+ */
+
+/**
+* \file can.h
+* \author Heinz-Jrgen Oertel, port GmbH
+* $Revision$
+* $Date$
+*
+* can4linux interface definitions
+*
+*
+*
+*/
+
+
+#ifndef __CAN_H
+#define __CAN_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+#define CAN4LINUXVERSION 0x0301 /*(Version 3.1)*/
+
+#ifndef __KERNEL__
+#include <sys/time.h>
+#endif
+ /*---------- the can message structure */
+
+#define CAN_MSG_LENGTH 8		/**< maximum length of a CAN frame */
+
+
+#define MSG_RTR		(1<<0)		/**< RTR Message */
+#define MSG_OVR		(1<<1)		/**< CAN controller Msg overflow error */
+#define MSG_EXT		(1<<2)		/**< extended message format */
+#define MSG_PASSIVE	(1<<4)		/**< controller in error passive */
+#define MSG_BUSOFF      (1<<5)		/**< controller Bus Off  */
+#define MSG_       	(1<<6)		/**<  */
+#define MSG_BOVR	(1<<7)		/**< receive/transmit buffer overflow */
+/**
+* mask used for detecting CAN errors in the canmsg_t flags field
+*/
+#define MSG_ERR_MASK	(MSG_OVR + MSG_PASSIVE + MSG_BUSOFF + MSG_BOVR)
+
+/**
+* The CAN message structure.
+* Used for all data transfers between the application and the driver
+* using read() or write().
+*/
+typedef struct {
+    /** flags, indicating or controlling special message properties */
+    int             flags;
+    int             cob;	 /**< CAN object number, used in Full CAN  */
+    unsigned   long id;		 /**< CAN message ID, 4 bytes  */
+    struct timeval  timestamp;	 /**< time stamp for received messages */
+    short      int  length;	 /**< number of bytes in the CAN message */
+    unsigned   char data[CAN_MSG_LENGTH]; /**< data, 0...8 bytes */
+} canmsg_t;
+
+
+
+/**
+---------- IOCTL requests */
+
+#define COMMAND 	 0	/**< IOCTL command request */
+#define CONFIG 		 1	/**< IOCTL configuration request */
+#define SEND 		 2	/**< IOCTL request */
+#define RECEIVE 	 3	/**< IOCTL request */
+#define CONFIGURERTR 	 4	/**< IOCTL request */
+#define STATUS           5      /**< IOCTL status request */
+
+/*---------- CAN ioctl parameter types */
+/**
+ IOCTL Command request parameter structure */
+struct Command_par {
+    int cmd;			/**< special driver command */
+    int target;			/**< special configuration target */
+    unsigned long val1;		/**< 1. parameter for the target */
+    unsigned long val2;		/**< 2. parameter for the target */
+    int error;	 		/**< return value */
+    unsigned long retval;	/**< return value */
+};
+
+
+typedef struct Command_par Command_par_t ;
+/**
+ PSW made them all the same
+ IOCTL Configuration request parameter structure */
+typedef struct Command_par  Config_par_t ;
+
+
+/**
+ IOCTL generic CAN controller status request parameter structure */
+typedef struct CanStatusPar { 
+    unsigned int baud;			/**< actual bit rate */
+    unsigned int status;		/**< CAN controller status register */
+    unsigned int error_warning_limit;	/**< the error warning limit */
+    unsigned int rx_errors;		/**< content of RX error counter */
+    unsigned int tx_errors;		/**< content of TX error counter */
+    unsigned int error_code;		/**< content of error code register */
+    unsigned int rx_buffer_size;	/**< size of rx buffer  */
+    unsigned int rx_buffer_used;	/**< number of messages */
+    unsigned int tx_buffer_size;	/**< size of tx buffer  */
+    unsigned int tx_buffer_used;	/**< number of messages */
+    unsigned long retval;		/**< return value */
+    unsigned int type;			/**< CAN controller / driver type */
+} CanStatusPar_t;
+
+/**
+ IOCTL  CanStatusPar.type CAN controller hardware chips */
+#define CAN_TYPE_UNSPEC		0
+#define CAN_TYPE_SJA1000	1
+#define CAN_TYPE_FlexCAN	2
+#define CAN_TYPE_TouCAN		3
+#define CAN_TYPE_82527		4
+#define CAN_TYPE_TwinCAN	5
+
+
+/**
+ IOCTL Send request parameter structure */
+typedef struct Send_par {
+    canmsg_t *Tx;		/**< CAN message struct  */
+    int error;	 		/**< return value for errno */
+    unsigned long retval;	/**< return value */
+} Send_par_t ;
+
+/**
+ IOCTL Receive request parameter structure */
+typedef struct Receive_par {
+    canmsg_t *Rx;		/**< CAN message struct  */
+    int error;	 		/**< return value for errno */
+    unsigned long retval;	/**< return value */
+} Receive_par_t ;
+
+/**
+IOCTL ConfigureRTR request parameter structure */
+typedef struct ConfigureRTR_par {
+    unsigned message;		/**< CAN message ID */
+    canmsg_t *Tx;		/**< CAN message struct  */
+    int error;	 		/**< return value for errno */
+    unsigned long retval;	/**< return value */
+} ConfigureRTR_par_t ;
+
+/**
+---------- IOCTL Command subcommands and there targets */
+
+# define CMD_START		1
+# define CMD_STOP 		2
+# define CMD_RESET		3
+# define CMD_CLEARBUFFERS	4
+
+
+
+
+/**
+---------- IOCTL Configure targets */
+
+# define CONF_ACC   	0	/* mask and code */
+# define CONF_ACCM   	1	/* mask only */
+# define CONF_ACCC   	2	/* code only */
+# define CONF_TIMING	3	/* bit timing */
+# define CONF_OMODE 	4	/* output control register */
+# define CONF_FILTER	5
+# define CONF_FENABLE	6
+# define CONF_FDISABLE	7
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif 	/* __CAN_H */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/drivers/can_can4linux/can_can4linux.c	Tue Jan 22 10:27:13 2008 +0100
@@ -0,0 +1,182 @@
+/*
+This file is part of CanFestival, a library implementing CanOpen Stack.
+
+Copyright (C): Jorge BERZOSA
+
+See COPYING file for copyrights details.
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+*/
+
+/*
+* can4linux driver
+*/
+
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <fcntl.h>
+
+#include "can4linux.h" 
+#include "can_driver.h"
+
+struct timeval init_time,current_time;
+
+/*********functions which permit to communicate with the board****************/
+UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m)
+{
+  int res,i;
+  canmsg_t canmsg;
+  long int time_period;
+
+	canmsg.flags = 0; 
+	do{
+   		res = read(fd0,&canmsg,1);
+   		if((res<0)&&(errno == -EAGAIN)) res = 0;
+	}while(res==0);
+
+	if(res !=1) // No new message
+   	return 1;
+
+	if(canmsg.flags&MSG_EXT){
+   		/* There is no mark for extended messages in CanFestival */;
+	}
+  		
+	m->cob_id.w = canmsg.id;
+	m->len = canmsg.length;
+	if(canmsg.flags&MSG_RTR){
+ 		m->rtr = 1;
+	}else{
+ 		m->rtr = 0;
+ 		memcpy(m->data,canmsg.data,8);
+	}
+	
+	
+ 	/*gettimeofday(&current_time,NULL);
+ 	time_period=(current_time.tv_sec - init_time.tv_sec)* 1000000 + current_time.tv_usec - init_time.tv_usec;
+ 	printf("%3ld.%3ld.%3ld - Receive ID: %lx ->",time_period/1000000,(time_period%1000000)/1000,time_period%1000,m->cob_id.w);
+ 	printf("Receive ID: %lx ->",m->cob_id.w);
+ 	for(i=0; i<canmsg.length;i++)printf("%x, ", m->data[i]);
+ 	printf("\n");*/
+ 
+  return 0;
+}
+
+/***************************************************************************/
+UNS8 canSend_driver(CAN_HANDLE fd0, Message *m)
+{
+  int res;
+  canmsg_t canmsg;
+
+  canmsg.flags = 0;
+  canmsg.id = m->cob_id.w;
+  canmsg.length = m->len;
+  if(m->rtr){
+    canmsg.flags |= MSG_RTR;
+  }else{
+    memcpy(canmsg.data,m->data,8);
+  }
+  
+ /*printf("Send ID: %lx ->",canmsg.id); 
+ for(i=0; i<canmsg.length;i++)printf("%x, ", canmsg.data[i]);
+ printf("\n");*/
+
+  if(canmsg.id >= 0x800){
+    canmsg.flags |= MSG_EXT;
+  }
+
+  res = write(fd0,&canmsg,1);
+  if(res!=1)
+    return 1;
+
+  return 0;
+}
+
+/***************************************************************************/
+int	set_bitrate( CAN_HANDLE fd, int baud)
+{
+    Config_par_t  cfg;	
+    volatile Command_par_t cmd;
+
+    cmd.cmd = CMD_STOP;
+    ioctl(fd, COMMAND, &cmd);
+
+    cfg.target = CONF_TIMING; 
+    cfg.val1   = baud;
+    ioctl(fd, CONFIG, &cfg);
+
+    cmd.cmd = CMD_START;
+    ioctl(fd, COMMAND, &cmd);
+    return 0;
+}
+
+/***************************************************************************/
+int TranslateBaudeRate(char* optarg){
+	if(!strcmp( optarg, "1M")) return (int)1000;
+	if(!strcmp( optarg, "500K")) return (int)500;
+	if(!strcmp( optarg, "250K")) return (int)250;
+	if(!strcmp( optarg, "125K")) return (int)125;
+	if(!strcmp( optarg, "100K")) return (int)100;
+	if(!strcmp( optarg, "50K")) return (int)50;
+	if(!strcmp( optarg, "20K")) return (int)20;
+	if(!strcmp( optarg, "10K")) return (int)10;
+	if(!strcmp( optarg, "5K")) return (int)5;
+	if(!strcmp( optarg, "none")) return 0;
+	return 0x0000;
+}
+
+/***************************************************************************/
+static const char lnx_can_dev_prefix[] = "/dev/can";
+
+CAN_HANDLE canOpen_driver(s_BOARD *board)
+{
+  int name_len = strlen(board->busname);
+  int prefix_len = strlen(lnx_can_dev_prefix);
+  char dev_name[prefix_len+name_len+1];
+  int o_flags = 0;
+  int baud = TranslateBaudeRate(board->baudrate);
+  int fd0;
+
+  
+  /*o_flags = O_NONBLOCK;*/
+
+  memcpy(dev_name,lnx_can_dev_prefix,prefix_len);
+  memcpy(dev_name+prefix_len,board->busname,name_len);
+  dev_name[prefix_len+name_len] = 0;
+
+  fd0 = open(dev_name, O_RDWR|o_flags);
+  if(fd0 <= 0){
+    fprintf(stderr,"!!! %s is unknown. See can4linux.c\n", dev_name);
+    goto error_ret;
+  }
+  
+  set_bitrate((CAN_HANDLE)fd0, baud);
+
+  printf("CAN device dev/can%s opened. Baudrate=>%s\n",board->busname, board->baudrate);
+
+  return (CAN_HANDLE)fd0;
+
+ error_ret:
+  return NULL;
+}
+
+/***************************************************************************/
+int canClose_driver(CAN_HANDLE fd0)
+{
+  if((int)fd0<=0)
+    return -1;
+  close(fd0);
+  return 0;
+}