From 8690fcd1d92789f14c22b547969ebe0f01bf050b Mon Sep 17 00:00:00 2001 From: Wenting Zhang Date: Fri, 5 Apr 2024 11:30:08 -0400 Subject: [PATCH] USB interface proof-of-concept implementation --- fw/caster.c | 8 +++++++- fw/caster.h | 2 ++ fw/usbapp.c | 38 ++++++++++++++++++++++++++++++++++++-- util/usb_example.py | 26 ++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 util/usb_example.py diff --git a/fw/caster.c b/fw/caster.c index 0d4e012..d5bac10 100644 --- a/fw/caster.c +++ b/fw/caster.c @@ -91,4 +91,10 @@ uint8_t caster_setmode(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, fpga_write_reg8(CSR_OP_PARAM, (uint8_t)mode); fpga_write_reg8(CSR_OP_CMD, OP_EXT_SETMODE); return 0; -} \ No newline at end of file +} + +uint8_t caster_setinput(uint8_t input_src) { + if (is_busy()) return 1; + fpga_write_reg8(CSR_CFG_IN_SRC, input_src); + return 0; +} diff --git a/fw/caster.h b/fw/caster.h index cfc25b5..49188c7 100644 --- a/fw/caster.h +++ b/fw/caster.h @@ -52,6 +52,7 @@ #define CSR_CFG_FBYTES_B2 27 #define CSR_CFG_FBYTES_B1 28 #define CSR_CFG_FBYTES_B0 29 +#define CSR_CFG_IN_SRC 30 #define CSR_STATUS 32 // Alias for 16bit registers #define CSR_LUT_ADDR CSR_LUT_ADDR_HI @@ -94,3 +95,4 @@ uint8_t caster_load_waveform(uint8_t *waveform, uint8_t frames); uint8_t caster_redraw(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1); uint8_t caster_setmode(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, UPDATE_MODE mode); +uint8_t caster_setinput(uint8_t input_src); diff --git a/fw/usbapp.c b/fw/usbapp.c index f982e4a..a2d83ea 100644 --- a/fw/usbapp.c +++ b/fw/usbapp.c @@ -71,8 +71,42 @@ void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_ (void) report_id; (void) report_type; - // echo back anything we received from host - tud_hid_report(0, buffer, bufsize); + // Process the request + uint16_t cmd = (buffer[1] << 8) | buffer[0]; + uint16_t param = (buffer[3] << 8) | buffer[2]; + uint16_t x0 = (buffer[5] << 8) | buffer[4]; + uint16_t y0 = (buffer[7] << 8) | buffer[6]; + uint16_t x1 = (buffer[9] << 8) | buffer[8]; + uint16_t y1 = (buffer[11] << 8) | buffer[10]; + uint16_t id = (buffer[13] << 8) | buffer[12]; + uint16_t chksum = (buffer[15] << 8) | buffer[14]; + uint8_t retval = 1; + switch (buffer[0]) { + case USBCMD_RESET: + // reset system + (*((volatile uint32_t*)(PPB_BASE + 0x0ED0C))) = 0x5FA0004; // Reset via NVIC + break; + case USBCMD_POWERDOWN: + // TODO + break; + case USBCMD_POWERUP: + // TODO + break; + case USBCMD_SETINPUT: + retval = caster_setinput((uint8_t)param); + break; + case USBCMD_REDRAW: + retval = caster_redraw(x0, y0, x1, y1); + break; + case USBCMD_SETMODE: + retval = caster_setmode(x0, y0, x1, y1, (UPDATE_MODE)param); + break; + } + + uint8_t txbuf[1]; + txbuf[0] = retval; + + tud_hid_report(0, txbuf, 1); } void usbapp_task(void) { diff --git a/util/usb_example.py b/util/usb_example.py new file mode 100644 index 0000000..f1edfff --- /dev/null +++ b/util/usb_example.py @@ -0,0 +1,26 @@ +# Install python3 HID package https://pypi.org/project/hid/ +import hid +import struct + +vid = 0xcafe +pid = 0x4004 + +dev = hid.Device(vid, pid) + +if not dev: + print("Unable to open device") + exit() + +cmd = 0x00 +param = 0x00 +x0 = 0 +y0 = 0 +x1 = 1600 - 1 +y1 = 1200 - 1 +pid = 0 +chksum = 0 + +str_out = struct.pack('>h>h>h>h>h>h>h>h', cmd, param, x0, y0, x1, y1, pid, chksum) +dev.write(str_out) +str_in = dev.read(1) +print("Received from HID Device:", str_in, '\n')