From b0eb4205ba7102c529b5a8e293dd9b17c26b3b7f Mon Sep 17 00:00:00 2001 From: imi415 Date: Sat, 28 May 2022 22:40:32 +0800 Subject: [PATCH] Initial commit. --- .clang-format | 11 ++++ .gitmodules | 3 + CMakeLists.txt | 6 ++ demos/i2cdetect/CMakeLists.txt | 10 +++ demos/i2cdetect/src/main.c | 113 +++++++++++++++++++++++++++++++++ libusbsio | 1 + 6 files changed, 144 insertions(+) create mode 100644 .clang-format create mode 100644 .gitmodules create mode 100644 CMakeLists.txt create mode 100644 demos/i2cdetect/CMakeLists.txt create mode 100644 demos/i2cdetect/src/main.c create mode 160000 libusbsio diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..1e81119 --- /dev/null +++ b/.clang-format @@ -0,0 +1,11 @@ +BasedOnStyle: Google +IndentWidth: 4 +AlignConsecutiveMacros: Consecutive +AlignConsecutiveDeclarations: true +AlignConsecutiveAssignments: true +BreakBeforeBraces: Custom +BraceWrapping: + AfterEnum: false + AfterStruct: false + SplitEmptyFunction: false +ColumnLimit: 120 \ No newline at end of file diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..a5fc269 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "libusbsio"] + path = libusbsio + url = https://git.minori.work/Embedded_Drivers/libusbsio.git diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..05b5f33 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.10) + +project(usbsio_demos) + +add_subdirectory(libusbsio) +add_subdirectory(demos/i2cdetect) \ No newline at end of file diff --git a/demos/i2cdetect/CMakeLists.txt b/demos/i2cdetect/CMakeLists.txt new file mode 100644 index 0000000..c1cb352 --- /dev/null +++ b/demos/i2cdetect/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.10) + +project(usbsio_i2cdetect) + +set(USBSIO_I2CDETECT_SRCS + "src/main.c" +) + +add_executable(${PROJECT_NAME} ${USBSIO_I2CDETECT_SRCS}) +target_link_libraries(${PROJECT_NAME} PRIVATE "usbsio") \ No newline at end of file diff --git a/demos/i2cdetect/src/main.c b/demos/i2cdetect/src/main.c new file mode 100644 index 0000000..0820181 --- /dev/null +++ b/demos/i2cdetect/src/main.c @@ -0,0 +1,113 @@ +#include + +/* USBSIO header */ +#include + +#define NXP_VID 0x1FC9 +#define MCULINK_PID 0x0143 + +static int i2c_detect(LPC_HANDLE handle, uint8_t port_num, uint8_t start_addr, uint8_t end_addr, + enum I2C_ClockRate_t rate); + +static int i2c_probe_addr(LPC_HANDLE i2c_handle, uint8_t addr); + +int main(int argc, const char *argv) { + int ret; + LPC_HANDLE dev_handle; + + fprintf(stdout, "USBSIO example - I2CDetect\n"); + + /* Find all SIO devices. */ + ret = LPCUSBSIO_GetNumPorts(NXP_VID, MCULINK_PID); + if (ret == 0) { + fprintf(stderr, "No MCU-Link devices found.\n"); + return -1; + } + + /* Open device */ + dev_handle = LPCUSBSIO_Open(0); /* For now, open the first device available. */ + if (dev_handle == NULL) { + fprintf(stderr, "Failed to open device #0\n"); + return -2; + } + + fprintf(stdout, "%s\n", LPCUSBSIO_GetVersion(dev_handle)); + + ret = i2c_detect(dev_handle, 0, 0x00, 0x7F, I2C_CLOCK_STANDARD_MODE); + if(ret < 0) { + fprintf(stderr, "Failed to probe devices.\n"); + } + + LPCUSBSIO_Close(dev_handle); + + return ret; +} + +static int i2c_detect(LPC_HANDLE handle, uint8_t port_num, uint8_t start_addr, uint8_t end_addr, + enum I2C_ClockRate_t rate) { + fprintf(stdout, "WARNING! This program can confuse your I2C bus, cause data loss and worse!\n"); + fprintf(stdout, + "WARNING! Current MCU-Link firmware has a bug which causes device to be unresponsive\n" + " if I2C bus timeout due to lack of external pull-up resistors.\n" + " In case that happens, don't panic, just re-plug the MCU-Link\n" + " and the device will be available again.\n" + ); + + LPC_HANDLE i2c_handle; + I2C_PORTCONFIG_T i2c_cfg = { + .ClockRate = rate, + .Options = 0U, + }; + + i2c_handle = I2C_Open(handle, &i2c_cfg, port_num); + if (i2c_handle == NULL) { + fprintf(stderr, "Failed to open I2C bus #%d\n", port_num); + return -1; + } + + fprintf(stdout, "I will probe port #%d.\n", port_num); + fprintf(stdout, "I will probe address range 0x%02x-0x%02x.\n", start_addr, end_addr); + fprintf(stdout, "Continue!\n"); + + fprintf(stdout, " 0 1 2 3 4 5 6 7 8 9 a b c d e f\n"); + for (uint8_t i = 0; i < 0x80; i++) { + if(i % 16 == 0) { + fprintf(stdout, "%02x: ", i); + } + + if(i < start_addr || i > end_addr) { + fprintf(stdout, " "); + } else { + /* Do actual probing here. */ + if(i2c_probe_addr(i2c_handle, i) > 0) { + fprintf(stdout, "%02x ", i); + } else { + fprintf(stdout, "-- "); + } + fflush(stdout); + } + + if(i % 16 == 15) { + fprintf(stdout, "\n"); + } + } + + I2C_Close(i2c_handle); +} + +static int i2c_probe_addr(LPC_HANDLE i2c_handle, uint8_t addr) { + uint8_t rx_buf = 0U; + I2C_FAST_XFER_T xfer = { + .slaveAddr = addr, + .txSz = 0U, + .txBuff = NULL, + .rxSz = 1U, + .rxBuff = &rx_buf, + .options = 0U, + }; + + int ret = I2C_FastXfer(i2c_handle, &xfer); + if(ret < 0) I2C_Reset(i2c_handle); + + return ret; +} \ No newline at end of file diff --git a/libusbsio b/libusbsio new file mode 160000 index 0000000..ec43d3c --- /dev/null +++ b/libusbsio @@ -0,0 +1 @@ +Subproject commit ec43d3ce9f4d98a195962a03a68fedc3cad2bc87