You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ma...@apache.org on 2015/12/04 23:03:09 UTC

[3/6] incubator-mynewt-larva git commit: Allow caller to limit the number of NFFS descriptors returned for given flash area. Can be used when underlying flash has large number of small sectors.

Allow caller to limit the number of NFFS descriptors returned for
given flash area. Can be used when underlying flash has large number
of small sectors.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/commit/b43cedc5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/b43cedc5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/b43cedc5

Branch: refs/heads/master
Commit: b43cedc52c80f6ffbec083d5b16b5de1750e1cf7
Parents: fb12884
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Fri Dec 4 13:50:38 2015 -0800
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Fri Dec 4 13:50:38 2015 -0800

----------------------------------------------------------------------
 libs/util/src/flash_map.c | 48 +++++++++++++++++++++++++++++++++++-------
 1 file changed, 40 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/b43cedc5/libs/util/src/flash_map.c
----------------------------------------------------------------------
diff --git a/libs/util/src/flash_map.c b/libs/util/src/flash_map.c
index 28f26c9..9493a0d 100644
--- a/libs/util/src/flash_map.c
+++ b/libs/util/src/flash_map.c
@@ -82,35 +82,67 @@ flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret)
 }
 
 #ifdef NFFS_PRESENT
+/*
+ * Turn flash region into a set of areas for NFFS use.
+ *
+ * Limit the number of regions we return to be less than *cnt.
+ * If sector count within region exceeds that, collect multiple sectors
+ * to a region.
+ */
 int
 flash_area_to_nffs_desc(int idx, int *cnt, struct nffs_area_desc *nad)
 {
-    int i;
+    int i, j;
     const struct hal_flash *hf;
     const struct flash_area *fa;
+    int max_cnt, move_on;
+    int first_idx, last_idx;
     uint32_t start, size;
+    uint32_t min_size;
 
     if (!flash_map || idx >= flash_map_entries) {
         return -1;
     }
+    first_idx = last_idx = -1;
+    max_cnt = *cnt;
     *cnt = 0;
+
     fa = &flash_map[idx];
 
     hf = bsp_flash_dev(fa->fa_flash_id);
     for (i = 0; i < hf->hf_sector_cnt; i++) {
         hf->hf_itf->hff_sector_info(i, &start, &size);
         if (start >= fa->fa_off && start < fa->fa_off + fa->fa_size) {
-            if (nad) {
-                nad->nad_flash_id = fa->fa_flash_id;
-                nad->nad_offset = start;
-                nad->nad_length = size;
-                nad++;
+            if (first_idx == -1) {
+                first_idx = i;
             }
+            last_idx = i;
             *cnt = *cnt + 1;
         }
     }
-    if (nad) {
-        memset(nad, 0, sizeof(*nad));
+    if (*cnt > max_cnt) {
+        min_size = fa->fa_size / max_cnt;
+    } else {
+        min_size = 0;
+    }
+    *cnt = 0;
+
+    move_on = 1;
+    for (i = first_idx, j = 0; i < last_idx + 1; i++) {
+        hf->hf_itf->hff_sector_info(i, &start, &size);
+        if (move_on) {
+            nad[j].nad_flash_id = fa->fa_flash_id;
+            nad[j].nad_offset = start;
+            nad[j].nad_length = size;
+            *cnt = *cnt + 1;
+            move_on = 0;
+        } else {
+            nad[j].nad_length += size;
+        }
+        if (nad[j].nad_length >= min_size) {
+            j++;
+            move_on = 1;
+        }
     }
     return 0;
 }