Skip to content

Commit bfc1c5a

Browse files
Merge pull request #4983 from geky/bd-trim-erase
bd: Tweaked block device API to fit SD cards and FTLs better
2 parents 4de4481 + ee88097 commit bfc1c5a

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

features/filesystem/bd/BlockDevice.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,26 @@ class BlockDevice
9191
* @param size Size to erase in bytes, must be a multiple of erase block size
9292
* @return 0 on success, negative error code on failure
9393
*/
94-
virtual int erase(bd_addr_t addr, bd_size_t size) = 0;
94+
virtual int erase(bd_addr_t addr, bd_size_t size)
95+
{
96+
return 0;
97+
}
98+
99+
/** Mark blocks as no longer in use
100+
*
101+
* This function provides a hint to the underlying block device that a region of blocks
102+
* is no longer in use and may be erased without side effects. Erase must still be called
103+
* before programming, but trimming allows flash-translation-layers to schedule erases when
104+
* the device is not busy.
105+
*
106+
* @param addr Address of block to mark as unused
107+
* @param size Size to mark as unused in bytes, must be a multiple of erase block size
108+
* @return 0 on success, negative error code on failure
109+
*/
110+
virtual int trim(bd_addr_t addr, bd_size_t size)
111+
{
112+
return 0;
113+
}
95114

96115
/** Get the size of a readable block
97116
*
@@ -111,7 +130,10 @@ class BlockDevice
111130
* @return Size of a eraseable block in bytes
112131
* @note Must be a multiple of the program size
113132
*/
114-
virtual bd_size_t get_erase_size() const = 0;
133+
virtual bd_size_t get_erase_size() const
134+
{
135+
return get_program_size();
136+
}
115137

116138
/** Get the total size of the underlying device
117139
*

features/filesystem/fat/ChaN/ffconf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@
171171
/ disk_ioctl() function. */
172172

173173

174-
#define _USE_TRIM 0
174+
#define _USE_TRIM 1
175175
/* This option switches ATA-TRIM feature. (0:Disable or 1:Enable)
176176
/ To enable Trim feature, also CTRL_TRIM command should be implemented to the
177177
/ disk_ioctl() function. */

features/filesystem/fat/FATFileSystem.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,15 @@ DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buff)
244244
case GET_BLOCK_SIZE:
245245
*((DWORD*)buff) = 1; // default when not known
246246
return RES_OK;
247+
case CTRL_TRIM:
248+
if (_ffs[pdrv] == NULL) {
249+
return RES_NOTRDY;
250+
} else {
251+
DWORD *sectors = (DWORD*)buff;
252+
DWORD ssize = disk_get_sector_size(pdrv);
253+
int err = _ffs[pdrv]->trim(sectors[0]*ssize, (sectors[1]-sectors[0]+1)*ssize);
254+
return err ? RES_PARERR : RES_OK;
255+
}
247256
}
248257

249258
return RES_PARERR;

0 commit comments

Comments
 (0)