You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2018/09/17 12:33:58 UTC

[GitHub] utzig closed pull request #1323: Allow flash devices to specify erase value

utzig closed pull request #1323: Allow flash devices to specify erase value
URL: https://github.com/apache/mynewt-core/pull/1323
 
 
   

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/bsp/nucleo-f401re/src/hal_bsp.c b/hw/bsp/nucleo-f401re/src/hal_bsp.c
index 0aa64b767e..fbc6ef2108 100644
--- a/hw/bsp/nucleo-f401re/src/hal_bsp.c
+++ b/hw/bsp/nucleo-f401re/src/hal_bsp.c
@@ -60,7 +60,8 @@ const struct hal_flash stm32f4_flash_dev = {
     .hf_base_addr = 0x08000000,
     .hf_size = 512 * 1024,
     .hf_sector_cnt = NAREAS - 1,
-    .hf_align = 1
+    .hf_align = 1,
+    .hf_erased_val = 0xff,
 };
 
 #if MYNEWT_VAL(UART_0)
diff --git a/hw/bsp/nucleo-f413zh/src/hal_bsp.c b/hw/bsp/nucleo-f413zh/src/hal_bsp.c
index 66beeb4137..eb2e56ed97 100644
--- a/hw/bsp/nucleo-f413zh/src/hal_bsp.c
+++ b/hw/bsp/nucleo-f413zh/src/hal_bsp.c
@@ -69,7 +69,8 @@ const struct hal_flash stm32f4_flash_dev = {
     .hf_base_addr = 0x08000000,
     .hf_size = 1536 * 1024,
     .hf_sector_cnt = NAREAS - 1,
-    .hf_align = 1
+    .hf_align = 1,
+    .hf_erased_val = 0xff,
 };
 
 #if MYNEWT_VAL(UART_0)
diff --git a/hw/bsp/olimex_stm32-e407_devboard/src/hal_bsp.c b/hw/bsp/olimex_stm32-e407_devboard/src/hal_bsp.c
index c290495b2d..818e5c4619 100644
--- a/hw/bsp/olimex_stm32-e407_devboard/src/hal_bsp.c
+++ b/hw/bsp/olimex_stm32-e407_devboard/src/hal_bsp.c
@@ -77,7 +77,8 @@ const struct hal_flash stm32f4_flash_dev = {
     .hf_base_addr = 0x08000000,
     .hf_size = 1024 * 1024,
     .hf_sector_cnt = NAREAS - 1,
-    .hf_align = 1
+    .hf_align = 1,
+    .hf_erased_val = 0xff,
 };
 
 #if MYNEWT_VAL(TRNG)
diff --git a/hw/bsp/stm32f429discovery/src/hal_bsp.c b/hw/bsp/stm32f429discovery/src/hal_bsp.c
index 9d8f4ea2d1..c5a4a3c25d 100644
--- a/hw/bsp/stm32f429discovery/src/hal_bsp.c
+++ b/hw/bsp/stm32f429discovery/src/hal_bsp.c
@@ -78,7 +78,8 @@ const struct hal_flash stm32f4_flash_dev = {
     .hf_base_addr = 0x08000000,
     .hf_size = 2048 * 1024,
     .hf_sector_cnt = NAREAS - 1,
-    .hf_align = 1
+    .hf_align = 1,
+    .hf_erased_val = 0xff,
 };
 
 #if MYNEWT_VAL(UART_0)
diff --git a/hw/bsp/stm32f4discovery/src/hal_bsp.c b/hw/bsp/stm32f4discovery/src/hal_bsp.c
index 7e2075b8a5..4f9d35da23 100644
--- a/hw/bsp/stm32f4discovery/src/hal_bsp.c
+++ b/hw/bsp/stm32f4discovery/src/hal_bsp.c
@@ -66,7 +66,8 @@ const struct hal_flash stm32f4_flash_dev = {
     .hf_base_addr = 0x08000000,
     .hf_size = 1024 * 1024,
     .hf_sector_cnt = NAREAS - 1,
-    .hf_align = 1
+    .hf_align = 1,
+    .hf_erased_val = 0xff,
 };
 
 #if MYNEWT_VAL(UART_0)
diff --git a/hw/drivers/flash/enc_flash/src/enc_flash.c b/hw/drivers/flash/enc_flash/src/enc_flash.c
index 02e4a6f7cd..83de4bc294 100644
--- a/hw/drivers/flash/enc_flash/src/enc_flash.c
+++ b/hw/drivers/flash/enc_flash/src/enc_flash.c
@@ -162,7 +162,7 @@ enc_flash_is_empty(const struct hal_flash *h_dev, uint32_t addr, uint32_t len)
     if (h_dev->hf_itf->hff_is_empty) {
         return h_dev->hf_itf->hff_is_empty(h_dev, addr, len);
     } else {
-        return hal_flash_is_ones(h_dev, addr, len);
+        return hal_flash_is_erased(h_dev, addr, len);
     }
 }
 
@@ -183,6 +183,7 @@ enc_flash_init(const struct hal_flash *h_dev)
     dev->efd_hal.hf_size =  h_dev->hf_size;
     dev->efd_hal.hf_sector_cnt = h_dev->hf_sector_cnt;
     dev->efd_hal.hf_align = h_dev->hf_align;
+    dev->efd_hal.hf_erased_val = h_dev->hf_erased_val;
 
     enc_flash_init_arch(dev);
 
diff --git a/hw/hal/include/hal/hal_flash.h b/hw/hal/include/hal/hal_flash.h
index 7230225026..cc42c2b3a8 100644
--- a/hw/hal/include/hal/hal_flash.h
+++ b/hw/hal/include/hal/hal_flash.h
@@ -42,6 +42,7 @@ int hal_flash_erase_sector(uint8_t flash_id, uint32_t sector_address);
 int hal_flash_erase(uint8_t flash_id, uint32_t address, uint32_t num_bytes);
 int hal_flash_isempty(uint8_t flash_id, uint32_t address, uint32_t num_bytes);
 uint8_t hal_flash_align(uint8_t flash_id);
+uint8_t hal_flash_erased_val(uint8_t flash_id);
 int hal_flash_init(void);
 
 #ifdef __cplusplus
diff --git a/hw/hal/include/hal/hal_flash_int.h b/hw/hal/include/hal/hal_flash_int.h
index bc8fdb320c..8ac9ef8514 100644
--- a/hw/hal/include/hal/hal_flash_int.h
+++ b/hw/hal/include/hal/hal_flash_int.h
@@ -51,6 +51,7 @@ struct hal_flash {
     uint32_t hf_size;
     int hf_sector_cnt;
     int hf_align;       /* Alignment requirement. 1 if unrestricted. */
+    uint8_t hf_erased_val;
 };
 
 /*
@@ -58,11 +59,7 @@ struct hal_flash {
  */
 uint32_t hal_flash_sector_size(const struct hal_flash *hf, int sec_idx);
 
-/*
- * Use as hal_flash_funcs.hff_is_empty if flash is erased to zeroes.
- */
-int hal_flash_is_zeroes(const struct hal_flash *, uint32_t, uint32_t);
-int hal_flash_is_ones(const struct hal_flash *, uint32_t, uint32_t);
+int hal_flash_is_erased(const struct hal_flash *, uint32_t, uint32_t);
 
 #ifdef __cplusplus
 }
diff --git a/hw/hal/src/hal_flash.c b/hw/hal/src/hal_flash.c
index d0811c5a26..bed168af86 100644
--- a/hw/hal/src/hal_flash.c
+++ b/hw/hal/src/hal_flash.c
@@ -57,6 +57,18 @@ hal_flash_align(uint8_t flash_id)
     return hf->hf_align;
 }
 
+uint8_t
+hal_flash_erased_val(uint8_t flash_id)
+{
+    const struct hal_flash *hf;
+
+    hf = hal_bsp_flash_dev(flash_id);
+    if (!hf) {
+        return 1;
+    }
+    return hf->hf_erased_val;
+}
+
 uint32_t
 hal_flash_sector_size(const struct hal_flash *hf, int sec_idx)
 {
@@ -127,7 +139,7 @@ hal_flash_cmp_erased(const struct hal_flash *hf, uint32_t address,
         }
 
         for (i = 0; i < chunk_sz; i++) {
-            if (buf[i] != 0xff) {
+            if (buf[i] != hf->hf_erased_val) {
                 return -1;
             }
         }
@@ -298,9 +310,9 @@ hal_flash_erase(uint8_t id, uint32_t address, uint32_t num_bytes)
     return 0;
 }
 
-static int
-hal_flash_is_setto(const struct hal_flash *hf, uint32_t address,
-                   uint32_t num_bytes, uint8_t val)
+int
+hal_flash_is_erased(const struct hal_flash *hf, uint32_t address,
+        uint32_t num_bytes)
 {
     uint8_t buf[32];
     uint32_t blksz;
@@ -315,7 +327,7 @@ hal_flash_is_setto(const struct hal_flash *hf, uint32_t address,
             return -1;
         }
         for (i = 0; i < blksz; i++) {
-            if (buf[i] != val) {
+            if (buf[i] != hf->hf_erased_val) {
                 return 0;
             }
         }
@@ -324,20 +336,6 @@ hal_flash_is_setto(const struct hal_flash *hf, uint32_t address,
     return 1;
 }
 
-int
-hal_flash_is_ones(const struct hal_flash *hf, uint32_t address,
-                   uint32_t num_bytes)
-{
-    return hal_flash_is_setto(hf, address, num_bytes, 0xff);
-}
-
-int
-hal_flash_is_zeroes(const struct hal_flash *hf, uint32_t address,
-                   uint32_t num_bytes)
-{
-    return hal_flash_is_setto(hf, address, num_bytes, 0);
-}
-
 int
 hal_flash_isempty(uint8_t id, uint32_t address, uint32_t num_bytes)
 {
@@ -354,7 +352,7 @@ hal_flash_isempty(uint8_t id, uint32_t address, uint32_t num_bytes)
     if (hf->hf_itf->hff_is_empty) {
         return hf->hf_itf->hff_is_empty(hf, address, num_bytes);
     } else {
-        return hal_flash_is_ones(hf, address, num_bytes);
+        return hal_flash_is_erased(hf, address, num_bytes);
     }
 }
 
diff --git a/hw/mcu/ambiq/apollo2/src/hal_flash.c b/hw/mcu/ambiq/apollo2/src/hal_flash.c
index bf0582b518..ea050d0f88 100644
--- a/hw/mcu/ambiq/apollo2/src/hal_flash.c
+++ b/hw/mcu/ambiq/apollo2/src/hal_flash.c
@@ -53,7 +53,8 @@ const struct hal_flash apollo2_flash_dev = {
         .hf_base_addr = 0x00000000,
         .hf_size = 1024 * 1024,
         .hf_sector_cnt = 128,
-        .hf_align = 1
+        .hf_align = 1,
+        .hf_erased_val = 0xff,
 };
 
 static int
diff --git a/hw/mcu/microchip/pic32mx470f512h/src/hal_flash.c b/hw/mcu/microchip/pic32mx470f512h/src/hal_flash.c
index 179ef94810..5197405eb4 100644
--- a/hw/mcu/microchip/pic32mx470f512h/src/hal_flash.c
+++ b/hw/mcu/microchip/pic32mx470f512h/src/hal_flash.c
@@ -56,9 +56,10 @@ const struct hal_flash pic32mx_flash_dev = {
     .hf_base_addr = 0x1D000000,
     .hf_size = 512 * 1024,
     .hf_sector_cnt = 128,
-    .hf_align = 4      /* num bytes must be a multiple of 4 as writes can only
+    .hf_align = 4,     /* num bytes must be a multiple of 4 as writes can only
                         * be done on word boundary.
                         */
+    .hf_erased_val = 0xff,
 };
 
 static int
diff --git a/hw/mcu/microchip/pic32mz2048efg100/src/hal_flash.c b/hw/mcu/microchip/pic32mz2048efg100/src/hal_flash.c
index 78f1d1f09e..75f47b4ade 100644
--- a/hw/mcu/microchip/pic32mz2048efg100/src/hal_flash.c
+++ b/hw/mcu/microchip/pic32mz2048efg100/src/hal_flash.c
@@ -56,10 +56,11 @@ const struct hal_flash pic32mz_flash_dev = {
     .hf_base_addr = 0x1D000000,
     .hf_size = 2048 * 1024,
     .hf_sector_cnt = 128,
-    .hf_align = 4      /* num bytes must be a multiple of 4 as writes can only
+    .hf_align = 4,     /* num bytes must be a multiple of 4 as writes can only
                         * be done on word boundary. This also assumes that
                         * ECC memory is disabled (default on Wi-Fire board).
                         */
+    .hf_erased_val = 0xff,
 };
 
 static int
diff --git a/hw/mcu/native/src/hal_flash.c b/hw/mcu/native/src/hal_flash.c
index 95134320dc..e3dde504ba 100644
--- a/hw/mcu/native/src/hal_flash.c
+++ b/hw/mcu/native/src/hal_flash.c
@@ -81,6 +81,7 @@ const struct hal_flash native_flash_dev = {
     .hf_size = 1024 * 1024,
     .hf_sector_cnt = FLASH_NUM_AREAS,
     .hf_align = MYNEWT_VAL(MCU_FLASH_MIN_WRITE_SIZE),
+    .hf_erased_val = 0xff,
 };
 
 static void
diff --git a/hw/mcu/nordic/nrf51xxx/src/hal_flash.c b/hw/mcu/nordic/nrf51xxx/src/hal_flash.c
index dc52da8d7d..6ac0ceef9e 100644
--- a/hw/mcu/nordic/nrf51xxx/src/hal_flash.c
+++ b/hw/mcu/nordic/nrf51xxx/src/hal_flash.c
@@ -50,7 +50,8 @@ const struct hal_flash nrf51_flash_dev = {
     .hf_base_addr = 0x00000000,
     .hf_size = 256 * 1024,	/* XXX read from factory info? */
     .hf_sector_cnt = 256,	/* XXX read from factory info? */
-    .hf_align = 1
+    .hf_align = 1,
+    .hf_erased_val = 0xff,
 };
 
 #define NRF51_FLASH_READY() (NRF_NVMC->READY == NVMC_READY_READY_Ready)
diff --git a/hw/mcu/nordic/nrf52xxx/src/hal_flash.c b/hw/mcu/nordic/nrf52xxx/src/hal_flash.c
index 50edd54d14..e42a54028d 100644
--- a/hw/mcu/nordic/nrf52xxx/src/hal_flash.c
+++ b/hw/mcu/nordic/nrf52xxx/src/hal_flash.c
@@ -49,7 +49,8 @@ const struct hal_flash nrf52k_flash_dev = {
     .hf_base_addr = 0x00000000,
     .hf_size = 1024 * 1024,	/* XXX read from factory info? */
     .hf_sector_cnt = 256,	/* XXX read from factory info? */
-    .hf_align = 1
+    .hf_align = 1,
+    .hf_erased_val = 0xff,
 };
 #elif defined(NRF52810_XXAA)
 const struct hal_flash nrf52k_flash_dev = {
diff --git a/hw/mcu/nxp/MK64F12/src/hal_flash.c b/hw/mcu/nxp/MK64F12/src/hal_flash.c
index 083dc29ef3..b2ba1c6a2a 100644
--- a/hw/mcu/nxp/MK64F12/src/hal_flash.c
+++ b/hw/mcu/nxp/MK64F12/src/hal_flash.c
@@ -61,7 +61,8 @@ static flash_config_t mk64f12_config;
 struct hal_flash mk64f12_flash_dev = {
     /* Most items are set after FLASH_Init() */
     .hf_itf = &mk64f12_flash_funcs,
-    .hf_align = MK64F12_FLASH_ALIGN
+    .hf_align = MK64F12_FLASH_ALIGN,
+    .hf_erased_val = 0xff,
 };
 
 static int
diff --git a/hw/mcu/sifive/fe310/src/hal_flash.c b/hw/mcu/sifive/fe310/src/hal_flash.c
index 2591c911b8..68cc7c863d 100644
--- a/hw/mcu/sifive/fe310/src/hal_flash.c
+++ b/hw/mcu/sifive/fe310/src/hal_flash.c
@@ -50,7 +50,8 @@ const struct hal_flash fe310_flash_dev = {
     .hf_base_addr = 0x20000000,
     .hf_size = 8 * 1024 * 1024,  /* XXX read from factory info? */
     .hf_sector_cnt = 4096,       /* XXX read from factory info? */
-    .hf_align = 1
+    .hf_align = 1,
+    .hf_erased_val = 0xff,
 };
 
 #define FLASH_CMD_READ_STATUS_REGISTER 0x05
diff --git a/hw/mcu/stm/stm32f1xx/src/hal_flash.c b/hw/mcu/stm/stm32f1xx/src/hal_flash.c
index 4c63e18072..78a29ba652 100644
--- a/hw/mcu/stm/stm32f1xx/src/hal_flash.c
+++ b/hw/mcu/stm/stm32f1xx/src/hal_flash.c
@@ -50,6 +50,7 @@ const struct hal_flash stm32f1_flash_dev = {
     .hf_size = _FLASH_SIZE,
     .hf_sector_cnt = _FLASH_SIZE / _FLASH_SECTOR_SIZE,
     .hf_align = 2,
+    .hf_erased_val = 0xff,
 };
 
 static int
diff --git a/hw/mcu/stm/stm32f3xx/src/hal_flash.c b/hw/mcu/stm/stm32f3xx/src/hal_flash.c
index 4c8c144647..98d8fe8cec 100644
--- a/hw/mcu/stm/stm32f3xx/src/hal_flash.c
+++ b/hw/mcu/stm/stm32f3xx/src/hal_flash.c
@@ -127,6 +127,7 @@ stm32f3_flash_dev()
         stm32f3_flash_dev_.hf_size = HAL_FLASH_SIZE;
         stm32f3_flash_dev_.hf_sector_cnt = HAL_FLASH_SIZE / HAL_FLASH_SECTOR_SIZE;
         stm32f3_flash_dev_.hf_align = 2;
+        stm32f3_flash_dev_.hf_erased_val = 0xff;
     }
     return &stm32f3_flash_dev_;
 }
diff --git a/hw/mcu/stm/stm32f7xx/src/hal_flash.c b/hw/mcu/stm/stm32f7xx/src/hal_flash.c
index 720cad36a8..017c7220a2 100644
--- a/hw/mcu/stm/stm32f7xx/src/hal_flash.c
+++ b/hw/mcu/stm/stm32f7xx/src/hal_flash.c
@@ -66,7 +66,8 @@ const struct hal_flash stm32f7_flash_dev = {
     .hf_base_addr = 0x08000000,
     .hf_size = 2 * 1024 * 1024,
     .hf_sector_cnt = STM32F7_FLASH_NUM_AREAS - 1,
-    .hf_align = 1
+    .hf_align = 1,
+    .hf_erased_val = 0xff,
 };
 
 static int
diff --git a/hw/mcu/stm/stm32l1xx/src/hal_flash.c b/hw/mcu/stm/stm32l1xx/src/hal_flash.c
index 4a4dfc6b6e..d0b6af6f9d 100644
--- a/hw/mcu/stm/stm32l1xx/src/hal_flash.c
+++ b/hw/mcu/stm/stm32l1xx/src/hal_flash.c
@@ -52,6 +52,7 @@ const struct hal_flash stm32l1_flash_dev = {
     .hf_size = _FLASH_SIZE,
     .hf_sector_cnt = _FLASH_SIZE / _FLASH_SECTOR_SIZE,
     .hf_align = MYNEWT_VAL(MCU_FLASH_MIN_WRITE_SIZE),
+    .hf_erased_val = 0,
 };
 
 static int
diff --git a/hw/mcu/stm/stm32l4xx/src/hal_flash.c b/hw/mcu/stm/stm32l4xx/src/hal_flash.c
index bbe5a42b8f..07d2745d17 100644
--- a/hw/mcu/stm/stm32l4xx/src/hal_flash.c
+++ b/hw/mcu/stm/stm32l4xx/src/hal_flash.c
@@ -51,6 +51,7 @@ const struct hal_flash stm32l4_flash_dev = {
     .hf_size = _FLASH_SIZE,
     .hf_sector_cnt = _FLASH_SIZE / _FLASH_SECTOR_SIZE,
     .hf_align = 8,
+    .hf_erased_val = 0xff,
 };
 
 static int
diff --git a/sys/flash_map/include/flash_map/flash_map.h b/sys/flash_map/include/flash_map/flash_map.h
index a5be399498..454c8ffb2e 100644
--- a/sys/flash_map/include/flash_map/flash_map.h
+++ b/sys/flash_map/include/flash_map/flash_map.h
@@ -93,6 +93,11 @@ int flash_area_isempty_at(const struct flash_area *, uint32_t off,
  */
 uint8_t flash_area_align(const struct flash_area *);
 
+/*
+ * Value read from flash when it is erased.
+ */
+uint32_t flash_area_erased_val(const struct flash_area *fa);
+
 /*
  * Given flash map index, return info about sectors within the area.
  */
diff --git a/sys/flash_map/src/flash_map.c b/sys/flash_map/src/flash_map.c
index 0637e4476c..028bbe7780 100644
--- a/sys/flash_map/src/flash_map.c
+++ b/sys/flash_map/src/flash_map.c
@@ -167,9 +167,16 @@ flash_area_align(const struct flash_area *fa)
     return hal_flash_align(fa->fa_device_id);
 }
 
+uint32_t
+flash_area_erased_val(const struct flash_area *fa)
+{
+    return hal_flash_erased_val(fa->fa_device_id);
+}
+
+
 /**
  * Checks if a flash area has been erased. Returns false if there are any
- * non 0xFFFFFFFF bytes.
+ * non non-erased bytes.
  *
  * @param fa                    An opened flash area to iterate.
  *                                  the count of flash area TLVs in the meta


 

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services