Skip to content

Commit 8b885d3

Browse files
committed
Support block device out of Mbed OS tree in KVStore
To support block device out of Mbed OS tree in KVStore, user needs to: 1. Configure blockdevice to "other". 2. Override get_other_blockdevice() to provide block device out of Mbed OS tree. 3. Override is_blockdevice_flash_type() to add check for if the above block device is flash type.
1 parent 71c84e8 commit 8b885d3

File tree

5 files changed

+115
-33
lines changed

5 files changed

+115
-33
lines changed

features/storage/kvstore/conf/filesystem/mbed_lib.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"value": "default"
1515
},
1616
"blockdevice": {
17-
"help": "Options are default, SPIF, DATAFASH, QSPIF or SD. If default, the block device will be chosen according to the component defined in targets.json",
17+
"help": "Options are default, SPIF, DATAFASH, QSPIF, SD or other. If default, the block device will be chosen according to the component defined in targets.json. If other, override get_other_blockdevice() and is_blockdevice_flash_type() to support block device out of Mbed OS tree.",
1818
"value": "default"
1919
},
2020
"external_size": {

features/storage/kvstore/conf/filesystem_no_rbp/mbed_lib.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"value": "default"
77
},
88
"blockdevice": {
9-
"help": "Options are default, SPIF, DATAFLASH, QSPIF or SD. If default the block device will be chosen by the defined component",
9+
"help": "Options are default, SPIF, DATAFLASH, QSPIF, SD or other. If default the block device will be chosen by the defined component. If other, override get_other_blockdevice() and is_blockdevice_flash_type() to support block device out of Mbed OS tree.",
1010
"value": "default"
1111
},
1212
"external_size": {

features/storage/kvstore/conf/kv_config.cpp

Lines changed: 111 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,23 @@ int _storage_config_FILESYSTEM_NO_RBP();
134134
int _storage_config_tdb_external_common();
135135
int _storage_config_filesystem_common();
136136

137+
/**
138+
* @brief If block device out of Mbed OS tree is to support, please overwrite this
139+
* function to provide it.
140+
*
141+
* @returns pointer to other block device.
142+
*/
143+
BlockDevice *get_other_blockdevice();
144+
145+
/**
146+
* @brief This function checks if given block device is flash type. If block device
147+
* out of Mbed OS tree is to support, please overwrite this function to add check
148+
* for it.
149+
*
150+
* @returns true for flash type or false for non-flash type.
151+
*/
152+
bool is_blockdevice_flash_type(BlockDevice *bd);
153+
137154
static const char *filesystemstore_folder_path = NULL;
138155

139156
using namespace mbed;
@@ -245,7 +262,16 @@ FileSystem *_get_filesystem_default(const char *mount)
245262
#elif COMPONENT_SD
246263
return _get_filesystem_FAT(mount);
247264
#else
248-
return NULL;
265+
BlockDevice *bd = get_other_blockdevice();
266+
if (bd) {
267+
if (is_blockdevice_flash_type(bd)) {
268+
return _get_filesystem_LITTLE(mount);
269+
} else {
270+
return _get_filesystem_FAT(mount);
271+
}
272+
} else {
273+
return NULL;
274+
}
249275
#endif
250276
}
251277

@@ -617,6 +643,73 @@ BlockDevice *_get_blockdevice_default(bd_addr_t start_address, bd_size_t size)
617643
#endif
618644
}
619645

646+
/* Same logic as _get_blockdevice_SD() except block device replaced with from
647+
* get_other_blockdevice() */
648+
BlockDevice *_get_blockdevice_other(bd_addr_t start_address, bd_size_t size)
649+
{
650+
bd_addr_t aligned_end_address;
651+
bd_addr_t aligned_start_address;
652+
653+
BlockDevice *bd = get_other_blockdevice();
654+
if (bd == NULL) {
655+
tr_error("KV Config: \"other\" block device init fail");
656+
return NULL;
657+
}
658+
659+
if (bd->init() != MBED_SUCCESS) {
660+
tr_error("KV Config: SDBlockDevice init fail");
661+
return NULL;
662+
}
663+
664+
if (strcmp(STR(MBED_CONF_STORAGE_STORAGE_TYPE), "TDB_EXTERNAL_NO_RBP") == 0 ||
665+
strcmp(STR(MBED_CONF_STORAGE_STORAGE_TYPE), "TDB_EXTERNAL") == 0) {
666+
//In TDBStore profile, we have a constraint of 4GByte
667+
if (start_address == 0 && size == 0 && bd->size() < (uint32_t)(-1)) {
668+
return bd;
669+
}
670+
671+
//If the size of external storage is bigger than 4G we need to slice it.
672+
size = size != 0 ? size : align_down(bd->size(), bd->get_erase_size(bd->size() - 1));
673+
674+
if (_get_addresses(bd, start_address, size, &aligned_start_address, &aligned_end_address) != 0) {
675+
tr_error("KV Config: Fail to get addresses for SlicingBlockDevice.");
676+
return NULL;
677+
}
678+
679+
if (aligned_end_address - aligned_start_address != (uint32_t)(aligned_end_address - aligned_start_address)) {
680+
aligned_end_address = aligned_start_address + (uint32_t)(-1);//Support up to 4G only
681+
}
682+
} else {
683+
//For all other KVStore profiles beside TDBStore we take the entire external memory space.
684+
if (start_address == 0 && size == 0) {
685+
return bd;
686+
}
687+
688+
if (_get_addresses(bd, start_address, size, &aligned_start_address, &aligned_end_address) != 0) {
689+
tr_error("KV Config: Fail to get addresses for SlicingBlockDevice.");
690+
return NULL;
691+
}
692+
}
693+
694+
aligned_end_address = align_down(aligned_end_address, bd->get_erase_size(aligned_end_address));
695+
static SlicingBlockDevice sbd(bd, aligned_start_address, aligned_end_address);
696+
return &sbd;
697+
}
698+
699+
MBED_WEAK BlockDevice *get_other_blockdevice()
700+
{
701+
return NULL;
702+
}
703+
704+
MBED_WEAK bool is_blockdevice_flash_type(BlockDevice *bd)
705+
{
706+
if (strcmp(bd->get_type(), "SD") == 0) {
707+
return false;
708+
}
709+
710+
return true;
711+
}
712+
620713
int _storage_config_TDB_INTERNAL()
621714
{
622715
#if COMPONENT_FLASHIAP
@@ -750,17 +843,10 @@ int _storage_config_TDB_EXTERNAL()
750843
return MBED_ERROR_FAILED_OPERATION ;
751844
}
752845

753-
//TDBStore needs a block device base on flash. so if this is SD block device or the default block device is SD
754-
//add FlashSimBlockDevice on top of the SDBlockDevice
755-
#if defined(COMPONENT_SD)
756-
if (strcmp(STR(MBED_CONF_STORAGE_TDB_EXTERNAL_BLOCKDEVICE), "SD") == 0
757-
#if defined(COMPONENT_SD) && !defined(COMPONENT_SPIF) && !defined(COMPONENT_QSPIF) && !defined(COMPONENT_DATAFLASH)
758-
|| strcmp(STR(MBED_CONF_STORAGE_TDB_EXTERNAL_BLOCKDEVICE), "default") == 0) {
759-
#else
760-
) {
761-
762-
#endif
763-
//TDBStore need FlashSimBlockDevice when working with SD block device
846+
//TDBStore needs a block device base on flash. So if this is non-flash type block device,
847+
//add FlashSimBlockDevice on top of it.
848+
if (!is_blockdevice_flash_type(bd)) {
849+
//TDBStore needs FlashSimBlockDevice when working with non-flash type block device
764850
if (bd->init() != MBED_SUCCESS) {
765851
tr_error("KV Config: Fail to init external BlockDevice.");
766852
return MBED_ERROR_FAILED_OPERATION ;
@@ -771,9 +857,6 @@ int _storage_config_TDB_EXTERNAL()
771857
} else {
772858
kvstore_config.external_bd = bd;
773859
}
774-
#else
775-
kvstore_config.external_bd = bd;
776-
#endif
777860

778861
kvstore_config.flags_mask = ~(0);
779862

@@ -795,17 +878,10 @@ int _storage_config_TDB_EXTERNAL_NO_RBP()
795878
return MBED_ERROR_FAILED_OPERATION ;
796879
}
797880

798-
//TDBStore needs a block device base on flash. so if this is SD block device or the default block device is SD
881+
//TDBStore needs a block device base on flash. So if this is non-flash type block device,
799882
//add FlashSimBlockDevice on top of the SDBlockDevice
800-
#if defined(COMPONENT_SD)
801-
if (strcmp(STR(MBED_CONF_STORAGE_TDB_EXTERNAL_NO_RBP_BLOCKDEVICE), "SD") == 0
802-
#if defined(COMPONENT_SD) && !defined(COMPONENT_SPIF) && !defined(COMPONENT_QSPIF) && !defined(COMPONENT_DATAFLASH)
803-
|| strcmp(STR(MBED_CONF_STORAGE_TDB_EXTERNAL_NO_RBP_BLOCKDEVICE), "default") == 0) {
804-
#else
805-
) {
806-
807-
#endif
808-
//TDBStore need FlashSimBlockDevice when working with SD block device
883+
if (!is_blockdevice_flash_type(bd)) {
884+
//TDBStore needs FlashSimBlockDevice when working with non-flash type block device
809885
if (bd->init() != MBED_SUCCESS) {
810886
tr_error("KV Config: Fail to init external BlockDevice.");
811887
return MBED_ERROR_FAILED_OPERATION ;
@@ -816,9 +892,6 @@ int _storage_config_TDB_EXTERNAL_NO_RBP()
816892
} else {
817893
kvstore_config.external_bd = bd;
818894
}
819-
#else
820-
kvstore_config.external_bd = bd;
821-
#endif
822895

823896
//Masking flag - Actually used to remove any KVStore flag which is not supported
824897
//in the chosen KVStore profile.
@@ -1069,7 +1142,16 @@ int _storage_config_default()
10691142
#elif COMPONENT_FLASHIAP
10701143
return _storage_config_TDB_INTERNAL();
10711144
#else
1072-
return MBED_ERROR_UNSUPPORTED;
1145+
BlockDevice *bd = get_other_blockdevice();
1146+
if (bd) {
1147+
if (is_blockdevice_flash_type(bd)) {
1148+
return _storage_config_TDB_EXTERNAL();
1149+
} else {
1150+
return _storage_config_FILESYSTEM();
1151+
}
1152+
} else {
1153+
return MBED_ERROR_UNSUPPORTED;
1154+
}
10731155
#endif
10741156
}
10751157

features/storage/kvstore/conf/tdb_external/mbed_lib.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"value": "0"
1212
},
1313
"blockdevice": {
14-
"help": "Options are default, SPIF, DATAFASH, QSPIF or SD. If default the block device will be chosen by the defined component",
14+
"help": "Options are default, SPIF, DATAFASH, QSPIF, SD or other. If default the block device will be chosen by the defined component. If other, override get_other_blockdevice() and is_blockdevice_flash_type() to support block device out of Mbed OS tree.",
1515
"value": "default"
1616
},
1717
"external_size": {

features/storage/kvstore/conf/tdb_external_no_rbp/mbed_lib.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "storage_tdb_external_no_rbp",
33
"config": {
44
"blockdevice": {
5-
"help": "Options are default, SPIF, DATAFASH, QSPIF or SD. If default the block device will be chosen by the defined component",
5+
"help": "Options are default, SPIF, DATAFASH, QSPIF, SD or other. If default the block device will be chosen by the defined component. If other, override get_other_blockdevice() and is_blockdevice_flash_type() to support block device out of Mbed OS tree.",
66
"value": "default"
77
},
88
"external_size": {

0 commit comments

Comments
 (0)