mirror of
https://gitlab.com/zephray/glider.git
synced 2025-01-03 11:17:10 +00:00
Merge branch 'usbhid' into r0p7_merge
This commit is contained in:
commit
3a16b26ec2
12 changed files with 602 additions and 67 deletions
|
@ -35,10 +35,12 @@ add_executable(fw
|
|||
power.c
|
||||
ptn3460.c
|
||||
tcpm_driver.c
|
||||
usb_descriptors.c
|
||||
usb_mux.c
|
||||
usb_pd_driver.c
|
||||
usb_pd_policy.c
|
||||
usb_pd_protocol.c
|
||||
usbapp.c
|
||||
utils.c
|
||||
)
|
||||
|
||||
|
@ -46,10 +48,14 @@ pico_set_program_name(fw "fw")
|
|||
pico_set_program_version(fw "0.1")
|
||||
|
||||
pico_enable_stdio_uart(fw 0)
|
||||
pico_enable_stdio_usb(fw 1)
|
||||
pico_enable_stdio_usb(fw 0)
|
||||
|
||||
# Make sure TinyUSB can find tusb_config.h
|
||||
target_include_directories(fw PUBLIC
|
||||
${CMAKE_CURRENT_LIST_DIR})
|
||||
|
||||
# Add the standard library to the build
|
||||
target_link_libraries(fw pico_stdlib hardware_sleep)
|
||||
target_link_libraries(fw pico_stdlib hardware_sleep tinyusb_device tinyusb_board)
|
||||
|
||||
# Add any user requested libraries
|
||||
target_link_libraries(fw
|
||||
|
|
27
fw/caster.c
27
fw/caster.c
|
@ -58,27 +58,33 @@ void caster_init(void) {
|
|||
fpga_write_reg8(CSR_LUT_FRAME, 38);
|
||||
}
|
||||
|
||||
void caster_load_waveform(uint8_t *waveform, uint8_t frames) {
|
||||
wait();
|
||||
static uint8_t is_busy() {
|
||||
uint8_t status = fpga_write_reg8(CSR_STATUS, 0x00);
|
||||
return !!(status & STATUS_OP_QUEUE);
|
||||
}
|
||||
|
||||
uint8_t caster_load_waveform(uint8_t *waveform, uint8_t frames) {
|
||||
fpga_write_reg8(CSR_LUT_FRAME, 0); // Reset value before loading
|
||||
fpga_write_reg16(CSR_LUT_ADDR, 0);
|
||||
fpga_write_bulk(CSR_LUT_WR, waveform, WAVEFORM_SIZE);
|
||||
waveform_frames = frames;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void caster_redraw(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) {
|
||||
wait();
|
||||
uint8_t caster_redraw(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) {
|
||||
if (is_busy()) return 1;
|
||||
fpga_write_reg16(CSR_OP_LEFT, x0);
|
||||
fpga_write_reg16(CSR_OP_TOP, y0);
|
||||
fpga_write_reg16(CSR_OP_RIGHT, x1);
|
||||
fpga_write_reg16(CSR_OP_BOTTOM, y1);
|
||||
fpga_write_reg8(CSR_OP_LENGTH, get_update_frames());
|
||||
fpga_write_reg8(CSR_OP_CMD, OP_EXT_REDRAW);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void caster_setmode(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) {
|
||||
wait();
|
||||
if (is_busy()) return 1;
|
||||
fpga_write_reg16(CSR_OP_LEFT, x0);
|
||||
fpga_write_reg16(CSR_OP_TOP, y0);
|
||||
fpga_write_reg16(CSR_OP_RIGHT, x1);
|
||||
|
@ -86,4 +92,11 @@ void caster_setmode(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1,
|
|||
fpga_write_reg8(CSR_OP_LENGTH, get_update_frames());
|
||||
fpga_write_reg8(CSR_OP_PARAM, (uint8_t)mode);
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -53,6 +53,7 @@
|
|||
#define CSR_CFG_FBYTES_B1 28
|
||||
#define CSR_CFG_FBYTES_B0 29
|
||||
#define CSR_CFG_MINDRV 30
|
||||
#define CSR_STATUS 32
|
||||
// Alias for 16bit registers
|
||||
#define CSR_LUT_ADDR CSR_LUT_ADDR_HI
|
||||
#define CSR_OP_LEFT CSR_OP_LEFT_HI
|
||||
|
@ -90,7 +91,8 @@ typedef enum {
|
|||
} UPDATE_MODE;
|
||||
|
||||
void caster_init(void);
|
||||
void caster_load_waveform(uint8_t *waveform, uint8_t frames);
|
||||
void caster_redraw(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1);
|
||||
void caster_setmode(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1,
|
||||
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);
|
||||
|
|
15
fw/fpga.c
15
fw/fpga.c
|
@ -53,22 +53,31 @@ static void fpga_send_byte(uint8_t byte) {
|
|||
}
|
||||
}
|
||||
|
||||
static void fpga_send_byte_slow(uint8_t byte) {
|
||||
static uint8_t fpga_send_byte_slow(uint8_t byte) {
|
||||
uint8_t rxbyte;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
gpio_put(FPGA_MOSI, byte & 0x80);
|
||||
delay_loop(20);
|
||||
rxbyte |= gpio_get(FPGA_MISO);
|
||||
gpio_put(FPGA_SCLK, 1);
|
||||
delay_loop(20);
|
||||
byte <<= 1;
|
||||
rxbyte <<= 1;
|
||||
gpio_put(FPGA_SCLK, 0);
|
||||
}
|
||||
delay_loop(20);
|
||||
rxbyte |= gpio_get(FPGA_MISO);
|
||||
gpio_put(FPGA_SCLK, 1);
|
||||
return rxbyte;
|
||||
}
|
||||
|
||||
void fpga_write_reg8(uint8_t addr, uint8_t val) {
|
||||
uint8_t fpga_write_reg8(uint8_t addr, uint8_t val) {
|
||||
uint8_t oldval;
|
||||
gpio_put(FPGA_CS, 0);
|
||||
fpga_send_byte_slow(addr);
|
||||
fpga_send_byte_slow(val);
|
||||
oldval = fpga_send_byte_slow(val);
|
||||
gpio_put(FPGA_CS, 1);
|
||||
return oldval;
|
||||
}
|
||||
|
||||
void fpga_write_reg16(uint8_t addr, uint16_t val) {
|
||||
|
|
|
@ -24,6 +24,6 @@
|
|||
void fpga_init(void);
|
||||
void fpga_suspend(void);
|
||||
void fpga_resume(void);
|
||||
void fpga_write_reg8(uint8_t addr, uint8_t val);
|
||||
uint8_t fpga_write_reg8(uint8_t addr, uint8_t val);
|
||||
void fpga_write_reg16(uint8_t addr, uint16_t val);
|
||||
void fpga_write_bulk(uint8_t addr, uint8_t *buf, int length);
|
117
fw/fw.c
117
fw/fw.c
|
@ -1,5 +1,5 @@
|
|||
//
|
||||
// Copyright 2022 Wenting Zhang <zephray@outlook.com>
|
||||
// Copyright 2024 Wenting Zhang <zephray@outlook.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
|
@ -35,6 +35,10 @@
|
|||
#include "edid.h"
|
||||
#include "button.h"
|
||||
#include "caster.h"
|
||||
#include "usbapp.h"
|
||||
|
||||
void osd_task(void); // OSD handling task
|
||||
void usb_pd_task(void); // USB PD handling task
|
||||
|
||||
int main()
|
||||
{
|
||||
|
@ -84,18 +88,34 @@ int main()
|
|||
|
||||
power_enable(true);
|
||||
|
||||
//sleep_run_from_xosc();
|
||||
//sleep_goto_dormant_until_edge_high(8);
|
||||
// https://ghubcoder.github.io/posts/awaking-the-pico/
|
||||
#ifdef INPUT_TYPEC
|
||||
int result = tcpm_init(0);
|
||||
if (result)
|
||||
fatal("Failed to initialize TCPC\n");
|
||||
#endif
|
||||
|
||||
fpga_init();
|
||||
caster_init();
|
||||
|
||||
button_init();
|
||||
|
||||
int mode_max = 6;
|
||||
int mode = 1;
|
||||
UPDATE_MODE modes[6] = {
|
||||
usbapp_init();
|
||||
|
||||
while (1) {
|
||||
osd_task();
|
||||
usb_pd_task();
|
||||
usbapp_task();
|
||||
sleep_ms(1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void osd_task(void) {
|
||||
#ifdef BOARD_HAS_BUTTON
|
||||
static int mode_max = 6;
|
||||
static int mode = 1;
|
||||
const UPDATE_MODE modes[6] = {
|
||||
UM_FAST_MONO_NO_DITHER,
|
||||
UM_FAST_MONO_BAYER,
|
||||
UM_FAST_MONO_BLUE_NOISE,
|
||||
|
@ -104,49 +124,44 @@ int main()
|
|||
UM_AUTO_LUT_ERROR_DIFFUSION
|
||||
};
|
||||
|
||||
extern int dp_enabled;
|
||||
bool hpd_sent = false;
|
||||
bool dp_valid = false;
|
||||
|
||||
while (1) {
|
||||
#ifdef HAS_TYPEC
|
||||
// TODO: Implement interrupt
|
||||
fusb302_tcpc_alert(0);
|
||||
pd_run_state_machine(0);
|
||||
if (dp_enabled && !hpd_sent && !pd_is_vdm_busy(0)) {
|
||||
printf("DP enabled\n");
|
||||
pd_send_hpd(0, hpd_high);
|
||||
hpd_sent = true;
|
||||
}
|
||||
if (dp_valid != ptn3460_is_valid()) {
|
||||
dp_valid = ptn3460_is_valid();
|
||||
printf(dp_valid ? "Input is valid\n" : "Input is invalid\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
// Key press logic
|
||||
uint32_t keys = button_scan();
|
||||
if (keys & 0x1) {
|
||||
// First key short press
|
||||
// Clear screen
|
||||
caster_redraw(0,0,2400,1800);
|
||||
}
|
||||
if (keys & 0x2) {
|
||||
// First key long press
|
||||
}
|
||||
if (keys & 0x4) {
|
||||
// Second key short press
|
||||
// Switch mode
|
||||
mode++;
|
||||
if (mode >= mode_max) mode = 0;
|
||||
caster_setmode(0,0,2400,1800,modes[mode]);
|
||||
}
|
||||
if (keys & 0x8) {
|
||||
// Second key long press
|
||||
}
|
||||
|
||||
sleep_ms(1);
|
||||
// Key press logic
|
||||
uint32_t keys = button_scan();
|
||||
if (keys & 0x1) {
|
||||
// First key short press
|
||||
// Clear screen
|
||||
caster_redraw(0,0,2400,1800);
|
||||
}
|
||||
if (keys & 0x2) {
|
||||
// First key long press
|
||||
}
|
||||
if (keys & 0x4) {
|
||||
// Second key short press
|
||||
// Switch mode
|
||||
mode++;
|
||||
if (mode >= mode_max) mode = 0;
|
||||
caster_setmode(0,0,2400,1800,modes[mode]);
|
||||
}
|
||||
if (keys & 0x8) {
|
||||
// Second key long press
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void usb_pd_task(void) {
|
||||
extern int dp_enabled;
|
||||
static bool hpd_sent = false;
|
||||
static bool dp_valid = false;
|
||||
|
||||
// TODO: Implement interrupt
|
||||
fusb302_tcpc_alert(0);
|
||||
pd_run_state_machine(0);
|
||||
if (dp_enabled && !hpd_sent && !pd_is_vdm_busy(0)) {
|
||||
printf("DP enabled\n");
|
||||
pd_send_hpd(0, hpd_high);
|
||||
hpd_sent = true;
|
||||
}
|
||||
if (dp_valid != ptn3460_is_valid()) {
|
||||
dp_valid = ptn3460_is_valid();
|
||||
printf(dp_valid ? "Input is valid\n" : "Input is invalid\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
111
fw/tusb_config.h
Normal file
111
fw/tusb_config.h
Normal file
|
@ -0,0 +1,111 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _TUSB_CONFIG_H_
|
||||
#define _TUSB_CONFIG_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
// COMMON CONFIGURATION
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
// defined by board.mk
|
||||
#ifndef CFG_TUSB_MCU
|
||||
#error CFG_TUSB_MCU must be defined
|
||||
#endif
|
||||
|
||||
// RHPort number used for device can be defined by board.mk, default to port 0
|
||||
#ifndef BOARD_DEVICE_RHPORT_NUM
|
||||
#define BOARD_DEVICE_RHPORT_NUM 0
|
||||
#endif
|
||||
|
||||
// RHPort max operational speed can defined by board.mk
|
||||
// Default to Highspeed for MCU with internal HighSpeed PHY (can be port specific), otherwise FullSpeed
|
||||
#ifndef BOARD_DEVICE_RHPORT_SPEED
|
||||
#if (CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX || \
|
||||
CFG_TUSB_MCU == OPT_MCU_NUC505 || CFG_TUSB_MCU == OPT_MCU_CXD56 || CFG_TUSB_MCU == OPT_MCU_SAMX7X)
|
||||
#define BOARD_DEVICE_RHPORT_SPEED OPT_MODE_HIGH_SPEED
|
||||
#else
|
||||
#define BOARD_DEVICE_RHPORT_SPEED OPT_MODE_FULL_SPEED
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Device mode with rhport and speed defined by board.mk
|
||||
#if BOARD_DEVICE_RHPORT_NUM == 0
|
||||
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | BOARD_DEVICE_RHPORT_SPEED)
|
||||
#elif BOARD_DEVICE_RHPORT_NUM == 1
|
||||
#define CFG_TUSB_RHPORT1_MODE (OPT_MODE_DEVICE | BOARD_DEVICE_RHPORT_SPEED)
|
||||
#else
|
||||
#error "Incorrect RHPort configuration"
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUSB_OS
|
||||
#define CFG_TUSB_OS OPT_OS_NONE
|
||||
#endif
|
||||
|
||||
// CFG_TUSB_DEBUG is defined by compiler in DEBUG build
|
||||
// #define CFG_TUSB_DEBUG 0
|
||||
|
||||
/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
|
||||
* Tinyusb use follows macros to declare transferring memory so that they can be put
|
||||
* into those specific section.
|
||||
* e.g
|
||||
* - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") ))
|
||||
* - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4)))
|
||||
*/
|
||||
#ifndef CFG_TUSB_MEM_SECTION
|
||||
#define CFG_TUSB_MEM_SECTION
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUSB_MEM_ALIGN
|
||||
#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4)))
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
// DEVICE CONFIGURATION
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
#ifndef CFG_TUD_ENDPOINT0_SIZE
|
||||
#define CFG_TUD_ENDPOINT0_SIZE 64
|
||||
#endif
|
||||
|
||||
//------------- CLASS -------------//
|
||||
#define CFG_TUD_HID 1
|
||||
#define CFG_TUD_CDC 0
|
||||
#define CFG_TUD_MSC 0
|
||||
#define CFG_TUD_MIDI 0
|
||||
#define CFG_TUD_VENDOR 0
|
||||
|
||||
// HID buffer size Should be sufficient to hold ID (if any) + Data
|
||||
#define CFG_TUD_HID_EP_BUFSIZE 16
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_CONFIG_H_ */
|
170
fw/usb_descriptors.c
Normal file
170
fw/usb_descriptors.c
Normal file
|
@ -0,0 +1,170 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "tusb.h"
|
||||
|
||||
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
|
||||
* Same VID/PID with different interface e.g MSC (first), then CDC (later) will possibly cause system error on PC.
|
||||
*
|
||||
* Auto ProductID layout's Bitmap:
|
||||
* [MSB] HID | MSC | CDC [LSB]
|
||||
*/
|
||||
#define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) )
|
||||
#define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \
|
||||
_PID_MAP(MIDI, 3) | _PID_MAP(VENDOR, 4) )
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Device Descriptors
|
||||
//--------------------------------------------------------------------+
|
||||
tusb_desc_device_t const desc_device =
|
||||
{
|
||||
.bLength = sizeof(tusb_desc_device_t),
|
||||
.bDescriptorType = TUSB_DESC_DEVICE,
|
||||
.bcdUSB = 0x0200,
|
||||
.bDeviceClass = 0x00,
|
||||
.bDeviceSubClass = 0x00,
|
||||
.bDeviceProtocol = 0x00,
|
||||
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
|
||||
|
||||
.idVendor = 0xCafe,
|
||||
.idProduct = USB_PID,
|
||||
.bcdDevice = 0x0100,
|
||||
|
||||
.iManufacturer = 0x01,
|
||||
.iProduct = 0x02,
|
||||
.iSerialNumber = 0x03,
|
||||
|
||||
.bNumConfigurations = 0x01
|
||||
};
|
||||
|
||||
// Invoked when received GET DEVICE DESCRIPTOR
|
||||
// Application return pointer to descriptor
|
||||
uint8_t const * tud_descriptor_device_cb(void)
|
||||
{
|
||||
return (uint8_t const *) &desc_device;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// HID Report Descriptor
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
uint8_t const desc_hid_report[] =
|
||||
{
|
||||
TUD_HID_REPORT_DESC_GENERIC_INOUT(CFG_TUD_HID_EP_BUFSIZE)
|
||||
};
|
||||
|
||||
// Invoked when received GET HID REPORT DESCRIPTOR
|
||||
// Application return pointer to descriptor
|
||||
// Descriptor contents must exist long enough for transfer to complete
|
||||
uint8_t const * tud_hid_descriptor_report_cb(uint8_t itf)
|
||||
{
|
||||
(void) itf;
|
||||
return desc_hid_report;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Configuration Descriptor
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
enum
|
||||
{
|
||||
ITF_NUM_HID,
|
||||
ITF_NUM_TOTAL
|
||||
};
|
||||
|
||||
#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_HID_INOUT_DESC_LEN)
|
||||
|
||||
#define EPNUM_HID 0x01
|
||||
|
||||
uint8_t const desc_configuration[] =
|
||||
{
|
||||
// Config number, interface count, string index, total length, attribute, power in mA
|
||||
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100),
|
||||
|
||||
// Interface number, string index, protocol, report descriptor len, EP Out & In address, size & polling interval
|
||||
TUD_HID_INOUT_DESCRIPTOR(ITF_NUM_HID, 0, HID_ITF_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, 0x80 | EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 10)
|
||||
};
|
||||
|
||||
// Invoked when received GET CONFIGURATION DESCRIPTOR
|
||||
// Application return pointer to descriptor
|
||||
// Descriptor contents must exist long enough for transfer to complete
|
||||
uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
|
||||
{
|
||||
(void) index; // for multiple configurations
|
||||
return desc_configuration;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// String Descriptors
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// array of pointer to string descriptors
|
||||
char const* string_desc_arr [] =
|
||||
{
|
||||
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
||||
"TinyUSB", // 1: Manufacturer
|
||||
"TinyUSB Device", // 2: Product
|
||||
"123456", // 3: Serials, should use chip ID
|
||||
};
|
||||
|
||||
static uint16_t _desc_str[32];
|
||||
|
||||
// Invoked when received GET STRING DESCRIPTOR request
|
||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
||||
{
|
||||
(void) langid;
|
||||
|
||||
uint8_t chr_count;
|
||||
|
||||
if ( index == 0)
|
||||
{
|
||||
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
||||
chr_count = 1;
|
||||
}else
|
||||
{
|
||||
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
||||
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
||||
|
||||
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
|
||||
|
||||
const char* str = string_desc_arr[index];
|
||||
|
||||
// Cap at max char
|
||||
chr_count = (uint8_t) strlen(str);
|
||||
if ( chr_count > 31 ) chr_count = 31;
|
||||
|
||||
// Convert ASCII string into UTF-16
|
||||
for(uint8_t i=0; i<chr_count; i++)
|
||||
{
|
||||
_desc_str[1+i] = str[i];
|
||||
}
|
||||
}
|
||||
|
||||
// first byte is length (including header), second byte is string type
|
||||
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8 ) | (2*chr_count + 2));
|
||||
|
||||
return _desc_str;
|
||||
}
|
37
fw/usb_descriptors.h
Normal file
37
fw/usb_descriptors.h
Normal file
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef USB_DESCRIPTORS_H_
|
||||
#define USB_DESCRIPTORS_H_
|
||||
|
||||
enum
|
||||
{
|
||||
REPORT_ID_KEYBOARD = 1,
|
||||
REPORT_ID_MOUSE,
|
||||
REPORT_ID_CONSUMER_CONTROL,
|
||||
REPORT_ID_GAMEPAD,
|
||||
REPORT_ID_COUNT
|
||||
};
|
||||
|
||||
#endif /* USB_DESCRIPTORS_H_ */
|
114
fw/usbapp.c
Normal file
114
fw/usbapp.c
Normal file
|
@ -0,0 +1,114 @@
|
|||
//
|
||||
// Copyright 2024 Wenting Zhang <zephray@outlook.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
#include <stdio.h>
|
||||
#include "pico/stdlib.h"
|
||||
#include "fpga.h"
|
||||
#include "caster.h"
|
||||
#include "tusb.h"
|
||||
#include "usb_descriptors.h"
|
||||
|
||||
void usbapp_init(void) {
|
||||
tusb_init();
|
||||
}
|
||||
|
||||
// Device callbacks
|
||||
void tud_mount_cb(void) {
|
||||
|
||||
}
|
||||
|
||||
void tud_umount_cb(void) {
|
||||
|
||||
}
|
||||
|
||||
void tud_suspend_cb(bool remote_wakeup_en) {
|
||||
// TODO: Force Suspend
|
||||
}
|
||||
|
||||
void tud_resume_cb(void) {
|
||||
|
||||
}
|
||||
|
||||
// Invoked when received GET_REPORT control request
|
||||
// Application must fill buffer report's content and return its length.
|
||||
// Return zero will cause the stack to STALL request
|
||||
uint16_t tud_hid_get_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen)
|
||||
{
|
||||
// TODO not Implemented
|
||||
(void) instance;
|
||||
(void) report_id;
|
||||
(void) report_type;
|
||||
(void) buffer;
|
||||
(void) reqlen;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Invoked when received SET_REPORT control request or
|
||||
// received data on OUT endpoint ( Report ID = 0, Type = 0 )
|
||||
void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize)
|
||||
{
|
||||
// This example doesn't use multiple report and report ID
|
||||
(void) instance;
|
||||
(void) report_id;
|
||||
(void) report_type;
|
||||
|
||||
// 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) {
|
||||
tud_task();
|
||||
}
|
32
fw/usbapp.h
Normal file
32
fw/usbapp.h
Normal file
|
@ -0,0 +1,32 @@
|
|||
//
|
||||
// Copyright 2024 Wenting Zhang <zephray@outlook.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#define USBCMD_RESET 0x00
|
||||
#define USBCMD_POWERDOWN 0x01
|
||||
#define USBCMD_POWERUP 0x02
|
||||
#define USBCMD_SETINPUT 0x03
|
||||
#define USBCMD_REDRAW 0x04
|
||||
#define USBCMD_SETMODE 0x05
|
||||
|
||||
void usbapp_init(void);
|
||||
void usbapp_task(void);
|
26
util/usb_example.py
Normal file
26
util/usb_example.py
Normal 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')
|
Loading…
Reference in a new issue