drivers/can_peak_win32/can_peak_win32.c
author groke6
Tue, 08 Jan 2008 10:47:10 +0100
changeset 349 1e6dd4ef46b9
parent 341 7ff01f109bbc
child 365 9b76e0881beb
permissions -rw-r--r--
fixed the DCF-LSS conflict
38
9b5bb1dcb4f5 Cygwin port. Still untested. Compiles and link.
etisserant
parents:
diff changeset
     1
/*
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack. 
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
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
     5
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
     6
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
     7
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
     8
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
     9
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
    10
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
    11
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
    12
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    13
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
    14
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
    15
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
    16
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
    17
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    18
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
    19
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
    20
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
    21
*/
9b5bb1dcb4f5 Cygwin port. Still untested. Compiles and link.
etisserant
parents:
diff changeset
    22
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    23
#if defined(WIN32) && !defined(__CYGWIN__)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    24
#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
    25
#else
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    26
#include <stdio.h>
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    27
#include <string.h>
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    28
#include <errno.h>
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    29
#include <fcntl.h>
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    30
#endif
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    31
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    32
#include "cancfg.h"
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    33
#include "can_driver.h"
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
    34
#include "def.h"
41
e70439f50539 First working cygwin/pcan_light commit. can_peak_win32.c is obfuscated.
etisserant
parents: 38
diff changeset
    35
#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
    36
	#define extra_PCAN_init_params /**/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    37
#else
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    38
	#define extra_PCAN_init_params\
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    39
		,getenv("PCANHwType") ? strtol(getenv("PCANHwType"),NULL,0):0\
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    40
		,getenv("PCANIO_Port") ? strtol(getenv("PCANIO_Port"),NULL,0):0\
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    41
		,getenv("PCANInterupt") ? strtol(getenv("PCANInterupt"),NULL,0):0
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    42
#endif
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    43
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    44
static s_BOARD *first_board = NULL;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    45
#ifdef PCAN2_HEADER_
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    46
static s_BOARD *second_board = NULL;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    47
#endif
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    48
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    49
//pthread_mutex_t PeakCan_mutex = PTHREAD_MUTEX_INITIALIZER;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    50
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    51
// Define for rtr CAN message
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    52
#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
    53
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    54
/***************************************************************************/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    55
int TranslateBaudeRate(char* optarg){
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    56
	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
    57
	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
    58
	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
    59
	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
    60
	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
    61
	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
    62
	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
    63
	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
    64
	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
    65
	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
    66
	return 0x0000;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    67
}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    68
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    69
void
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    70
canInit (s_BOARD *board)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    71
{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    72
	int baudrate;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    73
	
329
7717252e3ed9 Changes to compile for win32
greg
parents: 301
diff changeset
    74
#ifdef PCAN2_HEADER_
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    75
	// if not the first handler
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    76
	if(second_board == (s_BOARD *)board)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    77
		if(baudrate = TranslateBaudeRate(board->baudrate))
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    78
			CAN2_Init (baudrate,
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    79
			  CAN_INIT_TYPE_ST extra_PCAN_init_params);
329
7717252e3ed9 Changes to compile for win32
greg
parents: 301
diff changeset
    80
#endif
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    81
	if(first_board == (s_BOARD *)board)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    82
		if(baudrate = TranslateBaudeRate(board->baudrate))
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    83
			CAN_Init (baudrate,
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    84
			  CAN_INIT_TYPE_ST extra_PCAN_init_params);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    85
}
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
/*********functions which permit to communicate with the board****************/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    88
UNS8
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    89
canReceive_driver (CAN_HANDLE fd0, Message * m)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    90
{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    91
	UNS8 data;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    92
	TPCANMsg peakMsg;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    93
	DWORD Res;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    94
	do{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    95
		// We read the queue looking for messages.
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    96
		// 
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    97
		//pthread_mutex_lock (&PeakCan_mutex);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    98
#ifdef PCAN2_HEADER_
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
    99
		// if not the first handler
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   100
		if(second_board == (s_BOARD *)fd0)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   101
			Res = CAN2_Read (&peakMsg);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   102
		else
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   103
#endif
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   104
		if(first_board == (s_BOARD *)fd0)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   105
			Res = CAN_Read (&peakMsg);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   106
		else
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   107
			Res = CAN_ERR_BUSOFF;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   108
		// A message was received
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   109
		// We process the message(s)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   110
		// 
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   111
		if (Res == CAN_ERR_OK)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   112
		{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   113
			// if something different that 11bit or rtr... problem
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   114
			if (peakMsg.MSGTYPE & ~(MSGTYPE_STANDARD | MSGTYPE_RTR))
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   115
			{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   116
				if (peakMsg.MSGTYPE == CAN_ERR_BUSOFF)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   117
				{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   118
					printf ("!!! Peak board read : re-init\n");
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   119
					canInit((s_BOARD*) fd0);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   120
					usleep (10000);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   121
				}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   122
	
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   123
				// If status, return status if 29bit, return overrun
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   124
				//pthread_mutex_unlock (&PeakCan_mutex);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   125
				return peakMsg.MSGTYPE ==
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   126
					MSGTYPE_STATUS ? peakMsg.DATA[2] : CAN_ERR_OVERRUN;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   127
			}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   128
			m->cob_id.w = peakMsg.ID;
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   129
			
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   130
			if (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
   131
				m->rtr = 0;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   132
			else
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   133
				m->rtr = 1;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   134
			m->len = peakMsg.LEN;	/* 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
   135
			for (data = 0; data < peakMsg.LEN; data++)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   136
				m->data[data] = peakMsg.DATA[data];	/* data bytes, up to 8 */
341
7ff01f109bbc Windows related enhancements
etisserant
parents: 329
diff changeset
   137
#if defined DEBUG_MSG_CONSOLE_ON
7ff01f109bbc Windows related enhancements
etisserant
parents: 329
diff changeset
   138
			printf("in : ");
7ff01f109bbc Windows related enhancements
etisserant
parents: 329
diff changeset
   139
			print_message(m);
7ff01f109bbc Windows related enhancements
etisserant
parents: 329
diff changeset
   140
#endif
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   141
		}else{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   142
		//pthread_mutex_unlock (&PeakCan_mutex);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   143
		//if (Res != CAN_ERR_OK)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   144
		//{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   145
			if (!
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   146
				(Res & CAN_ERR_QRCVEMPTY || Res & CAN_ERR_BUSLIGHT
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   147
				 || Res & CAN_ERR_BUSHEAVY))
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   148
			{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   149
				printf ("canReceive returned error (%d)\n", Res);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   150
				return 1;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   151
			}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   152
			usleep (1000);		
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   153
		}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   154
	}while(Res != CAN_ERR_OK);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   155
	return 0;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   156
}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   157
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   158
/***************************************************************************/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   159
UNS8
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   160
canSend_driver (CAN_HANDLE fd0, Message * m)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   161
{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   162
	UNS8 data;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   163
	TPCANMsg peakMsg;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   164
	peakMsg.ID = m->cob_id.w;	/* 11/29 bit code */
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   165
	if (m->rtr == 0)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   166
		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
   167
	else
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   168
	{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   169
		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
   170
	}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   171
	peakMsg.LEN = m->len;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   172
	/* 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
   173
	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
   174
		peakMsg.DATA[data] = m->data[data];	/* data bytes, up to 8 */
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   175
	
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   176
	do
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   177
	{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   178
#ifdef PCAN2_HEADER_
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   179
		// if not the first handler
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   180
		if(second_board == (s_BOARD *)fd0)
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   181
		{
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   182
			errno = CAN2_Write (&peakMsg);
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   183
		}
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   184
		else 
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   185
#endif
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   186
		if(first_board == (s_BOARD *)fd0)
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   187
			{
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   188
				errno = CAN_Write (&peakMsg);
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   189
			}
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   190
		else 
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   191
			goto fail;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   192
		if (errno)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   193
		{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   194
			if (errno == CAN_ERR_BUSOFF)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   195
			{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   196
				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
   197
				canInit((s_BOARD*)fd0);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   198
				usleep (10000);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   199
			}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   200
			usleep (1000);
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   201
		}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   202
	}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   203
	while (errno != CAN_ERR_OK);
341
7ff01f109bbc Windows related enhancements
etisserant
parents: 329
diff changeset
   204
#if defined DEBUG_MSG_CONSOLE_ON
7ff01f109bbc Windows related enhancements
etisserant
parents: 329
diff changeset
   205
	printf("out : ");
7ff01f109bbc Windows related enhancements
etisserant
parents: 329
diff changeset
   206
	print_message(m);
7ff01f109bbc Windows related enhancements
etisserant
parents: 329
diff changeset
   207
#endif
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   208
	return 0;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   209
fail:
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   210
	return 1;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   211
}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   212
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   213
/***************************************************************************/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   214
CAN_HANDLE
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   215
canOpen_driver (s_BOARD * board)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   216
{
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   217
  char busname[64];
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   218
  char* pEnd;
329
7717252e3ed9 Changes to compile for win32
greg
parents: 301
diff changeset
   219
	
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   220
  //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
   221
  if (strtol(board->busname, &pEnd,0) == 0)
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   222
  {
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   223
      first_board = board;
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   224
      printf ("First Board selected\n");
329
7717252e3ed9 Changes to compile for win32
greg
parents: 301
diff changeset
   225
      canInit(board);	
7717252e3ed9 Changes to compile for win32
greg
parents: 301
diff changeset
   226
      return (CAN_HANDLE)board;
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   227
  }
329
7717252e3ed9 Changes to compile for win32
greg
parents: 301
diff changeset
   228
  #ifdef PCAN2_HEADER_
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   229
  if (strtol(board->busname, &pEnd,0) == 1)
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   230
  {
329
7717252e3ed9 Changes to compile for win32
greg
parents: 301
diff changeset
   231
      second_board = board;
7717252e3ed9 Changes to compile for win32
greg
parents: 301
diff changeset
   232
      printf ("Second Board selected\n");
7717252e3ed9 Changes to compile for win32
greg
parents: 301
diff changeset
   233
      canInit(board);	
7717252e3ed9 Changes to compile for win32
greg
parents: 301
diff changeset
   234
      return (CAN_HANDLE)board;
267
96c688ebcde7 Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents: 145
diff changeset
   235
  }
329
7717252e3ed9 Changes to compile for win32
greg
parents: 301
diff changeset
   236
  #endif
7717252e3ed9 Changes to compile for win32
greg
parents: 301
diff changeset
   237
  return NULL;
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   238
}
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
/***************************************************************************/
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   241
int
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   242
canClose_driver (CAN_HANDLE fd0)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   243
{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   244
#ifdef PCAN2_HEADER_
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   245
	// if not the first handler
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   246
	if(second_board == (s_BOARD *)fd0)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   247
	{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   248
		CAN2_Close ();
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   249
		second_board = (s_BOARD *)NULL;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   250
	}else	
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   251
#endif
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   252
	if(first_board == (s_BOARD *)fd0)
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   253
	{
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   254
		CAN_Close ();
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   255
		first_board = (s_BOARD *)NULL;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   256
	}
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   257
	return 0;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 42
diff changeset
   258
}