Commit 8d8fdcb4 authored by Tony Abboud's avatar Tony Abboud Committed by Damien George
Browse files

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.
parent 821b7f22
......@@ -35,6 +35,7 @@ codepoint2name[ord('}')] = 'brace_close'
codepoint2name[ord('*')] = 'star'
codepoint2name[ord('!')] = 'bang'
codepoint2name[ord('\\')] = 'backslash'
codepoint2name[ord('+')] = 'plus'
# this must match the equivalent function in qstr.c
def compute_hash(qstr, bytes_hash):
......
......@@ -97,6 +97,19 @@ Q(hid)
Q(hid_mouse)
Q(hid_keyboard)
// for usb modes
Q(host)
Q(VCP)
Q(MSC)
Q(HID)
Q(MSC+HID)
Q(VCP+MSC)
Q(VCP+HID)
// CDC is a synonym for VCP for backwards compatibility
Q(CDC)
Q(CDC+MSC)
Q(CDC+HID)
// for USB VCP class
Q(USB_VCP)
Q(setinterrupt)
......
......@@ -179,6 +179,7 @@ void usb_vcp_send_strn_cooked(const char *str, int len) {
We have:
pyb.usb_mode() # return the current usb mode
pyb.usb_mode(None) # disable USB
pyb.usb_mode('VCP') # enable with VCP interface
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_
{ MP_QSTR_hid, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = (mp_obj_t)&pyb_usb_hid_mouse_obj} },
};
// fetch the current usb mode -> pyb.usb_mode()
if (n_args == 0) {
#if defined(USE_HOST_MODE)
return MP_OBJ_NEW_QSTR(MP_QSTR_host);
#elif defined(USE_DEVICE_MODE)
uint8_t mode = USBD_GetMode();
switch (mode) {
case USBD_MODE_CDC:
return MP_OBJ_NEW_QSTR(MP_QSTR_VCP);
case USBD_MODE_MSC:
return MP_OBJ_NEW_QSTR(MP_QSTR_MSC);
case USBD_MODE_HID:
return MP_OBJ_NEW_QSTR(MP_QSTR_HID);
case USBD_MODE_CDC_MSC:
return MP_OBJ_NEW_QSTR(MP_QSTR_VCP_plus_MSC);
case USBD_MODE_CDC_HID:
return MP_OBJ_NEW_QSTR(MP_QSTR_VCP_plus_HID);
case USBD_MODE_MSC_HID:
return MP_OBJ_NEW_QSTR(MP_QSTR_MSC_plus_HID);
default:
return mp_const_none;
}
#endif
}
// parse args
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
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_
bad_mode:
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "bad USB mode"));
}
MP_DEFINE_CONST_FUN_OBJ_KW(pyb_usb_mode_obj, 1, pyb_usb_mode);
MP_DEFINE_CONST_FUN_OBJ_KW(pyb_usb_mode_obj, 0, pyb_usb_mode);
/******************************************************************************/
// Micro Python bindings for USB VCP
......
......@@ -94,6 +94,8 @@ extern USBD_ClassTypeDef USBD_CDC_MSC_HID;
// returns 0 on success, -1 on failure
int USBD_SelectMode(uint32_t mode, USBD_HID_ModeInfoTypeDef *hid_info);
// returns the current usb mode
uint8_t USBD_GetMode();
uint8_t USBD_CDC_RegisterInterface (USBD_HandleTypeDef *pdev, USBD_CDC_ItfTypeDef *fops);
uint8_t USBD_CDC_SetTxBuffer (USBD_HandleTypeDef *pdev, uint8_t *pbuff, uint16_t length);
......
......@@ -559,6 +559,11 @@ __ALIGN_BEGIN const uint8_t USBD_HID_KEYBOARD_ReportDesc[USBD_HID_KEYBOARD_REPOR
0xC0 // End Collection
};
// return the saved usb mode
uint8_t USBD_GetMode() {
return usbd_mode;
}
int USBD_SelectMode(uint32_t mode, USBD_HID_ModeInfoTypeDef *hid_info) {
// save mode
usbd_mode = mode;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment