2929#include "lib/fatfs/ff.h"
3030#include "fsusermount.h"
3131
32- // for user-mountable block device
33- fs_user_mount_t * fs_user_mount ;
34-
3532STATIC mp_obj_t pyb_mount (mp_uint_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
3633 static const mp_arg_t allowed_args [] = {
3734 { MP_QSTR_readonly , MP_ARG_KW_ONLY | MP_ARG_BOOL , {.u_bool = false} },
@@ -51,46 +48,46 @@ STATIC mp_obj_t pyb_mount(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *
5148 if (device == mp_const_none ) {
5249 // umount
5350 FRESULT res = FR_NO_FILESYSTEM ;
54- if (fs_user_mount != NULL ) {
55- res = f_mount (NULL , fs_user_mount -> str , 0 );
56- m_del_obj (fs_user_mount_t , fs_user_mount );
57- fs_user_mount = NULL ;
51+ if (MP_STATE_PORT ( fs_user_mount ) != NULL ) {
52+ res = f_mount (NULL , MP_STATE_PORT ( fs_user_mount ) -> str , 0 );
53+ m_del_obj (fs_user_mount_t , MP_STATE_PORT ( fs_user_mount ) );
54+ MP_STATE_PORT ( fs_user_mount ) = NULL ;
5855 }
5956 if (res != FR_OK ) {
6057 nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError , "can't umount" ));
6158 }
6259 } else {
6360 // mount
64- if (fs_user_mount != NULL ) {
61+ if (MP_STATE_PORT ( fs_user_mount ) != NULL ) {
6562 nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError , "device already mounted" ));
6663 }
6764
6865 // create new object
69- fs_user_mount = m_new_obj (fs_user_mount_t );
70- fs_user_mount -> str = mnt_str ;
71- fs_user_mount -> len = mnt_len ;
66+ MP_STATE_PORT ( fs_user_mount ) = m_new_obj (fs_user_mount_t );
67+ MP_STATE_PORT ( fs_user_mount ) -> str = mnt_str ;
68+ MP_STATE_PORT ( fs_user_mount ) -> len = mnt_len ;
7269
7370 // load block protocol methods
74- mp_load_method (device , MP_QSTR_readblocks , fs_user_mount -> readblocks );
75- mp_load_method_maybe (device , MP_QSTR_writeblocks , fs_user_mount -> writeblocks );
76- mp_load_method_maybe (device , MP_QSTR_sync , fs_user_mount -> sync );
77- mp_load_method (device , MP_QSTR_count , fs_user_mount -> count );
71+ mp_load_method (device , MP_QSTR_readblocks , MP_STATE_PORT ( fs_user_mount ) -> readblocks );
72+ mp_load_method_maybe (device , MP_QSTR_writeblocks , MP_STATE_PORT ( fs_user_mount ) -> writeblocks );
73+ mp_load_method_maybe (device , MP_QSTR_sync , MP_STATE_PORT ( fs_user_mount ) -> sync );
74+ mp_load_method (device , MP_QSTR_count , MP_STATE_PORT ( fs_user_mount ) -> count );
7875
7976 // Read-only device indicated by writeblocks[0] == MP_OBJ_NULL.
8077 // User can specify read-only device by:
8178 // 1. readonly=True keyword argument
8279 // 2. nonexistent writeblocks method (then writeblocks[0] == MP_OBJ_NULL already)
8380 if (args [0 ].u_bool ) {
84- fs_user_mount -> writeblocks [0 ] = MP_OBJ_NULL ;
81+ MP_STATE_PORT ( fs_user_mount ) -> writeblocks [0 ] = MP_OBJ_NULL ;
8582 }
8683
8784 // mount the block device
88- FRESULT res = f_mount (& fs_user_mount -> fatfs , fs_user_mount -> str , 1 );
85+ FRESULT res = f_mount (& MP_STATE_PORT ( fs_user_mount ) -> fatfs , MP_STATE_PORT ( fs_user_mount ) -> str , 1 );
8986
9087 // check the result
9188 if (res == FR_OK ) {
9289 } else if (res == FR_NO_FILESYSTEM && args [1 ].u_bool ) {
93- res = f_mkfs (fs_user_mount -> str , 1 , 0 );
90+ res = f_mkfs (MP_STATE_PORT ( fs_user_mount ) -> str , 1 , 0 );
9491 if (res != FR_OK ) {
9592 nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError , "can't mkfs" ));
9693 }
@@ -99,15 +96,15 @@ STATIC mp_obj_t pyb_mount(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *
9996 }
10097
10198 /*
102- if (fs_user_mount->writeblocks[0] == MP_OBJ_NULL) {
99+ if (MP_STATE_PORT( fs_user_mount) ->writeblocks[0] == MP_OBJ_NULL) {
103100 printf("mounted read-only");
104101 } else {
105102 printf("mounted read-write");
106103 }
107104 DWORD nclst;
108105 FATFS *fatfs;
109- f_getfree(fs_user_mount. str, &nclst, &fatfs);
110- printf(" on %s with %u bytes free\n", fs_user_mount. str, (uint)(nclst * fatfs->csize * 512));
106+ f_getfree(MP_STATE_PORT( fs_user_mount)-> str, &nclst, &fatfs);
107+ printf(" on %s with %u bytes free\n", MP_STATE_PORT( fs_user_mount)-> str, (uint)(nclst * fatfs->csize * 512));
111108 */
112109 }
113110 return mp_const_none ;
0 commit comments