Install signal handler for cleaning up.

This commit is contained in:
imi415 2022-06-16 09:46:23 +08:00
parent 735f87f3eb
commit 418dfc4050
Signed by: imi415
GPG Key ID: 885EC2B5A8A6F8A7
2 changed files with 38 additions and 22 deletions

View File

@ -1,4 +1,7 @@
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
/* USBSIO header */
#include <lpcusbsio.h>
@ -6,17 +9,24 @@
#define NXP_VID 0x1FC9
#define MCULINK_PID 0x0143
static bool s_running = true;
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);
static void signal_handler(int sig);
int main(int argc, const char *argv) {
int ret;
LPC_HANDLE dev_handle;
fprintf(stdout, "USBSIO example - I2CDetect\n");
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
/* Find all SIO devices. */
ret = LPCUSBSIO_GetNumPorts(NXP_VID, MCULINK_PID);
if (ret == 0) {
@ -34,7 +44,7 @@ int main(int argc, const char *argv) {
fprintf(stdout, "%s\n", LPCUSBSIO_GetVersion(dev_handle));
ret = i2c_detect(dev_handle, 0, 0x00, 0x7F, I2C_CLOCK_STANDARD_MODE);
if(ret < 0) {
if (ret < 0) {
fprintf(stderr, "Failed to probe devices.\n");
}
@ -46,12 +56,11 @@ int main(int argc, const char *argv) {
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"
);
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 = {
@ -71,15 +80,20 @@ static int i2c_detect(LPC_HANDLE handle, uint8_t port_num, uint8_t start_addr, u
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) {
if(!s_running) {
fprintf(stderr, "\nInterrupted.\n");
break;
}
if (i % 16 == 0) {
fprintf(stdout, "%02x: ", i);
}
if(i < start_addr || i > end_addr) {
if (i < start_addr || i > end_addr) {
fprintf(stdout, " ");
} else {
/* Do actual probing here. */
if(i2c_probe_addr(i2c_handle, i) > 0) {
if (i2c_probe_addr(i2c_handle, i) > 0) {
fprintf(stdout, "%02x ", i);
} else {
fprintf(stdout, "-- ");
@ -87,7 +101,7 @@ static int i2c_detect(LPC_HANDLE handle, uint8_t port_num, uint8_t start_addr, u
fflush(stdout);
}
if(i % 16 == 15) {
if (i % 16 == 15) {
fprintf(stdout, "\n");
}
}
@ -96,18 +110,20 @@ static int i2c_detect(LPC_HANDLE handle, uint8_t port_num, uint8_t start_addr, u
}
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,
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);
if (ret < 0) I2C_Reset(i2c_handle);
return ret;
}
}
static void signal_handler(int sig) { s_running = false; }

@ -1 +1 @@
Subproject commit ec43d3ce9f4d98a195962a03a68fedc3cad2bc87
Subproject commit 60907768d02c589a6846cf5f03480acda45eb8ee