Skip to content

Commit

Permalink
Merge pull request #11 from mac-can/bugfix10
Browse files Browse the repository at this point in the history
Bugfix: Wrong calculation of CAN bus parameters for devices with 24MHz CAN clock
  • Loading branch information
mac-can authored Jun 1, 2022
2 parents 875ac90 + 1d1c44c commit e334212
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Libraries/CANAPI/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ OBJECTS = $(OUTDIR)/KvaserCAN_Driver.o $(OUTDIR)/KvaserUSB_Device.o \
$(OUTDIR)/can_api.o $(OUTDIR)/can_btr.o


ifeq ($(current_OS),Darwin) # macOS - libUVCANKVL.dylib
ifeq ($(current_OS),Darwin) # macOS - libUVCANKVL.dylib

LIBRARY = libUVCANKVL

Expand Down
2 changes: 1 addition & 1 deletion Libraries/KvaserCAN/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ OBJECTS = $(OUTDIR)/KvaserCAN.o \
$(OUTDIR)/can_api.o $(OUTDIR)/can_btr.o


ifeq ($(current_OS),Darwin) # macOS - libKvaserCAN.dylib
ifeq ($(current_OS),Darwin) # macOS - libKvaserCAN.dylib

LIBRARY = libKvaserCAN

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### macOS® User-Space Driver for CAN Leaf Interfaces from Kvaser

_Copyright © 2020-2022 Uwe Vogt, UV Software, Berlin (info@mac-can.com)_
_Copyright © 2020-2022 Uwe Vogt, UV Software, Berlin (info@mac-can.com)_

# Running CAN and CAN FD on Mac®

Expand Down
2 changes: 1 addition & 1 deletion Sources/KvaserCAN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
#else
#define VERSION_MAJOR 0
#define VERSION_MINOR 2
#define VERSION_PATCH 99
#define VERSION_PATCH 1
#endif
#define VERSION_BUILD BUILD_NO
#define VERSION_STRING TOSTRING(VERSION_MAJOR) "." TOSTRING(VERSION_MINOR) "." TOSTRING(VERSION_PATCH) " (" TOSTRING(BUILD_NO) ")"
Expand Down
20 changes: 9 additions & 11 deletions Sources/Wrapper/can_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
#include "build_no.h"
#define VERSION_MAJOR 0
#define VERSION_MINOR 2
#define VERSION_PATCH 99
#define VERSION_PATCH 1
#define VERSION_BUILD BUILD_NO
#define VERSION_STRING TOSTRING(VERSION_MAJOR) "." TOSTRING(VERSION_MINOR) "." TOSTRING(VERSION_PATCH) " (" TOSTRING(BUILD_NO) ")"
#if defined(__APPLE__)
Expand Down Expand Up @@ -129,7 +129,7 @@ typedef struct { // Kvaser CAN interface:
/* ----------- prototypes ---------------------------------------------
*/
static int map_bitrate2busparams(const can_bitrate_t *bitrate, KvaserUSB_BusParams_t *busParams);
static int map_busparams2bitrate(const KvaserUSB_BusParams_t *busParams, can_bitrate_t *bitrate);
static int map_busparams2bitrate(const KvaserUSB_BusParams_t *busParams, can_bitrate_t *bitrate, int32_t canClock);
static int map_bitrate2busparams_fd(const can_bitrate_t *bitrate, KvaserUSB_BusParamsFd_t *busParams);
static int map_busparams2bitrate_fd(const KvaserUSB_BusParamsFd_t *busParams, can_bitrate_t *bitrate);
static int lib_parameter(uint16_t param, void *value, size_t nbyte);
Expand Down Expand Up @@ -550,12 +550,13 @@ int can_bitrate(int handle, can_bitrate_t *bitrate, can_speed_t *speed)
memset(&busParamsFd, 0, sizeof(KvaserUSB_BusParamsFd_t));
bool fdoe = can[handle].mode.fdoe ? true : false;
bool brse = can[handle].mode.brse ? true : false;
int32_t canClock = (int32_t)can[handle].device.clocks[0]; // FIXME: remove clocks array

// CAN 2.0 operation mode:
if (!can[handle].mode.fdoe) {
// get bit-rate settings from device
if ((rc = KvaserCAN_GetBusParams(&can[handle].device, &busParams)) == CANUSB_SUCCESS) {
if ((rc = map_busparams2bitrate(&busParams, &tmpBitrate)) == CANUSB_SUCCESS) {
if ((rc = map_busparams2bitrate(&busParams, &tmpBitrate, canClock)) == CANUSB_SUCCESS) {
rc = btr_bitrate2speed(&tmpBitrate, fdoe, brse, &tmpSpeed);
}
}
Expand Down Expand Up @@ -677,21 +678,18 @@ static int map_bitrate2busparams(const can_bitrate_t *bitrate, KvaserUSB_BusPara
return CANERR_NOERROR;
}

static int map_busparams2bitrate(const KvaserUSB_BusParams_t *busParams, can_bitrate_t *bitrate)
static int map_busparams2bitrate(const KvaserUSB_BusParams_t *busParams, can_bitrate_t *bitrate, int32_t canClock)
{
// sanity check
if (!busParams || !bitrate)
return CANERR_NULLPTR;

// Kvaser canLib32 doesn't offer the used controller frequency and bit-rate prescaler.
// We suppose it's running with 80MHz and calculate the bit-rate prescaler as follows:
//
// (1) brp = 80MHz / (bit-rate * (1 + tseg1 + tseq2))
// (1) brp = frequency / (bit-rate * (1 + tseg1 + tseq2))
//
if (busParams->bitRate <= 0) // divide-by-zero!
if (busParams->bitRate == 0) // divide-by-zero!
return CANERR_BAUDRATE;
bitrate->btr.frequency = (int32_t)80000000;
bitrate->btr.nominal.brp = (uint16_t)(80000000L
bitrate->btr.frequency = (int32_t)canClock;
bitrate->btr.nominal.brp = (uint16_t)(bitrate->btr.frequency
/ (busParams->bitRate * (int32_t)(1u + busParams->tseg1 + busParams->tseg2)));
bitrate->btr.nominal.tseg1 = (uint16_t)busParams->tseg1;
bitrate->btr.nominal.tseg2 = (uint16_t)busParams->tseg2;
Expand Down
4 changes: 2 additions & 2 deletions Trial/KvaserCAN.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@
"$(inherited)",
);
GENERATE_INFOPLIST_FILE = YES;
MACOSX_DEPLOYMENT_TARGET = 12.1;
MACOSX_DEPLOYMENT_TARGET = 11.0;
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = "uv-software.Testing";
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -558,7 +558,7 @@
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
GENERATE_INFOPLIST_FILE = YES;
MACOSX_DEPLOYMENT_TARGET = 12.1;
MACOSX_DEPLOYMENT_TARGET = 11.0;
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = "uv-software.Testing";
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down

0 comments on commit e334212

Please sign in to comment.