Skip to content

Commit 0ac1c97

Browse files
authored
Merge pull request #10355 from OpenNuvoton/nuvoton_kvstore_other_bd
KVStore: Support external storage out of mbed-os tree
2 parents b21c278 + 63d9cde commit 0ac1c97

File tree

5 files changed

+97
-33
lines changed

5 files changed

+97
-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() 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() 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: 93 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ 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+
137145
static const char *filesystemstore_folder_path = NULL;
138146

139147
using namespace mbed;
@@ -245,7 +253,16 @@ FileSystem *_get_filesystem_default(const char *mount)
245253
#elif COMPONENT_SD
246254
return _get_filesystem_FAT(mount);
247255
#else
248-
return NULL;
256+
BlockDevice *bd = get_other_blockdevice();
257+
if (bd) {
258+
if (bd->get_erase_value() != -1) {
259+
return _get_filesystem_LITTLE(mount);
260+
} else {
261+
return _get_filesystem_FAT(mount);
262+
}
263+
} else {
264+
return NULL;
265+
}
249266
#endif
250267
}
251268

@@ -617,6 +634,64 @@ BlockDevice *_get_blockdevice_default(bd_addr_t start_address, bd_size_t size)
617634
#endif
618635
}
619636

637+
/* Same logic as _get_blockdevice_SD() except block device replaced with from
638+
* get_other_blockdevice() */
639+
BlockDevice *_get_blockdevice_other(bd_addr_t start_address, bd_size_t size)
640+
{
641+
bd_addr_t aligned_end_address;
642+
bd_addr_t aligned_start_address;
643+
644+
BlockDevice *bd = get_other_blockdevice();
645+
if (bd == NULL) {
646+
tr_error("KV Config: \"other\" block device init fail");
647+
return NULL;
648+
}
649+
650+
if (bd->init() != MBED_SUCCESS) {
651+
tr_error("KV Config: SDBlockDevice init fail");
652+
return NULL;
653+
}
654+
655+
if (strcmp(STR(MBED_CONF_STORAGE_STORAGE_TYPE), "TDB_EXTERNAL_NO_RBP") == 0 ||
656+
strcmp(STR(MBED_CONF_STORAGE_STORAGE_TYPE), "TDB_EXTERNAL") == 0) {
657+
//In TDBStore profile, we have a constraint of 4GByte
658+
if (start_address == 0 && size == 0 && bd->size() < (uint32_t)(-1)) {
659+
return bd;
660+
}
661+
662+
//If the size of external storage is bigger than 4G we need to slice it.
663+
size = size != 0 ? size : align_down(bd->size(), bd->get_erase_size(bd->size() - 1));
664+
665+
if (_get_addresses(bd, start_address, size, &aligned_start_address, &aligned_end_address) != 0) {
666+
tr_error("KV Config: Fail to get addresses for SlicingBlockDevice.");
667+
return NULL;
668+
}
669+
670+
if (aligned_end_address - aligned_start_address != (uint32_t)(aligned_end_address - aligned_start_address)) {
671+
aligned_end_address = aligned_start_address + (uint32_t)(-1);//Support up to 4G only
672+
}
673+
} else {
674+
//For all other KVStore profiles beside TDBStore we take the entire external memory space.
675+
if (start_address == 0 && size == 0) {
676+
return bd;
677+
}
678+
679+
if (_get_addresses(bd, start_address, size, &aligned_start_address, &aligned_end_address) != 0) {
680+
tr_error("KV Config: Fail to get addresses for SlicingBlockDevice.");
681+
return NULL;
682+
}
683+
}
684+
685+
aligned_end_address = align_down(aligned_end_address, bd->get_erase_size(aligned_end_address));
686+
static SlicingBlockDevice sbd(bd, aligned_start_address, aligned_end_address);
687+
return &sbd;
688+
}
689+
690+
MBED_WEAK BlockDevice *get_other_blockdevice()
691+
{
692+
return NULL;
693+
}
694+
620695
int _storage_config_TDB_INTERNAL()
621696
{
622697
#if COMPONENT_FLASHIAP
@@ -750,17 +825,10 @@ int _storage_config_TDB_EXTERNAL()
750825
return MBED_ERROR_FAILED_OPERATION ;
751826
}
752827

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
828+
//TDBStore needs a block device base on flash. So if this is non-flash type block device,
829+
//add FlashSimBlockDevice on top of it.
830+
if (bd->get_erase_value() == -1) {
831+
//TDBStore needs FlashSimBlockDevice when working with non-flash type block device
764832
if (bd->init() != MBED_SUCCESS) {
765833
tr_error("KV Config: Fail to init external BlockDevice.");
766834
return MBED_ERROR_FAILED_OPERATION ;
@@ -771,9 +839,6 @@ int _storage_config_TDB_EXTERNAL()
771839
} else {
772840
kvstore_config.external_bd = bd;
773841
}
774-
#else
775-
kvstore_config.external_bd = bd;
776-
#endif
777842

778843
kvstore_config.flags_mask = ~(0);
779844

@@ -795,17 +860,10 @@ int _storage_config_TDB_EXTERNAL_NO_RBP()
795860
return MBED_ERROR_FAILED_OPERATION ;
796861
}
797862

798-
//TDBStore needs a block device base on flash. so if this is SD block device or the default block device is SD
863+
//TDBStore needs a block device base on flash. So if this is non-flash type block device,
799864
//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
865+
if (bd->get_erase_value() == -1) {
866+
//TDBStore needs FlashSimBlockDevice when working with non-flash type block device
809867
if (bd->init() != MBED_SUCCESS) {
810868
tr_error("KV Config: Fail to init external BlockDevice.");
811869
return MBED_ERROR_FAILED_OPERATION ;
@@ -816,9 +874,6 @@ int _storage_config_TDB_EXTERNAL_NO_RBP()
816874
} else {
817875
kvstore_config.external_bd = bd;
818876
}
819-
#else
820-
kvstore_config.external_bd = bd;
821-
#endif
822877

823878
//Masking flag - Actually used to remove any KVStore flag which is not supported
824879
//in the chosen KVStore profile.
@@ -1069,7 +1124,16 @@ int _storage_config_default()
10691124
#elif COMPONENT_FLASHIAP
10701125
return _storage_config_TDB_INTERNAL();
10711126
#else
1072-
return MBED_ERROR_UNSUPPORTED;
1127+
BlockDevice *bd = get_other_blockdevice();
1128+
if (bd) {
1129+
if (bd->get_erase_value() != -1) {
1130+
return _storage_config_TDB_EXTERNAL();
1131+
} else {
1132+
return _storage_config_FILESYSTEM();
1133+
}
1134+
} else {
1135+
return MBED_ERROR_UNSUPPORTED;
1136+
}
10731137
#endif
10741138
}
10751139

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() 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() to support block device out of Mbed OS tree.",
66
"value": "default"
77
},
88
"external_size": {

0 commit comments

Comments
 (0)