Skip to content

Commit 3046e31

Browse files
author
Cruz Monrreal
authored
Merge pull request #8589 from davidsaada/david_flash_erase_value
Support erase value in Flash HAL drivers, FlashIAP and block devices
2 parents 760b074 + 542744d commit 3046e31

File tree

30 files changed

+232
-4
lines changed

30 files changed

+232
-4
lines changed

TESTS/mbed_drivers/flashiap/main.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ void flashiap_program_test()
5555
TEST_ASSERT_TRUE(sector_size % page_size == 0);
5656
uint32_t prog_size = std::max(page_size, (uint32_t)8);
5757
uint8_t *data = new uint8_t[prog_size + 2];
58-
for (uint32_t i = 0; i < prog_size + 2; i++) {
59-
data[i] = i;
60-
}
6158

6259
// the one before the last sector in the system
6360
uint32_t address = (flash_device.get_flash_start() + flash_device.get_flash_size()) - (sector_size);
@@ -68,14 +65,27 @@ void flashiap_program_test()
6865
ret = flash_device.erase(address, sector_size);
6966
TEST_ASSERT_EQUAL_INT32(0, ret);
7067

68+
uint8_t erase_val = flash_device.get_erase_value();
69+
memset(data, erase_val, prog_size);
70+
71+
uint8_t *data_flashed = new uint8_t[prog_size];
72+
for (uint32_t i = 0; i < sector_size / prog_size; i++) {
73+
uint32_t page_addr = address + i * prog_size;
74+
ret = flash_device.read(data_flashed, page_addr, prog_size);
75+
TEST_ASSERT_EQUAL_INT32(0, ret);
76+
TEST_ASSERT_EQUAL_UINT8_ARRAY(data, data_flashed, prog_size);
77+
}
78+
79+
for (uint32_t i = 0; i < prog_size + 2; i++) {
80+
data[i] = i;
81+
}
7182

7283
for (uint32_t i = 0; i < sector_size / prog_size; i++) {
7384
uint32_t prog_addr = address + i * prog_size;
7485
ret = flash_device.program(data, prog_addr, prog_size);
7586
TEST_ASSERT_EQUAL_INT32(0, ret);
7687
}
7788

78-
uint8_t *data_flashed = new uint8_t[prog_size];
7989
for (uint32_t i = 0; i < sector_size / prog_size; i++) {
8090
uint32_t page_addr = address + i * prog_size;
8191
ret = flash_device.read(data_flashed, page_addr, prog_size);

components/storage/blockdevice/COMPONENT_FLASHIAP/FlashIAPBlockDevice.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,20 @@ bd_size_t FlashIAPBlockDevice::get_erase_size(bd_addr_t addr) const
227227
return erase_size;
228228
}
229229

230+
int FlashIAPBlockDevice::get_erase_value() const
231+
{
232+
if (!_is_initialized) {
233+
return -1;
234+
}
235+
236+
uint8_t erase_val = _flash.get_erase_value();
237+
238+
DEBUG_PRINTF("get_erase_value: %" PRIX8 "\r\n", erase_val);
239+
240+
return erase_val;
241+
}
242+
243+
230244
bd_size_t FlashIAPBlockDevice::size() const
231245
{
232246
DEBUG_PRINTF("size: %" PRIX64 "\r\n", _size);

components/storage/blockdevice/COMPONENT_FLASHIAP/FlashIAPBlockDevice.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ class FlashIAPBlockDevice : public BlockDevice {
109109
*/
110110
virtual bd_size_t get_erase_size(bd_addr_t addr) const;
111111

112+
/** Get the value of storage when erased
113+
*
114+
* @return The value of storage when erased
115+
*/
116+
virtual int get_erase_value() const;
117+
112118
/** Get the total size of the underlying device
113119
*
114120
* @return Size of the underlying device in bytes

drivers/FlashIAP.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,11 @@ uint32_t FlashIAP::get_flash_size() const
203203
return flash_get_size(&_flash);
204204
}
205205

206+
uint8_t FlashIAP::get_erase_value() const
207+
{
208+
return flash_get_erase_value(&_flash);
209+
}
210+
206211
}
207212

208213
#endif

drivers/FlashIAP.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ class FlashIAP : private NonCopyable<FlashIAP> {
131131
*/
132132
uint32_t get_page_size() const;
133133

134+
/** Get the flash erase value
135+
*
136+
* Get the value we read after erase operation
137+
* @return flash erase value
138+
*/
139+
uint8_t get_erase_value() const;
140+
134141
private:
135142

136143
/* Check if address and size are aligned to a sector

hal/TARGET_FLASH_CMSIS_ALGO/flash_common_algo.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,11 @@ MBED_NONSECURE_ENTRY uint32_t flash_get_size(const flash_t *obj)
260260
return obj->target_config->flash_size;
261261
}
262262

263+
MBED_NONSECURE_ENTRY uint8_t flash_get_erase_value(const flash_t *obj)
264+
{
265+
(void)obj;
266+
267+
return 0xFF;
268+
}
269+
263270
#endif // #ifndef DOMAIN_NS

hal/flash_api.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ uint32_t flash_get_start_address(const flash_t *obj);
117117
*/
118118
uint32_t flash_get_size(const flash_t *obj);
119119

120+
/** Get the flash erase value
121+
*
122+
* @param obj The flash object
123+
* @return The flash erase value
124+
*/
125+
uint8_t flash_get_erase_value(const flash_t *obj);
126+
120127
/**@}*/
121128

122129
#ifdef __cplusplus

targets/TARGET_ARM_FM/TARGET_FVP_MPS2/flash_api.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,10 @@ uint32_t flash_get_size(const flash_t *obj)
101101

102102
return ZBT_SRAM1_SIZE;
103103
}
104+
105+
uint8_t flash_get_erase_value(const flash_t *obj)
106+
{
107+
(void)obj;
108+
109+
return 0xFF;
110+
}

targets/TARGET_ARM_SSG/TARGET_CM3DS_MPS2/flash_api.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,10 @@ uint32_t flash_get_size(const flash_t *obj)
101101

102102
return FLASH_SIZE;
103103
}
104+
105+
uint8_t flash_get_erase_value(const flash_t *obj)
106+
{
107+
(void)obj;
108+
109+
return 0xFF;
110+
}

targets/TARGET_Cypress/TARGET_PSOC6/flash_api.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,11 @@ uint32_t flash_get_size(const flash_t *obj)
8282
return CY_FLASH_SIZE;
8383
}
8484

85+
uint8_t flash_get_erase_value(const flash_t *obj)
86+
{
87+
(void)obj;
88+
89+
return 0xFF;
90+
}
91+
8592
#endif // DEVICE_FLASH

0 commit comments

Comments
 (0)