USB interface proof-of-concept implementation

This commit is contained in:
Wenting Zhang 2024-04-05 11:30:08 -04:00
parent 2380384e70
commit 8690fcd1d9
4 changed files with 71 additions and 3 deletions

View file

@ -92,3 +92,9 @@ uint8_t caster_setmode(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1,
fpga_write_reg8(CSR_OP_CMD, OP_EXT_SETMODE);
return 0;
}
uint8_t caster_setinput(uint8_t input_src) {
if (is_busy()) return 1;
fpga_write_reg8(CSR_CFG_IN_SRC, input_src);
return 0;
}

View file

@ -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);

View file

@ -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) {

26
util/usb_example.py Normal file
View file

@ -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')