Skip to content

Commit 8d8fdcb

Browse files
tabbouddpgeorge
authored andcommitted
stmhal: add option to query for the current usb mode
Fetch the current usb mode and return a string representation when pyb.usb_mode() is called with no args. The possible string values are interned as qstr's. None will be returned if an incorrect mode is set.
1 parent 821b7f2 commit 8d8fdcb

5 files changed

Lines changed: 48 additions & 1 deletion

File tree

py/makeqstrdata.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
codepoint2name[ord('*')] = 'star'
3636
codepoint2name[ord('!')] = 'bang'
3737
codepoint2name[ord('\\')] = 'backslash'
38+
codepoint2name[ord('+')] = 'plus'
3839

3940
# this must match the equivalent function in qstr.c
4041
def compute_hash(qstr, bytes_hash):

stmhal/qstrdefsport.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ Q(hid)
9797
Q(hid_mouse)
9898
Q(hid_keyboard)
9999

100+
// for usb modes
101+
Q(host)
102+
Q(VCP)
103+
Q(MSC)
104+
Q(HID)
105+
Q(MSC+HID)
106+
Q(VCP+MSC)
107+
Q(VCP+HID)
108+
// CDC is a synonym for VCP for backwards compatibility
109+
Q(CDC)
110+
Q(CDC+MSC)
111+
Q(CDC+HID)
112+
100113
// for USB VCP class
101114
Q(USB_VCP)
102115
Q(setinterrupt)

stmhal/usb.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ void usb_vcp_send_strn_cooked(const char *str, int len) {
179179
180180
We have:
181181
182+
pyb.usb_mode() # return the current usb mode
182183
pyb.usb_mode(None) # disable USB
183184
pyb.usb_mode('VCP') # enable with VCP interface
184185
pyb.usb_mode('VCP+MSC') # enable with VCP and MSC interfaces
@@ -205,6 +206,31 @@ STATIC mp_obj_t pyb_usb_mode(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_
205206
{ MP_QSTR_hid, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = (mp_obj_t)&pyb_usb_hid_mouse_obj} },
206207
};
207208

209+
// fetch the current usb mode -> pyb.usb_mode()
210+
if (n_args == 0) {
211+
#if defined(USE_HOST_MODE)
212+
return MP_OBJ_NEW_QSTR(MP_QSTR_host);
213+
#elif defined(USE_DEVICE_MODE)
214+
uint8_t mode = USBD_GetMode();
215+
switch (mode) {
216+
case USBD_MODE_CDC:
217+
return MP_OBJ_NEW_QSTR(MP_QSTR_VCP);
218+
case USBD_MODE_MSC:
219+
return MP_OBJ_NEW_QSTR(MP_QSTR_MSC);
220+
case USBD_MODE_HID:
221+
return MP_OBJ_NEW_QSTR(MP_QSTR_HID);
222+
case USBD_MODE_CDC_MSC:
223+
return MP_OBJ_NEW_QSTR(MP_QSTR_VCP_plus_MSC);
224+
case USBD_MODE_CDC_HID:
225+
return MP_OBJ_NEW_QSTR(MP_QSTR_VCP_plus_HID);
226+
case USBD_MODE_MSC_HID:
227+
return MP_OBJ_NEW_QSTR(MP_QSTR_MSC_plus_HID);
228+
default:
229+
return mp_const_none;
230+
}
231+
#endif
232+
}
233+
208234
// parse args
209235
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
210236
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
@@ -296,7 +322,7 @@ STATIC mp_obj_t pyb_usb_mode(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_
296322
bad_mode:
297323
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "bad USB mode"));
298324
}
299-
MP_DEFINE_CONST_FUN_OBJ_KW(pyb_usb_mode_obj, 1, pyb_usb_mode);
325+
MP_DEFINE_CONST_FUN_OBJ_KW(pyb_usb_mode_obj, 0, pyb_usb_mode);
300326

301327
/******************************************************************************/
302328
// Micro Python bindings for USB VCP

stmhal/usbdev/class/inc/usbd_cdc_msc_hid.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ extern USBD_ClassTypeDef USBD_CDC_MSC_HID;
9494

9595
// returns 0 on success, -1 on failure
9696
int USBD_SelectMode(uint32_t mode, USBD_HID_ModeInfoTypeDef *hid_info);
97+
// returns the current usb mode
98+
uint8_t USBD_GetMode();
9799

98100
uint8_t USBD_CDC_RegisterInterface (USBD_HandleTypeDef *pdev, USBD_CDC_ItfTypeDef *fops);
99101
uint8_t USBD_CDC_SetTxBuffer (USBD_HandleTypeDef *pdev, uint8_t *pbuff, uint16_t length);

stmhal/usbdev/class/src/usbd_cdc_msc_hid.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,11 @@ __ALIGN_BEGIN const uint8_t USBD_HID_KEYBOARD_ReportDesc[USBD_HID_KEYBOARD_REPOR
559559
0xC0 // End Collection
560560
};
561561

562+
// return the saved usb mode
563+
uint8_t USBD_GetMode() {
564+
return usbd_mode;
565+
}
566+
562567
int USBD_SelectMode(uint32_t mode, USBD_HID_ModeInfoTypeDef *hid_info) {
563568
// save mode
564569
usbd_mode = mode;

0 commit comments

Comments
 (0)