From 1d0a96fa20c99c98edbb7797c7f17921a836ab1a Mon Sep 17 00:00:00 2001 From: Will Xyen Date: Wed, 27 May 2020 08:51:16 -0700 Subject: [PATCH] aciodrv: Make init more consistent by reading until we actually get a 0xAA (reboots / command interruptions will cause incorrect state on device side) --- Module.mk | 1 + src/main/aciodrv/device.c | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Module.mk b/Module.mk index c8f415c..e342cd6 100644 --- a/Module.mk +++ b/Module.mk @@ -183,6 +183,7 @@ $(zipdir)/tools.zip: \ $(V)zip -j $@ $^ $(zipdir)/tools-x64.zip: \ + build/bin/indep-64/aciotest.exe \ build/bin/indep-64/eamiotest.exe \ build/bin/indep-64/iidxiotest.exe \ build/bin/indep-64/iidx-ezusb-exit-hook.dll \ diff --git a/src/main/aciodrv/device.c b/src/main/aciodrv/device.c index ee42e6a..0cdf1f2 100644 --- a/src/main/aciodrv/device.c +++ b/src/main/aciodrv/device.c @@ -19,6 +19,7 @@ static char aviodrv_device_node_products[16][4]; static bool aciodrv_device_init(void) { uint8_t init_seq[1] = {AC_IO_SOF}; + uint8_t read_buff[1] = {0x00}; /* init/reset the device by sending 0xAA until 0xAA is returned */ int read = 0; @@ -27,17 +28,20 @@ static bool aciodrv_device_init(void) return false; } - read = aciodrv_port_read(init_seq, sizeof(init_seq)); - } while (read == 0); + read = aciodrv_port_read(read_buff, sizeof(read_buff)); + } while ((read <= 0) || (read_buff[0] != init_seq[0])); if (read > 0) { + log_warning("Obtained SOF, clearing out buffer now"); /* empty buffer by reading all data */ while (read > 0) { read = aciodrv_port_read(init_seq, sizeof(init_seq)); } + log_warning("Cleared buffer, init done"); return read == 0; } else { + log_warning("Read failure when trying to clear device state"); return false; } } @@ -314,15 +318,20 @@ bool aciodrv_device_get_node_product_ident(uint8_t node_id, char product[4]) bool aciodrv_send_and_recv(struct ac_io_message *msg, int resp_size) { msg->cmd.seq_no = aciodrv_device_msg_counter++; + int send_size = offsetof(struct ac_io_message, cmd.raw) + msg->cmd.nbytes; - if (aciodrv_device_send( - (uint8_t *) msg, - offsetof(struct ac_io_message, cmd.raw) + msg->cmd.nbytes) <= 0) { +#ifdef AC_IO_MSG_LOG + log_info("Beginning send on %d: %04x (%d b)", msg->addr, msg->cmd.code, send_size); +#endif + if (aciodrv_device_send((uint8_t *) msg, send_size) <= 0) { return false; } uint16_t req_code = msg->cmd.code; +#ifdef AC_IO_MSG_LOG + log_info("Beginning recv: (%d b)", resp_size); +#endif if (aciodrv_device_receive((uint8_t *) msg, resp_size) <= 0) { return false; }