Skip to content

NTCAN hardware_integration enhancement plus bugfix#672

Open
norbertmm wants to merge 2 commits intoOpen-Agriculture:mainfrom
norbertmm:norbertmm-ntcan_patch
Open

NTCAN hardware_integration enhancement plus bugfix#672
norbertmm wants to merge 2 commits intoOpen-Agriculture:mainfrom
norbertmm:norbertmm-ntcan_patch

Conversation

@norbertmm
Copy link

Describe your changes

NTCAN Hardware integration (driver) does not work with CAN-USB/2 device from esd electronics gmbh.
Two issues:

  • CAN interface device does not support esd's SmartFilter feature and therefore the driver init fails.
  • canWriteT() NTCAN API function does not work as expected and leads to 100% bus load due to infinite write loop

Both issues fixed

by this change and tested successfully with CAN-USB/2 and also EtherCAN/3-FD devices using the SeederExample and a Trimble XCN-1050 Terminal.

Fix for older esd CAN interfaces without SmartFilter feature and fix canWriteT() endless loop
Copy link
Member

@ad3154 ad3154 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this contribution!

I let a few comments mostly around some style things that could make the file a bit more consistent with the rest of the project and a bit tidier.

Since I don't have an NTCAN things to test with, folks like you who do are greatly appreciated!

Comment on lines +52 to +53
std::int32_t txQueueSize = min( NTCAN_MAX_TX_QUEUESIZE, 256 );
std::int32_t rxQueueSize = min( NTCAN_MAX_RX_QUEUESIZE, 256 );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit - I think this is std::min? So prefer explicit namespace

Suggested change
std::int32_t txQueueSize = min( NTCAN_MAX_TX_QUEUESIZE, 256 );
std::int32_t rxQueueSize = min( NTCAN_MAX_RX_QUEUESIZE, 256 );
std::int32_t txQueueSize = std::min( NTCAN_MAX_TX_QUEUESIZE, 256 );
std::int32_t rxQueueSize = std::min( NTCAN_MAX_RX_QUEUESIZE, 256 );

}
}

if(1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can probably get rid of this if(1) condition check?

isobus::CANStackLogger::warn("[NTCAN]: failed to enable CAN error event reporting");
}
}
//#define NTCAN_IS_EVENT(id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//#define NTCAN_IS_EVENT(id)

}
break;
default:
; // ignore
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit - Since there's only 1 case, this can be simplified down to an if check

default:
; // ignore
}
if (1) { // print EVT message
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can probably get rid of this if(1) condition check?

}
// no sleep here!
return false;
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this file will pass our style linter... before we approve it, can you format it using clang-format?

See our contributing guide: find . -iname *.hpp -o -iname *.cpp | xargs clang-format -i

} else {
// got a data frame, might be CC or FD
canFrame.dataLength = NTCAN_LEN_TO_DATASIZE(msgCanMessage.len);
memcpy(canFrame.data, msgCanMessage.data, canFrame.dataLength);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: namespacing

Suggested change
memcpy(canFrame.data, msgCanMessage.data, canFrame.dataLength);
std::memcpy(canFrame.data, msgCanMessage.data, canFrame.dataLength);

std::int32_t id = NTCAN_EV_CAN_ERROR; // canReadT() will report error events
NTCAN_RESULT res = canIdAdd(handle, id);
if(res != NTCAN_SUCCESS) {
isobus::CANStackLogger::warn("[NTCAN]: failed to enable CAN error event reporting");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: We're in the isobus namespace here I think, so can probably omit it? Keeps the lines shorter.

Suggested change
isobus::CANStackLogger::warn("[NTCAN]: failed to enable CAN error event reporting");
CANStackLogger::warn("[NTCAN]: failed to enable CAN error event reporting");

There are other places in this file that do the same thing - I'd suggest updating them all if removing that prefix compiles alright


if ( NTCAN_IS_EVENT(msgCanMessage.id) ) {
// got a an event frame
EVMSG_T *msgCanEvent = (EVMSG_T*)&msgCanMessage;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reinterpret cast is fairly nasty and feels like it might not be cross-platform (endianness) safe, but we can at least make that clearer by showing it as:

Suggested change
EVMSG_T *msgCanEvent = (EVMSG_T*)&msgCanMessage;
EVMSG_T *msgCanEvent = reinterpret_cast<EVMSG_T*>(&msgCanMessage);

par.num_baudrate = baudrate; // ???
char evt_msg[128];
evt_msg[0] = '\0';
result = canFormatEvent( (EVMSG*)msgCanEvent, &par, evt_msg, sizeof evt_msg);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another yikesy C style cast here... I'd suggest either being clear that it's a reinterpret_cast or avoid the cast

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NTCAN Hardware integration fails with CAN-USB/2

2 participants