drivers/can_peak_win32/can_peak_win32.c
author JaFojtik
Fri, 03 Feb 2012 22:13:25 +0100
changeset 696 381af6bb6155
parent 650 58b07a7e695b
child 713 ca2c2dad3a22
permissions -rwxr-xr-x
Windows CAN DLL driver sanity check.
38
9b5bb1dcb4f5 Cygwin port. Still untested. Compiles and link.
etisserant
parents:
diff changeset
     1
/*
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack.
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
     3
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
     4
Copyright (C): Edouard TISSERANT and Francis DUPIN
696
381af6bb6155 Windows CAN DLL driver sanity check.
JaFojtik
parents: 650
diff changeset
     5
Modified by: Jaroslav Fojtik
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
     6
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
     7
See COPYING file for copyrights details.
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
     8
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
     9
This library is free software; you can redistribute it and/or
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    10
modify it under the terms of the GNU Lesser General Public
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    11
License as published by the Free Software Foundation; either
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    12
version 2.1 of the License, or (at your option) any later version.
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    13
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    14
This library is distributed in the hope that it will be useful,
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    15
but WITHOUT ANY WARRANTY; without even the implied warranty of
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    17
Lesser General Public License for more details.
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    18
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    19
You should have received a copy of the GNU Lesser General Public
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    20
License along with this library; if not, write to the Free Software
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    21
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
38
9b5bb1dcb4f5 Cygwin port. Still untested. Compiles and link.
etisserant
parents:
diff changeset
    22
*/
9b5bb1dcb4f5 Cygwin port. Still untested. Compiles and link.
etisserant
parents:
diff changeset
    23
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    24
#if defined(WIN32) && !defined(__CYGWIN__)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    25
#define usleep(micro) Sleep(micro%1000 ? (micro/1000) + 1 : (micro/1000))
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    26
#else
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    27
#include <stdio.h>
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    28
#include <string.h>
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    29
#include <errno.h>
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    30
#include <fcntl.h>
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    31
#endif
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    32
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    33
#include "cancfg.h"
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    34
#include "can_driver.h"
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
    35
#include "def.h"
41
e70439f50539 First working cygwin/pcan_light commit. can_peak_win32.c is obfuscated.
etisserant
parents: 38
diff changeset
    36
#ifndef extra_PCAN_init_params
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    37
	#define extra_PCAN_init_params /**/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    38
#else
574
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    39
	long int print_getenv(const char* pcanparam)
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    40
	{
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    41
		char* param=NULL;
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    42
		long int res=0;
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    43
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    44
		param = getenv(pcanparam);
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    45
		if(param != NULL){
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    46
			res = strtol(param,NULL,0);
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    47
		}
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    48
		else
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    49
			printf("Environment variable %s not defined !\n", pcanparam);
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    50
		printf("Found environment variable %s : %ld\n", pcanparam ,res);
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    51
		return res;
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    52
	}
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    53
	#define extra_PCAN_init_params\
574
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    54
		,print_getenv("PCANHwType")\
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    55
		,print_getenv("PCANIO_Port")\
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    56
		,print_getenv("PCANInterupt")
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    57
#endif
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    58
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    59
static s_BOARD *first_board = NULL;
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
    60
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
    61
//Create the Event for the first board
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
    62
HANDLE hEvent1 = NULL;
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
    63
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
    64
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
    65
#ifdef PCAN2_HEADER_
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
    66
	static s_BOARD *second_board = NULL;
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
    67
	HANDLE hEvent2 = NULL;
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
    68
#endif
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    69
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    70
// Define for rtr CAN message
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    71
#define CAN_INIT_TYPE_ST_RTR MSGTYPE_STANDARD | MSGTYPE_RTR
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    72
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    73
/***************************************************************************/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    74
int TranslateBaudeRate(char* optarg){
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    75
	if(!strcmp( optarg, "1M")) return CAN_BAUD_1M;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    76
	if(!strcmp( optarg, "500K")) return CAN_BAUD_500K;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    77
	if(!strcmp( optarg, "250K")) return CAN_BAUD_250K;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    78
	if(!strcmp( optarg, "125K")) return CAN_BAUD_125K;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    79
	if(!strcmp( optarg, "100K")) return CAN_BAUD_100K;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    80
	if(!strcmp( optarg, "50K")) return CAN_BAUD_50K;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    81
	if(!strcmp( optarg, "20K")) return CAN_BAUD_20K;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    82
	if(!strcmp( optarg, "10K")) return CAN_BAUD_10K;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    83
	if(!strcmp( optarg, "5K")) return CAN_BAUD_5K;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    84
	if(!strcmp( optarg, "none")) return 0;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    85
	return 0x0000;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    86
}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    87
566
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
    88
UNS8 canInit (s_BOARD *board)
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    89
{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    90
	int baudrate;
566
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
    91
	int ret = 0;
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
    92
329
7717252e3ed9 Changes to compile for win32
greg
parents: 301
diff changeset
    93
#ifdef PCAN2_HEADER_
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    94
	// if not the first handler
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
    95
	if(second_board == (s_BOARD *)board) {
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    96
		if(baudrate = TranslateBaudeRate(board->baudrate))
566
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
    97
		{
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
    98
			ret = CAN2_Init(baudrate, CAN_INIT_TYPE_ST extra_PCAN_init_params);
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
    99
			if(ret != CAN_ERR_OK)
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   100
				return 0;
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   101
		}
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   102
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   103
		//Create the Event for the first board
625
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   104
		if(hEvent2 != NULL){
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   105
			hEvent2 = CreateEvent(NULL, // lpEventAttributes
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   106
			                      FALSE,  // bManualReset
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   107
			                      FALSE,  // bInitialState
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   108
			                      "");    // lpName
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   109
		}
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   110
		//Set Event Handle for CANReadExt
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   111
		CAN2_SetRcvEvent(hEvent2);
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   112
	}
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   113
	else
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   114
#endif
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   115
	if(first_board == (s_BOARD *)board) {
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   116
		if(baudrate = TranslateBaudeRate(board->baudrate))
566
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   117
		{
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   118
			ret = CAN_Init(baudrate, CAN_INIT_TYPE_ST extra_PCAN_init_params);
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   119
			if(ret != CAN_ERR_OK)
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   120
				return 0;
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   121
		}
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   122
		//Create the Event for the first board
625
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   123
		if(hEvent1 != NULL){
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   124
			hEvent1 = CreateEvent(NULL, // lpEventAttributes
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   125
			                      FALSE,  // bManualReset
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   126
			                      FALSE,  // bInitialState
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   127
			                      "");    // lpName
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   128
		}
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   129
		//Set Event Handle for CANReadExt
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   130
		CAN_SetRcvEvent(hEvent1);
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   131
	}
566
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   132
	return 1;
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   133
}
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   134
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   135
/********* functions which permit to communicate with the board ****************/
633
2c43383a1d57 Fix mingw32 cross build for can_peak_win32 and can_tcp_win32 server.
edouard
parents: 631
diff changeset
   136
UNS8 __stdcall canReceive_driver (CAN_HANDLE fd0, Message * m)
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   137
{
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   138
	int ret=0;
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   139
	UNS8 data;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   140
	TPCANMsg peakMsg;
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   141
	TPCANTimestamp peakRcvTime;
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   142
	DWORD Res;
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   143
	DWORD result;
570
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   144
	// loop until valid message or fatal error
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   145
	do{
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   146
#ifdef PCAN2_HEADER_
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   147
		// if not the first handler
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   148
		if(second_board == (s_BOARD *)fd0) {
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   149
			//wait for CAN msg...
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   150
			result = WaitForSingleObject(hEvent2, INFINITE);
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   151
			if (result == WAIT_OBJECT_0)
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   152
				Res = CAN2_ReadEx(&peakMsg, &peakRcvTime);
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   153
				// Exit receive thread when handle is no more valid
650
58b07a7e695b FIXED: - Renamed macro for peak win32 driver, patch from mailinglist (post from Christian Boepple, 2010-09-08 14:38)
Christian Taedcke
parents: 633
diff changeset
   154
				if(Res & CAN_ERR_ILLHANDLE)
570
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   155
					return 1;
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   156
		}
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   157
		else
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   158
#endif
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   159
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   160
		// We read the queue looking for messages.
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   161
		if(first_board == (s_BOARD *)fd0) {
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   162
			result = WaitForSingleObject(hEvent1, INFINITE);
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   163
			if (result == WAIT_OBJECT_0)
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   164
			{
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   165
				Res = CAN_ReadEx(&peakMsg, &peakRcvTime);
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   166
				// Exit receive thread when handle is no more valid
650
58b07a7e695b FIXED: - Renamed macro for peak win32 driver, patch from mailinglist (post from Christian Boepple, 2010-09-08 14:38)
Christian Taedcke
parents: 633
diff changeset
   167
				if(Res & CAN_ERR_ILLHANDLE)
570
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   168
					return 1;
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   169
			}
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   170
		}
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   171
		else
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   172
			Res = CAN_ERR_BUSOFF;
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   173
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   174
		// A message was received : we process the message(s)
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   175
		if (Res == CAN_ERR_OK)
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   176
		{
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   177
			// if something different that 11bit or rtr... problem
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   178
			if (peakMsg.MSGTYPE & ~(MSGTYPE_STANDARD | MSGTYPE_RTR))
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   179
			{
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   180
				if (peakMsg.MSGTYPE == CAN_ERR_BUSOFF)
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   181
				{
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   182
					printf ("!!! Peak board read : re-init\n");
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   183
					canInit((s_BOARD*) fd0);
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   184
					usleep (10000);
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   185
				}
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   186
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   187
				// If status, return status if 29bit, return overrun
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   188
				return peakMsg.MSGTYPE ==
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   189
					MSGTYPE_STATUS ? peakMsg.DATA[2] : CAN_ERR_OVERRUN;
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   190
			}
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   191
			m->cob_id = peakMsg.ID;
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   192
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   193
			if (peakMsg.MSGTYPE == CAN_INIT_TYPE_ST)	/* bits of MSGTYPE_ */
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   194
				m->rtr = 0;
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   195
			else
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   196
				m->rtr = 1;
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   197
			m->len = peakMsg.LEN;	/* count of data bytes (0..8) */
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   198
			for (data = 0; data < peakMsg.LEN; data++)
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   199
				m->data[data] = peakMsg.DATA[data];	/* data bytes, up to 8 */
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   200
#if defined DEBUG_MSG_CONSOLE_ON
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   201
			MSG("in : ");
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   202
			print_message(m);
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   203
#endif
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   204
		}
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   205
		else
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   206
		{
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   207
			// not benign error => fatal error
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   208
			if (!(Res & CAN_ERR_QRCVEMPTY
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   209
					|| Res & CAN_ERR_BUSLIGHT
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   210
					|| Res & CAN_ERR_BUSHEAVY))
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   211
			{
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   212
				printf ("canReceive returned error (%d)\n", Res);
566
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   213
				return 1;
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   214
			}
570
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   215
		}
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   216
	}while(Res != CAN_ERR_OK);
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   217
	return 0;
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   218
}
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   219
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   220
/***************************************************************************/
633
2c43383a1d57 Fix mingw32 cross build for can_peak_win32 and can_tcp_win32 server.
edouard
parents: 631
diff changeset
   221
UNS8 __stdcall canSend_driver (CAN_HANDLE fd0, Message const * m)
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   222
{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   223
	UNS8 data;
598
fff217764431 removed ligcc.a dependency and fix problem with errno value
'Gr?gory Tr?lat <gregory.trelat@lolitech.fr>'
parents: 574
diff changeset
   224
	DWORD localerrno;
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   225
	TPCANMsg peakMsg;
365
9b76e0881beb Changed cob_id from struct{UNS32} to UNS16
etisserant
parents: 341
diff changeset
   226
	peakMsg.ID = m->cob_id;	/* 11/29 bit code */
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   227
	if (m->rtr == 0)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   228
		peakMsg.MSGTYPE = CAN_INIT_TYPE_ST;	/* bits of MSGTYPE_ */
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   229
	else
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   230
	{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   231
		peakMsg.MSGTYPE = CAN_INIT_TYPE_ST_RTR;	/* bits of MSGTYPE_ */
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   232
	}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   233
	peakMsg.LEN = m->len;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   234
	/* count of data bytes (0..8) */
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   235
	for (data = 0; data < m->len; data++)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   236
		peakMsg.DATA[data] = m->data[data];	/* data bytes, up to 8 */
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   237
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   238
	do
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   239
	{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   240
#ifdef PCAN2_HEADER_
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   241
		// if not the first handler
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   242
		if(second_board == (s_BOARD *)fd0)
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   243
		{
598
fff217764431 removed ligcc.a dependency and fix problem with errno value
'Gr?gory Tr?lat <gregory.trelat@lolitech.fr>'
parents: 574
diff changeset
   244
			errno = localerrno = CAN2_Write (&peakMsg);
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   245
		}
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   246
		else
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   247
#endif
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   248
		if(first_board == (s_BOARD *)fd0)
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   249
			{
598
fff217764431 removed ligcc.a dependency and fix problem with errno value
'Gr?gory Tr?lat <gregory.trelat@lolitech.fr>'
parents: 574
diff changeset
   250
				errno = localerrno = CAN_Write (&peakMsg);
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   251
			}
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   252
		else
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   253
			goto fail;
598
fff217764431 removed ligcc.a dependency and fix problem with errno value
'Gr?gory Tr?lat <gregory.trelat@lolitech.fr>'
parents: 574
diff changeset
   254
		if (localerrno)
fff217764431 removed ligcc.a dependency and fix problem with errno value
'Gr?gory Tr?lat <gregory.trelat@lolitech.fr>'
parents: 574
diff changeset
   255
		{
fff217764431 removed ligcc.a dependency and fix problem with errno value
'Gr?gory Tr?lat <gregory.trelat@lolitech.fr>'
parents: 574
diff changeset
   256
			if (localerrno == CAN_ERR_BUSOFF)
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   257
			{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   258
				printf ("!!! Peak board write : re-init\n");
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   259
				canInit((s_BOARD*)fd0);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   260
				usleep (10000);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   261
			}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   262
			usleep (1000);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   263
		}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   264
	}
598
fff217764431 removed ligcc.a dependency and fix problem with errno value
'Gr?gory Tr?lat <gregory.trelat@lolitech.fr>'
parents: 574
diff changeset
   265
	while (localerrno != CAN_ERR_OK);
341
7ff01f109bbc Windows related enhancements
etisserant
parents: 329
diff changeset
   266
#if defined DEBUG_MSG_CONSOLE_ON
454
bc000083297a - add RTAI support
greg
parents: 384
diff changeset
   267
	MSG("out : ");
341
7ff01f109bbc Windows related enhancements
etisserant
parents: 329
diff changeset
   268
	print_message(m);
7ff01f109bbc Windows related enhancements
etisserant
parents: 329
diff changeset
   269
#endif
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   270
	return 0;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   271
fail:
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   272
	return 1;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   273
}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   274
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   275
/***************************************************************************/
633
2c43383a1d57 Fix mingw32 cross build for can_peak_win32 and can_tcp_win32 server.
edouard
parents: 631
diff changeset
   276
UNS8 __stdcall canChangeBaudRate_driver( CAN_HANDLE fd, char* baud)
384
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 365
diff changeset
   277
{
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 365
diff changeset
   278
	printf("canChangeBaudRate not yet supported by this driver\n");
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 365
diff changeset
   279
	return 0;
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 365
diff changeset
   280
}
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 365
diff changeset
   281
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 365
diff changeset
   282
/***************************************************************************/
633
2c43383a1d57 Fix mingw32 cross build for can_peak_win32 and can_tcp_win32 server.
edouard
parents: 631
diff changeset
   283
CAN_HANDLE __stdcall canOpen_driver (s_BOARD * board)
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   284
{
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   285
  char busname[64];
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   286
  char* pEnd;
566
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   287
  int ret;
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   288
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   289
  //printf ("Board Busname=%d.\n",strtol(board->busname, &pEnd,0));
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   290
  if (strtol(board->busname, &pEnd,0) == 0)
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   291
  {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   292
      first_board = board;
566
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   293
      //printf ("First Board selected\n");
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   294
      ret = canInit(board);
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   295
      if(ret)
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   296
    	  return (CAN_HANDLE)board;
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   297
  }
329
7717252e3ed9 Changes to compile for win32
greg
parents: 301
diff changeset
   298
  #ifdef PCAN2_HEADER_
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   299
  if (strtol(board->busname, &pEnd,0) == 1)
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   300
  {
329
7717252e3ed9 Changes to compile for win32
greg
parents: 301
diff changeset
   301
      second_board = board;
566
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   302
      //printf ("Second Board selected\n");
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   303
      ret = canInit(board);
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   304
      if(ret)
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   305
    	  return (CAN_HANDLE)board;
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   306
  }
329
7717252e3ed9 Changes to compile for win32
greg
parents: 301
diff changeset
   307
  #endif
7717252e3ed9 Changes to compile for win32
greg
parents: 301
diff changeset
   308
  return NULL;
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   309
}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   310
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   311
/***************************************************************************/
633
2c43383a1d57 Fix mingw32 cross build for can_peak_win32 and can_tcp_win32 server.
edouard
parents: 631
diff changeset
   312
int __stdcall canClose_driver (CAN_HANDLE fd0)
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   313
{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   314
#ifdef PCAN2_HEADER_
625
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   315
      // if not the first handler
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   316
      if(second_board == (s_BOARD *)fd0)
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   317
      {
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   318
           CAN2_SetRcvEvent(NULL);
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   319
           CAN2_Close ();
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   320
           if(hEvent2)
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   321
           {
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   322
             SetEvent(hEvent2);
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   323
             CloseHandle(hEvent2);
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   324
             hEvent2 = NULL;
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   325
           }
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   326
           second_board = (s_BOARD *)NULL;
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   327
      }else
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   328
#endif
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   329
      if(first_board == (s_BOARD *)fd0)
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   330
      {
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   331
           CAN_SetRcvEvent(NULL);
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   332
           CAN_Close ();
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   333
           if(hEvent1)
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   334
           {
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   335
             SetEvent(hEvent1);
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   336
             CloseHandle(hEvent1);
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   337
             hEvent1 = NULL;
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   338
            }
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   339
           first_board = (s_BOARD *)NULL;
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   340
      }
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   341
      return 0;
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   342
}