@@ -70,16 +70,45 @@ bi_decl(bi_block_device(
70
70
BINARY_INFO_BLOCK_DEV_FLAG_PT_UNKNOWN ));
71
71
72
72
STATIC mp_obj_t rp2_flash_make_new (const mp_obj_type_t * type , size_t n_args , size_t n_kw , const mp_obj_t * all_args ) {
73
- // Check args.
74
- mp_arg_check_num (n_args , n_kw , 0 , 0 , false);
73
+ // Parse arguments
74
+ enum { ARG_start , ARG_len };
75
+ static const mp_arg_t allowed_args [] = {
76
+ { MP_QSTR_start , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = -1 } },
77
+ { MP_QSTR_len , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = -1 } },
78
+ };
79
+ mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
80
+ mp_arg_parse_all_kw_array (n_args , n_kw , all_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
81
+
82
+ if (args [ARG_start ].u_int == -1 && args [ARG_len ].u_int == -1 ) {
83
+ #ifndef NDEBUG
84
+ extern char __flash_binary_end ;
85
+ assert ((uintptr_t )& __flash_binary_end - XIP_BASE <= MICROPY_HW_FLASH_STORAGE_BASE );
86
+ #endif
87
+
88
+ // Default singleton object that accesses entire flash
89
+ return MP_OBJ_FROM_PTR (& rp2_flash_obj );
90
+ }
91
+
92
+ rp2_flash_obj_t * self = mp_obj_malloc (rp2_flash_obj_t , & rp2_flash_type );
93
+
94
+ mp_int_t start = args [ARG_start ].u_int ;
95
+ if (start == -1 ) {
96
+ start = 0 ;
97
+ } else if (!(0 <= start && start < MICROPY_HW_FLASH_STORAGE_BYTES && start % BLOCK_SIZE_BYTES == 0 )) {
98
+ mp_raise_ValueError (NULL );
99
+ }
100
+
101
+ mp_int_t len = args [ARG_len ].u_int ;
102
+ if (len == -1 ) {
103
+ len = MICROPY_HW_FLASH_STORAGE_BYTES ;
104
+ } else if (!(0 < len && start + len <= MICROPY_HW_FLASH_STORAGE_BYTES && len % BLOCK_SIZE_BYTES == 0 )) {
105
+ mp_raise_ValueError (NULL );
106
+ }
75
107
76
- #ifndef NDEBUG
77
- extern char __flash_binary_end ;
78
- assert ((uintptr_t )& __flash_binary_end - XIP_BASE <= MICROPY_HW_FLASH_STORAGE_BASE );
79
- #endif
108
+ self -> flash_base = MICROPY_HW_FLASH_STORAGE_BASE + start ;
109
+ self -> flash_size = len ;
80
110
81
- // Return singleton object.
82
- return MP_OBJ_FROM_PTR (& rp2_flash_obj );
111
+ return MP_OBJ_FROM_PTR (self );
83
112
}
84
113
85
114
STATIC mp_obj_t rp2_flash_readblocks (size_t n_args , const mp_obj_t * args ) {
0 commit comments