Skip to content

Commit 63b45dc

Browse files
author
Deepika
authored
Merge pull request ARMmbed#57 from ARMmbed/g-trim
Remove erase in favor of trim
2 parents c29add2 + 7a67bc5 commit 63b45dc

File tree

2 files changed

+24
-31
lines changed

2 files changed

+24
-31
lines changed

SDBlockDevice.cpp

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,13 @@
147147
#include "mbed_debug.h"
148148
#include <errno.h>
149149

150-
/* Required version: 5.5.4 and above */
150+
/* Required version: 5.6.1 and above */
151151
#ifdef MBED_MAJOR_VERSION
152-
#if (MBED_VERSION < MBED_ENCODE_VERSION(5,5,4))
152+
#if (MBED_VERSION < MBED_ENCODE_VERSION(5,6,1))
153153
#error "Incompatible mbed-os version detected! Required 5.5.4 and above"
154154
#endif
155155
#else
156-
#warning "mbed-os version 5.5.4 or above required"
156+
#warning "mbed-os version 5.6.1 or above required"
157157
#endif
158158

159159
#define SD_COMMAND_TIMEOUT 5000 /*!< Timeout in ms for response */
@@ -513,9 +513,17 @@ int SDBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)
513513
return status;
514514
}
515515

516-
int SDBlockDevice::erase(bd_addr_t addr, bd_size_t size)
516+
bool SDBlockDevice::_is_valid_trim(bd_addr_t addr, bd_size_t size)
517517
{
518-
if (!is_valid_erase(addr, size)) {
518+
return (
519+
addr % _erase_size == 0 &&
520+
size % _erase_size == 0 &&
521+
addr + size <= this->size());
522+
}
523+
524+
int SDBlockDevice::trim(bd_addr_t addr, bd_size_t size)
525+
{
526+
if (!_is_valid_trim(addr, size)) {
519527
return SD_BLOCK_DEVICE_ERROR_PARAMETER;
520528
}
521529

@@ -526,15 +534,7 @@ int SDBlockDevice::erase(bd_addr_t addr, bd_size_t size)
526534
}
527535
int status = BD_ERROR_OK;
528536

529-
// Toss out erases that are too small, this is fine as the block device api
530-
// doesn't garuntee the final state of erased bytes and a following program
531-
// will still work fine.
532-
if (size < _erase_size) {
533-
_lock.unlock();
534-
return status;
535-
}
536-
537-
size -= _erase_size;
537+
size -= _block_size;
538538
// SDSC Card (CCS=0) uses byte unit address
539539
// SDHC and SDXC Cards (CCS=1) use block unit address (512 Bytes unit)
540540
if (SDCARD_V2HC == _card_type) {
@@ -568,11 +568,6 @@ bd_size_t SDBlockDevice::get_program_size() const
568568
return _block_size;
569569
}
570570

571-
bd_size_t SDBlockDevice::get_erase_size() const
572-
{
573-
return _block_size;
574-
}
575-
576571
bd_size_t SDBlockDevice::size() const
577572
{
578573
bd_size_t sectors = 0;

SDBlockDevice.h

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,18 @@ class SDBlockDevice : public BlockDevice {
8585
*/
8686
virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
8787

88-
/** Erase blocks on a block device
88+
/** Mark blocks as no longer in use
8989
*
90-
* The state of an erased block is undefined until it has been programmed
90+
* This function provides a hint to the underlying block device that a region of blocks
91+
* is no longer in use and may be erased without side effects. Erase must still be called
92+
* before programming, but trimming allows flash-translation-layers to schedule erases when
93+
* the device is not busy.
9194
*
92-
* @param addr Address of block to begin erasing
93-
* @param size Size to erase in bytes, must be a multiple of erase block size
95+
* @param addr Address of block to mark as unused
96+
* @param size Size to mark as unused in bytes, must be a multiple of erase block size
9497
* @return 0 on success, negative error code on failure
9598
*/
96-
virtual int erase(bd_addr_t addr, bd_size_t size);
99+
virtual int trim(bd_addr_t addr, bd_size_t size);
97100

98101
/** Get the size of a readable block
99102
*
@@ -108,13 +111,6 @@ class SDBlockDevice : public BlockDevice {
108111
*/
109112
virtual bd_size_t get_program_size() const;
110113

111-
/** Get the size of a eraseable block
112-
*
113-
* @return Size of a eraseable block in bytes
114-
* @note Must be a multiple of the program size
115-
*/
116-
virtual bd_size_t get_erase_size() const;
117-
118114
/** Get the total size of the underlying device
119115
*
120116
* @return Size of the underlying device in bytes
@@ -196,6 +192,8 @@ class SDBlockDevice : public BlockDevice {
196192
uint32_t _sectors;
197193
uint32_t _sd_sectors();
198194

195+
bool _is_valid_trim(bd_addr_t addr, bd_size_t size);
196+
199197
/* SPI functions */
200198
Timer _spi_timer; /**< Timer Class object used for busy wait */
201199
uint32_t _init_sck; /**< Intial SPI frequency */

0 commit comments

Comments
 (0)