Add support for USB-CAN devices from VScom
authorgreg
Mon, 05 May 2008 11:25:14 +0200
changeset 455 26f762d4116d
parent 454 bc000083297a
child 456 67e29972e242
Add support for USB-CAN devices from VScom
configure
drivers/can_vscom/Makefile.in
drivers/can_vscom/can_vscom.c
--- a/configure	Fri May 02 17:30:37 2008 +0200
+++ b/configure	Mon May 05 11:25:14 2008 +0200
@@ -380,6 +380,10 @@
     SUB_CAN_DLL_CFLAGS=$SUB_CAN_DLL_CFLAGS\ -lpcan
 fi
 
+if [ "$SUB_CAN_DRIVER" = "vscom" ]; then
+    SUB_CAN_DLL_CFLAGS=$SUB_CAN_DLL_CFLAGS\ -lvs_can_api
+fi
+
 PW32DIR=drivers/can_peak_win32
 if [ "$SUB_CAN_DRIVER" = "peak_win32" ]; then
 	if [ "$PCAN_HEADER" = "" -o "$PCAN_LIB" = "" ]; then
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/drivers/can_vscom/Makefile.in	Mon May 05 11:25:14 2008 +0200
@@ -0,0 +1,91 @@
+#! 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
+CAN_DLL_CFLAGS=SUB_CAN_DLL_CFLAGS -L.
+
+# define target specific environment
+ifeq ($(TARGET),win32)
+DLL_TYPE=dll
+CAN_MINGW_DLL_FLAGS=
+CAN_LINUX_DLL_FLAGS=
+else
+DLL_TYPE=so
+CAN_MINGW_DLL_FLAGS=
+CAN_LINUX_DLL_FLAGS=-Wl,-soname,libcanfestival_$(CAN_DRIVER).so
+endif
+
+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).$(DLL_TYPE)
+else
+DRIVER = $(OBJS)
+endif
+
+TARGET_SOFILES = $(PREFIX)/lib/$(DRIVER)
+
+all: driver
+
+vs_can_api.h:
+	wget http://www.vscom.de/download/multiio/linux/driver/VSCAN_API_1_0.zip
+	@if which unzip &> /dev/null; then \
+	unzip VSCAN_API_1_0.zip; \
+	else \
+	echo "No unzip found. Please decompress VSCAN_API_1_0.zip manually"; \
+	exit 1; \
+	fi;
+
+driver: vs_can_api.h $(DRIVER)
+
+%o: %c
+	$(CC) $(CFLAGS) $(PROG_CFLAGS) ${PROGDEFINES} $(INCLUDES) -o $@ -c $<
+
+libcanfestival_$(CAN_DRIVER).$(DLL_TYPE): $(OBJS)
+	$(CC) -shared $(CAN_LINUX_DLL_FLAGS) $(CAN_MINGW_DLL_FLAGS) -o $@ $< $(CAN_DLL_CFLAGS)
+
+install: libcanfestival_$(CAN_DRIVER).so
+	mkdir -p $(PREFIX)/lib/
+	cp $< $(PREFIX)/lib/
+		
+uninstall:
+	rm -f $(TARGET_SOFILES)
+
+clean:
+	rm -f $(OBJS)
+	rm -f *.zip
+	rm -f libvs_can_api*
+	rm -f vs_can_api*
+
+mrproper: clean
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/drivers/can_vscom/can_vscom.c	Mon May 05 11:25:14 2008 +0200
@@ -0,0 +1,187 @@
+/*
+This file is part of CanFestival, a library implementing CanOpen Stack. 
+
+Copyright (C): VScom
+
+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
+*/
+
+/**
+ * @file can_vscom.c
+ * @author VScom (http://www.vscom.de)
+ * @date 17/04/08
+ *
+ * This file implements interface between CanFestival and the VSCAN API 
+ * (supported VScom products SER-CAN, USB-CAN, NET-CAN). 
+ *
+ * To build this interface following files will needed:
+ * vs_can_api.h
+ * va_can_api.lib (for Windows builds)
+*/
+
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <fcntl.h>
+
+// VSCAN API header
+#include "vs_can_api.h" // for CAN_HANDLE
+
+#include "can_driver.h"
+
+/*********functions which permit to communicate with the board****************/
+UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m)
+{
+  VSCAN_MSG Msg[1];
+  UNS8 i; 
+  DWORD dwRead; /* number of read frames */
+
+
+  if (VSCAN_Read((VSCAN_HANDLE)fd0, Msg, 1, &dwRead) != VSCAN_ERR_OK)
+  {
+    printf("canReceive_driver (VScom): error receiving frame)\n");
+    return 1;
+  }
+
+  /* identifier of the CAN frame */
+  m->cob_id = Msg[0].Id;   
+
+  /* CAN frame type */
+  if (Msg[0].Flags == VSCAN_FLAGS_STANDARD)
+    m->rtr = 0;
+  else 
+    m->rtr = 1;
+
+  /* width of the data bytes */
+  m->len = Msg[0].Size;	
+  
+  /* copy data bytes from the CAN frame, up to 8 */
+  for(i = 0  ; i < Msg[0].Size ; i++)             			
+  {
+    m->data[i] = Msg[0].Data[i];           
+  }
+
+  return 0;
+}
+
+/***************************************************************************/
+UNS8 canSend_driver(CAN_HANDLE fd0, Message *m)
+{
+  VSCAN_MSG Msg[1];
+  UNS8 i;
+  DWORD dwWritten; /* number of written frames */
+
+  /* identifier of the CAN frame */
+  Msg[0].Id = m->cob_id;
+
+  /* CAN frame type */
+  if(m->rtr == 0)	
+    Msg[0].Flags = VSCAN_FLAGS_STANDARD;
+  else
+    Msg[0].Flags = VSCAN_FLAGS_REMOTE;
+
+  /* width of the data bytes */
+  Msg[0].Size = m->len;
+
+  /* copy data bytes to the CAN frame, up to 8 */
+  for(i = 0 ; i <  m->len; i++)
+  	Msg[0].Data[i] = m->data[i];         	
+
+  /* copy CAN frame to the output buffer */
+  if (!(VSCAN_Write((VSCAN_HANDLE)fd0, Msg, (DWORD)1, &dwWritten) == VSCAN_ERR_OK && dwWritten))
+  {
+    perror("canSend_driver (VScom): error writing to output buffer.\n");
+    return 1;
+  }
+  
+  /* really send CAN frame */
+  if(VSCAN_Flush((VSCAN_HANDLE)fd0) != VSCAN_ERR_OK)
+  {
+    perror("canSend_driver (VScom): error flushing.\n");
+    return 1;
+  }
+
+  return 0;
+}
+
+
+/***************************************************************************/
+int TranslateBaudeRate(char* optarg){
+	if(!strcmp( optarg, "1M")) return (int)VSCAN_SPEED_1M;
+	if(!strcmp( optarg, "500K")) return (int)VSCAN_SPEED_500K;
+	if(!strcmp( optarg, "250K")) return (int)VSCAN_SPEED_250K;
+	if(!strcmp( optarg, "125K")) return (int)VSCAN_SPEED_125K;
+	if(!strcmp( optarg, "100K")) return (int)VSCAN_SPEED_100K;
+	if(!strcmp( optarg, "50K")) return (int)VSCAN_SPEED_50K;
+	if(!strcmp( optarg, "20K")) return (int)VSCAN_SPEED_20K;
+	if(!strcmp( optarg, "none")) return 0;
+	return 0x0000;
+}
+
+UNS8 canChangeBaudRate_driver( CAN_HANDLE fd, char* baud)
+{
+  int baudrate;
+
+  baudrate = TranslateBaudeRate(baud);
+  if(baudrate == 0)
+    return 0;
+
+  if (VSCAN_Ioctl((VSCAN_HANDLE)fd, VSCAN_IOCTL_SET_SPEED, (void *)baudrate) != VSCAN_ERR_OK)
+  {
+    fprintf(stderr, "canOpen_driver (VScom): IOCTL set speed failed\n");
+    return 0;
+  }
+
+  return 1;
+}
+
+/***************************************************************************/
+CAN_HANDLE canOpen_driver(s_BOARD *board)
+{
+  VSCAN_HANDLE fd0 = 0;
+  char busname[64];
+  char* pEnd;
+  int i;  
+  int baudrate;
+  
+  printf("bus %s ", board->busname);
+  fd0 = VSCAN_Open(board->busname, VSCAN_MODE_NORMAL);
+  if(fd0 <= 0)
+  {
+    fprintf(stderr, "canOpen_driver (VScom): error opening %s\n", board->busname);
+    return (CAN_HANDLE)fd0;
+  }
+  printf("(fd = %d)\n", fd0);
+  baudrate = TranslateBaudeRate(board->baudrate);
+  if(baudrate == 0)
+    return 0;
+
+  if (VSCAN_Ioctl((VSCAN_HANDLE)fd0, VSCAN_IOCTL_SET_SPEED, (void *)baudrate) != VSCAN_ERR_OK)
+  {
+    fprintf(stderr, "canOpen_driver (VScom): IOCTL set speed failed\n");
+    return 0;
+  }
+
+   return (CAN_HANDLE)fd0;
+}
+
+/***************************************************************************/
+int canClose_driver(CAN_HANDLE fd0)
+{
+  VSCAN_Close((VSCAN_HANDLE)fd0);
+  return 0;
+}