Skip to content

Commit

Permalink
[#90]: Add Windows PEAK PCAN Support
Browse files Browse the repository at this point in the history
Adds the windows PCAN libraries from the PCAN API.
Adds a new hardware plugin for PCAN on Windows. (The linux one is different and I will need to add it seperately).
Added readme for 3rd party drivers where we can put any important info related to them.
Made all examples support Windows if compiled with -DCAN_DRIVER=WindowsPCANBasic.
Fixed attempting to compile unit tests if BUILD_TESTING is enabled but socket CAN is not present.
Added a bunch of crazy CMake to detect the target architecture when compiling with MSVC because that functionality is totally broken in CMAKE. See https://gitlab.kitware.com/cmake/cmake/-/issues/15170
  • Loading branch information
ad3154 committed Dec 31, 2022
1 parent ed58ba2 commit b90a377
Show file tree
Hide file tree
Showing 17 changed files with 855 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Makefile

# build
*build
*out

# Doxygen
*docs
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ if(NOT TARGET GTest::gtest_main)
add_library(GTest::gtest_main ALIAS GTest::Main)
endif()

if ("SocketCAN" IN_LIST CAN_DRIVER)
add_executable(unit_tests test/address_claim_test.cpp test/test_CAN_glue.cpp test/identifier_tests.cpp test/dm_13_tests.cpp)
target_link_libraries(unit_tests PRIVATE GTest::gtest_main ${PROJECT_NAME}::Isobus ${PROJECT_NAME}::HardwareIntegration ${PROJECT_NAME}::SystemTiming)

include(GoogleTest)
gtest_discover_tests(unit_tests name_tests identifier_tests)
endif()
endif()

install( TARGETS Isobus SystemTiming HardwareIntegration
EXPORT isobusTargets
Expand Down
6 changes: 4 additions & 2 deletions cmake/isobusConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# https://cliutils.gitlab.io/modern-cmake/chapters/install/installing.html
include(CMakeFindDependencyMacro)

# Add the targets file
include("${CMAKE_CURRENT_LIST_DIR}/isobusTargets.cmake")
if (NOT WIN32)
# Add the targets file
include("${CMAKE_CURRENT_LIST_DIR}/isobusTargets.cmake")
endif()
4 changes: 2 additions & 2 deletions doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ RECURSIVE = YES
# Note that relative paths are relative to the directory from which doxygen is
# run.

EXCLUDE = "hardware_integration/README.md"
EXCLUDE =

# The EXCLUDE_SYMLINKS tag can be used to select whether or not files or
# directories that are symbolic links (a Unix file system feature) are excluded
Expand All @@ -873,7 +873,7 @@ EXCLUDE_SYMLINKS = NO
# Note that the wildcards are matched against the file with absolute path, so to
# exclude all test directories for example use the pattern */test/*

EXCLUDE_PATTERNS =
EXCLUDE_PATTERNS = */hardware_integration/*.md

# The EXCLUDE_SYMBOLS tag can be used to specify one or more symbol names
# (namespaces, classes, functions, etc.) that should be excluded from the
Expand Down
9 changes: 8 additions & 1 deletion examples/diagnostic_protocol/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@
#include <iterator>
#include <memory>

static std::shared_ptr<isobus::InternalControlFunction> TestInternalECU = nullptr;
#ifdef WIN32
#include "isobus/hardware_integration/pcan_basic_windows_plugin.hpp"
static PCANBasicWindowsPlugin canDriver(PCAN_USBBUS1);
#else
#include "isobus/hardware_integration/socket_can_interface.hpp"
static SocketCANInterface canDriver("can0");
#endif

static std::shared_ptr<isobus::InternalControlFunction> TestInternalECU = nullptr;

using namespace std;

Expand Down
9 changes: 8 additions & 1 deletion examples/nmea2000/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@
#include <iterator>
#include <memory>

#ifdef WIN32
#include "isobus/hardware_integration/pcan_basic_windows_plugin.hpp"
static PCANBasicWindowsPlugin canDriver(PCAN_USBBUS1);
#else
#include "isobus/hardware_integration/socket_can_interface.hpp"
static SocketCANInterface canDriver("can0");
#endif

static std::shared_ptr<isobus::InternalControlFunction> TestInternalECU = nullptr;
static SocketCANInterface canDriver("vcan0");

using namespace std;

Expand Down
9 changes: 8 additions & 1 deletion examples/pgn_requests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@
#include <iostream>
#include <memory>

#ifdef WIN32
#include "isobus/hardware_integration/pcan_basic_windows_plugin.hpp"
static PCANBasicWindowsPlugin canDriver(PCAN_USBBUS1);
#else
#include "isobus/hardware_integration/socket_can_interface.hpp"
static SocketCANInterface canDriver("can0");
#endif

static std::shared_ptr<isobus::InternalControlFunction> TestInternalECU = nullptr;
static std::uint32_t propARepetitionRate_ms = 0xFFFFFFFF;
static isobus::ControlFunction *repetitionRateRequestor = nullptr;
static SocketCANInterface canDriver("can0");

using namespace std;

Expand Down
9 changes: 8 additions & 1 deletion examples/transport_layer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
#include <iterator>
#include <memory>

#ifdef WIN32
#include "isobus/hardware_integration/pcan_basic_windows_plugin.hpp"
static PCANBasicWindowsPlugin canDriver(PCAN_USBBUS1);
#else
#include "isobus/hardware_integration/socket_can_interface.hpp"
static SocketCANInterface canDriver("can0");
#endif

static std::shared_ptr<isobus::InternalControlFunction> TestInternalECU = nullptr;
static isobus::PartneredControlFunction *TestPartner = nullptr;
std::vector<isobus::NAMEFilter> vtNameFilters;
Expand All @@ -19,7 +27,6 @@ std::uint8_t *TPTestBuffer = nullptr;
std::uint8_t *ETPTestBuffer = nullptr;
constexpr std::uint16_t MAX_TP_SIZE_BYTES = 1785;
constexpr std::uint32_t ETP_TEST_SIZE = 2048;
static SocketCANInterface canDriver("vcan0");

using namespace std;

Expand Down
10 changes: 8 additions & 2 deletions examples/vt_version_3_object_pool/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "isobus/hardware_integration/can_hardware_interface.hpp"
#include "isobus/hardware_integration/socket_can_interface.hpp"
#include "isobus/isobus/can_general_parameter_group_numbers.hpp"
#include "isobus/isobus/can_network_manager.hpp"
#include "isobus/isobus/can_partnered_control_function.hpp"
Expand All @@ -8,6 +7,14 @@
#include "isobus/utility/iop_file_interface.hpp"
#include "objectPoolObjects.h"

#ifdef WIN32
#include "isobus/hardware_integration/pcan_basic_windows_plugin.hpp"
static PCANBasicWindowsPlugin canDriver(PCAN_USBBUS1);
#else
#include "isobus/hardware_integration/socket_can_interface.hpp"
static SocketCANInterface canDriver("can0");
#endif

#include <csignal>
#include <iostream>
#include <memory>
Expand All @@ -18,7 +25,6 @@ static std::shared_ptr<isobus::VirtualTerminalClient> TestVirtualTerminalClient
std::vector<isobus::NAMEFilter> vtNameFilters;
const isobus::NAMEFilter testFilter(isobus::NAME::NAMEParameters::FunctionCode, static_cast<std::uint8_t>(isobus::NAME::Function::VirtualTerminal));
static std::vector<std::uint8_t> testPool;
static SocketCANInterface canDriver("can0");

using namespace std;

Expand Down
71 changes: 57 additions & 14 deletions hardware_integration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,29 @@ set(HARDWARE_INTEGRATION_SRC_DIR "src")
set(HARDWARE_INTEGRATION_INCLUDE_DIR "include/isobus/hardware_integration")

if(NOT CAN_DRIVER)
message(AUTHOR_WARNING "No CAN driver specified, choosing Linux socket CAN by default. Use -DCAN_DRIVER=socketCAN to avoid this, or choose another driver and set it explicitly.")
set(CAN_DRIVER "socketCAN")
message(AUTHOR_WARNING "No CAN driver specified, choosing Linux socket CAN by default. Use -DCAN_DRIVER=\"SocketCAN\" to specify another driver. or -DCAN_DRIVER=\"SocketCAN;WindowsPCANBasic\" to specify multiple drivers.")
set(CAN_DRIVER "SocketCAN")
endif()

if(CAN_DRIVER AND CAN_DRIVER MATCHES "socketCAN")
set(HARDWARE_INTEGRATION_SRC
"can_hardware_interface.cpp"
"socket_can_interface.cpp"
)

# Set the include files
set(HARDWARE_INTEGRATION_INCLUDE
"can_hardware_interface.hpp"
"can_hardware_plugin.hpp"
"socket_can_interface.hpp"
)
# Set the source files
set(HARDWARE_INTEGRATION_SRC
"can_hardware_interface.cpp"
)

# Set the include files
set(HARDWARE_INTEGRATION_INCLUDE
"can_hardware_interface.hpp"
"can_hardware_plugin.hpp"
)

# Add the source/include files based on the CAN driver chosen
if ("SocketCAN" IN_LIST CAN_DRIVER)
list(APPEND HARDWARE_INTEGRATION_SRC "socket_can_interface.cpp")
list(APPEND HARDWARE_INTEGRATION_INCLUDE "socket_can_interface.hpp")
endif()
if ("WindowsPCANBasic" IN_LIST CAN_DRIVER)
list(APPEND HARDWARE_INTEGRATION_SRC "pcan_basic_windows_plugin.cpp")
list(APPEND HARDWARE_INTEGRATION_INCLUDE "pcan_basic_windows_plugin.hpp")
endif()

# Prepend the source directory path to all the source files
Expand All @@ -37,6 +44,42 @@ add_library(HardwareIntegration ${HARDWARE_INTEGRATION_SRC} ${HARDWARE_INTEGRATI
add_library(${PROJECT_NAME}::HardwareIntegration ALIAS HardwareIntegration)
target_link_libraries(HardwareIntegration PRIVATE ${PROJECT_NAME}::SystemTiming ${PROJECT_NAME}::Isobus)

if ("WindowsPCANBasic" IN_LIST CAN_DRIVER)
if(MSVC)
# See https://gitlab.kitware.com/cmake/cmake/-/issues/15170
set(CMAKE_SYSTEM_PROCESSOR ${MSVC_CXX_ARCHITECTURE_ID})
endif()

message(STATUS "Target Arch: ${CMAKE_SYSTEM_PROCESSOR}")
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "AMD64")
message(STATUS "Detected AMD64, linking to PCAN x64 Library")
target_link_libraries(HardwareIntegration PRIVATE ${CMAKE_CURRENT_LIST_DIR}/lib/Windows/PCANBasic_x64.lib)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/lib/Windows/PCANBasic_x64.lib
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_LIST_DIR}/lib/Windows/PCANBasic_x64.lib
${CMAKE_CURRENT_BINARY_DIR}/PCANBasic_x64.lib)
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "ARM64")
message(STATUS "Detected ARM64, linking to PCAN ARM64 Library")
target_link_libraries(HardwareIntegration PRIVATE ${CMAKE_CURRENT_LIST_DIR}/lib/Windows/PCANBasic_ARM64.lib)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/lib/Windows/PCANBasic_ARM64.lib
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_LIST_DIR}/lib/Windows/PCANBasic_ARM64.lib
${CMAKE_CURRENT_BINARY_DIR}/PCANBasic_ARM64.lib)
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "X86")
message(STATUS "Detected x86, linking to PCAN x86 Library")
target_link_libraries(HardwareIntegration PRIVATE ${CMAKE_CURRENT_LIST_DIR}/lib/Windows/PCANBasic_x86.lib)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/lib/Windows/PCANBasic_x86.lib
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_LIST_DIR}/lib/Windows/PCANBasic_x86.lib
${CMAKE_CURRENT_BINARY_DIR}/PCANBasic_x86.lib)
else()
message(FATAL_ERROR "Windows PCAN Selected but no supported processor arch was detected. Only x64, x86, and ARM64 are supported by PEAK's drivers.")
endif()
endif()

# Specify the include directory to be exported for other moduels to use. The
# PUBLIC keyword here allows other libraries or exectuables to link to this
# library and use its functionality.
Expand Down
Loading

0 comments on commit b90a377

Please sign in to comment.