2929#include "py/runtime.h"
3030#include "extmod/vfs.h"
3131#include "samd_soc.h"
32- #include "hal_flash.h"
32+
33+ #if MICROPY_HW_MCUFLASH
3334
3435// ASF 4
3536#include "hal_flash.h"
@@ -62,18 +63,9 @@ STATIC samd_flash_obj_t samd_flash_obj = {
6263 .flash_size = (uint32_t )& _sflash_fs , // Get from MCU-Specific loader script.
6364};
6465
65- // FLASH stuff
66- STATIC mp_obj_t samd_flash_make_new (const mp_obj_type_t * type , size_t n_args , size_t n_kw , const mp_obj_t * all_args ) {
67- // No args required. bdev=Flash(). Start Addr & Size defined in samd_flash_obj.
68- mp_arg_check_num (n_args , n_kw , 0 , 0 , false);
69-
70- // Return singleton object.
71- return MP_OBJ_FROM_PTR (& samd_flash_obj );
72- }
73-
7466// Flash init (from cctpy)
7567// Method is needed for when MP starts up in _boot.py
76- STATIC mp_obj_t samd_flash_init (void ) {
68+ STATIC void samd_flash_init (void ) {
7769 #ifdef SAMD51
7870 hri_mclk_set_AHBMASK_NVMCTRL_bit (MCLK );
7971 #endif
@@ -82,9 +74,17 @@ STATIC mp_obj_t samd_flash_init(void) {
8274 #endif
8375
8476 flash_init (& flash_desc , NVMCTRL );
85- return mp_const_none ;
8677}
87- STATIC MP_DEFINE_CONST_FUN_OBJ_0 (samd_flash_init_obj , samd_flash_init );
78+
79+ STATIC mp_obj_t samd_flash_make_new (const mp_obj_type_t * type , size_t n_args , size_t n_kw , const mp_obj_t * all_args ) {
80+ // No args required. bdev=Flash(). Start Addr & Size defined in samd_flash_obj.
81+ mp_arg_check_num (n_args , n_kw , 0 , 0 , false);
82+
83+ samd_flash_init ();
84+
85+ // Return singleton object.
86+ return MP_OBJ_FROM_PTR (& samd_flash_obj );
87+ }
8888
8989// Function for ioctl.
9090STATIC mp_obj_t eraseblock (uint32_t sector_in ) {
@@ -102,14 +102,6 @@ STATIC mp_obj_t samd_flash_version(void) {
102102}
103103STATIC MP_DEFINE_CONST_FUN_OBJ_0 (samd_flash_version_obj , samd_flash_version );
104104
105- STATIC mp_obj_t samd_flash_size (void ) {
106- // ASF4 API calls
107- mp_int_t PAGES = flash_get_total_pages (& flash_desc );
108- mp_int_t PAGE_SIZE = flash_get_page_size (& flash_desc );
109- return MP_OBJ_NEW_SMALL_INT (PAGES * PAGE_SIZE );
110- }
111- STATIC MP_DEFINE_CONST_FUN_OBJ_0 (samd_flash_size_obj , samd_flash_size );
112-
113105STATIC mp_obj_t samd_flash_readblocks (size_t n_args , const mp_obj_t * args ) {
114106 uint32_t offset = (mp_obj_get_int (args [1 ]) * BLOCK_SIZE ) + samd_flash_obj .flash_base ;
115107 mp_buffer_info_t bufinfo ;
@@ -171,8 +163,6 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_3(samd_flash_ioctl_obj, samd_flash_ioctl);
171163
172164STATIC const mp_rom_map_elem_t samd_flash_locals_dict_table [] = {
173165 { MP_ROM_QSTR (MP_QSTR_flash_version ), MP_ROM_PTR (& samd_flash_version_obj ) },
174- { MP_ROM_QSTR (MP_QSTR_flash_size ), MP_ROM_PTR (& samd_flash_size_obj ) },
175- { MP_ROM_QSTR (MP_QSTR_flash_init ), MP_ROM_PTR (& samd_flash_init_obj ) },
176166 { MP_ROM_QSTR (MP_QSTR_readblocks ), MP_ROM_PTR (& samd_flash_readblocks_obj ) },
177167 { MP_ROM_QSTR (MP_QSTR_writeblocks ), MP_ROM_PTR (& samd_flash_writeblocks_obj ) },
178168 { MP_ROM_QSTR (MP_QSTR_ioctl ), MP_ROM_PTR (& samd_flash_ioctl_obj ) },
@@ -186,3 +176,5 @@ MP_DEFINE_CONST_OBJ_TYPE(
186176 make_new , samd_flash_make_new ,
187177 locals_dict , & samd_flash_locals_dict
188178 );
179+
180+ #endif // MICROPY_HW_MCUFLASH
0 commit comments