summaryrefslogtreecommitdiffstatshomepage
path: root/stmhal/usb.c
diff options
context:
space:
mode:
authorTony Abboud <tdabboud@hotmail.com>2015-08-30 17:20:38 -0400
committerDamien George <damien.p.george@gmail.com>2015-09-03 23:30:43 +0100
commit8d8fdcb4bec5024503b636bcdbad244bef4e6576 (patch)
tree21dcf9e6e35ec71543abd30c39c2b5368ef1b11c /stmhal/usb.c
parent821b7f22fec342cf0b1b61d959e6c07b9543c696 (diff)
downloadmicropython-8d8fdcb4bec5024503b636bcdbad244bef4e6576.tar.gz
micropython-8d8fdcb4bec5024503b636bcdbad244bef4e6576.zip
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.
Diffstat (limited to 'stmhal/usb.c')
-rw-r--r--stmhal/usb.c28
1 files changed, 27 insertions, 1 deletions
diff --git a/stmhal/usb.c b/stmhal/usb.c
index 1209ec5f12..085eae1299 100644
--- a/stmhal/usb.c
+++ b/stmhal/usb.c
@@ -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