From 84fbd7b135d5ea3e67d82e5c8fa806283f073cce Mon Sep 17 00:00:00 2001 From: Fossifous Acid Date: Sun, 29 Dec 2024 23:36:15 -0500 Subject: [PATCH 1/5] Added line number and -Wall compile options --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7fde4de..3540874 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,8 @@ project(pinedio-usb C) set(CMAKE_C_STANDARD 11) +add_compile_options(-g -Wall) + add_library(pinedio-usb STATIC libpinedio-usb.c) find_package(PkgConfig REQUIRED) pkg_check_modules(LIBUSB REQUIRED libusb-1.0) From 90f168a59f53e440a3e6048392a1e791d53e24a6 Mon Sep 17 00:00:00 2001 From: Fossifous Acid Date: Sun, 29 Dec 2024 23:41:50 -0500 Subject: [PATCH 2/5] Added struct init function --- libpinedio-usb.c | 27 ++++++++++++++------------- libpinedio-usb.h | 1 + test.c | 2 +- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/libpinedio-usb.c b/libpinedio-usb.c index 66b8475..11b813e 100644 --- a/libpinedio-usb.c +++ b/libpinedio-usb.c @@ -208,15 +208,22 @@ static uint8_t reverse_byte(uint8_t x) { return x; } +/** + * This will initialize the struct and set the default options. + */ +struct pinedio_inst pinedio_struct_default() { + struct pinedio_inst inst = {0}; + inst.options[PINEDIO_OPTION_AUTO_CS] = 1; + inst.options[PINEDIO_OPTION_VID] = 0x1A86; + inst.options[PINEDIO_OPTION_PID] = 0x5512; + return inst; +} + +/** + * Use pinedio_struct_default() to initialize inst + */ int32_t pinedio_init(struct pinedio_inst *inst, void *driver) { int32_t ret; - inst->int_running_cnt = 0; - inst->pin_poll_thread_exit = false; - for (int i = 0; i < PINEDIO_INT_PIN_MAX; i++) { - inst->interrupts[i].callback = NULL; - } - - inst->options[PINEDIO_OPTION_AUTO_CS] = 1; ret = pthread_mutex_init(&inst->usb_access_mutex, NULL); if (ret != 0) { @@ -231,12 +238,6 @@ int32_t pinedio_init(struct pinedio_inst *inst, void *driver) { } libusb_set_option(NULL, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_INFO); - if (inst->options[PINEDIO_OPTION_VID] == 0) { - inst->options[PINEDIO_OPTION_VID] = 0x1A86; - } - if (inst->options[PINEDIO_OPTION_PID] == 0) { - inst->options[PINEDIO_OPTION_PID] = 0x5512; - } // discover devices libusb_device **list; diff --git a/libpinedio-usb.h b/libpinedio-usb.h index 3b61d89..432adcd 100644 --- a/libpinedio-usb.h +++ b/libpinedio-usb.h @@ -72,6 +72,7 @@ struct pinedio_inst { char product_string[97]; }; +struct pinedio_inst pinedio_struct_default(); int32_t pinedio_init(struct pinedio_inst* inst, void* driver); int32_t pinedio_set_option(struct pinedio_inst* inst, enum pinedio_option option, uint32_t value); int32_t pinedio_set_pin_mode(struct pinedio_inst *inst, uint32_t pin, uint32_t mode); diff --git a/test.c b/test.c index 895aab8..a1d0bec 100644 --- a/test.c +++ b/test.c @@ -7,7 +7,7 @@ void callback_test() { } int main(int argc, char** argv) { - struct pinedio_inst inst; + struct pinedio_inst inst = pinedio_struct_default(); pinedio_init(&inst, NULL); #if 1 for (int i = 0; i < 10; i++) { From 5c97345022409a88651dcd03bed3c5b7f53ddd4d Mon Sep 17 00:00:00 2001 From: Fossifous Acid Date: Sun, 29 Dec 2024 23:42:55 -0500 Subject: [PATCH 3/5] Moved CS failed print to set_cs function --- libpinedio-usb.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libpinedio-usb.c b/libpinedio-usb.c index 11b813e..034a8ff 100644 --- a/libpinedio-usb.c +++ b/libpinedio-usb.c @@ -362,16 +362,16 @@ int32_t pinedio_digital_write(struct pinedio_inst *inst, uint32_t pin, bool acti }; int32_t ret = usb_transfer(inst, __func__, sizeof(buf), 0, buf, NULL, true); - if (ret < 0) { - printf("Failed to set CS pin.\n"); - } return ret; } int32_t pinedio_set_cs(struct pinedio_inst *inst, bool active) { - return pinedio_digital_write(inst, 0, active); - + int32_t ret = pinedio_digital_write(inst, 0, active); + if (ret < 0) { + printf("Failed to set CS pin.\n"); + } + return ret; } int32_t pinedio_write_read(struct pinedio_inst* inst, uint8_t *writearr, uint32_t writecnt, uint8_t* readarr, uint32_t readcnt) { From 92c49aff36208f9d765aeab059e5988cf989d4c6 Mon Sep 17 00:00:00 2001 From: Fossifous Acid Date: Sun, 29 Dec 2024 23:43:52 -0500 Subject: [PATCH 4/5] Changed read_buf loop to avoid unsequenced modifications --- libpinedio-usb.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/libpinedio-usb.c b/libpinedio-usb.c index 034a8ff..d730a46 100644 --- a/libpinedio-usb.c +++ b/libpinedio-usb.c @@ -461,9 +461,8 @@ int32_t pinedio_transceive(struct pinedio_inst* inst, uint8_t *write_buf, uint8_ if (ret < 0) return -1; - unsigned int i; - for (i = 0; i < count; i++) { - *read_buf++ = reverse_byte(*read_buf); + for (; read_buf < read_buf + count; read_buf++) { + *read_buf = reverse_byte(*read_buf); } return 0; From 324503f15b478182cf2728b8aa8e63d10dccbf16 Mon Sep 17 00:00:00 2001 From: Fossifous Acid Date: Sun, 29 Dec 2024 23:44:57 -0500 Subject: [PATCH 5/5] Check return code of pinedio_init in test before continuing. --- test.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test.c b/test.c index a1d0bec..b243f67 100644 --- a/test.c +++ b/test.c @@ -8,7 +8,10 @@ void callback_test() { int main(int argc, char** argv) { struct pinedio_inst inst = pinedio_struct_default(); - pinedio_init(&inst, NULL); + int ret; + if ((ret = pinedio_init(&inst, NULL)) < 0) { + return ret; + } #if 1 for (int i = 0; i < 10; i++) { #if 1