diff options
author | Paul Selkirk <paul@psgd.org> | 2015-04-08 16:01:31 -0400 |
---|---|---|
committer | Paul Selkirk <paul@psgd.org> | 2015-04-08 16:03:15 -0400 |
commit | aeaf94f4e83826fe56f38fc670973a60a5010ef1 (patch) | |
tree | 6f3d3ba5d0caf5be29f2a4f873a97b8070d6adc9 /eim/sw/tc_eim.c | |
parent | 891a24d969181f02762c031b9cfe0fd96c116634 (diff) |
Unify and refactor eim and i2c software.
Unify memory maps.
Move tc_init, tc_next, tc_wait_* into tc_[eim|i2c].c.
Move eim_setup into tc_eim.c, move i2c_open into tc_i2c.c.
Diffstat (limited to 'eim/sw/tc_eim.c')
-rw-r--r-- | eim/sw/tc_eim.c | 70 |
1 files changed, 59 insertions, 11 deletions
diff --git a/eim/sw/tc_eim.c b/eim/sw/tc_eim.c index 0d8c83c..d44b685 100644 --- a/eim/sw/tc_eim.c +++ b/eim/sw/tc_eim.c @@ -44,10 +44,26 @@ #include "tc_eim.h" extern int debug; +static int inited = 0; + +/* ---------------- EIM low-level code ---------------- */ +static int init(void) +{ + if (inited) + return 0; + + if (eim_setup() != 0) { + fprintf(stderr, "EIM setup failed\n"); + return -1; + } + + inited = 1; + return 0; +} /* ---------------- test-case low-level code ---------------- */ -static void dump(char *label, const uint8_t *buf, int len) +static void dump(char *label, const uint8_t *buf, size_t len) { if (debug) { int i; @@ -58,8 +74,11 @@ static void dump(char *label, const uint8_t *buf, int len) } } -int tc_write(off_t offset, const uint8_t *buf, int len) +int tc_write(off_t offset, const uint8_t *buf, size_t len) { + if (init() != 0) + return -1; + dump("write ", buf, len); for (; len > 0; offset += 4, buf += 4, len -= 4) { @@ -71,11 +90,14 @@ int tc_write(off_t offset, const uint8_t *buf, int len) return 0; } -int tc_read(off_t offset, uint8_t *buf, int len) +int tc_read(off_t offset, uint8_t *buf, size_t len) { uint8_t *rbuf = buf; int rlen = len; + if (init() != 0) + return -1; + for (; rlen > 0; offset += 4, rbuf += 4, rlen -= 4) { uint32_t val; eim_read_32(offset, &val); @@ -87,7 +109,7 @@ int tc_read(off_t offset, uint8_t *buf, int len) return 0; } -int tc_expected(off_t offset, const uint8_t *expected, int len) +int tc_expected(off_t offset, const uint8_t *expected, size_t len) { uint8_t *buf; int i; @@ -116,22 +138,48 @@ errout: return 1; } +int tc_init(off_t offset) +{ + uint8_t buf[4] = { 0, 0, 0, CTRL_INIT_CMD }; + + return tc_write(offset, buf, 4); +} + +int tc_next(off_t offset) +{ + uint8_t buf[4] = { 0, 0, 0, CTRL_NEXT_CMD }; + + return tc_write(offset, buf, 4); +} + int tc_wait(off_t offset, uint8_t status, int *count) { uint8_t buf[4]; int i; for (i = 1; ; ++i) { - if (count && (*count > 0) && (i >= *count)) { - fprintf(stderr, "tc_wait timed out\n"); - return 1; - } + if (count && (*count > 0) && (i >= *count)) { + fprintf(stderr, "tc_wait timed out\n"); + return 1; + } if (tc_read(offset, buf, 4) != 0) return -1; if (buf[3] & status) { - if (count) - *count = i; + if (count) + *count = i; return 0; - } + } } } + +int tc_wait_ready(off_t offset) +{ + int limit = 256; + return tc_wait(offset, STATUS_READY_BIT, &limit); +} + +int tc_wait_valid(off_t offset) +{ + int limit = 256; + return tc_wait(offset, STATUS_VALID_BIT, &limit); +} |