6060//| the host.
6161//| Note that `data` is *disabled* by default."""
6262
63-
64-
65-
66- //| def enable_repl(enabled:bool) -> None:
67- //| """Enable or disable the `repl` USB serial connection. The REPL
68- //| is enabled by default.
69- //| Can be changed in ``boot.py``, before USB is connected."""
70- //| ...
63+ //| def configure_usb(repl_enabled: bool, data_enabled: bool) -> None:
64+ //| """Configure the USB CDC devices. Can be called in ``boot.py``, before USB is connected.
7165//|
72- STATIC mp_obj_t usb_cdc_enable_repl (mp_obj_t enabled ) {
73- if (!common_hal_usb_cdc_enable_repl (mp_obj_is_true (enabled ))) {
74- mp_raise_RuntimeError (translate ("Cannot change USB devices now" ));
75- }
76- return mp_const_none ;
77- }
78- MP_DEFINE_CONST_FUN_OBJ_1 (usb_cdc_enable_repl_obj , usb_cdc_enable_repl );
79-
80- //| def enable_data(enabled: bool) -> None:
81- //| """Enable or disable the `data` USB serial connection;n
82- //| *disabled* by default.
83- //| Can be changed in ``boot.py``, before USB is connected."""
66+ //| :param repl_enabled bool: Enable or disable the `repl` USB serial device.
67+ //| True to enable; False to disable. Enabled by default.
68+ //| :param data_enabled bool: Enable or disable the `data` USB serial device.
69+ //| True to enable; False to disable. *Disabled* by default."""
8470//| ...
8571//|
86- STATIC mp_obj_t usb_cdc_enable_data (mp_obj_t enabled ) {
87- if (!common_hal_usb_cdc_enable_data (mp_obj_is_true (enabled ))) {
72+ STATIC mp_obj_t usb_cdc_configure_usb (mp_obj_t repl_enabled , mp_obj_t data_enabled ) {
73+ if (!common_hal_usb_cdc_configure_usb (
74+ mp_obj_is_true (repl_enabled ),
75+ mp_obj_is_true (data_enabled ))) {
8876 mp_raise_RuntimeError (translate ("Cannot change USB devices now" ));
8977 }
9078 return mp_const_none ;
9179}
92- MP_DEFINE_CONST_FUN_OBJ_1 ( usb_cdc_enable_data_obj , usb_cdc_enable_data );
80+ MP_DEFINE_CONST_FUN_OBJ_2 ( usb_cdc_configure_usb_obj , usb_cdc_configure_usb );
9381
9482// The usb_cdc module dict is mutable so that .repl and .data may
9583// be set to a Serial or to None depending on whether they are enabled or not.
9684static mp_map_elem_t usb_cdc_module_globals_table [] = {
97- { MP_ROM_QSTR (MP_QSTR___name__ ), MP_ROM_QSTR (MP_QSTR_usb_cdc ) },
98- { MP_ROM_QSTR (MP_QSTR_Serial ), MP_OBJ_FROM_PTR (& usb_cdc_serial_type ) },
99- { MP_ROM_QSTR (MP_QSTR_repl ), mp_const_none },
100- { MP_ROM_QSTR (MP_QSTR_data ), mp_const_none },
101- { MP_ROM_QSTR (MP_QSTR_enable_repl ), MP_OBJ_FROM_PTR (& usb_cdc_enable_repl_obj ) },
102- { MP_ROM_QSTR (MP_QSTR_enable_data ), MP_OBJ_FROM_PTR (& usb_cdc_enable_data_obj ) },
85+ { MP_ROM_QSTR (MP_QSTR___name__ ), MP_ROM_QSTR (MP_QSTR_usb_cdc ) },
86+ { MP_ROM_QSTR (MP_QSTR_Serial ), MP_OBJ_FROM_PTR (& usb_cdc_serial_type ) },
87+ { MP_ROM_QSTR (MP_QSTR_repl ), mp_const_none },
88+ { MP_ROM_QSTR (MP_QSTR_data ), mp_const_none },
89+ { MP_ROM_QSTR (MP_QSTR_configure_usb ), MP_OBJ_FROM_PTR (& usb_cdc_configure_usb_obj ) },
10390};
10491
10592static MP_DEFINE_MUTABLE_DICT (usb_cdc_module_globals , usb_cdc_module_globals_table ) ;
@@ -108,3 +95,18 @@ const mp_obj_module_t usb_cdc_module = {
10895 .base = { & mp_type_module },
10996 .globals = (mp_obj_dict_t * )& usb_cdc_module_globals ,
11097};
98+
99+ static void set_module_dict_entry (mp_obj_t key_qstr , mp_obj_t serial_obj ) {
100+ mp_map_elem_t * elem = mp_map_lookup (& usb_cdc_module_globals .map , key_qstr , MP_MAP_LOOKUP );
101+ if (elem ) {
102+ elem -> value = serial_obj ;
103+ }
104+ }
105+
106+ void usb_cdc_set_repl (mp_obj_t serial_obj ) {
107+ set_module_dict_entry (MP_ROM_QSTR (MP_QSTR_repl ), serial_obj );
108+ }
109+
110+ void usb_cdc_set_data (mp_obj_t serial_obj ) {
111+ set_module_dict_entry (MP_ROM_QSTR (MP_QSTR_data ), serial_obj );
112+ }
0 commit comments