Skip to content

Commit 1a98c6b

Browse files
committed
Add vendor endpoint to the rp2040 firmware
1 parent 68bbdb3 commit 1a98c6b

File tree

4 files changed

+74
-3
lines changed

4 files changed

+74
-3
lines changed

hardware/firmware/box_rp2040/src/usb/tusb_config.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,11 @@
6464
#define CFG_TUD_MSC 0
6565
#define CFG_TUD_HID 0
6666
#define CFG_TUD_MIDI 0
67-
#define CFG_TUD_VENDOR 0
67+
#define CFG_TUD_VENDOR 1
6868

6969
// CDC FIFO size of TX and RX
7070
#define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
7171
#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
72+
73+
#define CFG_TUD_VENDOR_RX_BUFSIZE 1024
74+
#define CFG_TUD_VENDOR_TX_BUFSIZE 1024

hardware/firmware/box_rp2040/src/usb/usb.c

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "usb.h"
22
#include "config.h"
33
#include "pico/bootrom.h"
4-
54
// TODO: implement vendor-reset interface
65

76
void usb_init(void) {
@@ -67,3 +66,58 @@ void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* p_line_coding)
6766
reset_usb_boot(0, 0);
6867
}
6968
}
69+
70+
bool vendor_request_temperature_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request)
71+
{
72+
if (request->bmRequestType_bit.direction != TUSB_DIR_IN)
73+
return false;
74+
75+
if (stage != CONTROL_STAGE_SETUP)
76+
return true;
77+
78+
uint8_t reg = 0;
79+
uint8_t buffer[2] = {0};
80+
i2c_write_blocking_until(PWR_BRD_I2C_INST, PWR_BRD_TEMP_SENS_ADDR, &reg, 1, true, make_timeout_time_ms(20));
81+
i2c_read_blocking_until(PWR_BRD_I2C_INST, PWR_BRD_TEMP_SENS_ADDR, &buffer, 2, false, make_timeout_time_ms(20));
82+
io_say_f(" TEMP: [%d, %d]\r\n", buffer[0], buffer[1]);
83+
84+
return tud_control_xfer(rhport, request, &buffer, sizeof(buffer));
85+
}
86+
87+
bool vendor_request_fanspeed_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request)
88+
{
89+
if (request->bmRequestType_bit.direction != TUSB_DIR_IN)
90+
return false;
91+
92+
if (stage != CONTROL_STAGE_SETUP)
93+
return true;
94+
95+
uint8_t buffer[2] = {0};
96+
uint16_t fs = 0;
97+
fan_ctl_get_fan_speed(request->wIndex, &fs);
98+
buffer[0] = (fs>>8) & 0xFF;
99+
buffer[1] = (fs>>0) & 0xFF;
100+
io_say_f(" FANSPEED %d: %d\r\n", request->wIndex, fs);
101+
return tud_control_xfer(rhport, request, &buffer, sizeof(buffer));
102+
}
103+
104+
105+
bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const* request) {
106+
bool result = false;
107+
switch (request->bmRequestType_bit.type) {
108+
case TUSB_REQ_TYPE_VENDOR:
109+
io_say_f("XFER: bRequest=%d, wValue=%d, wIndex=%d, wLength=%d\r\n", request->bRequest, request->wValue, request->wIndex, request->wLength);
110+
switch (request->bRequest) {
111+
case VENDOR_REQUEST_TEMPERATURE:
112+
result = vendor_request_temperature_cb(rhport, stage, request);
113+
break;
114+
case VENDOR_REQUEST_FANSPEED:
115+
result = vendor_request_fanspeed_cb(rhport, stage, request);
116+
break;
117+
}
118+
break;
119+
default:
120+
break;
121+
}
122+
return result;
123+
}

hardware/firmware/box_rp2040/src/usb/usb.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
#define USB_ENABLED (USB_INSTANCE >= 0)
99

10+
#define VENDOR_REQUEST_TEMPERATURE (0x10)
11+
#define VENDOR_REQUEST_FANSPEED (0x11)
12+
1013
void usb_init(void);
1114
void usb_task(void);
1215

hardware/firmware/box_rp2040/src/usb/usb_descriptors.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,27 @@ uint8_t const * tud_descriptor_device_cb(void)
4949
enum {
5050
ITF_NUM_CDC = 0,
5151
ITF_NUM_CDC_DATA,
52+
ITF_NUM_VENDOR,
5253
ITF_NUM_TOTAL
5354
};
5455

55-
#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN)
56+
#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_VENDOR_DESC_LEN)
5657

5758
#define EPNUM_CDC_NOTIF 0x81
5859
#define EPNUM_CDC_OUT 0x02
5960
#define EPNUM_CDC_IN 0x82
61+
#define EPNUM_VENDOR_OUT 0x03
62+
#define EPNUM_VENDOR_IN 0x83
6063

6164
uint8_t const desc_fs_configuration[] = {
6265
// Config number, interface count, string index, total length, attribute, power in mA
6366
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100),
6467

6568
// Interface number, string index, EP Out & EP In address, EP size
6669
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT, EPNUM_CDC_IN, 64),
70+
71+
// Interface number, string index, EP Out & EP In address, EP size
72+
TUD_VENDOR_DESCRIPTOR(ITF_NUM_VENDOR, 5, EPNUM_VENDOR_OUT, EPNUM_VENDOR_IN, 64),
6773
};
6874

6975
#if TUD_OPT_HIGH_SPEED
@@ -73,6 +79,9 @@ uint8_t const desc_hs_configuration[] = {
7379

7480
// Interface number, string index, EP Out & EP In address, EP size
7581
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT, EPNUM_CDC_IN, 512),
82+
83+
// Interface number, string index, EP Out & EP In address, EP size
84+
TUD_VENDOR_DESCRIPTOR(ITF_NUM_VENDOR, 5, EPNUM_VENDOR_OUT, EPNUM_VENDOR_IN, 1024),
7685
};
7786
#endif
7887

@@ -102,6 +111,8 @@ char const* string_desc_arr [] =
102111
"FOSDEM", // 1: Manufacturer
103112
"Krab controller", // 2: Product
104113
"123456", // 3: Serials, should use chip ID
114+
"Serial port",
115+
"Vendor port",
105116
};
106117

107118
static uint16_t _desc_str[32];

0 commit comments

Comments
 (0)