Skip to content

Commit 13a4c12

Browse files
committed
lib/fatfs: Add support for sector sizes larger than 512 bytes.
If MICROPY_FATFS_MAX_SS is defined to power of 2 value between 1024 and 4096, support for dynamic sector size in FatFs will be enabled. Note that FatFs reserves static buffer of MICROPY_FATFS_MAX_SS size for each filesystem in use, so that value should be set sparingly. Initial patch provided by @pfalcon.
1 parent c33ad60 commit 13a4c12

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

lib/fatfs/ffconf.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,11 @@
217217

218218

219219
#define _MIN_SS 512
220+
#ifdef MICROPY_FATFS_MAX_SS
221+
#define _MAX_SS (MICROPY_FATFS_MAX_SS)
222+
#else
220223
#define _MAX_SS 512
224+
#endif
221225
/* These options configure the range of sector size to be supported. (512, 1024,
222226
/ 2048 or 4096) Always set both 512 for most systems, all type of memory cards and
223227
/ harddisk. But a larger value may be required for on-board flash memory and some

stmhal/diskio.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
//#define BP_IOCTL_DEINIT (2) // unused
4545
#define BP_IOCTL_SYNC (3)
4646
#define BP_IOCTL_SEC_COUNT (4)
47+
#define BP_IOCTL_SEC_SIZE (5)
4748

4849
/*-----------------------------------------------------------------------*/
4950
/* Initialize a Drive */
@@ -273,6 +274,15 @@ DRESULT disk_ioctl (
273274
return RES_OK;
274275
}
275276

277+
case GET_SECTOR_SIZE: {
278+
vfs->u.ioctl[2] = MP_OBJ_NEW_SMALL_INT(BP_IOCTL_SEC_SIZE);
279+
vfs->u.ioctl[3] = MP_OBJ_NEW_SMALL_INT(0); // unused
280+
mp_obj_t ret = mp_call_method_n_kw(2, 0, vfs->u.ioctl);
281+
*((WORD*)buff) = mp_obj_get_int(ret);
282+
vfs->u.ioctl[3] = MP_OBJ_SENTINEL; // indicate new protocol
283+
return RES_OK;
284+
}
285+
276286
case GET_BLOCK_SIZE:
277287
*((DWORD*)buff) = 1; // erase block size in units of sector size
278288
return RES_OK;
@@ -292,6 +302,10 @@ DRESULT disk_ioctl (
292302
return RES_OK;
293303
}
294304

305+
case GET_SECTOR_SIZE:
306+
*((WORD*)buff) = 512; // old protocol had fixed sector size
307+
return RES_OK;
308+
295309
case GET_BLOCK_SIZE:
296310
*((DWORD*)buff) = 1; // erase block size in units of sector size
297311
return RES_OK;

0 commit comments

Comments
 (0)