author | 'Laurent Bessard <laurent.bessard@lolitech.fr>' |
Fri, 07 Aug 2009 15:59:35 +0200 | |
changeset 583 | a2c74eca6bf2 |
parent 574 | e5d7f4412a4c |
child 598 | fff217764431 |
permissions | -rwxr-xr-x |
38 | 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 |
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 | 21 |
*/ |
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 |
574
e5d7f4412a4c
Add print_getenv function wich print extra init params
greg
parents:
570
diff
changeset
|
38 |
long int print_getenv(const char* pcanparam) |
e5d7f4412a4c
Add print_getenv function wich print extra init params
greg
parents:
570
diff
changeset
|
39 |
{ |
e5d7f4412a4c
Add print_getenv function wich print extra init params
greg
parents:
570
diff
changeset
|
40 |
char* param=NULL; |
e5d7f4412a4c
Add print_getenv function wich print extra init params
greg
parents:
570
diff
changeset
|
41 |
long int res=0; |
e5d7f4412a4c
Add print_getenv function wich print extra init params
greg
parents:
570
diff
changeset
|
42 |
|
e5d7f4412a4c
Add print_getenv function wich print extra init params
greg
parents:
570
diff
changeset
|
43 |
param = getenv(pcanparam); |
e5d7f4412a4c
Add print_getenv function wich print extra init params
greg
parents:
570
diff
changeset
|
44 |
if(param != NULL){ |
e5d7f4412a4c
Add print_getenv function wich print extra init params
greg
parents:
570
diff
changeset
|
45 |
res = strtol(param,NULL,0); |
e5d7f4412a4c
Add print_getenv function wich print extra init params
greg
parents:
570
diff
changeset
|
46 |
} |
e5d7f4412a4c
Add print_getenv function wich print extra init params
greg
parents:
570
diff
changeset
|
47 |
else |
e5d7f4412a4c
Add print_getenv function wich print extra init params
greg
parents:
570
diff
changeset
|
48 |
printf("Environment variable %s not defined !\n", pcanparam); |
e5d7f4412a4c
Add print_getenv function wich print extra init params
greg
parents:
570
diff
changeset
|
49 |
printf("Found environment variable %s : %ld\n", pcanparam ,res); |
e5d7f4412a4c
Add print_getenv function wich print extra init params
greg
parents:
570
diff
changeset
|
50 |
return res; |
e5d7f4412a4c
Add print_getenv function wich print extra init params
greg
parents:
570
diff
changeset
|
51 |
} |
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\ |
574
e5d7f4412a4c
Add print_getenv function wich print extra init params
greg
parents:
570
diff
changeset
|
53 |
,print_getenv("PCANHwType")\ |
e5d7f4412a4c
Add print_getenv function wich print extra init params
greg
parents:
570
diff
changeset
|
54 |
,print_getenv("PCANIO_Port")\ |
e5d7f4412a4c
Add print_getenv function wich print extra init params
greg
parents:
570
diff
changeset
|
55 |
,print_getenv("PCANInterupt") |
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
56 |
#endif |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
57 |
|
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
58 |
static s_BOARD *first_board = NULL; |
563
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
59 |
|
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
60 |
//Create the Event for the first board |
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
61 |
HANDLE hEvent1 = NULL; |
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
62 |
|
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
63 |
|
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
64 |
#ifdef PCAN2_HEADER_ |
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
65 |
static s_BOARD *second_board = NULL; |
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
66 |
HANDLE hEvent2 = NULL; |
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
67 |
#endif |
145
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 |
// Define for rtr CAN message |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
70 |
#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
|
71 |
|
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
72 |
/***************************************************************************/ |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
73 |
int TranslateBaudeRate(char* optarg){ |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
74 |
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
|
75 |
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
|
76 |
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
|
77 |
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
|
78 |
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
|
79 |
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
|
80 |
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
|
81 |
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
|
82 |
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
|
83 |
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
|
84 |
return 0x0000; |
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 |
|
566
365d555fc0c6
fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents:
563
diff
changeset
|
87 |
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
|
88 |
{ |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
89 |
int baudrate; |
566
365d555fc0c6
fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents:
563
diff
changeset
|
90 |
int ret = 0; |
563
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
91 |
|
329 | 92 |
#ifdef PCAN2_HEADER_ |
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
93 |
// if not the first handler |
563
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
94 |
if(second_board == (s_BOARD *)board) { |
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
95 |
if(baudrate = TranslateBaudeRate(board->baudrate)) |
566
365d555fc0c6
fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents:
563
diff
changeset
|
96 |
{ |
365d555fc0c6
fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents:
563
diff
changeset
|
97 |
ret = CAN2_Init(baudrate, CAN_INIT_TYPE_ST extra_PCAN_init_params); |
365d555fc0c6
fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents:
563
diff
changeset
|
98 |
if(ret != CAN_ERR_OK) |
365d555fc0c6
fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents:
563
diff
changeset
|
99 |
return 0; |
365d555fc0c6
fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents:
563
diff
changeset
|
100 |
} |
563
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
101 |
|
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
102 |
//Create the Event for the first board |
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
103 |
hEvent2 = CreateEvent(NULL, // lpEventAttributes |
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
104 |
FALSE, // bManualReset |
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
105 |
FALSE, // bInitialState |
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
106 |
""); // lpName |
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
107 |
|
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
108 |
//Set Event Handle for CANReadExt |
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
109 |
CAN2_SetRcvEvent(hEvent2); |
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
110 |
} |
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
111 |
else |
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
112 |
#endif |
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
113 |
if(first_board == (s_BOARD *)board) { |
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
114 |
if(baudrate = TranslateBaudeRate(board->baudrate)) |
566
365d555fc0c6
fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents:
563
diff
changeset
|
115 |
{ |
365d555fc0c6
fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents:
563
diff
changeset
|
116 |
ret = CAN_Init(baudrate, CAN_INIT_TYPE_ST extra_PCAN_init_params); |
365d555fc0c6
fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents:
563
diff
changeset
|
117 |
if(ret != CAN_ERR_OK) |
365d555fc0c6
fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents:
563
diff
changeset
|
118 |
return 0; |
365d555fc0c6
fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents:
563
diff
changeset
|
119 |
} |
365d555fc0c6
fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents:
563
diff
changeset
|
120 |
//Create the Event for the first board |
563
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
121 |
hEvent1 = CreateEvent(NULL, // lpEventAttributes |
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
122 |
FALSE, // bManualReset |
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
123 |
FALSE, // bInitialState |
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
124 |
""); // lpName |
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
125 |
//Set Event Handle for CANReadExt |
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
126 |
CAN_SetRcvEvent(hEvent1); |
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
127 |
} |
566
365d555fc0c6
fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents:
563
diff
changeset
|
128 |
return 1; |
563
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
129 |
} |
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
130 |
|
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
131 |
/********* functions which permit to communicate with the board ****************/ |
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
132 |
UNS8 canReceive_driver (CAN_HANDLE fd0, Message * m) |
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
133 |
{ |
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
134 |
int ret=0; |
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
135 |
UNS8 data; |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
136 |
TPCANMsg peakMsg; |
563
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
137 |
TPCANTimestamp peakRcvTime; |
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
138 |
DWORD Res; |
563
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
139 |
DWORD result; |
570 | 140 |
// loop until valid message or fatal error |
141 |
do{ |
|
142 |
#ifdef PCAN2_HEADER_ |
|
143 |
// if not the first handler |
|
144 |
if(second_board == (s_BOARD *)fd0) { |
|
145 |
//wait for CAN msg... |
|
146 |
result = WaitForSingleObject(hEvent2, INFINITE); |
|
147 |
if (result == WAIT_OBJECT_0) |
|
148 |
Res = CAN2_ReadEx(&peakMsg, &peakRcvTime); |
|
149 |
// Exit receive thread when handle is no more valid |
|
150 |
if(Res & CAN_ERRMASK_ILLHANDLE) |
|
151 |
return 1; |
|
152 |
} |
|
153 |
else |
|
154 |
#endif |
|
155 |
||
156 |
// We read the queue looking for messages. |
|
157 |
if(first_board == (s_BOARD *)fd0) { |
|
158 |
result = WaitForSingleObject(hEvent1, INFINITE); |
|
159 |
if (result == WAIT_OBJECT_0) |
|
160 |
{ |
|
161 |
Res = CAN_ReadEx(&peakMsg, &peakRcvTime); |
|
162 |
// Exit receive thread when handle is no more valid |
|
163 |
if(Res & CAN_ERRMASK_ILLHANDLE) |
|
164 |
return 1; |
|
165 |
} |
|
166 |
} |
|
167 |
else |
|
168 |
Res = CAN_ERR_BUSOFF; |
|
169 |
||
170 |
// A message was received : we process the message(s) |
|
171 |
if (Res == CAN_ERR_OK) |
|
172 |
{ |
|
173 |
// if something different that 11bit or rtr... problem |
|
174 |
if (peakMsg.MSGTYPE & ~(MSGTYPE_STANDARD | MSGTYPE_RTR)) |
|
175 |
{ |
|
176 |
if (peakMsg.MSGTYPE == CAN_ERR_BUSOFF) |
|
177 |
{ |
|
178 |
printf ("!!! Peak board read : re-init\n"); |
|
179 |
canInit((s_BOARD*) fd0); |
|
180 |
usleep (10000); |
|
181 |
} |
|
182 |
||
183 |
// If status, return status if 29bit, return overrun |
|
184 |
return peakMsg.MSGTYPE == |
|
185 |
MSGTYPE_STATUS ? peakMsg.DATA[2] : CAN_ERR_OVERRUN; |
|
186 |
} |
|
187 |
m->cob_id = peakMsg.ID; |
|
188 |
||
189 |
if (peakMsg.MSGTYPE == CAN_INIT_TYPE_ST) /* bits of MSGTYPE_ */ |
|
190 |
m->rtr = 0; |
|
191 |
else |
|
192 |
m->rtr = 1; |
|
193 |
m->len = peakMsg.LEN; /* count of data bytes (0..8) */ |
|
194 |
for (data = 0; data < peakMsg.LEN; data++) |
|
195 |
m->data[data] = peakMsg.DATA[data]; /* data bytes, up to 8 */ |
|
196 |
#if defined DEBUG_MSG_CONSOLE_ON |
|
197 |
MSG("in : "); |
|
198 |
print_message(m); |
|
199 |
#endif |
|
200 |
} |
|
201 |
else |
|
202 |
{ |
|
203 |
// not benign error => fatal error |
|
204 |
if (!(Res & CAN_ERR_QRCVEMPTY |
|
205 |
|| Res & CAN_ERR_BUSLIGHT |
|
206 |
|| Res & CAN_ERR_BUSHEAVY)) |
|
207 |
{ |
|
208 |
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
|
209 |
return 1; |
365d555fc0c6
fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents:
563
diff
changeset
|
210 |
} |
570 | 211 |
} |
212 |
}while(Res != CAN_ERR_OK); |
|
563
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
213 |
return 0; |
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
214 |
} |
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
215 |
|
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
216 |
/***************************************************************************/ |
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
217 |
UNS8 canSend_driver (CAN_HANDLE fd0, Message * m) |
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
218 |
{ |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
219 |
UNS8 data; |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
220 |
TPCANMsg peakMsg; |
365 | 221 |
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
|
222 |
if (m->rtr == 0) |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
223 |
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
|
224 |
else |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
225 |
{ |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
226 |
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
|
227 |
} |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
228 |
peakMsg.LEN = m->len; |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
229 |
/* 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
|
230 |
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
|
231 |
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
|
232 |
|
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
233 |
do |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
234 |
{ |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
235 |
#ifdef PCAN2_HEADER_ |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
236 |
// if not the first handler |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
237 |
if(second_board == (s_BOARD *)fd0) |
267
96c688ebcde7
Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
145
diff
changeset
|
238 |
{ |
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
239 |
errno = CAN2_Write (&peakMsg); |
267
96c688ebcde7
Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
145
diff
changeset
|
240 |
} |
563
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
241 |
else |
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
242 |
#endif |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
243 |
if(first_board == (s_BOARD *)fd0) |
267
96c688ebcde7
Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
145
diff
changeset
|
244 |
{ |
96c688ebcde7
Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
145
diff
changeset
|
245 |
errno = CAN_Write (&peakMsg); |
96c688ebcde7
Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
145
diff
changeset
|
246 |
} |
563
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
247 |
else |
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
248 |
goto fail; |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
249 |
if (errno) |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
250 |
{ |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
251 |
if (errno == CAN_ERR_BUSOFF) |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
252 |
{ |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
253 |
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
|
254 |
canInit((s_BOARD*)fd0); |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
255 |
usleep (10000); |
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 |
usleep (1000); |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
258 |
} |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
259 |
} |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
260 |
while (errno != CAN_ERR_OK); |
341 | 261 |
#if defined DEBUG_MSG_CONSOLE_ON |
454 | 262 |
MSG("out : "); |
341 | 263 |
print_message(m); |
264 |
#endif |
|
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
265 |
return 0; |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
266 |
fail: |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
267 |
return 1; |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
268 |
} |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
269 |
|
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
270 |
/***************************************************************************/ |
384 | 271 |
UNS8 canChangeBaudRate_driver( CAN_HANDLE fd, char* baud) |
272 |
{ |
|
273 |
printf("canChangeBaudRate not yet supported by this driver\n"); |
|
274 |
return 0; |
|
275 |
} |
|
276 |
||
277 |
/***************************************************************************/ |
|
563
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
278 |
CAN_HANDLE canOpen_driver (s_BOARD * board) |
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
279 |
{ |
267
96c688ebcde7
Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
145
diff
changeset
|
280 |
char busname[64]; |
96c688ebcde7
Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
145
diff
changeset
|
281 |
char* pEnd; |
566
365d555fc0c6
fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents:
563
diff
changeset
|
282 |
int ret; |
563
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
283 |
|
267
96c688ebcde7
Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
145
diff
changeset
|
284 |
//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
|
285 |
if (strtol(board->busname, &pEnd,0) == 0) |
96c688ebcde7
Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
145
diff
changeset
|
286 |
{ |
96c688ebcde7
Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
145
diff
changeset
|
287 |
first_board = board; |
566
365d555fc0c6
fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents:
563
diff
changeset
|
288 |
//printf ("First Board selected\n"); |
365d555fc0c6
fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents:
563
diff
changeset
|
289 |
ret = canInit(board); |
365d555fc0c6
fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents:
563
diff
changeset
|
290 |
if(ret) |
365d555fc0c6
fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents:
563
diff
changeset
|
291 |
return (CAN_HANDLE)board; |
267
96c688ebcde7
Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
145
diff
changeset
|
292 |
} |
329 | 293 |
#ifdef PCAN2_HEADER_ |
267
96c688ebcde7
Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
145
diff
changeset
|
294 |
if (strtol(board->busname, &pEnd,0) == 1) |
96c688ebcde7
Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
145
diff
changeset
|
295 |
{ |
329 | 296 |
second_board = board; |
566
365d555fc0c6
fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents:
563
diff
changeset
|
297 |
//printf ("Second Board selected\n"); |
365d555fc0c6
fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents:
563
diff
changeset
|
298 |
ret = canInit(board); |
365d555fc0c6
fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents:
563
diff
changeset
|
299 |
if(ret) |
365d555fc0c6
fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents:
563
diff
changeset
|
300 |
return (CAN_HANDLE)board; |
267
96c688ebcde7
Add win32 target (--can=win32) to compile with msys and mingw32
greg
parents:
145
diff
changeset
|
301 |
} |
329 | 302 |
#endif |
303 |
return NULL; |
|
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
304 |
} |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
305 |
|
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
306 |
/***************************************************************************/ |
563
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
307 |
int 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
|
308 |
{ |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
309 |
#ifdef PCAN2_HEADER_ |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
310 |
// if not the first handler |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
311 |
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
|
312 |
{ |
570 | 313 |
CAN2_SetRcvEvent(NULL); |
314 |
CAN2_Close (); |
|
566
365d555fc0c6
fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents:
563
diff
changeset
|
315 |
SetEvent(hEvent2); |
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
316 |
second_board = (s_BOARD *)NULL; |
563
394440e3b6e4
update canReceive_driver to match with Peak new read function (event)
greg
parents:
454
diff
changeset
|
317 |
}else |
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
318 |
#endif |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
319 |
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
|
320 |
{ |
570 | 321 |
CAN_SetRcvEvent(NULL); |
322 |
CAN_Close (); |
|
566
365d555fc0c6
fixed : unlock and terminate CanReceive loop thread when canclose is called.
greg
parents:
563
diff
changeset
|
323 |
SetEvent(hEvent1); |
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
324 |
first_board = (s_BOARD *)NULL; |
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 |
return 0; |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
42
diff
changeset
|
327 |
} |