Skip to content

Commit b46da65

Browse files
author
Kyle Kearney
committed
TDBStore: Handle odd number of sectors in block
Rework TDBStore::calc_area_params so that it can handle situations where the block device size is not an even multiple of the sector size (while retaining its ability to handle non-uniform erase sizes). This avoids intermittent asserts on boards where TDBStore is implemented in internal flash, in which case the size of the block device varies with the application size and a minor change (or a shift in optimization level) can shift TDBStore from an odd to an even number of sectors.
1 parent 7fce7f5 commit b46da65

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

features/storage/kvstore/tdbstore/TDBStore.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,16 +200,35 @@ void TDBStore::calc_area_params()
200200

201201
memset(_area_params, 0, sizeof(_area_params));
202202
size_t area_0_size = 0;
203-
204-
while (area_0_size < bd_size / 2) {
203+
size_t area_1_size = 0;
204+
205+
// The size calculations are a bit complex because we need to make sure we're
206+
// always aligned to an erase block boundary (whose size may not be uniform
207+
// across the address space), and we also need to make sure that the first
208+
// area never goes over half of the total size even if bd_size is an odd
209+
// number of sectors.
210+
while (true) {
205211
bd_size_t erase_unit_size = _bd->get_erase_size(area_0_size);
206-
area_0_size += erase_unit_size;
212+
if (area_0_size + erase_unit_size <= (bd_size / 2)) {
213+
area_0_size += erase_unit_size;
214+
} else {
215+
break;
216+
}
217+
}
218+
219+
while (true) {
220+
bd_size_t erase_unit_size = _bd->get_erase_size(area_0_size + area_1_size);
221+
if (area_1_size + erase_unit_size <= (bd_size / 2)) {
222+
area_1_size += erase_unit_size;
223+
} else {
224+
break;
225+
}
207226
}
208227

209228
_area_params[0].address = 0;
210229
_area_params[0].size = area_0_size;
211230
_area_params[1].address = area_0_size;
212-
_area_params[1].size = bd_size - area_0_size;
231+
_area_params[1].size = area_1_size;
213232

214233
// The areas must be of same size
215234
MBED_ASSERT(_area_params[0].size == _area_params[1].size);

0 commit comments

Comments
 (0)