mkiiskila closed pull request #687: add flash_area_getnext_sector(), and knob 
to select a style of emulated flash in sim
URL: https://github.com/apache/mynewt-core/pull/687
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/hw/mcu/native/src/hal_flash.c b/hw/mcu/native/src/hal_flash.c
index b892cf66c..94c1c007f 100644
--- a/hw/mcu/native/src/hal_flash.c
+++ b/hw/mcu/native/src/hal_flash.c
@@ -6,7 +6,7 @@
  * to you under the Apache License, Version 2.0 (the
  * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
- * 
+ *
  *  http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
@@ -23,6 +23,9 @@
 #include <string.h>
 #include <inttypes.h>
 #include <stdlib.h>
+
+#include <syscfg/syscfg.h>
+
 #include "hal/hal_flash_int.h"
 #include "mcu/mcu_sim.h"
 
@@ -48,6 +51,7 @@ static const struct hal_flash_funcs native_flash_funcs = {
     .hff_init = native_flash_init
 };
 
+#if MYNEWT_VAL(MCU_FLASH_STYLE_ST)
 static const uint32_t native_flash_sectors[] = {
     0x00000000, /* 16 * 1024 */
     0x00004000, /* 16 * 1024 */
@@ -62,6 +66,11 @@ static const uint32_t native_flash_sectors[] = {
     0x000c0000, /* 128 * 1024 */
     0x000e0000, /* 128 * 1024 */
 };
+#elif MYNEWT_VAL(MCU_FLASH_STYLE_NORDIC)
+static uint32_t native_flash_sectors[1024 * 1024 / 2048];
+#else
+#error "Need to specify either MCU_FLASH_STYLE_NORDIC or MCU_FLASH_STYLE_ST"
+#endif
 
 #define FLASH_NUM_AREAS   (int)(sizeof native_flash_sectors /           \
                                 sizeof native_flash_sectors[0])
@@ -245,5 +254,12 @@ native_flash_init(const struct hal_flash *dev)
     if (native_flash_file) {
         flash_native_file_open(native_flash_file);
     }
+#if MYNEWT_VAL(MCU_FLASH_STYLE_NORDIC)
+    int i;
+
+    for (i = 0; i < FLASH_NUM_AREAS; i++) {
+        native_flash_sectors[i] = i * 2048;
+    }
+#endif
     return 0;
 }
diff --git a/hw/mcu/native/syscfg.yml b/hw/mcu/native/syscfg.yml
index f73846a9c..36f892e4d 100644
--- a/hw/mcu/native/syscfg.yml
+++ b/hw/mcu/native/syscfg.yml
@@ -49,3 +49,14 @@ syscfg.defs:
         description: >
             Set to indicate that we are using native mcu.
         value: 1
+    MCU_FLASH_STYLE_ST:
+        description: Emulated flash layout is similar to one in STM32.
+        value: 1
+        restrictions:
+            - "!MCU_FLASH_STYLE_NORDIC"
+    MCU_FLASH_STYLE_NORDIC:
+        description: >
+            Emulated flash layout is similar to one in NRF51/2 and SAMD21.
+        value: 0
+        restrictions:
+            - "!MCU_FLASH_STYLE_ST"
diff --git a/sys/flash_map/include/flash_map/flash_map.h 
b/sys/flash_map/include/flash_map/flash_map.h
index 34045df0c..146d5355a 100644
--- a/sys/flash_map/include/flash_map/flash_map.h
+++ b/sys/flash_map/include/flash_map/flash_map.h
@@ -88,6 +88,12 @@ uint8_t flash_area_align(const struct flash_area *);
  */
 int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret);
 
+/*
+ * Get-next interface for obtaining info about sectors.
+ * To start the get-next walk, call with *sec_id set to -1.
+ */
+int flash_area_getnext_sector(int id, int *sec_id, struct flash_area *ret);
+
 int flash_area_id_from_image_slot(int slot);
 int flash_area_id_to_image_slot(int area_id);
 
diff --git a/sys/flash_map/src/flash_map.c b/sys/flash_map/src/flash_map.c
index 5996907b0..ddc702038 100644
--- a/sys/flash_map/src/flash_map.c
+++ b/sys/flash_map/src/flash_map.c
@@ -93,9 +93,48 @@ flash_area_to_sectors(int id, int *cnt, struct flash_area 
*ret)
             (*cnt)++;
         }
     }
+    flash_area_close(fa);
     return 0;
 }
 
+int
+flash_area_getnext_sector(int id, int *sec_id, struct flash_area *ret)
+{
+    const struct flash_area *fa;
+    const struct hal_flash *hf;
+    uint32_t start;
+    uint32_t size;
+    int rc;
+    int i;
+
+    rc = flash_area_open(id, &fa);
+    if (rc) {
+        return rc;
+    }
+    if (!ret || *sec_id < -1) {
+        rc = SYS_EINVAL;
+        goto end;
+    }
+    hf = hal_bsp_flash_dev(fa->fa_device_id);
+    i = *sec_id + 1;
+    for (; i < hf->hf_sector_cnt; i++) {
+        hf->hf_itf->hff_sector_info(hf, i, &start, &size);
+        if (start >= fa->fa_off && start < fa->fa_off + fa->fa_size) {
+            ret->fa_id = id;
+            ret->fa_device_id = fa->fa_device_id;
+            ret->fa_off = start;
+            ret->fa_size = size;
+            *sec_id = i;
+            rc = 0;
+            goto end;
+        }
+    }
+    rc = SYS_ENOENT;
+end:
+    flash_area_close(fa);
+    return rc;
+}
+
 int
 flash_area_read(const struct flash_area *fa, uint32_t off, void *dst,
     uint32_t len)
@@ -153,7 +192,7 @@ flash_area_is_empty(const struct flash_area *fa, bool 
*empty)
     uint8_t i;
     int rc;
 
-    while(data_off < fa->fa_size) {
+    while (data_off < fa->fa_size) {
         bytes_to_read = min(64, fa->fa_size - data_off);
         rc = flash_area_read(fa, data_off, data, bytes_to_read);
         if (rc) {
@@ -164,7 +203,7 @@ flash_area_is_empty(const struct flash_area *fa, bool 
*empty)
             goto not_empty;
           }
         }
-        data_off+=bytes_to_read;
+        data_off += bytes_to_read;
     }
     *empty = true;
     return 0;
diff --git a/sys/flash_map/test/src/flash_map_test.c 
b/sys/flash_map/test/src/flash_map_test.c
index ec60787d8..c0c55afb7 100644
--- a/sys/flash_map/test/src/flash_map_test.c
+++ b/sys/flash_map/test/src/flash_map_test.c
@@ -40,11 +40,13 @@ struct flash_area *fa_sectors;
 
 TEST_CASE_DECL(flash_map_test_case_1)
 TEST_CASE_DECL(flash_map_test_case_2)
+TEST_CASE_DECL(flash_map_test_case_3)
 
 TEST_SUITE(flash_map_test_suite)
 {
     flash_map_test_case_1();
     flash_map_test_case_2();
+    flash_map_test_case_3();
 }
 
 #if MYNEWT_VAL(SELFTEST)
diff --git a/sys/flash_map/test/src/testcases/flash_map_test_case3.c 
b/sys/flash_map/test/src/testcases/flash_map_test_case3.c
new file mode 100644
index 000000000..799c2723b
--- /dev/null
+++ b/sys/flash_map/test/src/testcases/flash_map_test_case3.c
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include "flash_map_test.h"
+
+extern struct flash_area *fa_sectors;
+
+/*
+ * Test flash_area_to_sectors() vs flash_area_getnext_sector()
+ */
+TEST_CASE(flash_map_test_case_3)
+{
+    int areas[] = {
+        FLASH_AREA_BOOTLOADER,
+        FLASH_AREA_IMAGE_0,
+        FLASH_AREA_IMAGE_1,
+        FLASH_AREA_IMAGE_SCRATCH,
+        FLASH_AREA_REBOOT_LOG,
+        FLASH_AREA_NFFS
+    };
+    int i;
+    int sec_cnt;
+    int aid;
+    int sec_idx;
+    int j;
+    int rc;
+    struct flash_area ret;
+
+    for (i = 0; i < sizeof(areas) / sizeof(areas[0]); i++) {
+        aid = areas[i];
+        rc = flash_area_to_sectors(aid, &sec_cnt, fa_sectors);
+        TEST_ASSERT_FATAL(rc == 0, "flash_area_to_sectors failed");
+
+        sec_idx = -1;
+        j = 0;
+        while (1) {
+            rc = flash_area_getnext_sector(aid, &sec_idx, &ret);
+            if (rc == 0) {
+                TEST_ASSERT(fa_sectors[j].fa_device_id == ret.fa_device_id);
+                TEST_ASSERT(fa_sectors[j].fa_id == ret.fa_id);
+                TEST_ASSERT(fa_sectors[j].fa_off == ret.fa_off);
+                TEST_ASSERT(fa_sectors[j].fa_size == ret.fa_size);
+                j++;
+            } else {
+                TEST_ASSERT(sec_cnt == j);
+                break;
+            }
+        }
+    }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to