39
39
#include "supervisor/shared/translate/translate.h"
40
40
#include "supervisor/shared/workflow.h"
41
41
42
+ #if CIRCUITPY_USB_IDENTIFICATION
43
+ #include "supervisor/usb.h"
44
+ #endif
45
+
42
46
#include "shared-bindings/microcontroller/__init__.h"
43
47
#include "shared-bindings/supervisor/__init__.h"
44
48
#include "shared-bindings/time/__init__.h"
@@ -298,6 +302,71 @@ STATIC mp_obj_t supervisor_reset_terminal(mp_obj_t x_pixels, mp_obj_t y_pixels)
298
302
}
299
303
MP_DEFINE_CONST_FUN_OBJ_2 (supervisor_reset_terminal_obj , supervisor_reset_terminal );
300
304
305
+ //| def set_usb_identification(manufacturer: Optional[str] = None, product: Optional[str] = None, vid: int = -1, pid: int = -1) -> None:
306
+ //| """Override identification constants in the USB Device Descriptor.
307
+ //|
308
+ //| If passed, `manufacturer` and `product` must be ASCII strings (or buffers) of at most 126
309
+ //| characters. Any omitted arguments will be left at their default values.
310
+ //|
311
+ //| This method must be called in boot.py to have any effect.
312
+
313
+ //| Not available on boards without native USB support.
314
+ //| """
315
+ //| ...
316
+ //|
317
+ STATIC mp_obj_t supervisor_set_usb_identification (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
318
+ #if CIRCUITPY_USB_IDENTIFICATION
319
+ static const mp_arg_t allowed_args [] = {
320
+ { MP_QSTR_manufacturer , MP_ARG_OBJ , {.u_rom_obj = mp_const_none } },
321
+ { MP_QSTR_product , MP_ARG_OBJ , {.u_rom_obj = mp_const_none } },
322
+ { MP_QSTR_vid , MP_ARG_INT , {.u_int = -1 } },
323
+ { MP_QSTR_pid , MP_ARG_INT , {.u_int = -1 } },
324
+ };
325
+ struct {
326
+ mp_arg_val_t manufacturer ;
327
+ mp_arg_val_t product ;
328
+ mp_arg_val_t vid ;
329
+ mp_arg_val_t pid ;
330
+ } args ;
331
+ mp_arg_parse_all (n_args , pos_args , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , (mp_arg_val_t * )& args );
332
+
333
+ if (!usb_identification_allocation ) {
334
+ usb_identification_allocation = allocate_memory (sizeof (usb_identification_t ), false, true);
335
+ }
336
+ usb_identification_t * identification = (usb_identification_t * )usb_identification_allocation -> ptr ;
337
+
338
+ mp_arg_validate_int_range (args .vid .u_int , -1 , (1 << 16 ) - 1 , MP_QSTR_vid );
339
+ mp_arg_validate_int_range (args .pid .u_int , -1 , (1 << 16 ) - 1 , MP_QSTR_pid );
340
+
341
+ identification -> vid = args .vid .u_int > -1 ? args .vid .u_int : USB_VID ;
342
+ identification -> pid = args .pid .u_int > -1 ? args .pid .u_int : USB_PID ;
343
+
344
+ mp_buffer_info_t info ;
345
+ if (args .manufacturer .u_obj != mp_const_none ) {
346
+ mp_get_buffer_raise (args .manufacturer .u_obj , & info , MP_BUFFER_READ );
347
+ mp_arg_validate_length_range (info .len , 0 , 126 , MP_QSTR_manufacturer );
348
+ memcpy (identification -> manufacturer_name , info .buf , info .len );
349
+ identification -> manufacturer_name [info .len ] = 0 ;
350
+ } else {
351
+ strcpy (identification -> manufacturer_name , USB_MANUFACTURER );
352
+ }
353
+
354
+ if (args .product .u_obj != mp_const_none ) {
355
+ mp_get_buffer_raise (args .product .u_obj , & info , MP_BUFFER_READ );
356
+ mp_arg_validate_length_range (info .len , 0 , 126 , MP_QSTR_product );
357
+ memcpy (identification -> product_name , info .buf , info .len );
358
+ identification -> product_name [info .len ] = 0 ;
359
+ } else {
360
+ strcpy (identification -> product_name , USB_PRODUCT );
361
+ }
362
+
363
+ return mp_const_none ;
364
+ #else
365
+ mp_raise_NotImplementedError (NULL );
366
+ #endif
367
+ }
368
+ MP_DEFINE_CONST_FUN_OBJ_KW (supervisor_set_usb_identification_obj , 0 , supervisor_set_usb_identification );
369
+
301
370
STATIC const mp_rom_map_elem_t supervisor_module_globals_table [] = {
302
371
{ MP_ROM_QSTR (MP_QSTR___name__ ), MP_ROM_QSTR (MP_QSTR_supervisor ) },
303
372
{ MP_ROM_QSTR (MP_QSTR_set_rgb_status_brightness ), MP_ROM_PTR (& supervisor_set_rgb_status_brightness_obj ) },
@@ -310,6 +379,7 @@ STATIC const mp_rom_map_elem_t supervisor_module_globals_table[] = {
310
379
{ MP_ROM_QSTR (MP_QSTR_get_previous_traceback ), MP_ROM_PTR (& supervisor_get_previous_traceback_obj ) },
311
380
{ MP_ROM_QSTR (MP_QSTR_disable_ble_workflow ), MP_ROM_PTR (& supervisor_disable_ble_workflow_obj ) },
312
381
{ MP_ROM_QSTR (MP_QSTR_reset_terminal ), MP_ROM_PTR (& supervisor_reset_terminal_obj ) },
382
+ { MP_ROM_QSTR (MP_QSTR_set_usb_identification ), MP_ROM_PTR (& supervisor_set_usb_identification_obj ) },
313
383
{ MP_ROM_QSTR (MP_QSTR_status_bar ), MP_ROM_PTR (& shared_module_supervisor_status_bar_obj ) },
314
384
};
315
385
0 commit comments