# HG changeset patch # User greg # Date 1215522888 -7200 # Node ID aa36efce6e78739fca489ee2af23249afe21a9ef # Parent a204a86a71f1ee3e360b6985a1a4fa96165fb32c add anagate device support in CanFestival diff -r a204a86a71f1 -r aa36efce6e78 configure --- a/configure Tue Jul 01 15:21:03 2008 +0200 +++ b/configure Tue Jul 08 15:14:48 2008 +0200 @@ -145,6 +145,8 @@ echo " \"win32\" for win32 systems (native, mingw or VC++)" echo " \"hcs12\" for HCS12 micro-controller" echo " --can=foo Use 'foo' as CAN driver" + echo " \"anagate_linux\" use AnaGate CAN(duo) driver for linux" + echo " \"anagate_win32\" use AnaGate CAN(duo) driver for win32" echo " \"peak_linux\" use Linux build host installed Peak driver and library" echo " see http://www.peak-system.com/linux/" echo " \"peak_win32\" use win32 PcanLight Peak driver and library with Cygwin" @@ -286,6 +288,9 @@ elif [ -e /usr/lib/libcanlib.so ]; then echo "Choosing installed Kvaser driver as CAN driver. (unix)" SUB_CAN_DRIVER=kvaser + elif [ -e /usr/local/lib/libAnaGateAPIDLL.so ]; then + echo "Choosing installed AnaGate driver as CAN driver. (unix)" + SUB_CAN_DRIVER=anagate_linux elif [ "$SUB_OS_NAME" = "CYGWIN" -a "$KVASER_DLL_PATH" != "" ]; then echo "Choosing installed Kvaser driver as CAN driver. (cygwin)" SUB_CAN_DRIVER=kvaser @@ -316,6 +321,14 @@ fi fi +# Warn for unstalled peak driver if choosen +if [ "$SUB_CAN_DRIVER" = "anagate_linux" ]; then + if [ ! -e /usr/local/lib/libAnaGateAPIDLL.so ]; then + echo "AnaGateCAN driver hasn't been installed !" + exit -1 + fi +fi + # Warn for unstalled kvaser driver if choosen if [ "$SUB_CAN_DRIVER" = "kvaser" ]; then if [ "$SUB_OS_NAME" = "CYGWIN" ]; then @@ -377,10 +390,20 @@ SUB_CAN_DLL_CFLAGS=$SUB_CAN_DLL_CFLAGS\ -lpcan fi +if [ "$SUB_CAN_DRIVER" = "anagate_linux" ]; then + SUB_CAN_DLL_CFLAGS=$SUB_CAN_DLL_CFLAGS\ -lAnaGateAPIDLL + SUB_CAN_DLL_CFLAGS=$SUB_CAN_DLL_CFLAGS\ -lAnaCommon +fi + + if [ "$SUB_CAN_DRIVER" = "vscom" ]; then SUB_CAN_DLL_CFLAGS=$SUB_CAN_DLL_CFLAGS\ -lvs_can_api fi +if [ "$SUB_CAN_DRIVER" = "anagate_win32" ]; then + SUB_CAN_DLL_CFLAGS=$SUB_CAN_DLL_CFLAGS\ -lAnaGateCan +fi + PW32DIR=drivers/can_peak_win32 if [ "$SUB_CAN_DRIVER" = "peak_win32" ]; then if [ "$PCAN_HEADER" = "" -o "$PCAN_LIB" = "" ]; then diff -r a204a86a71f1 -r aa36efce6e78 drivers/can_anagate_linux/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/drivers/can_anagate_linux/Makefile Tue Jul 08 15:14:48 2008 +0200 @@ -0,0 +1,68 @@ +#! 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 = gcc +OPT_CFLAGS = -O2 +CFLAGS = $(OPT_CFLAGS) +PROG_CFLAGS = +PREFIX = /usr/local +TARGET = unix +CAN_DRIVER = can_anagate_linux +TIMERS_DRIVER = timers_unix +ENABLE_DLL_DRIVERS=1 +CAN_DLL_CFLAGS= -lAnaGateAPIDLL -lAnaCommon + +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_SOFILES) + +clean: + rm -f $(OBJS) + +mrproper: clean diff -r a204a86a71f1 -r aa36efce6e78 drivers/can_anagate_linux/Makefile.in --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/drivers/can_anagate_linux/Makefile.in Tue Jul 08 15:14:48 2008 +0200 @@ -0,0 +1,68 @@ +#! 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 + +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_SOFILES) + +clean: + rm -f $(OBJS) + +mrproper: clean diff -r a204a86a71f1 -r aa36efce6e78 drivers/can_anagate_linux/can_anagate_linux.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/drivers/can_anagate_linux/can_anagate_linux.c Tue Jul 08 15:14:48 2008 +0200 @@ -0,0 +1,320 @@ +/* +This file is part of CanFestival, a library implementing CanOpen Stack. + +Copyright (C): Edouard TISSERANT and Francis DUPIN + +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 +*/ + + +#include +#include +#include +#include + + +#include "can_driver.h" + +struct SAnaGatePort +{ + int hHandle; + Message sMsgBuffer; + struct SAnaGatePort *pNext; + struct SAnaGatePort *pPrev; + int bBufferFull; +}; + + +struct SAnaGatePort *pFistAnaGatePort = NULL; + + +/********* AnaGate API CAN receive callback Funciton ****************/ +void AnaGateReceiveCallBack (int nIdentifier, const char* pcBuffer, int nBufferLen, int nFlags, int hHandle) +{ + int i; + struct SAnaGatePort *pAnaGatePort = pFistAnaGatePort; + + while (pAnaGatePort->hHandle != hHandle ) + { + pAnaGatePort = pAnaGatePort->pNext; + if (pAnaGatePort == pFistAnaGatePort ) + { + pAnaGatePort = NULL; + printf("AnaGateReceiveCallBack (AnaGate_Linux): ERROR: Can't find AnaGatePort-Objekt to the Received Handle %d\n",hHandle); + return; + } + } + while (pAnaGatePort->bBufferFull) + { + usleep(5000); + } + pAnaGatePort->sMsgBuffer.cob_id = nIdentifier; + pAnaGatePort->sMsgBuffer.len= nBufferLen; + if (nFlags == 2) + pAnaGatePort->sMsgBuffer.rtr = 1; + else + pAnaGatePort->sMsgBuffer.rtr = 0; + + for (i = 0 ; i < nBufferLen; i++) + { + pAnaGatePort->sMsgBuffer.data[i] = pcBuffer[i]; + } + + pAnaGatePort->bBufferFull = 1; + +} + + +/*********functions which permit to communicate with the board****************/ +UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m) +{ + int i; + struct SAnaGatePort* pAnaGatePort = (struct SAnaGatePort*)fd0; + + while (pAnaGatePort->bBufferFull == 0) + { + usleep (5000); + } + + m->cob_id = pAnaGatePort->sMsgBuffer.cob_id; + m->len = pAnaGatePort->sMsgBuffer.len; + m->rtr = pAnaGatePort->sMsgBuffer.rtr; + for (i = 0 ; i < pAnaGatePort->sMsgBuffer.len; i++) + { + m->data[i] = pAnaGatePort->sMsgBuffer.data[i]; + } + + pAnaGatePort->bBufferFull = 0; + + return 0; +} + +/***************************************************************************/ +UNS8 canSend_driver(CAN_HANDLE fd0, Message *m) +{ + struct SAnaGatePort* pAnaCanPort = (struct SAnaGatePort*)fd0; + char cErrorMsg[100]; + int nRetCode; + int nMsgTyp; + + if (m->rtr == 0) + { + nMsgTyp = 0; //Normal; + } + else + { + nMsgTyp = 2; //Remote frame; + } + + if ( (nRetCode = CANWrite(pAnaCanPort->hHandle , m->cob_id,(const char*) m->data, m->len, nMsgTyp) ) ) + { + CANErrorMessage( nRetCode, cErrorMsg ,100 ); // Convert returncode to error messge + fprintf(stderr,"canSend_driver (AnaGate_Linux) %s \n",nRetCode); + //printf("canSend_driver (AnaGate_Linux) %s \n",nRetCode); + return 1; + } + + return 0; +} + + +/***************************************************************************/ +int TranslateBaudeRate(char* optarg){ + if(!strcmp( optarg, "1M")) return 1000000; + if(!strcmp( optarg, "800K")) return 800000; + if(!strcmp( optarg, "500K")) return 500000; + if(!strcmp( optarg, "250K")) return 250000; + if(!strcmp( optarg, "125K")) return 125000; + if(!strcmp( optarg, "100K")) return 100000; + if(!strcmp( optarg, "50K")) return 50000; + if(!strcmp( optarg, "20K")) return 20000; + if(!strcmp( optarg, "10K")) return 10000; + + return 0x0000; +} + +/***************************************************************************/ +UNS8 canChangeBaudRate_driver( CAN_HANDLE fd0, char* baud) +{ + int nRetCode; + char cErrorMsg[100]; + struct SAnaGatePort* pAnaGatePort = (struct SAnaGatePort*)fd0; + + if (nRetCode = CANSetGlobals (pAnaGatePort->hHandle, TranslateBaudeRate(baud), 0, 0, 1) ) + { + CANErrorMessage( nRetCode, cErrorMsg ,100 ); // Convert returncode to error messge + fprintf(stderr, "canChangeBaudRate_drive (AnaGate_Linux): %s\n", cErrorMsg); + //printf("canChangeBaudRate_drive (AnaGate_Linux): %s\n", cErrorMsg); + return 1; + } + return 0; +} + +/***************************************************************************/ +/* To open a connection to AnaGate CAN the s_BOARD board->busname must be + the AnaGate IP-Adresse followed from the CAN-Port (A or B) you want to use + For example "192.168.1.254:A" +*/ + +CAN_HANDLE canOpen_driver(s_BOARD *board) +{ + int nPortNr; + char cErrorMsg[100]; + int nRetCode; + char sIPAddress[16]; + struct SAnaGatePort *pNewAnaGatePort; + unsigned int nBusnameLen; + char bBusnameValid = 1; + + + /////////////////////////////////////////// + // Do some checkings concerning the busname + // format should be IP-Adress:Port + // e.g. 192.168.1.254:A + /////////////////////////////////////////// + nBusnameLen = strlen(board->busname); + + if ( nBusnameLen < strlen( "1.2.3.4:A" ) ) bBusnameValid = 0; // check minimum length of busname + if ( nBusnameLen > strlen( "123.234.345.456:A" ) ) bBusnameValid = 0; // check maximum length of busname + if ( bBusnameValid ) + { + switch (board->busname[nBusnameLen-1]) // check Portname of busname + { + case ('A'): nPortNr = 0; break; + case ('B'): nPortNr = 1; break; + case ('C'): nPortNr = 2; break; + case ('D'): nPortNr = 3; break; + default : bBusnameValid = 0; break; + } + if (board->busname[nBusnameLen-2] != ':' ) bBusnameValid = 0; // check Colon before Portname + } + + if ( ! bBusnameValid ) + { + fprintf(stderr, "canOpen_driver (AnaGate_Win32): busname (\"%s\") has a wrong format. Use IPAddr:CANPort for example \"192.168.1.254:A\"\n",board->busname); + return (CAN_HANDLE) NULL; + } + + board->busname[nBusnameLen-2] = 0; // NULL Terminator for IP Address string + strcpy (sIPAddress, board->busname); + + pNewAnaGatePort = (struct SAnaGatePort*) malloc(sizeof (struct SAnaGatePort)); + if (pFistAnaGatePort == NULL) + { + pFistAnaGatePort = pNewAnaGatePort; + pNewAnaGatePort->pNext = pNewAnaGatePort; + pNewAnaGatePort->pPrev = pNewAnaGatePort; + } + else + { pNewAnaGatePort->pNext = pFistAnaGatePort; + pNewAnaGatePort->pPrev = pFistAnaGatePort->pPrev; + pFistAnaGatePort->pPrev->pNext = pNewAnaGatePort; + pFistAnaGatePort->pPrev = pNewAnaGatePort; + + } + + // Connect to AnaGate + if ( nRetCode = CANOpenDevice (&pNewAnaGatePort->hHandle, + 0, /*confimation off*/ + 1, /*Monitor on*/ + nPortNr, + sIPAddress, + 1000 /*TimeOut*/ ) ) + { + CANErrorMessage( nRetCode, cErrorMsg ,100 ); // Convert returncode to error messge + fprintf(stderr, "canOpen_driver (AnaGate_Linux): %s @ %s Port:%d\n", cErrorMsg,sIPAddress,nPortNr); + //printf( "canOpen_driver (AnaGate_Linux): %s @ %s Port:%d\n", cErrorMsg,sIPAddress,nPortNr); + return (CAN_HANDLE) NULL; + } + + // Inizial Baudrate + + if (nRetCode = CANSetGlobals (pNewAnaGatePort->hHandle, + TranslateBaudeRate(board->baudrate), + 0,/*OperatingMode = normal*/ + 0,/*CAN-Termination = off*/ + 1 /*HighSpeedMode = on*/) ) + { + CANErrorMessage( nRetCode, cErrorMsg ,100 ); // Convert returncode to error messge + fprintf(stderr, "canOpen_driver (AnaGate_Linux): %s @ %s\n", cErrorMsg,sIPAddress); + //printf("canOpen_driver (AnaGate_Linux): %s @ %s\n", cErrorMsg,sIPAddress); + return (CAN_HANDLE) NULL; + } + + // Creat receive and receive-acknoledge event + /*pNewAnaGatePort->hAnaRecEvent = CreateEvent( + NULL, // default security attributes + FALSE, // manual-reset event + FALSE, // initial state is nonsignaled + NULL // object name + ); + + pNewAnaGatePort->hFesticalRecAcknowledge = CreateEvent( + NULL, // default security attributes + FALSE, // manual-reset event + FALSE, // initial state is nonsignaled + NULL // object name + ); + */ + + pNewAnaGatePort->bBufferFull = 0; + + // Install receive callback funktion + + if (nRetCode = CANSetCallback(pNewAnaGatePort->hHandle, AnaGateReceiveCallBack) ) + { + canClose_driver (pNewAnaGatePort); + CANErrorMessage( nRetCode, cErrorMsg ,100 ); // Convert returncode to error messge + fprintf(stderr, "canOpen_driver (AnaGate_Linux): %s @ %s\n", cErrorMsg,sIPAddress); + //printf("canOpen_driver (AnaGate_Linux): %s @ %s\n", cErrorMsg,sIPAddress); + return (CAN_HANDLE) NULL; + } + + + return (CAN_HANDLE)pNewAnaGatePort; +} + +/***************************************************************************/ +int canClose_driver(CAN_HANDLE fd0) +{ + struct SAnaGatePort* pAnaGatePort = (struct SAnaGatePort*)fd0; + char cErrorMsg[100]; + int nRetCode; + + pAnaGatePort->bBufferFull = 1; + + if ( nRetCode = CANCloseDevice(pAnaGatePort->hHandle) ) + { + CANErrorMessage( nRetCode, cErrorMsg ,100 ); // Convert returncode to error messge + fprintf(stderr, "canClose_driver (AnaGate_Linux): %s\n", cErrorMsg); + printf("canClose_driver (AnaGate_Linux): %s\n", cErrorMsg); + } + + if (pAnaGatePort->pNext == pAnaGatePort) + { + free (pAnaGatePort); + pFistAnaGatePort=NULL; + } + else + { + pAnaGatePort->pNext->pPrev = pAnaGatePort->pPrev; + pAnaGatePort->pPrev->pNext = pAnaGatePort->pNext; + free (pAnaGatePort); + } + + return 0; +} diff -r a204a86a71f1 -r aa36efce6e78 drivers/can_anagate_win32/Makefile.in --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/drivers/can_anagate_win32/Makefile.in Tue Jul 08 15:14:48 2008 +0200 @@ -0,0 +1,81 @@ +#! gmake + +# +# Copyright (C) 2006 Edouard TISSERANT +# +# 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 +CXX = SUB_CXX +LD = SUB_LD +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 + +INCLUDES = -I../../include -I../../include/$(TARGET) -I../../include/$(CAN_DRIVER) -IAnaGateCAN_API + +OBJS = $(CAN_DRIVER).o + +ifeq ($(ENABLE_DLL_DRIVERS),1) +#CFLAGS += -fPIC +DRIVER = cyg$(CAN_DRIVER).dll +else +DRIVER = $(OBJS) +endif + + +TARGET_SOFILES = $(PREFIX)/lib/$(DRIVER) + +all: driver + +driver: $(DRIVER) + + +%o: %c + $(CC) $(CFLAGS) $(PROG_CFLAGS) ${PROGDEFINES} $(INCLUDES) -o $@ -c $< + +%o: %cpp + $(CXX) $(CFLAGS) $(PROG_CFLAGS) ${PROGDEFINES} $(INCLUDES) -o $@ -c $< + +$(DRIVER): $(OBJS) + $(LD) $(PROG_CFLAGS) -shared -o $@ \ + -Wl,--export-all-symbols \ + -Wl,--enable-auto-import \ + -Wl,--whole-archive $^ \ + -Wl,--no-whole-archive $(CAN_DLL_CFLAGS) \ + AnaGateCAN_API/AnaGateCANDll.lib \ + -Wl,--exclude-libs,ALL -lws2_32 + +install: libcanfestival_$(CAN_DRIVER).so + mkdir -p $(PREFIX)/lib/ + cp $< $(PREFIX)/lib/ + +uninstall: + rm -f $(TARGET_SOFILES) + +clean: + rm -f $(OBJS) $(DRIVER) $(SERVER).exe + +mrproper: clean diff -r a204a86a71f1 -r aa36efce6e78 drivers/can_anagate_win32/can_anagate_win32.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/drivers/can_anagate_win32/can_anagate_win32.c Tue Jul 08 15:14:48 2008 +0200 @@ -0,0 +1,323 @@ +/* +This file is part of CanFestival, a library implementing CanOpen Stack. + +Copyright (C): Edouard TISSERANT and Francis DUPIN + +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 +*/ + +#if defined(WIN32) && !defined(__CYGWIN__) +#define usleep(micro) Sleep(micro%1000 ? (micro/1000) + 1 : (micro/1000)) +#define _CRT_SECURE_NO_DEPRECATE +#else +#include +#include +#include +#include +#endif + +#include "can_driver.h" +#include "AnaGateDllCan.h" + + +typedef struct SAnaGatePort +{ +int hHandle; +HANDLE hAnaRecEvent; +HANDLE hFesticalRecAcknowledge; +Message sMsgBuffer; +struct SAnaGatePort *pNext; +struct SAnaGatePort *pPrev; +} SAnaGatePort; + + +SAnaGatePort *pFirstAnaGatePort=NULL; + + +/********* AnaGate API CAN receive callback Funciton ****************/ +void WINAPI AnaGateReceiveCallBack (int nIdentifier, const char* pcBuffer, int nBufferLen, int nFlags, int hHandle) +{ + SAnaGatePort *pAnaGatePort = pFirstAnaGatePort; + int i; + + while (pAnaGatePort->hHandle != hHandle ) + { + pAnaGatePort = pAnaGatePort->pNext; + if (pAnaGatePort == pFirstAnaGatePort ) + { + pAnaGatePort = NULL; + printf("AnaGateReceiveCallBack (AnaGate_Win32): ERROR: Can't find AnaGatePort-Objekt to the Received Handle %d\n",hHandle); + return; + } + } + + pAnaGatePort->sMsgBuffer.cob_id = nIdentifier; + pAnaGatePort->sMsgBuffer.len= nBufferLen; + if (nFlags == 2) + pAnaGatePort->sMsgBuffer.rtr = 1; + else + pAnaGatePort->sMsgBuffer.rtr = 0; + + for (i = 0 ; i < nBufferLen; i++) + { + pAnaGatePort->sMsgBuffer.data[i] = pcBuffer[i]; + } + + SetEvent(pAnaGatePort->hAnaRecEvent); + + WaitForSingleObject(pAnaGatePort->hFesticalRecAcknowledge,INFINITE); +} + + +/*********functions which permit to communicate with the board****************/ +UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m) +{ + SAnaGatePort* pAnaGatePort = (SAnaGatePort*)fd0; + int i; + + WaitForSingleObject( // Wait for receive event + pAnaGatePort->hAnaRecEvent, // event handle + INFINITE); // indefinite wait + + + m->cob_id = pAnaGatePort->sMsgBuffer.cob_id; + m->len = pAnaGatePort->sMsgBuffer.len; + m->rtr = pAnaGatePort->sMsgBuffer.rtr; + for (i = 0 ; i < pAnaGatePort->sMsgBuffer.len; i++) + { + m->data[i] = pAnaGatePort->sMsgBuffer.data[i]; + } + + SetEvent(pAnaGatePort->hFesticalRecAcknowledge); //Set Acknollede event + + return 0; +} + +/***************************************************************************/ +UNS8 canSend_driver(CAN_HANDLE fd0, Message *m) +{ + SAnaGatePort* pAnaCanPort = (SAnaGatePort*)fd0; + char cErrorMsg[100]; + int nRetCode; + int nMsgTyp; + + if (m->rtr == 0) + { + nMsgTyp = 0; //Normal; + } + else + { + nMsgTyp = 2; //Remote frame; + } + + if ( (nRetCode = CANWrite(pAnaCanPort->hHandle , m->cob_id,(const char*) m->data, m->len, nMsgTyp) ) ) + { + CANErrorMessage( nRetCode, cErrorMsg ,100 ); // Convert returncode to error messge + fprintf(stderr,"canSend_driver (AnaGate_Win32) %s \n",nRetCode); + //printf("canSend_driver (AnaGate_Win32) %s \n",nRetCode); + return 1; + } + + return 0; +} + + +/***************************************************************************/ +int TranslateBaudeRate(char* optarg){ + if(!strcmp( optarg, "1M")) return 1000000; + if(!strcmp( optarg, "800K")) return 800000; + if(!strcmp( optarg, "500K")) return 500000; + if(!strcmp( optarg, "250K")) return 250000; + if(!strcmp( optarg, "125K")) return 125000; + if(!strcmp( optarg, "100K")) return 100000; + if(!strcmp( optarg, "50K")) return 50000; + if(!strcmp( optarg, "20K")) return 20000; + if(!strcmp( optarg, "10K")) return 10000; + + return 0x0000; +} +/****************************************************************************/ + +UNS8 canChangeBaudRate_driver( CAN_HANDLE fd0, char* baud) +{ + int nRetCode; + char cErrorMsg[100]; + struct SAnaGatePort* pAnaGatePort = (struct SAnaGatePort*)fd0; + + if (nRetCode = CANSetGlobals (pAnaGatePort->hHandle, TranslateBaudeRate(baud), 0, 0, 1) ) + { + CANErrorMessage( nRetCode, cErrorMsg ,100 ); // Convert returncode to error messge + fprintf(stderr, "canChangeBaudRate_drive (AnaGate_Win32): %s\n", cErrorMsg); + printf("canChangeBaudRate_drive (AnaGate_Win32): %s\n", cErrorMsg); + return 1; + } + return 0; +} + +/***************************************************************************/ +/* To open a connection to AnaGate CAN the s_BOARD board->busname must be + the AnaGate IP-Adresse followed from the CAN-Port (A or B) you want to use + For example "192.168.1.254:A" +*/ + +CAN_HANDLE canOpen_driver(s_BOARD *board) +{ + int PortNr; + char cErrorMsg[100]; + int nRetCode; + char sIPAddress[16]; + struct SAnaGatePort *pNewAnaGatePort; + char bBusnameValid = TRUE; + unsigned int nBusnameLen; + + /////////////////////////////////////////// + // Do some checkings concerning the busname + // format should be IP-Adress:Port + // e.g. 192.168.1.254:A + /////////////////////////////////////////// + nBusnameLen = strlen(board->busname); + + if ( nBusnameLen < strlen( "1.2.3.4:A" ) ) bBusnameValid = FALSE; // check minimum length of busname + if ( nBusnameLen > strlen( "123.234.345.456:A" ) ) bBusnameValid = FALSE; // check maximum length of busname + if ( bBusnameValid ) + { + switch (board->busname[nBusnameLen-1]) // check Portname of busname + { + case ('A'): PortNr = 0; break; + case ('B'): PortNr = 1; break; + case ('C'): PortNr = 2; break; + case ('D'): PortNr = 3; break; + default : bBusnameValid = FALSE; break; + } + if (board->busname[nBusnameLen-2] != ':' ) bBusnameValid = FALSE; // check Colon before Portname + } + + if ( ! bBusnameValid ) + { + fprintf(stderr, "canOpen_driver (AnaGate_Win32): busname (\"%s\") has a wrong format. Use IPAddr:CANPort for example \"192.168.1.254:A\"\n",board->busname); + return (CAN_HANDLE) NULL; + } + + board->busname[nBusnameLen-2] = 0; // NULL Terminator for IP Address string + strcpy (sIPAddress, board->busname); + + pNewAnaGatePort = (SAnaGatePort*) malloc(sizeof (struct SAnaGatePort)); + if (pFirstAnaGatePort == NULL) + { + pFirstAnaGatePort = pNewAnaGatePort; + pNewAnaGatePort->pNext = pNewAnaGatePort; + pNewAnaGatePort->pPrev = pNewAnaGatePort; + } + else + { pNewAnaGatePort->pNext = pFirstAnaGatePort; + pNewAnaGatePort->pPrev = pFirstAnaGatePort->pPrev; + pFirstAnaGatePort->pPrev->pNext = pNewAnaGatePort; + pFirstAnaGatePort->pPrev = pNewAnaGatePort; + + } + // Connect to AnaGate + if ( nRetCode = CANOpenDevice (&pNewAnaGatePort->hHandle, + 0, /*confirmation*/ + 1, /*Monitor*/ + PortNr, + sIPAddress, + 1000 /*TimeOut*/ ) ) + { + CANErrorMessage( nRetCode, cErrorMsg ,100 ); // Convert returncode to error messge + fprintf(stderr, "canOpen_driver (AnaGate_Win32): %s @ %s\n", cErrorMsg,sIPAddress); + //printf( "canOpen_driver (AnaGate_Win32): %s @ %s\n", cErrorMsg,sIPAddress); + return (CAN_HANDLE) NULL; + } + + // Inizial Baudrate + if (nRetCode = CANSetGlobals (pNewAnaGatePort->hHandle, + TranslateBaudeRate(board->baudrate), + 0,/*OperatingMode = normal*/ + 0,/*CAN-Termination = off*/ + 1 /*HighSpeedMode = on*/) ) + { + CANErrorMessage( nRetCode, cErrorMsg ,100 ); // Convert returncode to error messge + fprintf(stderr, "canOpen_driver (AnaGate_Win32): %s @ %s\n", cErrorMsg,sIPAddress); + //printf("canOpen_driver (AnaGate_Win32): %s @ %s\n", cErrorMsg,sIPAddress); + return (CAN_HANDLE) NULL; + } + + // Creat receive and receive-acknoledge event + pNewAnaGatePort->hAnaRecEvent = CreateEvent( + NULL, // default security attributes + FALSE, // manual-reset event + FALSE, // initial state is nonsignaled + NULL // object name + ); + + pNewAnaGatePort->hFesticalRecAcknowledge = CreateEvent( + NULL, // default security attributes + FALSE, // manual-reset event + FALSE, // initial state is nonsignaled + NULL // object name + ); + + // Install receive callback funktion + if (nRetCode = CANSetCallback(pNewAnaGatePort->hHandle, AnaGateReceiveCallBack) ) + { + canClose_driver (pNewAnaGatePort); + CANErrorMessage( nRetCode, cErrorMsg ,100 ); // Convert returncode to error messge + fprintf(stderr, "canOpen_driver (AnaGate_Win32): %s @ %s\n", cErrorMsg,sIPAddress); + //printf("canOpen_driver (AnaGate_Win32): %s @ %s\n", cErrorMsg,sIPAddress); + return (CAN_HANDLE) NULL; + } + + return (CAN_HANDLE)pNewAnaGatePort; +} + +/***************************************************************************/ +int canClose_driver(CAN_HANDLE fd0) +{ + SAnaGatePort* pAnaCanPort = (SAnaGatePort*)fd0; + char cErrorMsg[100]; + int nRetCode; + + + SetEvent (pAnaCanPort->hAnaRecEvent); + CloseHandle (pAnaCanPort->hAnaRecEvent); + CloseHandle (pAnaCanPort->hFesticalRecAcknowledge); + + if ( nRetCode = CANCloseDevice(pAnaCanPort->hHandle) ) + { + CANErrorMessage( nRetCode, cErrorMsg ,100 ); // Convert returncode to error messge + fprintf(stderr, "canClose_driver (AnaGate_Linux): %s\n", cErrorMsg); + //printf("canClose_driver (AnaGate_Linux): %s\n", cErrorMsg); + } + + if (pAnaCanPort->pNext == pAnaCanPort) + { + free (pAnaCanPort); + pFirstAnaGatePort=NULL; + } + else + { pAnaCanPort->pNext->pPrev = pAnaCanPort->pPrev; + pAnaCanPort->pPrev->pNext = pAnaCanPort->pNext; + free (pAnaCanPort); + } + + return 0; +} + + + + + diff -r a204a86a71f1 -r aa36efce6e78 drivers/can_anagate_win32/can_anagate_win32.def --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/drivers/can_anagate_win32/can_anagate_win32.def Tue Jul 08 15:14:48 2008 +0200 @@ -0,0 +1,8 @@ +LIBRARY can_anagate_win32 + +EXPORTS + canReceive_driver + canSend_driver + canOpen_driver + canClose_driver + canChangeBaudRate_driver \ No newline at end of file diff -r a204a86a71f1 -r aa36efce6e78 drivers/can_anagate_win32/can_anagate_win32.vcproj --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/drivers/can_anagate_win32/can_anagate_win32.vcproj Tue Jul 08 15:14:48 2008 +0200 @@ -0,0 +1,207 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +