Skip to content

Commit

Permalink
Merge pull request #21 from mac-can/development
Browse files Browse the repository at this point in the history
Release Candidate 3 for Version 0.3
  • Loading branch information
mac-can authored Aug 2, 2024
2 parents 999a737 + f7dce4d commit 93c8eff
Show file tree
Hide file tree
Showing 14 changed files with 606 additions and 221 deletions.
93 changes: 89 additions & 4 deletions Examples/Python/CANAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
Interface API for various CAN interfaces from different
vendors running under multiple operating systems.
$Author: makemake $
$Author: quaoar $
$Rev: 1274 $
$Rev: 1278 $
"""
from ctypes import *
import platform
Expand All @@ -69,7 +69,7 @@

# CAN API V3 - Python Wrapper
#
CAN_API_V3_PYTHON = {'major': 0, 'minor': 2, 'patch': 2}
CAN_API_V3_PYTHON = {'major': 0, 'minor': 3, 'patch': 1}

# CAN Identifier Ranges
#
Expand Down Expand Up @@ -122,6 +122,13 @@
(49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64): c_uint8(0xF)
}

# CAN Acceptance Filter: (code ^ id) & mask == 0
#
CANACC_CODE_11BIT = 0x000 # mask for 11-bit acceptance code
CANACC_MASK_11BIT = 0x000 # mask for 11-bit acceptance mask
CANACC_CODE_29BIT = 0x00000000 # mask for 29-bit acceptance code
CANACC_MASK_29BIT = 0x00000000 # mask for 29-bit acceptance mask

# CAN 2.0 Predefined Bit-rates (as index acc. CiA)
#
CANBTR_INDEX_1M = c_int32(0) # bit-rate: 1000 kbit/s
Expand Down Expand Up @@ -638,6 +645,70 @@ def bitrate(self):
print('+++ exception: {}'.format(e))
raise

def filter11bit(self, code, mask):
"""
sets a 11-bit filter for the CAN controller.
:param code: 11-bit code for the filter (or None)
:param mask: 11-bit mask for the filter (or None)
:return: result, code, mask
result: 0 if successful, or a negative value on error
code: 11-bit code for the filter
mask: 11-bit mask for the filter
"""
try:
# set the 11-bit filter (if code or mask are not None)
if code is not None or mask is not None:
__filter = c_uint64(0)
if code is not None:
__filter.value = code << 32
if mask is not None:
__filter.value |= mask & 0xFFFFFFFF
result = self.__m_library.can_property(self.__m_handle, 42, byref(__filter), 8)
if result < 0:
return int(result), None, None
# get the 11-bit filter
__value = c_uint64(0)
result = self.__m_library.can_property(self.__m_handle, 40, byref(__value), 8)
if result < 0:
return int(result), None, None
return int(result), int(__filter.value >> 32), int(__filter.value & 0xFFFFFFFF)
except Exception as e:
print('+++ exception: {}'.format(e))
raise

def filter29bit(self, code, mask):
"""
sets a 29-bit filter for the CAN controller.
:param code: 29-bit code for the filter (or None)
:param mask: 29-bit mask for the filter (or None)
:return: result, code, mask
result: 0 if successful, or a negative value on error
code: 29-bit code for the filter
mask: 29-bit mask for the filter
"""
try:
# set the 29-bit filter (if code or mask are not None)
if code is not None or mask is not None:
__filter = c_uint64(0)
if code is not None:
__filter.value = code << 32
if mask is not None:
__filter.value |= mask & 0xFFFFFFFF
result = self.__m_library.can_property(self.__m_handle, 43, byref(__filter), 8)
if result < 0:
return int(result), None, None
# get the 29-bit filter
__value = c_uint64(0)
result = self.__m_library.can_property(self.__m_handle, 41, byref(__value), 8)
if result < 0:
return int(result), None, None
return int(result), int(__filter.value >> 32), int(__filter.value & 0xFFFFFFFF)
except Exception as e:
print('+++ exception: {}'.format(e))
raise

def hardware(self):
"""
retrieves the hardware version of the CAN controller
Expand Down Expand Up @@ -780,6 +851,20 @@ def len2dlc(length):
else:
print('>>> can.status() >>> 0x{:02X}'.format(status.byte))

# set acceptance filter
code = CANACC_CODE_11BIT
mask = CANACC_MASK_11BIT
print('>>> can.filter11bit(0x{:03X}, 0x{:03X})'.format(code, mask))
res, code, mask = can.filter11bit(code=code, mask=mask)
if res < CANERR_NOERROR:
print('+++ error: can.filter11bit returned {}'.format(res))
code = CANACC_CODE_29BIT
mask = CANACC_MASK_29BIT
print('>>> can.filter29bit(0x{:08X}, 0x{:08X})'.format(code, mask))
res, code, mask = can.filter29bit(code=code, mask=mask)
if res < CANERR_NOERROR:
print('+++ error: can.filter29bit returned {}'.format(res))

# start the CAN controller
if bitRate.index > 0: # FIXME: Expected type 'int', got 'c_int32[int]' instead
print('>>> can.start([{},[{},{},{},{},{}],[{},{},{},{},])'.format(bitRate.btr.frequency,
Expand Down Expand Up @@ -831,5 +916,5 @@ def len2dlc(length):
# have a great time
print('Bye, bye!')

# * $Id: CANAPI.py 1274 2024-04-21 17:34:21Z makemake $ *** (c) UV Software, Berlin ***
# * $Id: CANAPI.py 1278 2024-04-23 08:34:36Z quaoar $ *** (c) UV Software, Berlin ***
#
22 changes: 18 additions & 4 deletions Examples/Python/can_recv.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
#
def sigterm(signo, frame):
print()
# print('>>> can.kill()')
# result = can.kill()
# if result < 0:
# print('+++ error: can.kill returned {}'.format(result))
print('>>> can.kill()')
result = can.kill()
if result < 0:
print('+++ error: can.kill returned {}'.format(result))
print('>>> can.exit()')
result = can.exit()
if result < 0:
Expand Down Expand Up @@ -69,6 +69,20 @@ def sigterm(signo, frame):
else:
print('>>> can.status() >>> 0x{:02X}'.format(status.byte))

# set acceptance filter
code = CANACC_CODE_11BIT
mask = CANACC_MASK_11BIT
print('>>> can.filter11bit(0x{:03X}, 0x{:03X})'.format(code, mask))
res, code, mask = can.filter11bit(code=code, mask=mask)
if res < CANERR_NOERROR:
print('+++ error: can.filter11bit returned {}'.format(res))
code = CANACC_CODE_29BIT
mask = CANACC_MASK_29BIT
print('>>> can.filter29bit(0x{:08X}, 0x{:08X})'.format(code, mask))
res, code, mask = can.filter29bit(code=code, mask=mask)
if res < CANERR_NOERROR:
print('+++ error: can.filter29bit returned {}'.format(res))

# start the CAN controller
if int(bitRate.index) > 0: # FIXME: Expected type 'int', got 'c_int32[int]' instead
print('>>> can.start([{},[{},{},{},{},{}],[{},{},{},{},])'.format(bitRate.btr.frequency,
Expand Down
8 changes: 4 additions & 4 deletions Examples/Python/can_send.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
#
def sigterm(signo, frame):
print()
# print('>>> can.kill()')
# result = can.kill()
# if result < 0:
# print('+++ error: can.kill returned {}'.format(result))
print('>>> can.kill()')
result = can.kill()
if result < 0:
print('+++ error: can.kill returned {}'.format(result))
print('>>> can.exit()')
result = can.exit()
if result < 0:
Expand Down
6 changes: 3 additions & 3 deletions Libraries/CANAPI/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ ifeq ($(current_OS),Linux) # linux - libuvcanpcb.so

LIBRARY = libuvcanpcb

SONAME = $(LIBRARY).so
SONAME = $(LIBRARY).so.1
TARGET = $(SONAME).$(VERSION)
STATIC = $(LIBRARY).a

Expand Down Expand Up @@ -225,8 +225,8 @@ ifeq ($(current_OS),Darwin)
$(RM) $(INSTALL)/$(LIBRARY).dylib ; $(LN) $(INSTALL)/$(TARGET) $(INSTALL)/$(LIBRARY).dylib
endif
ifeq ($(current_OS),Linux)
$(RM) $(INSTALL)/$(SONAME) ; ln -s $(INSTALL)/$(TARGET) $(INSTALL)/$(SONAME)
$(RM) $(INSTALL)/$(LIBRARY).so ; ln -s $(INSTALL)/$(SONAME) $(INSTALL)/$(LIBRARY).so
$(RM) $(INSTALL)/$(SONAME) ; $(LN) $(INSTALL)/$(TARGET) $(INSTALL)/$(SONAME)
$(RM) $(INSTALL)/$(LIBRARY).so ; $(LN) $(INSTALL)/$(SONAME) $(INSTALL)/$(LIBRARY).so
endif


Expand Down
6 changes: 3 additions & 3 deletions Libraries/PeakCAN/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ ifeq ($(current_OS),Linux) # Linux - libpeakcan.so

LIBRARY = libpeakcan

SONAME = $(LIBRARY).so
SONAME = $(LIBRARY).so.1
TARGET = $(SONAME).$(VERSION)
STATIC = $(LIBRARY).a

Expand Down Expand Up @@ -227,8 +227,8 @@ ifeq ($(current_OS),Darwin)
$(RM) $(INSTALL)/$(LIBRARY).dylib ; $(LN) $(INSTALL)/$(TARGET) $(INSTALL)/$(LIBRARY).dylib
endif
ifeq ($(current_OS),Linux)
$(RM) $(INSTALL)/$(SONAME) ; ln -s $(INSTALL)/$(TARGET) $(INSTALL)/$(SONAME)
$(RM) $(INSTALL)/$(LIBRARY).so ; ln -s $(INSTALL)/$(SONAME) $(INSTALL)/$(LIBRARY).so
$(RM) $(INSTALL)/$(SONAME) ; $(LN) $(INSTALL)/$(TARGET) $(INSTALL)/$(SONAME)
$(RM) $(INSTALL)/$(LIBRARY).so ; $(LN) $(INSTALL)/$(SONAME) $(INSTALL)/$(LIBRARY).so
endif


Expand Down
8 changes: 8 additions & 0 deletions Utilities/can_moni/Sources/Options.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ struct SOptions {
uint32_t m_u32Mask;
} m_StdFilter, m_XtdFilter;
char* m_szExcludeList;
#if (CAN_TRACE_SUPPORTED != 0)
enum {
eTraceOff,
eTraceBinary,
eTraceLogger,
eTraceVendor
} m_eTraceMode;
#endif
bool m_fListBitrates;
bool m_fListBoards;
bool m_fTestBoards;
Expand Down
55 changes: 46 additions & 9 deletions Utilities/can_moni/Sources/Options_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ static const char* c_szWarranty = CAN_MONI_WARRANTY;
static const char* c_szLicense = CAN_MONI_LICENSE;
static const char* c_szBasename = CAN_MONI_PROGRAM;
static const char* c_szInterface = "(unknown)";
static const char* c_szExcludeList = "~0x00-0x7FF";

SOptions::SOptions() {
// to have default bus speed from bit-timing index
Expand All @@ -77,12 +76,14 @@ SOptions::SOptions() {
m_StdFilter.m_u32Mask = CANACC_MASK_11BIT;
m_XtdFilter.m_u32Code = CANACC_CODE_29BIT;
m_XtdFilter.m_u32Mask = CANACC_MASK_29BIT;
m_szExcludeList = (char*)c_szExcludeList;
m_szExcludeList = (char*)NULL;
#if (CAN_TRACE_SUPPORTED != 0)
m_eTraceMode = SOptions::eTraceOff;
#endif
m_fListBitrates = false;
m_fListBoards = false;
m_fTestBoards = false;
m_fVerbose = false;
m_fVerbose = false;
m_fExit = false;
}

Expand Down Expand Up @@ -110,6 +111,9 @@ int SOptions::ScanCommanline(int argc, const char* argv[], FILE* err, FILE* out)
int optFmtWrap = 0;
#endif
int optExclude = 0;
#if (CAN_TRACE_SUPPORTED != 0)
int optTraceMode = 0;
#endif
int optListBitrates = 0;
int optListBoards = 0;
int optTestBoards = 0;
Expand Down Expand Up @@ -154,6 +158,7 @@ int SOptions::ScanCommanline(int argc, const char* argv[], FILE* err, FILE* out)
{"wraparound", required_argument, 0, 'w'},
{"exclude", required_argument, 0, 'x'},
{"script", required_argument, 0, 's'},
{"trace", required_argument, 0, 'y'},
{"list-bitrates", optional_argument, 0, 'l'},
#if (OPTION_CANAPI_LIBRARY != 0)
{"list-boards", optional_argument, 0, 'L'},
Expand All @@ -179,9 +184,9 @@ int SOptions::ScanCommanline(int argc, const char* argv[], FILE* err, FILE* out)
#endif
// (2) scan command-line for options
#if (OPTION_CANAPI_LIBRARY != 0)
while ((opt = getopt_long(argc, (char * const *)argv, "b:vp:m:t:i:d:a:w:x:s:lLTh", long_options, NULL)) != -1) {
while ((opt = getopt_long(argc, (char * const *)argv, "b:vp:m:t:i:d:a:w:x:s:y:lLTh", long_options, NULL)) != -1) {
#else
while ((opt = getopt_long(argc, (char * const *)argv, "b:vm:t:i:d:a:w:x:s:lLTj:h", long_options, NULL)) != -1) {
while ((opt = getopt_long(argc, (char * const *)argv, "b:vm:t:i:d:a:w:x:s:y:lLTj:h", long_options, NULL)) != -1) {
#endif
switch (opt) {
/* option '--baudrate=<baudrate>' (-b) */
Expand Down Expand Up @@ -323,8 +328,7 @@ int SOptions::ScanCommanline(int argc, const char* argv[], FILE* err, FILE* out)
fprintf(err, "%s: illegal argument for option `--error-frames'\n", m_szBasename);
return 1;
}
m_OpMode.byte |= CANMODE_ERR;

m_OpMode.byte |= CANMODE_ERR;
break;
/* option '--no-extended-frames' */
case 'X':
Expand All @@ -336,7 +340,7 @@ int SOptions::ScanCommanline(int argc, const char* argv[], FILE* err, FILE* out)
fprintf(err, "%s: illegal argument for option `--no-extended-frames'\n", m_szBasename);
return 1;
}
m_OpMode.byte |= CANMODE_NXTD;
m_OpMode.byte |= CANMODE_NXTD;
break;
/* option '--no-remote-frames' */
case 'R':
Expand All @@ -348,7 +352,7 @@ int SOptions::ScanCommanline(int argc, const char* argv[], FILE* err, FILE* out)
fprintf(err, "%s: missing argument for option `--no-remote-frames'\n", m_szBasename);
return 1;
}
m_OpMode.byte |= CANMODE_NRTR;
m_OpMode.byte |= CANMODE_NRTR;
break;
/* option '--code=<11-bit-code>' */
case '1':
Expand Down Expand Up @@ -430,6 +434,36 @@ int SOptions::ScanCommanline(int argc, const char* argv[], FILE* err, FILE* out)
}
m_XtdFilter.m_u32Mask = (uint32_t)intarg;
break;
/* option '--trace=(ON|OFF)' (-y) */
#if (CAN_TRACE_SUPPORTED != 0)
case 'y':
if (optTraceMode++) {
fprintf(err, "%s: duplicated option `--trace'\n", m_szBasename);
return 1;
}
if (optarg == NULL) {
fprintf(err, "%s: missing argument for option `--trace'\n", m_szBasename);
return 1;
}
#if (CAN_TRACE_SUPPORTED == 1)
if (!strcasecmp(optarg, "OFF") || !strcasecmp(optarg, "NO") || !strcasecmp(optarg, "n") || !strcasecmp(optarg, "0"))
m_eTraceMode = SOptions::eTraceOff;
else if (!strcasecmp(optarg, "ON") || !strcasecmp(optarg, "YES") || !strcasecmp(optarg, "y") || !strcasecmp(optarg, "1"))
m_eTraceMode = SOptions::eTraceVendor;
#else
if (!strcasecmp(optarg, "BIN") || !strcasecmp(optarg, "BINARY") || !strcasecmp(optarg, "default"))
m_eTraceMode = SOptions::eTraceBinary;
else if (!strcasecmp(optarg, "CSV") || !strcasecmp(optarg, "logger") || !strcasecmp(optarg, "log"))
m_eTraceMode = SOptions::eTraceLogger;
else if (!strcasecmp(optarg, "TRC") || !strcasecmp(optarg, "vendor"))
m_eTraceMode = SOptions::eTraceVendor;
#endif
else {
fprintf(err, "%s: illegal argument for option `--trace'\n", m_szBasename);
return 1;
}
break;
#endif
/* option '--time=(ABS|REL|ZERO)' (-t) */
case 't':
if (optFmtTime++) {
Expand Down Expand Up @@ -761,6 +795,9 @@ void SOptions::ShowUsage(FILE* stream, bool args) {
fprintf(stream, " -b, --baudrate=<baudrate> CAN bit-timing in kbps (default=250), or\n");
fprintf(stream, " --bitrate=<bit-rate> CAN bit-rate settings (as key/value list)\n");
fprintf(stream, " -v, --verbose show detailed bit-rate settings\n");
#if (CAN_TRACE_SUPPORTED != 0)
fprintf(stream, " -y, --trace=(ON|OFF) write a trace file (default=OFF)\n");
#endif
#if (CAN_FD_SUPPORTED != 0)
fprintf(stream, " --list-bitrates[=<mode>] list standard bit-rate settings and exit\n");
#else
Expand Down
Loading

0 comments on commit 93c8eff

Please sign in to comment.