Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
42 changes: 21 additions & 21 deletions libpinedio-usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -361,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) {
Expand Down Expand Up @@ -460,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;
Expand Down
1 change: 1 addition & 0 deletions libpinedio-usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ void callback_test() {
}

int main(int argc, char** argv) {
struct pinedio_inst inst;
pinedio_init(&inst, NULL);
struct pinedio_inst inst = pinedio_struct_default();
int ret;
if ((ret = pinedio_init(&inst, NULL)) < 0) {
return ret;
}
#if 1
for (int i = 0; i < 10; i++) {
#if 1
Expand Down