summaryrefslogtreecommitdiff
path: root/src/test_cx.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/test_cx.c')
-rw-r--r--src/test_cx.c174
1 files changed, 174 insertions, 0 deletions
diff --git a/src/test_cx.c b/src/test_cx.c
new file mode 100644
index 0000000..6612264
--- /dev/null
+++ b/src/test_cx.c
@@ -0,0 +1,174 @@
+/*
+ * test_cx.c - bring-up / protocol self-test for mpegcap
+ *
+ * Steps through the CX23416 init exactly as the known-good reference
+ * (yamori813 mpeg-usb mpegcapt.c / Linux pvrusb2) but prints every stage,
+ * so we can see where the real hardware diverges.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#include "pvrusb2.h"
+
+static void step(const char *s) { printf("--- %s\n", s); fflush(stdout); }
+static void msleep(int ms)
+{
+ struct timespec ts = { ms / 1000, (long)(ms % 1000) * 1000000L };
+ nanosleep(&ts, NULL);
+}
+
+static int i2c_dump(pvr2_dev *p, uint16_t reg, int len)
+{
+ uint8_t buf[64];
+ int i;
+ if (pvr2_i2c_read(p, CX2584X_I2C_ADDR, reg, buf, len) < 0) {
+ printf(" i2c read %04x failed\n", reg);
+ return -1;
+ }
+ printf(" i2c %04x:", reg);
+ for (i = 0; i < len; i++) printf(" %02x", buf[i]);
+ printf("\n");
+ return 0;
+}
+
+int main(int argc, char **argv)
+{
+ pvr2_dev *p;
+ uint8_t *fw = NULL;
+ size_t fwlen = 0;
+ const char *fwp = argc > 1 ? argv[1] : "firmware/cx23416/cx_1_swapped.bin";
+ const char *fwp2 = argc > 2 ? argv[2] : NULL;
+ uint32_t v;
+ uint8_t c1;
+ int r;
+
+ p = calloc(1, sizeof(*p));
+ pvr2_init(p);
+ p->verbose = 1;
+
+ if (pvr2_find_device(p, DEFAULT_VID, DEFAULT_PID) < 0) {
+ fprintf(stderr, "device not found\n");
+ return 1;
+ }
+
+ step("USB speed");
+ r = pvr2_get_usb_speed(p);
+ printf(" speed byte 0x%02x (expect 0x80 = high speed)\n", r);
+
+ step("FX2 command 0xde (encoder resume)");
+ pvr2_simple_cmd(p, 0xde, NULL, 0);
+
+ step("FX2 command 0x0b (get usb speed, read response)");
+ {
+ uint8_t b[64];
+ int n;
+ pvr2_simple_cmd(p, 0x0b, NULL, 0);
+ n = bulk_cmd_in(p, b, sizeof(b));
+ printf(" read %d bytes: %02x\n", n, n > 0 ? b[0] : 0);
+ }
+
+ step("FX2 command 0xeb (eeprom addr)");
+ {
+ uint8_t b[64];
+ int n;
+ pvr2_simple_cmd(p, 0xeb, NULL, 0);
+ n = bulk_cmd_in(p, b, sizeof(b));
+ printf(" read %d bytes: %02x\n", n, n > 0 ? b[0] : 0);
+ }
+
+ step("I2C device id (0x0100, 2 bytes)");
+ i2c_dump(p, 0x0100, 2);
+
+ step("I2C video decoder status");
+ i2c_dump(p, 0x040d, 1);
+ i2c_dump(p, 0x040e, 1);
+
+ step("register 0x0048 <- 0xffffffff");
+ printf(" 0x0048 readback: ");
+ if (pvr2_read_reg(p, 0x0048, &v) == 0) printf("%08x\n", v);
+ else printf("FAILED\n");
+
+ step("gpio dir/out, deep reset");
+ pvr2_write_reg(p, 0x0048, 0xffffffff);
+ pvr2_gpio_dir(p, 0xffffffff, 0x00000088);
+ pvr2_gpio_out(p, 0xffffffff, 0x00000008);
+ pvr2_simple_cmd(p, 0xdd, NULL, 0);
+ msleep(100);
+ pvr2_write_reg(p, 0xa064, 0x00000000);
+ pvr2_gpio_dir(p, 0xffffffff, 0x00000408);
+ pvr2_gpio_out(p, 0xffffffff, 0x00000008);
+ pvr2_write_reg(p, 0x9058, 0xffffffed);
+ pvr2_write_reg(p, 0x9054, 0xfffffffd);
+ pvr2_write_reg(p, 0x07f8, 0x80000800);
+ pvr2_write_reg(p, 0x07fc, 0x0000001a);
+ pvr2_write_reg(p, 0x0700, 0x00000000);
+ pvr2_write_reg(p, 0xaa00, 0x00000000);
+ pvr2_write_reg(p, 0xaa04, 0x00057810);
+ pvr2_write_reg(p, 0xaa10, 0x00148500);
+ pvr2_write_reg(p, 0xaa18, 0x00840000);
+
+ step("0x52 FWPOST1, 0x06 MEMSEL");
+ pvr2_simple_cmd(p, 0x52, NULL, 0);
+ pvr2_simple_cmd(p, 0x06, &(uint8_t){0}, 1);
+ msleep(100);
+
+ step("reset 0x9054/0x9058 sanity read");
+ printf(" 0x9054: ");
+ if (pvr2_read_reg(p, 0x9054, &v) == 0) printf("%08x\n", v); else printf("FAILED\n");
+ printf(" 0x9058: ");
+ if (pvr2_read_reg(p, 0x9058, &v) == 0) printf("%08x\n", v); else printf("FAILED\n");
+
+ step("load CX23416 firmware");
+ if (pvr2_load_file(fwp, &fw, &fwlen) < 0) return 1;
+ printf(" %s (%zu bytes)\n", fwp, fwlen);
+ if (pvr2_upload_firmware2(p, fw, fwlen) < 0) {
+ fprintf(stderr, "upload failed\n");
+ return 1;
+ }
+
+ step("post: 0x9054 <- ffffffff, 0x9058 <- fffffffe8, MEMSEL");
+ pvr2_write_reg(p, 0x9054, 0xffffffff);
+ pvr2_write_reg(p, 0x9058, 0xffffffe8);
+ pvr2_simple_cmd(p, 0x06, &(uint8_t){0}, 1);
+ msleep(200);
+
+ step("read back 0x9054 / 0x9058");
+ printf(" 0x9054: ");
+ if (pvr2_read_reg(p, 0x9054, &v) == 0) printf("%08x\n", v); else printf("FAILED\n");
+ printf(" 0x9058: ");
+ if (pvr2_read_reg(p, 0x9058, &v) == 0) printf("%08x\n", v); else printf("FAILED\n");
+
+ step("MBOX raw read at 0x44 (4 words)");
+ {
+ uint32_t w[4];
+ int i;
+ if (pvr2_mbox_read(p, 0x44, w, 4) == 0) {
+ for (i = 0; i < 4; i++) printf(" mbox[%d] = %08x\n", i, w[i]);
+ } else printf(" FAILED\n");
+ }
+
+ step("PING (0x80)");
+ if (pvr2_enc_ping(p) == 0) {
+ printf(" PING OK\n");
+ step("GET_VERSION (0xc4)");
+ if (pvr2_enc_get_version(p, &v) == 0)
+ printf(" version %u.%u.%u (raw %08x)\n",
+ (v >> 24) & 0xff, (v >> 16) & 0xff, v & 0xffff, v);
+ else
+ printf(" GET_VERSION failed\n");
+ } else {
+ printf(" PING FAILED\n");
+ }
+
+ /* Optional: try alternate firmware if given */
+ if (fwp2) {
+ printf("\n=== (skipping alt firmware retry; power cycle needed) ===\n");
+ }
+ (void)c1;
+ free(fw);
+ pvr2_exit(p);
+ free(p);
+ return 0;
+}