summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorMaxwell Beck <max@rastertail.net>2025-05-29 13:05:58 -0500
committerMaxwell Beck <max@rastertail.net>2025-05-29 13:05:58 -0500
commit3ee6d4b5f90cea5f0959ed0724151551c8fb5029 (patch)
tree4e9460de709aaa7021f39624eafa997431517bf1 /src
parent5660f7984f5aee21dd248bb721987bacde290bc8 (diff)
perf: Use 4K sectors for flash access
Diffstat (limited to 'src')
-rw-r--r--src/ff/ffconf.h4
-rw-r--r--src/ff/ffsystem.c208
-rw-r--r--src/pico1541.c43
-rw-r--r--src/tusb_config.h2
4 files changed, 25 insertions, 232 deletions
diff --git a/src/ff/ffconf.h b/src/ff/ffconf.h
index caa02a6..c8370e6 100644
--- a/src/ff/ffconf.h
+++ b/src/ff/ffconf.h
@@ -193,8 +193,8 @@
 /  will be available. */

 

 

-#define FF_MIN_SS		512

-#define FF_MAX_SS		512

+#define FF_MIN_SS		4096

+#define FF_MAX_SS		4096

 /* This set of options configures the range of sector size to be supported. (512,

 /  1024, 2048 or 4096) Always set both 512 for most systems, generic memory card and

 /  harddisk, but a larger value may be required for on-board flash memory and some

diff --git a/src/ff/ffsystem.c b/src/ff/ffsystem.c
deleted file mode 100644
index 2657fe2..0000000
--- a/src/ff/ffsystem.c
+++ /dev/null
@@ -1,208 +0,0 @@
-/*------------------------------------------------------------------------*/

-/* A Sample Code of User Provided OS Dependent Functions for FatFs        */

-/*------------------------------------------------------------------------*/

-

-#include "ff.h"

-

-

-#if FF_USE_LFN == 3	/* Use dynamic memory allocation */

-

-/*------------------------------------------------------------------------*/

-/* Allocate/Free a Memory Block                                           */

-/*------------------------------------------------------------------------*/

-

-#include <stdlib.h>		/* with POSIX API */

-

-

-void* ff_memalloc (	/* Returns pointer to the allocated memory block (null if not enough core) */

-	UINT msize		/* Number of bytes to allocate */

-)

-{

-	return malloc((size_t)msize);	/* Allocate a new memory block */

-}

-

-

-void ff_memfree (

-	void* mblock	/* Pointer to the memory block to free (no effect if null) */

-)

-{

-	free(mblock);	/* Free the memory block */

-}

-

-#endif

-

-

-

-

-#if FF_FS_REENTRANT	/* Mutal exclusion */

-/*------------------------------------------------------------------------*/

-/* Definitions of Mutex                                                   */

-/*------------------------------------------------------------------------*/

-

-#define OS_TYPE	0	/* 0:Win32, 1:uITRON4.0, 2:uC/OS-II, 3:FreeRTOS, 4:CMSIS-RTOS */

-

-

-#if   OS_TYPE == 0	/* Win32 */

-#include <windows.h>

-static HANDLE Mutex[FF_VOLUMES + 1];	/* Table of mutex handle */

-

-#elif OS_TYPE == 1	/* uITRON */

-#include "itron.h"

-#include "kernel.h"

-static mtxid Mutex[FF_VOLUMES + 1];		/* Table of mutex ID */

-

-#elif OS_TYPE == 2	/* uc/OS-II */

-#include "includes.h"

-static OS_EVENT *Mutex[FF_VOLUMES + 1];	/* Table of mutex pinter */

-

-#elif OS_TYPE == 3	/* FreeRTOS */

-#include "FreeRTOS.h"

-#include "semphr.h"

-static SemaphoreHandle_t Mutex[FF_VOLUMES + 1];	/* Table of mutex handle */

-

-#elif OS_TYPE == 4	/* CMSIS-RTOS */

-#include "cmsis_os.h"

-static osMutexId Mutex[FF_VOLUMES + 1];	/* Table of mutex ID */

-

-#endif

-

-

-

-/*------------------------------------------------------------------------*/

-/* Create a Mutex                                                         */

-/*------------------------------------------------------------------------*/

-/* This function is called in f_mount function to create a new mutex

-/  or semaphore for the volume. When a 0 is returned, the f_mount function

-/  fails with FR_INT_ERR.

-*/

-

-int ff_mutex_create (	/* Returns 1:Function succeeded or 0:Could not create the mutex */

-	int vol				/* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */

-)

-{

-#if OS_TYPE == 0	/* Win32 */

-	Mutex[vol] = CreateMutex(NULL, FALSE, NULL);

-	return (int)(Mutex[vol] != INVALID_HANDLE_VALUE);

-

-#elif OS_TYPE == 1	/* uITRON */

-	T_CMTX cmtx = {TA_TPRI,1};

-

-	Mutex[vol] = acre_mtx(&cmtx);

-	return (int)(Mutex[vol] > 0);

-

-#elif OS_TYPE == 2	/* uC/OS-II */

-	OS_ERR err;

-

-	Mutex[vol] = OSMutexCreate(0, &err);

-	return (int)(err == OS_NO_ERR);

-

-#elif OS_TYPE == 3	/* FreeRTOS */

-	Mutex[vol] = xSemaphoreCreateMutex();

-	return (int)(Mutex[vol] != NULL);

-

-#elif OS_TYPE == 4	/* CMSIS-RTOS */

-	osMutexDef(cmsis_os_mutex);

-

-	Mutex[vol] = osMutexCreate(osMutex(cmsis_os_mutex));

-	return (int)(Mutex[vol] != NULL);

-

-#endif

-}

-

-

-/*------------------------------------------------------------------------*/

-/* Delete a Mutex                                                         */

-/*------------------------------------------------------------------------*/

-/* This function is called in f_mount function to delete a mutex or

-/  semaphore of the volume created with ff_mutex_create function.

-*/

-

-void ff_mutex_delete (	/* Returns 1:Function succeeded or 0:Could not delete due to an error */

-	int vol				/* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */

-)

-{

-#if OS_TYPE == 0	/* Win32 */

-	CloseHandle(Mutex[vol]);

-

-#elif OS_TYPE == 1	/* uITRON */

-	del_mtx(Mutex[vol]);

-

-#elif OS_TYPE == 2	/* uC/OS-II */

-	OS_ERR err;

-

-	OSMutexDel(Mutex[vol], OS_DEL_ALWAYS, &err);

-

-#elif OS_TYPE == 3	/* FreeRTOS */

-	vSemaphoreDelete(Mutex[vol]);

-

-#elif OS_TYPE == 4	/* CMSIS-RTOS */

-	osMutexDelete(Mutex[vol]);

-

-#endif

-}

-

-

-/*------------------------------------------------------------------------*/

-/* Request a Grant to Access the Volume                                   */

-/*------------------------------------------------------------------------*/

-/* This function is called on enter file functions to lock the volume.

-/  When a 0 is returned, the file function fails with FR_TIMEOUT.

-*/

-

-int ff_mutex_take (	/* Returns 1:Succeeded or 0:Timeout */

-	int vol			/* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */

-)

-{

-#if OS_TYPE == 0	/* Win32 */

-	return (int)(WaitForSingleObject(Mutex[vol], FF_FS_TIMEOUT) == WAIT_OBJECT_0);

-

-#elif OS_TYPE == 1	/* uITRON */

-	return (int)(tloc_mtx(Mutex[vol], FF_FS_TIMEOUT) == E_OK);

-

-#elif OS_TYPE == 2	/* uC/OS-II */

-	OS_ERR err;

-

-	OSMutexPend(Mutex[vol], FF_FS_TIMEOUT, &err));

-	return (int)(err == OS_NO_ERR);

-

-#elif OS_TYPE == 3	/* FreeRTOS */

-	return (int)(xSemaphoreTake(Mutex[vol], FF_FS_TIMEOUT) == pdTRUE);

-

-#elif OS_TYPE == 4	/* CMSIS-RTOS */

-	return (int)(osMutexWait(Mutex[vol], FF_FS_TIMEOUT) == osOK);

-

-#endif

-}

-

-

-

-/*------------------------------------------------------------------------*/

-/* Release a Grant to Access the Volume                                   */

-/*------------------------------------------------------------------------*/

-/* This function is called on leave file functions to unlock the volume.

-*/

-

-void ff_mutex_give (

-	int vol			/* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */

-)

-{

-#if OS_TYPE == 0	/* Win32 */

-	ReleaseMutex(Mutex[vol]);

-

-#elif OS_TYPE == 1	/* uITRON */

-	unl_mtx(Mutex[vol]);

-

-#elif OS_TYPE == 2	/* uC/OS-II */

-	OSMutexPost(Mutex[vol]);

-

-#elif OS_TYPE == 3	/* FreeRTOS */

-	xSemaphoreGive(Mutex[vol]);

-

-#elif OS_TYPE == 4	/* CMSIS-RTOS */

-	osMutexRelease(Mutex[vol]);

-

-#endif

-}

-

-#endif	/* FF_FS_REENTRANT */

-

diff --git a/src/pico1541.c b/src/pico1541.c
index e46c4b9..63cb74a 100644
--- a/src/pico1541.c
+++ b/src/pico1541.c
@@ -170,8 +170,8 @@ bool tud_msc_test_unit_ready_cb(uint8_t lun) {
 }
 
 void tud_msc_capacity_cb(uint8_t lun, uint32_t *block_count, uint16_t *block_size) {
-	*block_count = FS_BYTES / 512;
-	*block_size = 512;
+	*block_count = FS_BYTES / 4096;
+	*block_size = 4096;
 }
 
 bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject) {
@@ -182,19 +182,26 @@ bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, boo
 }
 
 int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void *buffer, uint32_t bufsize) {
-	if (lba >= FS_BYTES / 512) {
+	if (lba >= FS_BYTES / 4096) {
 		return -1;
 	}
 
-	uint8_t *addr = FS + 512 * lba + offset;
+	if (offset > 0) {
+		return -1;
+	}
+
+	uint8_t *addr = FS + 4096 * lba;
 	memcpy(buffer, addr, bufsize);
 
 	return (int32_t)bufsize;
 }
 
-// FIXME This is pretty naive....
 int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t *buffer, uint32_t bufsize) {
-	if (lba >= FS_BYTES / 512) {
+	if (lba >= FS_BYTES / 4096) {
+		return -1;
+	}
+
+	if (offset > 0) {
 		return -1;
 	}
 
@@ -202,22 +209,16 @@ int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t *
 	uint32_t sect_ofs = lba * 512 + offset - flash_sect;
 	for (uint32_t i = 0; i < bufsize; i += 4096) {
 		uint8_t flash_buf[4096];
-		memcpy(flash_buf, FS + flash_sect + i, 4096);
 
-		if (i == 0) {
-			if (bufsize < 4096 - sect_ofs) {
-				memcpy(flash_buf + sect_ofs, buffer, bufsize);
-			} else {
-				memcpy(flash_buf + sect_ofs, buffer, 4096 - sect_ofs);
-			}
-		} else if (bufsize - i < 4096) {
-			memcpy(flash_buf, buffer + i - sect_ofs, bufsize - i);
+		if (bufsize - i < 4096) {
+			memcpy(flash_buf, FS + lba * 4096 + i, 4096);
+			memcpy(flash_buf, buffer + i, bufsize - i);
 		} else {
-			memcpy(flash_buf, buffer + i - sect_ofs, 4096);
+			memcpy(flash_buf, buffer + i, 4096);
 		}
 
-		flash_range_erase((uint32_t)FS + flash_sect - 0x10000000, 4096);
-		flash_range_program((uint32_t)FS + flash_sect - 0x10000000, flash_buf, 4096);	
+		flash_range_erase((uint32_t)FS + lba * 4096 - 0x10000000, 4096);
+		flash_range_program((uint32_t)FS + lba * 4096 - 0x10000000, flash_buf, 4096);	
 	}
 
 	return (int32_t)bufsize;
@@ -241,7 +242,7 @@ DSTATUS disk_initialize(BYTE pdrv) {
 }
 
 DRESULT disk_read(BYTE pdrv, BYTE *buff, LBA_t sector, UINT count) {
-	memcpy(buff, FS + 512 * sector, 512 * count);
+	memcpy(buff, FS + 4096 * sector, 4096 * count);
 	return RES_OK;
 }
 
@@ -255,10 +256,10 @@ DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buff) {
 		case CTRL_SYNC:
 			return RES_OK;
 		case GET_SECTOR_COUNT:
-			*(LBA_t *)buff = (FS_BYTES / 512);
+			*(LBA_t *)buff = (FS_BYTES / 4096);
 			return RES_OK;
 		case GET_SECTOR_SIZE:
-			*(WORD *)buff = 512;
+			*(WORD *)buff = 4096;
 			return RES_OK;
 		case GET_BLOCK_SIZE:
 			*(DWORD *)buff = 4096;
diff --git a/src/tusb_config.h b/src/tusb_config.h
index 0a75828..e226a64 100644
--- a/src/tusb_config.h
+++ b/src/tusb_config.h
@@ -98,7 +98,7 @@
 #define CFG_TUD_VENDOR           0
 
 // MSC Buffer size of Device Mass storage
-#define CFG_TUD_MSC_EP_BUFSIZE   512
+#define CFG_TUD_MSC_EP_BUFSIZE   4096
 
 #ifdef __cplusplus
  }