Commit 9563d688 authored by Gallacchi Mattia's avatar Gallacchi Mattia
Browse files

Merge branch 'master' of labinfo.ing.he-arc.ch:igib/public/mcp2221a-libs

parents 1c70c313 194e57bb
Loading
Loading
Loading
Loading

.vscode/launch.json

0 → 100644
+56 −0
Original line number Diff line number Diff line
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C++ Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/tests/i2c_scan",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        },
        {
            "name": "HID Test Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/debug/tests/hid_test",
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}/build/debug/tests",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true

                }
            ],
            "preLaunchTask": "build tests",
            "miDebuggerPath": "/usr/bin/gdb"
        }


    ]
}
 No newline at end of file

.vscode/tasks.json

0 → 100644
+32 −0
Original line number Diff line number Diff line
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "configure debug",
            "type": "shell",
            "command": "cmake",
            "args": [
                "--preset",
                "linux-debug",
            ]
        },
        {
            "label": "build tests",
            "type": "cppbuild",
            "command": "cmake",
            "args": [
                "--build",
                "build/debug"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": [
                "$gcc"
            ],
            "dependsOn": "configure debug",
            "dependsOrder": "sequence"
        }
    ]
}
 No newline at end of file
+30 −6
Original line number Diff line number Diff line
@@ -14,10 +14,6 @@ function(copy_runtime_dll target)
    endif()
endfunction()

# if (NOT UNIX)
#     message(FATAL_ERROR "This project only supports Unix-like systems.")
# endif()

option(BUILD_TESTS "Build the tests" ON)

project(mcp2221a LANGUAGES C CXX)
@@ -27,6 +23,7 @@ if (UNIX)
    include(ExternalProject)
    set(EXTERNAL_DIR ${CMAKE_CURRENT_BINARY_DIR}/external)

    # Download and build i2c-tools
    ExternalProject_Add(
        i2c-tools
        GIT_REPOSITORY https://git.kernel.org/pub/scm/utils/i2c-tools/i2c-tools.git
@@ -49,11 +46,38 @@ if (UNIX)
    add_library(i2c STATIC IMPORTED)
    set_target_properties(i2c PROPERTIES IMPORTED_LOCATION ${I2C_LIB_PATH})
    include_directories(${I2C_INCLUDE_DIR})

    # Download and build libusb
    ExternalProject_Add(
        libusb
        GIT_REPOSITORY https://github.com/libusb/libusb.git
        GIT_TAG v1.0.27
        PREFIX ${EXTERNAL_DIR}/libusb
        SOURCE_DIR ${EXTERNAL_DIR}/libusb/src
        STAMP_DIR ${EXTERNAL_DIR}/libusb/stamp
        CONFIGURE_COMMAND pwd && ./bootstrap.sh && ./configure --disable-udev --enable-static --disable-shared --prefix=${EXTERNAL_DIR}/libusb/install
        BUILD_COMMAND make -j4
        BUILD_IN_SOURCE 1
        INSTALL_COMMAND ""
    )
    ExternalProject_Get_Property(libusb BINARY_DIR)
    message(STATUS "libusb build directory: ${BINARY_DIR}")
    set(LIBUSB_LIB_PATH ${BINARY_DIR}/libusb/.libs/libusb-1.0.a)
    set(LIBUSB_INCLUDE_DIR ${EXTERNAL_DIR}/libusb/src/libusb)

    add_library(libusb-1.0.27 STATIC IMPORTED)
    set_target_properties(libusb PROPERTIES IMPORTED_LOCATION ${LIBUSB_LIB_PATH})
    include_directories(${LIBUSB_INCLUDE_DIR})
    message(STATUS "Libusb include dir: ${LIBUSB_INCLUDE_DIR}, lib path: ${LIBUSB_LIB_PATH}")

elseif(WIN32)
    include_directories(${CMAKE_SOURCE_DIR}/MCP2221_DLL/unmanaged/dll)
    include_directories(${CMAKE_CURRENT_SOURCE_DIR}/MCP2221_DLL/unmanaged/dll)
    set(MCP_DLL "${CMAKE_CURRENT_SOURCE_DIR}/MCP2221_DLL/unmanaged/dll/mcp2221_dll_um_x64.dll" CACHE INTERNAL "Path to MCP2221 DLL")
endif()

include_directories(lib)
include_directories(
    lib
)
add_subdirectory(lib)

if (BUILD_TESTS)
+14 −3
Original line number Diff line number Diff line
@@ -7,7 +7,6 @@
            "binaryDir": "${sourceDir}/build",
            "cacheVariables": {
                "CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
                "CMAKE_BUILD_TYPE": "Release",
                "CMAKE_CXX_STANDARD": "20"
            }
        },
@@ -20,11 +19,23 @@
                "value": "x64"
            }
        },
        {
            "name": "linux-debug",
            "binaryDir": "${sourceDir}/build/debug",
            "generator": "Unix Makefiles",
            "inherits": ["default"],
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Debug"
            }
        },
        {
            "name": "linux",
            "binaryDir": "${sourceDir}/build",
            "binaryDir": "${sourceDir}/build/release",
            "generator": "Unix Makefiles",
            "inherits": ["default"]
            "inherits": ["default"],
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Release"
            }
        }
    ]
  }
 No newline at end of file
+19 −12
Original line number Diff line number Diff line
@@ -2,16 +2,20 @@

Support for user space usage of the [MCP2221A][1] IC from Microchip. 


## Linux

To access the USB device you must add the following rule under */etc/udev/rules.d/* (i.e in a file called mcp2221a.rules) :
To access the USB HID device you must add the following rule under */etc/udev/rules.d/* (i.e in a file called 99-mcp2221a.rules) :

```bash
SUBSYSTEM=="usb", ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="00dd", GROUP="plugdev", TAG+="uaccess"
```

You must reboot after this.
Then update *udev* with the following commands:

```bash
sudo udevadm control --reload
sudo udevadm trigger
```

To access the I2C devices loaded by the [*mcp2221*][2] kernel driver you must add your user to the *i2c* group:

@@ -31,23 +35,26 @@ You must login again in order for the changes to take effect.

## Windows 

The device needs a windows driver to be recognized. 

## Dependencies

```bash
sudo apt install -y libi2c-dev
```
Nothing to configure the Microchip *dll* contains all the configuration necessary.

## Build

Standard **CMAKE**:

For Linux:

```bash
cmake --preset default
cmake --preset linux
cmake --build build/
```

For Windows:

```bash
cmake --preset windows
cmake --build build --config Release
```

## Try it out

Run the test software found in the tests folder. Expected result is:
@@ -64,7 +71,7 @@ Testing I2C: Success
## TODO

- [ ] Add python library 
- [ ] Add Windows support
- [X] Add Windows support
- [ ] Add hidapi support

[1]:http://ww1.microchip.com/downloads/en/devicedoc/20005565b.pdf
Loading