@@ -629,7 +629,15 @@ static int nvs_gc(struct nvs_fs *fs)
629629 continue ;
630630 }
631631
632+ #ifdef CONFIG_NVS_LOOKUP_CACHE
633+ wlk_addr = fs -> lookup_cache [nvs_lookup_cache_pos (gc_ate .id )];
634+
635+ if (wlk_addr == NVS_LOOKUP_CACHE_NO_ADDR ) {
636+ wlk_addr = fs -> ate_wra ;
637+ }
638+ #else
632639 wlk_addr = fs -> ate_wra ;
640+ #endif
633641 do {
634642 wlk_prev_addr = wlk_addr ;
635643 rc = nvs_prev_ate (fs , & wlk_addr , & wlk_ate );
@@ -854,6 +862,16 @@ static int nvs_startup(struct nvs_fs *fs)
854862 fs -> ate_wra &= ADDR_SECT_MASK ;
855863 fs -> ate_wra += (fs -> sector_size - 2 * ate_size );
856864 fs -> data_wra = (fs -> ate_wra & ADDR_SECT_MASK );
865+ #ifdef CONFIG_NVS_LOOKUP_CACHE
866+ /**
867+ * At this point, the lookup cache wasn't built but the gc function need to use it.
868+ * So, temporarily, we set the lookup cache to the end of the fs.
869+ * The cache will be rebuilt afterwards
870+ **/
871+ for (int i = 0 ; i < CONFIG_NVS_LOOKUP_CACHE_SIZE ; i ++ ) {
872+ fs -> lookup_cache [i ] = fs -> ate_wra ;
873+ }
874+ #endif
857875 rc = nvs_gc (fs );
858876 goto end ;
859877 }
@@ -1016,7 +1034,15 @@ ssize_t nvs_write(struct nvs_fs *fs, uint16_t id, const void *data, size_t len)
10161034 }
10171035
10181036 /* find latest entry with same id */
1037+ #ifdef CONFIG_NVS_LOOKUP_CACHE
1038+ wlk_addr = fs -> lookup_cache [nvs_lookup_cache_pos (id )];
1039+
1040+ if (wlk_addr == NVS_LOOKUP_CACHE_NO_ADDR ) {
1041+ goto no_cached_entry ;
1042+ }
1043+ #else
10191044 wlk_addr = fs -> ate_wra ;
1045+ #endif
10201046 rd_addr = wlk_addr ;
10211047
10221048 while (1 ) {
@@ -1034,6 +1060,10 @@ ssize_t nvs_write(struct nvs_fs *fs, uint16_t id, const void *data, size_t len)
10341060 }
10351061 }
10361062
1063+ #ifdef CONFIG_NVS_LOOKUP_CACHE
1064+ no_cached_entry :
1065+ #endif
1066+
10371067 if (prev_found ) {
10381068 /* previous entry found */
10391069 rd_addr &= ADDR_SECT_MASK ;
0 commit comments