-
Notifications
You must be signed in to change notification settings - Fork 0
/
libpcan.h
232 lines (201 loc) · 9.27 KB
/
libpcan.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#ifndef __LIBPCAN_H__
#define __LIBPCAN_H__
//****************************************************************************
// Copyright (C) 2001-2007 PEAK System-Technik GmbH
//
// linux@peak-system.com
// www.peak-system.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Maintainer(s): Klaus Hitschler (klaus.hitschler@gmx.de)
// Contributions: Mudiaga Obada (obada@vsi.cs.uni-frankfurt.de)
//****************************************************************************
//****************************************************************************
//
// libpcan.h
// common header to access the functions within pcanlib.so.x.x,
// originally created from Wilhelm Hoppe in pcan_pci.h
//
// $Id: libpcan.h 455 2007-02-11 22:19:11Z khitschler $
//
//****************************************************************************
//****************************************************************************
// INCLUDES
#include "pcan.h"
//****************************************************************************
// compatibilty defines
#if defined(LPSTR) || defined(HANDLE)
#error "double define for LPSTR, HANDLE found"
#endif
#define LPSTR char *
#define HANDLE void *
//****************************************************************************
// for CAN_Open(...)
//****************************************************************************
// for CAN_Init(...)
// parameter wBTR0BTR1
// bitrate codes of BTR0/BTR1 registers
#define CAN_BAUD_1M 0x0014 // 1 MBit/s
#define CAN_BAUD_500K 0x001C // 500 kBit/s
#define CAN_BAUD_250K 0x011C // 250 kBit/s
#define CAN_BAUD_125K 0x031C // 125 kBit/s
#define CAN_BAUD_100K 0x432F // 100 kBit/s
#define CAN_BAUD_50K 0x472F // 50 kBit/s
#define CAN_BAUD_20K 0x532F // 20 kBit/s
#define CAN_BAUD_10K 0x672F // 10 kBit/s
#define CAN_BAUD_5K 0x7F7F // 5 kBit/s
// parameter nCANMsgType
#define CAN_INIT_TYPE_EX 0x01 //Extended Frame
#define CAN_INIT_TYPE_ST 0x00 //Standart Frame
//****************************************************************************
// error codes are defined in pcan.h
#define CAN_ERR_ANYBUSERR (CAN_ERR_BUSLIGHT | CAN_ERR_BUSHEAVY | CAN_ERR_BUSOFF)
//****************************************************************************
// PROTOTYPES
#ifdef __cplusplus
extern "C"
{
#endif
//****************************************************************************
// CAN_Open()
// creates a path to a CAN port
//
// for PCAN-Dongle call: CAN_Open(HW_DONGLE_.., DWORD dwPort, WORD wIrq);
// for PCAN-ISA or PCAN-PC/104 call: CAN_Open(HW_ISA_SJA, DWORD dwPort, WORD wIrq);
// for PCAN-PCI call: CAN_Open(HW_PCI, int nPort); .. enumerate nPort 1..8.
//
// if ((dwPort == 0) && (wIrq == 0)) CAN_Open() takes the 1st default ISA or DONGLE port.
// if (nPort == 0) CAN_Open() takes the 1st default PCI port.
// returns NULL when open failes.
//
// The first CAN_Open() to a CAN hardware initializes the hardware to default
// parameter 500 kbit/sec and acceptance of extended frames.
//
HANDLE CAN_Open(WORD wHardwareType, ...);
//****************************************************************************
// CAN_Init()
// initializes the CAN hardware with the BTR0 + BTR1 constant "CAN_BAUD_...".
// nCANMsgType must be filled with "CAN_INIT_TYPE_..".
// The default initialisation, e.g. CAN_Init is not called,
// is 500 kbit/sec and extended frames.
//
DWORD CAN_Init(HANDLE hHandle, WORD wBTR0BTR1, int nCANMsgType);
//****************************************************************************
// CAN_Close()
// closes the path to the CAN hardware.
// The last close on the hardware put the chip into passive state.
DWORD CAN_Close(HANDLE hHandle);
//****************************************************************************
// CAN_Status()
// request the current (stored) status of the CAN hardware. After the read the
// stored status is reset.
// If the status is negative a system error is returned (e.g. -EBADF).
DWORD CAN_Status(HANDLE hHandle);
//****************************************************************************
// CAN_Write()
// writes a message to the CAN bus. If the write queue is full the current
// write blocks until either a message is sent or a error occured.
//
DWORD CAN_Write(HANDLE hHandle, TPCANMsg* pMsgBuff);
//****************************************************************************
// LINUX_CAN_Write_Timeout()
// writes a message to the CAN bus. If the (software) message buffer is full
// the current write request blocks until a write slot gets empty
// or a timeout or a error occures.
// nMicroSeconds > 0 -> Timeout in microseconds
// nMicroSeconds == 0 -> polling
// nMicroSeconds < 0 -> blocking, same as CAN_Write()
DWORD LINUX_CAN_Write_Timeout(HANDLE hHandle, TPCANMsg* pMsgBuff, int nMicroSeconds);
//****************************************************************************
// CAN_Read()
// reads a message from the CAN bus. If there is no message to read the current
// request blocks until either a new message arrives or a error occures.
DWORD CAN_Read(HANDLE hHandle, TPCANMsg* pMsgBuff);
//****************************************************************************
// LINUX_CAN_Read()
// reads a message WITH TIMESTAMP from the CAN bus. If there is no message
// to read the current request blocks until either a new message arrives
// or a error occures.
DWORD LINUX_CAN_Read(HANDLE hHandle, TPCANRdMsg* pMsgBuff);
//****************************************************************************
// LINUX_CAN_Read_Timeout()
// reads a message WITH TIMESTAMP from the CAN bus. If there is no message
// to read the current request blocks until either a new message arrives
// or a timeout or a error occures.
// nMicroSeconds > 0 -> Timeout in microseconds
// nMicroSeconds == 0 -> polling
// nMicroSeconds < 0 -> blocking, same as LINUX_CAN_Read()
DWORD LINUX_CAN_Read_Timeout(HANDLE hHandle, TPCANRdMsg* pMsgBuff, int nMicroSeconds);
//***************************************************************************
// CAN_ResetFilter() - removes all current Message Filters
// Caution! Currently this operation influences all read paths
//
DWORD CAN_ResetFilter(HANDLE hHandle);
//***************************************************************************
// CAN_MsgFilter() - reduce received data in to FromID <= ID <= ToID
// Type may be MSGTYPE_STANDARD or MSGTYPE_EXTENDED
// This function can be called multiple to add more ranges.
// Caution! Currently this operation influences all read paths
//
DWORD CAN_MsgFilter(HANDLE hHandle, DWORD FromID, DWORD ToID, int nCANMsgType);
//***************************************************************************
// LINUX_CAN_FileHandle() - return PCAN driver file handle for select(2)
//
int LINUX_CAN_FileHandle(HANDLE hHandle);
//****************************************************************************
// LINUX_CAN_Extended_Status()
// get the same as CAN_Status() with additional informaton about pending reads or writes
//
// There is a uncertainty of 1 message for "nPendingWrites" for a small amount
// of time between the messages is put into the CAN sender and the telegram is
// successfuly sent or an error is thrown.
DWORD LINUX_CAN_Extended_Status(HANDLE hHandle, int *nPendingReads, int *nPendingWrites);
//****************************************************************************
// CAN_VersionInfo()
// returns a text string with driver version info.
//
DWORD CAN_VersionInfo(HANDLE hHandle, LPSTR lpszTextBuff);
//****************************************************************************
// nGetLastError()
// returns the last stored error (errno of the shared library). The returend
// error is independend of any path.
//
int nGetLastError(void);
//****************************************************************************
// LINUX_CAN_Open() - another open, LINUX like
// creates a path to a CAN port
//
// input: the path to the device node (e.g. /dev/pcan0)
// returns NULL when open failes
//
HANDLE LINUX_CAN_Open(const char *szDeviceName, int nFlag);
//****************************************************************************
// LINUX_CAN_Statistics() - get statistics about this devices
//
DWORD LINUX_CAN_Statistics(HANDLE hHandle, TPDIAG *diag);
//****************************************************************************
// LINUX_CAN_BTR0BTR1() - get the BTR0 and BTR1 from bitrate, LINUX like
//
// input: the handle to the device node
// the bitrate in bits / second, e.g. 500000 bits/sec
//
// returns 0 if not possible
// BTR0BTR1 for the interface
//
WORD LINUX_CAN_BTR0BTR1(HANDLE hHandle, DWORD dwBitRate);
#ifdef __cplusplus
}
#endif
#endif // __LIBPCAN_H__