Skip to content

Commit 853e48a

Browse files
committed
Add vendor endpoint to the rp2040 firmware
1 parent b1546ac commit 853e48a

File tree

4 files changed

+87
-3
lines changed

4 files changed

+87
-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: 69 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,72 @@ 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+
uint8_t to_send[6] = {9, 9, 9, 9, 9, 9};
71+
bool veandor_request_temperature_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request)
72+
{
73+
bool result = false;
74+
75+
if (request->bmRequestType_bit.direction == TUSB_DIR_IN) {
76+
io_say_f(" DIR_IN\n");
77+
switch (stage) {
78+
case CONTROL_STAGE_SETUP:
79+
result = tud_control_xfer(rhport, request, &to_send, sizeof(to_send));
80+
break;
81+
case CONTROL_STAGE_DATA:
82+
case CONTROL_STAGE_ACK:
83+
result = true;
84+
break;
85+
}
86+
} else {
87+
io_say_f(" DIR_OUT\n");
88+
switch (stage) {
89+
case CONTROL_STAGE_SETUP:
90+
result = tud_control_xfer(rhport, request, &to_send, sizeof(to_send));
91+
break;
92+
case CONTROL_STAGE_DATA:
93+
case CONTROL_STAGE_ACK:
94+
result = true;
95+
break;
96+
}
97+
}
98+
return result;
99+
}
100+
101+
bool vendor_request_temperature_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request)
102+
{
103+
if (request->bmRequestType_bit.direction != TUSB_DIR_IN)
104+
return false;
105+
106+
if (stage != CONTROL_STAGE_SETUP)
107+
return true;
108+
109+
uint8_t reg = 0;
110+
uint8_t buffer[2] = {0};
111+
i2c_write_blocking_until(PWR_BRD_I2C_INST, PWR_BRD_TEMP_SENS_ADDR, &reg, 1, true, make_timeout_time_ms(20));
112+
i2c_read_blocking_until(PWR_BRD_I2C_INST, PWR_BRD_TEMP_SENS_ADDR, &buffer, 2, false, make_timeout_time_ms(20));
113+
114+
return tud_control_xfer(rhport, request, &buffer, sizeof(buffer));
115+
}
116+
117+
118+
bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const* request) {
119+
bool result = false;
120+
switch (request->bmRequestType_bit.type) {
121+
case TUSB_REQ_TYPE_VENDOR:
122+
io_say_f("XFER: bRequest=%d, wValue=%d, wIndex=%d, wLength=%d\n", request->bRequest, request->wValue, request->wIndex, request->wLength);
123+
switch (request->bRequest) {
124+
case VENDOR_REQUEST_TEMPERATURE:
125+
result = vendor_request_temperature_cb(rhport, stage, request);
126+
break;
127+
}
128+
break;
129+
case TUSB_REQ_TYPE_STANDARD:
130+
io_say_f("STANDARD: bRequest=%d\n", request->bRequest);
131+
break;
132+
case TUSB_REQ_TYPE_CLASS:
133+
io_say_f("CLASS: bRequest=%d\n", request->bRequest);
134+
break;
135+
}
136+
return result;
137+
}

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

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

88
#define USB_ENABLED (USB_INSTANCE >= 0)
99

10+
#define VENDOR_REQUEST_TEMPERATURE (0x10)
11+
1012
void usb_init(void);
1113
void usb_task(void);
1214

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)