drivers/can_ixxat_win32/can_ixxat_win.c
author Edouard Tisserant
Sat, 12 Oct 2013 09:09:04 +0900
changeset 788 6121ebff4da6
parent 787 e13fa74d169c
permissions -rw-r--r--
Merge changes from bitbucket.org/HMSFAE/canfestival_ixxat
787
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
     1
/*
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack.
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
     3
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
     4
Copyright (C): Edouard TISSERANT and Francis DUPIN
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
     5
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
     6
See COPYING file for copyrights details.
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
     7
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
     8
This library is free software; you can redistribute it and/or
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
     9
modify it under the terms of the GNU Lesser General Public
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    10
License as published by the Free Software Foundation; either
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    11
version 2.1 of the License, or (at your option) any later version.
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    12
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    13
This library is distributed in the hope that it will be useful,
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    14
but WITHOUT ANY WARRANTY; without even the implied warranty of
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    16
Lesser General Public License for more details.
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    17
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    18
You should have received a copy of the GNU Lesser General Public
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    19
License along with this library; if not, write to the Free Software
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    20
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    21
*/
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    22
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    23
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    24
/*
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    25
	Wrapper Layer for CANFestival to interact with IXXAT VCI V3.X.X Drivers (vcisdk.lib)
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    26
	Provides external references for win32 library see win32.c (CanFestival-3)
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    27
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    28
	http://www.ixxat.com/download_vci_v3_en.html
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    29
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    30
	Currently the VCI CAN driver for Windows supports the following IXXAT interfaces:
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    31
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    32
	PC-I 04/PCI
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    33
	iPC-I 320/PCI II
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    34
	iPC-I 165/PCI
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    35
	iPC-I XC16/PCI
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    36
	iPC-I XC16/PMC
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    37
	iPC-I XC16/PCIe
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    38
	USB-to-CAN compact
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    39
	USB-to-CAN II
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    40
	CAN-IB100/PCIe
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    41
	CAN-IB200/PCIe
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    42
	CAN-IB120/PCIe Mini
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    43
	CAN-IB130/PCIe 104
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    44
	CAN-IB230/PCIe 104
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    45
	CAN@net II/VCI
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    46
	CANblue II
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    47
	FR-IB100/PCIe (only in combination with VCI V2.20)
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    48
	tinCAN 161
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    49
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    50
*/
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    51
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    52
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    53
/*************************************************************************
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    54
**    
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    55
**************************************************************************
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    56
**
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    57
**    File: can_ixxat_win32.c
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    58
**    Summary: Wrapper to encapsulate handling of VCI3
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    59
**	  Include vcisdk.lib	
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    60
**
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    61
**************************************************************************
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    62
**************************************************************************
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    63
**
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    64
**  Functions:  canOpen_driver
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    65
**              canReceive_driver
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    66
**              TimerProc1
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    67
**              canClose_driver
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    68
**				canSend_drive
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    69
**				
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    70
**/
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    71
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    72
#include <stdio.h> 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    73
#include "can_driver.h"
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    74
#include "def.h"
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    75
#include "winuser.h"
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    76
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    77
//  Include a path to the following header files provided with VCI V3.X.X
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    78
//      ...sdk/Microsoft_VisualC/inc
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    79
#include "vcinpl.h" 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    80
#include "VCI3.h"  
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    81
#include "vcitype.h"
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    82
#include "vciguid.h"
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    83
#include "vcierr.h" 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    84
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    85
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    86
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    87
/************************************************************************
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    88
**   bus status polling cycle  milliseconds
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    89
*************************************************************************/
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    90
#define STATUS_POLLING_CYCLE 1000
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    91
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    92
/*************************************************************************
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    93
**   function prototypes
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    94
*************************************************************************/
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    95
void CALLBACK TimerProc1(void* lpParametar, BOOL TimerOrWaitFired );
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    96
void Display_Error(HRESULT hResult);
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    97
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    98
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
    99
/*************************************************************************
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   100
**   static variables 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   101
*************************************************************************/
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   102
static HANDLE hDevice;       // device handle
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   103
static LONG   lCtrlNo;       // controller number
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   104
static HANDLE hCanCtl;       // controller handle 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   105
static HANDLE hCanChn;       // channel handle
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   106
static LONG   lMustQuit = 0; // quit flag for the receive thread
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   107
static HANDLE hTimerHandle;  // timer handle 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   108
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   109
/*************************************************************************
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   110
**    datatypes
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   111
*************************************************************************/
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   112
 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   113
struct sLook_up_table
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   114
{
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   115
	char baud_rate[20];
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   116
	UINT8 bt0;
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   117
	UINT8 bt1; 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   118
};
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   119
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   120
struct sInterface_lookup_table
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   121
{
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   122
	char board_num[10];
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   123
	UINT8 num;
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   124
};
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   125
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   126
/*************************************************************************
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   127
**
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   128
**    Function      : canOpen_driver
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   129
**
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   130
**    Description   : Initializes the Control and Message Channels
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   131
**    Parameters    : s_BOARD *board		- pointer to board information
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   132
**    Returnvalue   : (CAN_HANDLE)board		- handle for CAN controller 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   133
**   
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   134
*************************************************************************/
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   135
CAN_HANDLE __stdcall canOpen_driver(s_BOARD *board)
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   136
{
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   137
    HANDLE        hEnumerator;     // enumerator handle
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   138
    VCIDEVICEINFO VCIDeviceInfo;   // device info
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   139
	HRESULT hResult;
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   140
	int index,  boardNum; 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   141
	BOOL bResult; 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   142
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   143
	struct sLook_up_table sBitRate_lookup[9] = {
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   144
		{"10K",0x31,0x1C},
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   145
		{"20K",0x18,0x1C},
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   146
		{"50K",0x09,0x1C},
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   147
        {"100K",0x04,0x1C},
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   148
        {"125K",0x03,0x1C},
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   149
        {"250K",0x01,0x1C},
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   150
        {"500K",0x00,0x1C},
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   151
        {"800K",0x00,0x16},
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   152
        {"1M",0x00,0x14}};
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   153
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   154
	struct sInterface_lookup_table sInterfaceList[4]={
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   155
		{"vcan0",0},
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   156
		{"vcan1",1},
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   157
		{"vcan2",2},
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   158
		{"vcan3",3}};
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   159
	
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   160
	for (boardNum =0 ; boardNum<4;boardNum++)   // determine canline selected 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   161
	{
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   162
		if (strcmp(sInterfaceList[boardNum].board_num,board->busname )==0)
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   163
			break;
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   164
	}
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   165
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   166
	for (index = 0; index < 10; ++index)       // determine baudrate 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   167
	{
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   168
		if (strcmp(sBitRate_lookup[index].baud_rate,board->baudrate)==0)
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   169
          break;
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   170
	}
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   171
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   172
	if (index == 9)
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   173
	{
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   174
		MSG_ERR_DRV("IXXAT::open: The given baudrate %S is invalid.", baud_rate);
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   175
		return NULL ;
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   176
	}
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   177
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   178
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   179
	/*
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   180
	**	The following can be used when the client has multiple CAN interfaces to slect from, and does not wish to use the 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   181
	**  selection dialog box
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   182
	/*
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   183
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   184
    /*		 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   185
	hResult= vciEnumDeviceOpen(&hEnumerator);
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   186
	 // This loop will increment the index of the device list and returns the decription of the CAN line chose by user
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   187
	if (hResult== VCI_OK)
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   188
	{
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   189
		for (index1=0; index1 <= sInterfaceList[boardNum].num; index1++){
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   190
			hResult=vciEnumDeviceNext(hEnumerator, &VCIDeviceInfo);
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   191
		}
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   192
	}
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   193
	printf("Device Selected: %s %s\n",VCIDeviceInfo.Manufacturer, VCIDeviceInfo.Description); 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   194
	if (hResult== VCI_OK)
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   195
		hResult=vciEnumDeviceClose(hEnumerator);
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   196
	
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   197
	if (hResult== VCI_OK)
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   198
   		hResult= vciDeviceOpen(&VCIDeviceInfo.VciObjectId, &hDevice);
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   199
	*/
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   200
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   201
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   202
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   203
	/* 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   204
	**  Display Interface Selection Dialog Box 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   205
	*/
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   206
	hResult= vciDeviceOpenDlg(0, &hDevice);
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   207
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   208
	/*
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   209
	**  Establish and activate the message Channel 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   210
	*/
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   211
	if (hResult== VCI_OK)
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   212
		hResult= canChannelOpen(hDevice, 0, TRUE, &hCanChn);
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   213
	//  Select Rx fifo size, Rx threshold, Tx fifo size, Tx threshold
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   214
    if (hResult== VCI_OK)
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   215
		hResult=canChannelInitialize( hCanChn, 1024, 1,128,1);  
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   216
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   217
    if (hResult== VCI_OK)
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   218
	    hResult=canChannelActivate(hCanChn, TRUE);
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   219
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   220
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   221
	/* 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   222
	** Establish and Activate the Contol Channel 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   223
	*/
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   224
    if (hResult== VCI_OK)
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   225
		 hResult=canControlOpen(hDevice, 0, &hCanCtl);
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   226
	 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   227
	//  Select 11 or 29 bit IDs, Select operating mode 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   228
    if (hResult== VCI_OK)
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   229
		hResult=canControlInitialize( hCanCtl, CAN_OPMODE_STANDARD | CAN_OPMODE_ERRFRAME, sBitRate_lookup[index].bt0, sBitRate_lookup[index].bt1 );
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   230
    
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   231
	
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   232
	//  With VCI it is possible to filter IDs, See VCI V3 manual. The following will accept all IDs
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   233
	if (hResult== VCI_OK)
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   234
	     hResult= canControlSetAccFilter( hCanCtl, FALSE, CAN_ACC_CODE_ALL, CAN_ACC_MASK_ALL);
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   235
    
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   236
	if (hResult== VCI_OK)
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   237
	    hResult=canControlStart(hCanCtl, TRUE);
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   238
  
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   239
	if (hResult!=VCI_OK)
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   240
	{
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   241
		Display_Error(hResult);
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   242
		return NULL; 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   243
	}
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   244
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   245
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   246
	/*
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   247
	**   Create timer to poll bus status 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   248
	*/  
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   249
	bResult= CreateTimerQueueTimer(
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   250
			& hTimerHandle,
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   251
			NULL,
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   252
			TimerProc1,    // Callback function
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   253
			NULL,
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   254
		    0,
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   255
			STATUS_POLLING_CYCLE,
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   256
			WT_EXECUTEINIOTHREAD
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   257
		    );
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   258
	
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   259
	return (CAN_HANDLE)board;
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   260
}
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   261
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   262
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   263
/*************************************************************************
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   264
**
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   265
**    Function      : canReceive_driver 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   266
**
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   267
**    Description   : Transfers RX messages to Festival application
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   268
**    Parameters    : CAN_HANDLE inst		- handle for CAN controller 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   269
					  Message *m			- pointer to Message struct
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   270
**    Returnvalue   : hResult				-   VCI_OK if success
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   271
**   
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   272
*************************************************************************/
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   273
UNS8 __stdcall canReceive_driver(CAN_HANDLE inst, Message *m)
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   274
{
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   275
    HRESULT hResult;
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   276
	CANMSG rCanMsg;
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   277
	//Read message from the receive buffer 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   278
	hResult=canChannelReadMessage( hCanChn, INFINITE, &rCanMsg );
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   279
	if (hResult !=VCI_OK )
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   280
		return 1; 		
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   281
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   282
	m->cob_id =  rCanMsg.dwMsgId;   
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   283
	m->len = rCanMsg.uMsgInfo.Bits.dlc;
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   284
	m->rtr = rCanMsg.uMsgInfo.Bits.rtr;
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   285
	   if (m->rtr == NOT_A_REQUEST)
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   286
	    	memcpy(m->data, rCanMsg.abData , m->len);
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   287
	return (UNS8)hResult; 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   288
}
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   289
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   290
/*************************************************************************
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   291
**
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   292
**    Function      : canSend_driver 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   293
**
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   294
**    Description   : Transfers RX messages to Interface 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   295
**    Parameters    : CAN_HANDLE inst		- handle for CAN controller 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   296
**					  Message *m			- pointer to Message struct
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   297
**    Returnvalue   : hResult				-   VCI_OK if success
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   298
**   
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   299
*************************************************************************/
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   300
UNS8 __stdcall canSend_driver(CAN_HANDLE inst, Message const *m)
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   301
{
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   302
   	HRESULT hResult;
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   303
	CANMSG sCanMsg; 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   304
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   305
	sCanMsg.uMsgInfo.Bytes.bType  = CAN_MSGTYPE_DATA;                
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   306
	sCanMsg.uMsgInfo.Bytes.bFlags = CAN_MAKE_MSGFLAGS(8,0,0,0,0);
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   307
	sCanMsg.uMsgInfo.Bits.srr     = 0;
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   308
	sCanMsg.dwTime   = 0;
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   309
	sCanMsg.dwMsgId  = m->cob_id ;                         
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   310
	memcpy(sCanMsg.abData,m->data, m->len);	                     
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   311
	sCanMsg.uMsgInfo.Bits.dlc = m->len;     
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   312
	sCanMsg.uMsgInfo.Bits.rtr=m->rtr; 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   313
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   314
	// write the CAN message into the transmit FIFO without waiting for transmission
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   315
	 hResult = canChannelPostMessage(hCanChn, &sCanMsg);
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   316
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   317
	 return (UNS8)hResult; 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   318
}
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   319
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   320
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   321
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   322
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   323
/*************************************************************************
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   324
**
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   325
**    Function      : canClose_driver 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   326
**
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   327
**    Description   : Close the message and control channel
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   328
**    Parameters    : CAN_HANDLE inst		- handle for CAN controller 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   329
**    Returnvalue   : 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   330
**   
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   331
*************************************************************************/
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   332
 int __stdcall canClose_driver(CAN_HANDLE inst)
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   333
   {
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   334
	printf("CAN close \n");
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   335
	canControlReset(hCanCtl);
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   336
	canChannelClose(hCanChn);
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   337
	canControlClose(hCanCtl);
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   338
	vciDeviceClose(hDevice);
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   339
	DeleteTimerQueueTimer(NULL,hTimerHandle,NULL); 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   340
   return 1;
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   341
   }
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   342
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   343
 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   344
/*************************************************************************
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   345
**
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   346
**    Function      : canChangeBaudRate_driver 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   347
**
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   348
**    Description   : Changes the Baudrate of the interface (not supported) 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   349
**    Parameters    : 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   350
**    Returnvalue   : 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   351
**   
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   352
*************************************************************************/
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   353
   UNS8 __stdcall canChangeBaudRate_driver( CAN_HANDLE fd, char* baud)
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   354
	{
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   355
	//printf("canChangeBaudRate not yet supported by this driver\n");
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   356
	return 0;
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   357
	}
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   358
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   359
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   360
 /*************************************************************************
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   361
**
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   362
**    Function      : TimerProc1 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   363
**
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   364
**    Description   :  Basic Polling of the  the canline status,
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   365
**					   print debug message for buffer overflow, and BUSOFF condition
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   366
**    Parameters    :  void* lpParametar, BOOL TimerOrWaitFired
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   367
**    Returnvalue   :  none 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   368
**   
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   369
*************************************************************************/
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   370
   void CALLBACK TimerProc1(void* lpParametar, BOOL TimerOrWaitFired )
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   371
   {
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   372
	   HRESULT hResult;
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   373
	   CANLINESTATUS canStatus;
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   374
	  
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   375
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   376
	   /* Establish CAN controller status */
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   377
	   hResult = canControlGetStatus ( hCanCtl, &canStatus);
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   378
	   if (hResult!=VCI_OK)
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   379
		   printf("Error did not read CAN STATUS\n"); 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   380
	   switch ( canStatus.dwStatus)
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   381
	   {
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   382
		   case CAN_STATUS_OVRRUN:
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   383
			   printf("Overrun of the recieve buffer\n");
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   384
			   break;
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   385
		   case CAN_STATUS_ERRLIM:
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   386
				printf("Overrun of the CAN controller error counter \n");
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   387
			   break;
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   388
		   case CAN_STATUS_BUSOFF:
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   389
				printf("CAN status: BUSOFF");
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   390
			   break; 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   391
		   case CAN_STATUS_ININIT:				
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   392
			   break; 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   393
		   case (CAN_STATUS_BUSOFF)+(CAN_STATUS_ININIT):
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   394
			   printf("CAN status: BUSOFF\n");
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   395
			    
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   396
	/* 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   397
	** Bus off recovery should come after a software reset, and 128*11 recessive bits.  
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   398
	** This can only happen when an Error-Active node sends an active error flag,
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   399
	** and other nodes respond with Error Echo Flag
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   400
	*/
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   401
			   break;
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   402
		   default: 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   403
			   break; 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   404
	   }
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   405
   };
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   406
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   407
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   408
 /*************************************************************************
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   409
**
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   410
**    Function      : Display_Error
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   411
**
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   412
**    Description   :  Display the CANline Error
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   413
**    Parameters    :  HANDLE hResult
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   414
**    Returnvalue   :  none 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   415
**   
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   416
*************************************************************************/
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   417
	void Display_Error(HRESULT hResult )
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   418
   {
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   419
	char szError[VCI_MAX_ERRSTRLEN];
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   420
	if (hResult != NO_ERROR)
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   421
	{
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   422
		if (hResult == -1)
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   423
			hResult = GetLastError();
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   424
		szError[0] = 0;
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   425
		vciFormatError(hResult, szError, sizeof(szError));
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   426
		printf("Error Establishing CAN channel, Error Code: %s\n",szError); 
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   427
	}
e13fa74d169c VCI3 driver added
Ian Tracy <iatr@hms.se>
parents:
diff changeset
   428
   }