drivers/can_peak_win32/can_peak_win32.c
author Edouard Tisserant
Thu, 08 Mar 2018 15:09:22 +0100
changeset 799 6d3ad380dcd3
parent 730 b5f4b3e2d01c
permissions -rwxr-xr-x
Update can_socket.c to Xenomai 3 API
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"
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
    36
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
    37
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
    38
#define VERSION_2
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
    39
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
    40
/* dummy implementation for older version. */
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
    41
#ifndef VERSION_2
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
    42
void CAN_SetRcvEvent(HANDLE hEventx)
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
    43
{
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
    44
  SetEvent(hEventx);
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
    45
}
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
    46
#endif
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
    47
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
    48
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
    49
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
    50
41
e70439f50539 First working cygwin/pcan_light commit. can_peak_win32.c is obfuscated.
etisserant
parents: 38
diff changeset
    51
#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
    52
	#define extra_PCAN_init_params /**/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    53
#else
574
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    54
	long int print_getenv(const char* pcanparam)
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    55
	{
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    56
		char* param=NULL;
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    57
		long int res=0;
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    58
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    59
		param = getenv(pcanparam);
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    60
		if(param != NULL){
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    61
			res = strtol(param,NULL,0);
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    62
		}
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    63
		else
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    64
			printf("Environment variable %s not defined !\n", pcanparam);
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    65
		printf("Found environment variable %s : %ld\n", pcanparam ,res);
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    66
		return res;
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    67
	}
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    68
	#define extra_PCAN_init_params\
574
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    69
		,print_getenv("PCANHwType")\
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    70
		,print_getenv("PCANIO_Port")\
e5d7f4412a4c Add print_getenv function wich print extra init params
greg
parents: 570
diff changeset
    71
		,print_getenv("PCANInterupt")
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    72
#endif
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    73
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
    74
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    75
static s_BOARD *first_board = NULL;
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
    76
//Create the Event for the first board
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
    77
HANDLE hEvent1 = NULL;
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
    78
CRITICAL_SECTION InitLock1;
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
    79
CRITICAL_SECTION InitLock2;
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
    80
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
    81
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
    82
#ifdef PCAN2_HEADER_
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
    83
  static s_BOARD *second_board = NULL;
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
    84
  HANDLE hEvent2 = NULL;
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
    85
#endif
145
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
// Define for rtr CAN message
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    88
#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
    89
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    90
/***************************************************************************/
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
    91
static int TranslateBaudeRate(char* optarg)
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
    92
{
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    93
	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
    94
	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
    95
	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
    96
	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
    97
	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
    98
	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
    99
	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
   100
	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
   101
	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
   102
	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
   103
	return 0x0000;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   104
}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   105
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   106
static 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
   107
{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   108
	int baudrate;
566
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   109
	int ret = 0;
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   110
329
7717252e3ed9 Changes to compile for win32
greg
parents: 301
diff changeset
   111
#ifdef PCAN2_HEADER_
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   112
	// if not the first handler
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   113
	if(second_board == (s_BOARD *)board) {
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   114
		if(hEvent2==NULL)
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   115
		{	/* Create the Event for the first board */
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   116
		  hEvent2 = CreateEvent(NULL, // lpEventAttributes
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   117
							FALSE,  // bManualReset
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   118
							FALSE,  // bInitialState
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   119
							"");    // lpName
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   120
		  InitializeCriticalSection(&InitLock2);
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   121
		}
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   122
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   123
		EnterCriticalSection(&InitLock2);
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   124
		if(baudrate = TranslateBaudeRate(board->baudrate))
566
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   125
		{
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   126
		  ret = CAN2_Init(baudrate, CAN_INIT_TYPE_ST extra_PCAN_init_params);
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   127
		  if(ret != CAN_ERR_OK)
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   128
		  {
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   129
		    LeaveCriticalSection(&InitLock2);
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   130
		    return 0;
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   131
		  }
625
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   132
		}
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   133
		//Set Event Handle for CANReadExt
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   134
		CAN2_SetRcvEvent(hEvent2);
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   135
		LeaveCriticalSection(&InitLock2);
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   136
	}
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   137
	else
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   138
#endif
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   139
	if(first_board == (s_BOARD *)board) {
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   140
		//Create the Event for the first board
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   141
		if(hEvent1==NULL)
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   142
		{
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   143
		  hEvent1 = CreateEvent(NULL, // lpEventAttributes
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   144
							FALSE,  // bManualReset
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   145
							FALSE,  // bInitialState
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   146
							"");    // lpName
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   147
		  InitializeCriticalSection(&InitLock1);
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   148
                }
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   149
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   150
		EnterCriticalSection(&InitLock1);
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   151
		if(baudrate = TranslateBaudeRate(board->baudrate))
566
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   152
		{
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   153
		  ret = CAN_Init(baudrate, CAN_INIT_TYPE_ST extra_PCAN_init_params);
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   154
		  if(ret != CAN_ERR_OK)
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   155
		  {
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   156
		    LeaveCriticalSection(&InitLock1);
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   157
		    return 0;
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   158
		  }
625
5482b98e82a3 Fix non freed windows handle. (thx to jaroslav.fojtik)
edouard
parents: 598
diff changeset
   159
		}
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   160
		//Set Event Handle for CANReadExt
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   161
		CAN_SetRcvEvent(hEvent1);
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   162
		LeaveCriticalSection(&InitLock1);
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   163
	}
566
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   164
	return 1;
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   165
}
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   166
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   167
/********* functions which permit to communicate with the board ****************/
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   168
UNS8 LIBAPI canReceive_driver(CAN_HANDLE fd0, Message * m)
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   169
{
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   170
static int HeavyCounter = 0;
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   171
	int ret=0;
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   172
	UNS8 data;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   173
	TPCANMsg peakMsg;
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   174
#ifdef CAN_READ_EX
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   175
	TPCANTimestamp peakRcvTime;
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   176
#endif
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   177
	DWORD Res;
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   178
	DWORD result;
570
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   179
	// loop until valid message or fatal error
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   180
	do{
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   181
#ifdef PCAN2_HEADER_
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   182
		// if not the first handler
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   183
		if(second_board == (s_BOARD *)fd0) {
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   184
			//wait for CAN msg...
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   185
			result = WaitForSingleObject(hEvent2, INFINITE);
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   186
			if (result == WAIT_OBJECT_0)
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   187
				Res = CAN2_ReadEx(&peakMsg, &peakRcvTime);
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   188
				// Exit receive thread when handle is no more valid
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   189
				if(Res & CAN_ERRMASK_ILLHANDLE)
570
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   190
					return 1;
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   191
		}
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   192
		else
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   193
#endif
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   194
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   195
		// We read the queue looking for messages.
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   196
		if(first_board == (s_BOARD *)fd0) 
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   197
		{
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   198
#ifdef VERSION_2
570
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   199
			result = WaitForSingleObject(hEvent1, INFINITE);
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   200
			if (result == WAIT_OBJECT_0)
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   201
#endif
570
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   202
			{
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   203
#ifdef CAN_READ_EX
570
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   204
				Res = CAN_ReadEx(&peakMsg, &peakRcvTime);
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   205
#else
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   206
				Res = CAN_Read(&peakMsg);
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   207
#endif
570
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   208
				// Exit receive thread when handle is no more valid
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   209
#ifdef CAN_ERRMASK_ILLHANDLE
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   210
				if(Res & CAN_ERRMASK_ILLHANDLE) return 1;
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   211
#else
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   212
				if(Res & CAN_ERR_ILLHANDLE) return 1;
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   213
#endif				
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   214
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   215
#ifndef VERSION_2
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   216
				if(Res != CAN_ERR_OK) 
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   217
					result = WaitForSingleObject(hEvent1, 1);  	//pooling
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   218
#endif
570
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   219
			}
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   220
		}
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   221
#ifdef VERSION_2
570
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   222
		else
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   223
			Res = CAN_ERR_BUSOFF;
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   224
#endif
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   225
		
570
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   226
		// A message was received : we process the message(s)
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   227
		if(Res == CAN_ERR_OK)
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   228
		{			
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   229
		  switch(peakMsg.MSGTYPE)
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   230
		  {
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   231
		    case MSGTYPE_STATUS:
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   232
			  switch(peakMsg.DATA[3])
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   233
		          {
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   234
			    case CAN_ERR_BUSHEAVY: 
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   235
				      break;
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   236
		            case CAN_ERR_BUSOFF: 
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   237
			              printf ("Peak board read BUSOFF: re-init!!!\n");
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   238
				      canInit((s_BOARD*)fd0);
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   239
				      usleep(33);
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   240
				      break;
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   241
			  }
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   242
			  return peakMsg.DATA[3];	/* if something different that 11bit or rtr... problem */
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   243
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   244
		    case MSGTYPE_STANDARD:		/* bits of MSGTYPE_ */
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   245
		    case MSGTYPE_EXTENDED:
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   246
			  m->rtr = 0;
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   247
			  break;
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   248
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   249
		    case MSGTYPE_RTR:			/* bits of MSGTYPE_ */
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   250
			  m->rtr = 1;
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   251
			  break;
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   252
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   253
		    default: return CAN_ERR_OVERRUN;	/* If status, return status if 29bit, return overrun. */
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   254
		    
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   255
		    }
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   256
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   257
		   m->cob_id = peakMsg.ID;				
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   258
		   if (peakMsg.MSGTYPE == CAN_INIT_TYPE_ST)  /* bits of MSGTYPE_ */
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   259
                     m->rtr = 0;
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   260
		   else
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   261
		     m->rtr = 1;
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   262
		   m->len = peakMsg.LEN;		/* count of data bytes (0..8) */
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   263
		   for (data = 0; data < peakMsg.LEN; data++)
730
b5f4b3e2d01c can_peak_win32 code:
Christian Taedcke <hacking@taedcke.com>
parents: 713
diff changeset
   264
			m->data[data] = peakMsg.DATA[data];	/* data bytes, up to 8 */
570
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   265
#if defined DEBUG_MSG_CONSOLE_ON
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   266
			MSG("in : ");
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   267
			print_message(m);
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   268
#endif
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   269
		}
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   270
		else
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   271
		{
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   272
			// not benign error => fatal error
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   273
			if (!(Res & CAN_ERR_QRCVEMPTY
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   274
					|| Res & CAN_ERR_BUSLIGHT
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   275
					|| Res & CAN_ERR_BUSHEAVY))
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   276
			{
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   277
				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
   278
				return 1;
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   279
			}
570
c18397a2b035 fix method to exit properly CanReceiveLoop
greg
parents: 566
diff changeset
   280
		}
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   281
	} while(Res != CAN_ERR_OK);
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   282
	return 0;
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   283
}
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   284
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   285
/***************************************************************************/
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   286
UNS8 LIBAPI 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
   287
{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   288
	UNS8 data;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   289
	TPCANMsg peakMsg;
365
9b76e0881beb Changed cob_id from struct{UNS32} to UNS16
etisserant
parents: 341
diff changeset
   290
	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
   291
	if (m->rtr == 0)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   292
	{
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   293
	  if(peakMsg.ID > 0x7FF)
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   294
		peakMsg.MSGTYPE = MSGTYPE_EXTENDED;	/* bits of MSGTYPE_ */
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   295
	  else
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   296
		peakMsg.MSGTYPE = MSGTYPE_STANDARD;	/* bits of MSGTYPE_ */
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   297
	}
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   298
	else	
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   299
		peakMsg.MSGTYPE = MSGTYPE_RTR;		/* bits of MSGTYPE_ */
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   300
	
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   301
	peakMsg.LEN = m->len;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   302
	/* 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
   303
	for (data = 0; data < m->len; data++)
730
b5f4b3e2d01c can_peak_win32 code:
Christian Taedcke <hacking@taedcke.com>
parents: 713
diff changeset
   304
		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
   305
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   306
	do
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   307
	{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   308
#ifdef PCAN2_HEADER_
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   309
		// if not the first handler
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   310
		if(second_board == (s_BOARD *)fd0)
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   311
		{
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   312
			errno = CAN2_Write (&peakMsg);
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   313
		}
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   314
		else
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   315
#endif
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   316
		if(first_board == (s_BOARD *)fd0)
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   317
			{
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   318
				errno = CAN_Write (&peakMsg);
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   319
			}
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   320
		else
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   321
			goto fail;
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   322
		if (errno)
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   323
		{
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   324
			if (errno == CAN_ERR_BUSOFF)
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   325
			{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   326
				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
   327
				canInit((s_BOARD*)fd0);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   328
				usleep (10000);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   329
			}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   330
			usleep (1000);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   331
		}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   332
	}
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   333
	while (errno != CAN_ERR_OK);
341
7ff01f109bbc Windows related enhancements
etisserant
parents: 329
diff changeset
   334
#if defined DEBUG_MSG_CONSOLE_ON
454
bc000083297a - add RTAI support
greg
parents: 384
diff changeset
   335
	MSG("out : ");
341
7ff01f109bbc Windows related enhancements
etisserant
parents: 329
diff changeset
   336
	print_message(m);
7ff01f109bbc Windows related enhancements
etisserant
parents: 329
diff changeset
   337
#endif
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   338
	return 0;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   339
fail:
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   340
	return 1;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   341
}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   342
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   343
/***************************************************************************/
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   344
UNS8 LIBAPI canChangeBaudRate_driver(CAN_HANDLE fd, char* baud)
384
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 365
diff changeset
   345
{
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 365
diff changeset
   346
	printf("canChangeBaudRate not yet supported by this driver\n");
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 365
diff changeset
   347
	return 0;
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 365
diff changeset
   348
}
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 365
diff changeset
   349
83793fc7ce48 added canChangeBaudRate to the driver interface
groke6
parents: 365
diff changeset
   350
/***************************************************************************/
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   351
LIBPUBLIC CAN_HANDLE LIBAPI canOpen_driver(s_BOARD * board)
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   352
{
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   353
  char* pEnd;
566
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   354
  int ret;
563
394440e3b6e4 update canReceive_driver to match with Peak new read function (event)
greg
parents: 454
diff changeset
   355
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   356
  //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
   357
  if (strtol(board->busname, &pEnd,0) == 0)
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   358
  {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   359
      first_board = board;
566
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   360
      //printf ("First Board selected\n");
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   361
      ret = canInit(board);
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   362
      if(ret)
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   363
    	  return (CAN_HANDLE)board;
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   364
  }
329
7717252e3ed9 Changes to compile for win32
greg
parents: 301
diff changeset
   365
  #ifdef PCAN2_HEADER_
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   366
  if (strtol(board->busname, &pEnd,0) == 1)
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   367
  {
329
7717252e3ed9 Changes to compile for win32
greg
parents: 301
diff changeset
   368
      second_board = board;
566
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   369
      //printf ("Second Board selected\n");
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   370
      ret = canInit(board);
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   371
      if(ret)
365d555fc0c6 fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents: 563
diff changeset
   372
    	  return (CAN_HANDLE)board;
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   373
  }
329
7717252e3ed9 Changes to compile for win32
greg
parents: 301
diff changeset
   374
  #endif
7717252e3ed9 Changes to compile for win32
greg
parents: 301
diff changeset
   375
  return NULL;
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   376
}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   377
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   378
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   379
/***************************************************************************/
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   380
int LIBAPI 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
   381
{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   382
#ifdef PCAN2_HEADER_
713
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   383
	// if not the first handler
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   384
	if(second_board == (s_BOARD *)fd0)
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   385
	{
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   386
		CAN2_SetRcvEvent(NULL);
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   387
		CAN2_Close ();		
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   388
		if(hEvent2)
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   389
		{
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   390
		  SetEvent(hEvent2);
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   391
		  CloseHandle(hEvent2);		
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   392
		  hEvent2 = NULL;
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   393
		}
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   394
		second_board = (s_BOARD *)NULL;
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   395
	}else
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   396
#endif
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   397
	if(first_board == (s_BOARD *)fd0)
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   398
	{
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   399
		CAN_SetRcvEvent(NULL);
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   400
		CAN_Close ();		
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   401
		if(hEvent1) 
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   402
		{
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   403
                  SetEvent(hEvent1);
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   404
		  CloseHandle(hEvent1);
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   405
		  hEvent1 = NULL;
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   406
                }
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   407
		first_board = (s_BOARD *)NULL;
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   408
	}
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   409
	return 0;
ca2c2dad3a22 Fixed PEAK Win 32 driver, for PEAK Light API 1.x and 2.x
JaFojtik
parents: 696
diff changeset
   410
}