Initial commit.

This commit is contained in:
imi415 2022-05-28 22:40:32 +08:00
commit b0eb4205ba
Signed by: imi415
GPG Key ID: 885EC2B5A8A6F8A7
6 changed files with 144 additions and 0 deletions

11
.clang-format Normal file
View File

@ -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

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "libusbsio"]
path = libusbsio
url = https://git.minori.work/Embedded_Drivers/libusbsio.git

6
CMakeLists.txt Normal file
View File

@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.10)
project(usbsio_demos)
add_subdirectory(libusbsio)
add_subdirectory(demos/i2cdetect)

View File

@ -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")

113
demos/i2cdetect/src/main.c Normal file
View File

@ -0,0 +1,113 @@
#include <stdio.h>
/* USBSIO header */
#include <lpcusbsio.h>
#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;
}

1
libusbsio Submodule

@ -0,0 +1 @@
Subproject commit ec43d3ce9f4d98a195962a03a68fedc3cad2bc87