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/11/06 02:51:28 UTC

[2/4] incubator-mynewt-larva git commit: Switch to use updated API in flash drivers. Expose these with current BSPs.

Switch to use updated API in flash drivers. Expose these with current
BSPs.


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/280392d9
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/280392d9
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/280392d9

Branch: refs/heads/master
Commit: 280392d92d3d43d6efd1ac002c6acb0919b7cffa
Parents: 4b867c7
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Thu Nov 5 17:31:16 2015 -0800
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Thu Nov 5 17:31:16 2015 -0800

----------------------------------------------------------------------
 hw/bsp/native/src/hal_bsp.c                     |  34 +++++
 hw/bsp/olimex_stm32-e407_devboard/src/hal_bsp.c |  16 +-
 hw/mcu/native/include/mcu/native_bsp.h          |  21 +++
 hw/mcu/native/src/hal_flash.c                   | 153 ++++++++-----------
 hw/mcu/stm/stm32f4xx/include/mcu/stm32f4_bsp.h  |   3 +
 hw/mcu/stm/stm32f4xx/src/hal_flash.c            | 109 ++++++-------
 6 files changed, 187 insertions(+), 149 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/280392d9/hw/bsp/native/src/hal_bsp.c
----------------------------------------------------------------------
diff --git a/hw/bsp/native/src/hal_bsp.c b/hw/bsp/native/src/hal_bsp.c
new file mode 100644
index 0000000..6ece5e9
--- /dev/null
+++ b/hw/bsp/native/src/hal_bsp.c
@@ -0,0 +1,34 @@
+/**
+ * Copyright (c) 2015 Runtime Inc.
+ *
+ * Licensed 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 <stdio.h>
+#include <assert.h>
+#include <string.h>
+#include <inttypes.h>
+#include "hal/hal_flash_int.h"
+#include "mcu/native_bsp.h"
+
+struct hal_flash *
+bsp_flash_dev(uint8_t id)
+{
+    /*
+     * Just one to start with
+     */
+    if (id != 0) {
+        return NULL;
+    }
+    return (struct hal_flash *)&native_flash_dev;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/280392d9/hw/bsp/olimex_stm32-e407_devboard/src/hal_bsp.c
----------------------------------------------------------------------
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 f700a0b..b83c60a 100644
--- a/hw/bsp/olimex_stm32-e407_devboard/src/hal_bsp.c
+++ b/hw/bsp/olimex_stm32-e407_devboard/src/hal_bsp.c
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 #include "hal/hal_gpio.h"
+#include "hal/hal_flash_int.h"
 #include "mcu/stm32f407xx.h"
 #include "mcu/stm32f4xx_hal_gpio_ex.h"
 #include "mcu/stm32f4_bsp.h"
@@ -34,8 +35,21 @@ static const struct stm32f4_uart_cfg uart_cfg[UART_CNT] = {
     }
 };
 
-const struct stm32f4_uart_cfg *bsp_uart_config(int port)
+const struct stm32f4_uart_cfg *
+bsp_uart_config(int port)
 {
     assert(port < UART_CNT);
     return &uart_cfg[port];
 }
+
+struct hal_flash *
+bsp_flash_dev(uint8_t id)
+{
+    /*
+     * Internal flash mapped to id 0.
+     */
+    if (id != 0) {
+        return NULL;
+    }
+    return &stm32f4_flash_dev;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/280392d9/hw/mcu/native/include/mcu/native_bsp.h
----------------------------------------------------------------------
diff --git a/hw/mcu/native/include/mcu/native_bsp.h b/hw/mcu/native/include/mcu/native_bsp.h
new file mode 100644
index 0000000..91d41dc
--- /dev/null
+++ b/hw/mcu/native/include/mcu/native_bsp.h
@@ -0,0 +1,21 @@
+/**
+ * Copyright (c) 2015 Runtime Inc.
+ *
+ * Licensed 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.
+ */
+#ifndef H_NATIVE_BSP_
+#define H_NATIVE_BSP_
+
+extern const struct hal_flash native_flash_dev;
+
+#endif /* H_NATIVE_BSP_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/280392d9/hw/mcu/native/src/hal_flash.c
----------------------------------------------------------------------
diff --git a/hw/mcu/native/src/hal_flash.c b/hw/mcu/native/src/hal_flash.c
index 63f3645..b33f906 100644
--- a/hw/mcu/native/src/hal_flash.c
+++ b/hw/mcu/native/src/hal_flash.c
@@ -4,7 +4,7 @@
  * Licensed 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
@@ -18,31 +18,47 @@
 #include <assert.h>
 #include <string.h>
 #include <inttypes.h>
-#include "hal/hal_flash.h"
+#include "hal/hal_flash_int.h"
 
 static FILE *file;
 
-static const struct flash_area_desc {
-    uint32_t fad_offset;
-    uint32_t fad_length;
-    uint32_t fad_sector_id;
-} flash_area_descs[] = {
-    { 0x00000000, 16 * 1024,  0 },
-    { 0x00004000, 16 * 1024,  1 },
-    { 0x00008000, 16 * 1024,  2 },
-    { 0x0000c000, 16 * 1024,  3 },
-    { 0x00010000, 64 * 1024,  4 },
-    { 0x00020000, 128 * 1024, 5 },
-    { 0x00040000, 128 * 1024, 6 },
-    { 0x00060000, 128 * 1024, 7 },
-    { 0x00080000, 128 * 1024, 8 },
-    { 0x000a0000, 128 * 1024, 9 },
-    { 0x000c0000, 128 * 1024, 10 },
-    { 0x000e0000, 128 * 1024, 11 },
+static int native_flash_init(void);
+static int native_flash_read(uint32_t address, void *dst, uint32_t length);
+static int native_flash_write(uint32_t address, const void *src,
+  uint32_t length);
+static int native_flash_erase_sector(uint32_t sector_address);
+
+static const struct hal_flash_funcs native_flash_funcs = {
+    .hff_read = native_flash_read,
+    .hff_write = native_flash_write,
+    .hff_erase_sector = native_flash_erase_sector,
+    .hff_init = native_flash_init
+};
+
+static const uint32_t native_flash_sectors[] = {
+    0x00000000, /* 16 * 1024 */
+    0x00004000, /* 16 * 1024 */
+    0x00008000, /* 16 * 1024 */
+    0x0000c000, /* 16 * 1024 */
+    0x00010000, /* 64 * 1024 */
+    0x00020000, /* 128 * 1024 */
+    0x00040000, /* 128 * 1024 */
+    0x00060000, /* 128 * 1024 */
+    0x00080000, /* 128 * 1024 */
+    0x000a0000, /* 128 * 1024 */
+    0x000c0000, /* 128 * 1024 */
+    0x000e0000, /* 128 * 1024 */
 };
 
-#define FLASH_NUM_AREAS   (int)(sizeof flash_area_descs /       \
-                                sizeof flash_area_descs[0])
+#define FLASH_NUM_AREAS   (int)(sizeof native_flash_sectors /           \
+                                sizeof native_flash_sectors[0])
+
+const struct hal_flash native_flash_dev = {
+    .hf_itf = &native_flash_funcs,
+    .hf_size = 1024 * 1024,
+    .hf_sector_cnt = FLASH_NUM_AREAS,
+    .hf_sectors = native_flash_sectors
+};
 
 static void
 flash_native_erase(uint32_t addr, uint32_t len)
@@ -75,18 +91,10 @@ flash_native_erase(uint32_t addr, uint32_t len)
 static void
 flash_native_ensure_file_open(void)
 {
-    int expected_sz;
-    int i;
-
     if (file == NULL) {
-        expected_sz = 0;
-        for (i = 0; i < FLASH_NUM_AREAS; i++) {
-            expected_sz += flash_area_descs[i].fad_length;
-        }
-
         file = tmpfile();
         assert(file != NULL);
-        flash_native_erase(0, expected_sz);
+        flash_native_erase(0, native_flash_dev.hf_size);
     }
 }
 
@@ -121,7 +129,7 @@ flash_native_write_internal(uint32_t address, const void *src, uint32_t length,
 
         /* Ensure data is not being overwritten. */
         if (!allow_overwrite) {
-            rc = flash_read(cur, buf, chunk_sz);
+            rc = native_flash_read(cur, buf, chunk_sz);
             assert(rc == 0);
             for (i = 0; i < chunk_sz; i++) {
                 assert(buf[i] == 0xff);
@@ -139,13 +147,13 @@ flash_native_write_internal(uint32_t address, const void *src, uint32_t length,
     return 0;
 }
 
-int
-flash_write(uint32_t address, const void *src, uint32_t length)
+static int
+native_flash_write(uint32_t address, const void *src, uint32_t length)
 {
     return flash_native_write_internal(address, src, length, 0);
 }
 
-int
+static int
 flash_native_overwrite(uint32_t address, const void *src, uint32_t length)
 {
     return flash_native_write_internal(address, src, length, 1);
@@ -179,8 +187,8 @@ flash_native_memset(uint32_t offset, uint8_t c, uint32_t len)
     return 0;
 }
 
-int
-flash_read(uint32_t address, void *dst, uint32_t length)
+static int
+native_flash_read(uint32_t address, void *dst, uint32_t length)
 {
     flash_native_ensure_file_open();
     fseek(file, address, SEEK_SET);
@@ -195,7 +203,7 @@ find_area(uint32_t address)
     int i;
 
     for (i = 0; i < FLASH_NUM_AREAS; i++) {
-        if (flash_area_descs[i].fad_offset == address) {
+        if (native_flash_sectors[i] == address) {
             return i;
         }
     }
@@ -203,71 +211,38 @@ find_area(uint32_t address)
     return -1;
 }
 
-int
-flash_erase_sector(uint32_t sector_address)
+static int
+flash_sector_len(int sector)
 {
-    int next_sector_id;
-    int sector_id;
-    int area_id;
-
-    flash_native_ensure_file_open();
-
-    area_id = find_area(sector_address);
-    if (area_id == -1) {
-        return -1;
-    }
-
-    sector_id = flash_area_descs[area_id].fad_sector_id;
-    while (1) {
-        flash_native_erase(sector_address,
-                           flash_area_descs[area_id].fad_length);
-
-        area_id++;
-        if (area_id >= FLASH_NUM_AREAS) {
-            break;
-        }
-
-        next_sector_id = flash_area_descs[area_id].fad_sector_id;
-        if (next_sector_id != sector_id) {
-            break;
-        }
+    uint32_t end;
 
-        sector_id = next_sector_id;
+    if (sector == FLASH_NUM_AREAS - 1) {
+        end = native_flash_dev.hf_size + native_flash_sectors[0];
+    } else {
+        end = native_flash_sectors[sector + 1];
     }
-
-    return 0;
+    return end - native_flash_sectors[sector];
 }
 
-int
-flash_erase(uint32_t address, uint32_t num_bytes)
+static int
+native_flash_erase_sector(uint32_t sector_address)
 {
-    const struct flash_area_desc *area;
-    uint32_t end;
-    int i;
+    int area_id;
+    uint32_t len;
 
     flash_native_ensure_file_open();
 
-    end = address + num_bytes;
-
-    for (i = 0; i < FLASH_NUM_AREAS; i++) {
-        area = flash_area_descs + i;
-
-        if (area->fad_offset >= end) {
-            return 0;
-        }
-
-        if (address >= area->fad_offset &&
-            address < area->fad_offset + area->fad_length) {
-
-            flash_native_erase(area->fad_offset, area->fad_length);
-        }
+    area_id = find_area(sector_address);
+    if (area_id == -1) {
+        return -1;
     }
-
+    len = flash_sector_len(area_id);
+    flash_native_erase(sector_address, len);
     return 0;
 }
 
-int
-flash_init(void)
+static int
+native_flash_init(void)
 {
     return 0;
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/280392d9/hw/mcu/stm/stm32f4xx/include/mcu/stm32f4_bsp.h
----------------------------------------------------------------------
diff --git a/hw/mcu/stm/stm32f4xx/include/mcu/stm32f4_bsp.h b/hw/mcu/stm/stm32f4xx/include/mcu/stm32f4_bsp.h
index 41c2d3c..3dac3ba 100644
--- a/hw/mcu/stm/stm32f4xx/include/mcu/stm32f4_bsp.h
+++ b/hw/mcu/stm/stm32f4xx/include/mcu/stm32f4_bsp.h
@@ -39,4 +39,7 @@ const struct stm32f4_uart_cfg *bsp_uart_config(int port);
  */
 int hal_gpio_init_af(int pin, uint8_t af_type, enum gpio_pull pull);
 
+struct hal_flash;
+extern struct hal_flash stm32f4_flash_dev;
+
 #endif /* __MCU_STM32F4_BSP_H_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/280392d9/hw/mcu/stm/stm32f4xx/src/hal_flash.c
----------------------------------------------------------------------
diff --git a/hw/mcu/stm/stm32f4xx/src/hal_flash.c b/hw/mcu/stm/stm32f4xx/src/hal_flash.c
index 3f84a10..ad93b3b 100644
--- a/hw/mcu/stm/stm32f4xx/src/hal_flash.c
+++ b/hw/mcu/stm/stm32f4xx/src/hal_flash.c
@@ -18,39 +18,56 @@
 #include "mcu/stm32f4xx_hal_def.h"
 #include "mcu/stm32f4xx_hal_flash.h"
 #include "mcu/stm32f4xx_hal_flash_ex.h"
-#include "hal/hal_flash.h"
-
-static const struct flash_area_desc {
-    uint32_t fad_offset;
-    uint32_t fad_length;
-    int fad_sector_id;
-} flash_area_descs[] = {
-    { 0x08000000, 16 * 1024, FLASH_SECTOR_0 },
-    { 0x08004000, 16 * 1024, FLASH_SECTOR_1 },
-    { 0x08008000, 16 * 1024, FLASH_SECTOR_2 },
-    { 0x0800c000, 16 * 1024, FLASH_SECTOR_3 },
-    { 0x08010000, 64 * 1024, FLASH_SECTOR_4 },
-    { 0x08020000, 128 * 1024, FLASH_SECTOR_5 },
-    { 0x08040000, 128 * 1024, FLASH_SECTOR_6 },
-    { 0x08060000, 128 * 1024, FLASH_SECTOR_7 },
-    { 0x08080000, 128 * 1024, FLASH_SECTOR_8 },
-    { 0x080a0000, 128 * 1024, FLASH_SECTOR_9 },
-    { 0x080c0000, 128 * 1024, FLASH_SECTOR_10 },
-    { 0x080e0000, 128 * 1024, FLASH_SECTOR_11 },
+#include "hal/hal_flash_int.h"
+
+static int stm32f4_flash_read(uint32_t address, void *dst, uint32_t num_bytes);
+static int stm32f4_flash_write(uint32_t address, const void *src,
+  uint32_t num_bytes);
+static int stm32f4_flash_erase_sector(uint32_t sector_address);
+static int stm32f4_flash_init(void);
+
+static const struct hal_flash_funcs stm32f4_flash_funcs = {
+    .hff_read = stm32f4_flash_read,
+    .hff_write = stm32f4_flash_write,
+    .hff_erase_sector = stm32f4_flash_erase_sector,
+    .hff_init = stm32f4_flash_init
 };
 
-#define FLASH_NUM_AREAS   (int)(sizeof flash_area_descs /       \
-                                sizeof flash_area_descs[0])
+static const uint32_t stm32f4_flash_sectors[] = {
+    0x08000000,
+    0x08004000,
+    0x08008000,
+    0x0800c000,
+    0x08010000,
+    0x08020000,
+    0x08040000,
+    0x08060000,
+    0x08080000,
+    0x080a0000,
+    0x080c0000,
+    0x080e0000
+};
+
+#define STM32F4_FLASH_NUM_AREAS                                         \
+    (int)(sizeof(stm32f4_flash_sectors) /                               \
+      sizeof(stm32f4_flash_sectors[0]))
 
-int
-flash_read(uint32_t address, void *dst, uint32_t num_bytes)
+const struct hal_flash stm32f4_flash_dev = {
+    .hf_itf = &stm32f4_flash_funcs,
+    .hf_size = 1024 * 1024,
+    .hf_sector_cnt = STM32F4_FLASH_NUM_AREAS,
+    .hf_sectors = stm32f4_flash_sectors
+};
+
+static int
+stm32f4_flash_read(uint32_t address, void *dst, uint32_t num_bytes)
 {
     memcpy(dst, (void *)address, num_bytes);
     return 0;
 }
 
-int
-flash_write(uint32_t address, const void *src, uint32_t num_bytes)
+static int
+stm32f4_flash_write(uint32_t address, const void *src, uint32_t num_bytes)
 {
     const uint8_t *sptr;
     uint32_t i;
@@ -70,19 +87,19 @@ flash_write(uint32_t address, const void *src, uint32_t num_bytes)
 }
 
 static void
-flash_erase_sector_id(int sector_id)
+stm32f4_flash_erase_sector_id(int sector_id)
 {
     FLASH_Erase_Sector(sector_id, FLASH_VOLTAGE_RANGE_1);
 }
 
-int
-flash_erase_sector(uint32_t sector_address)
+static int
+stm32f4_flash_erase_sector(uint32_t sector_address)
 {
     int i;
 
-    for (i = 0; i < FLASH_NUM_AREAS; i++) {
-        if (flash_area_descs[i].fad_offset == sector_address) {
-            flash_erase_sector_id(flash_area_descs[i].fad_sector_id);
+    for (i = 0; i < STM32F4_FLASH_NUM_AREAS; i++) {
+        if (stm32f4_flash_sectors[i] == sector_address) {
+            stm32f4_flash_erase_sector_id(i);
             return 0;
         }
     }
@@ -90,34 +107,8 @@ flash_erase_sector(uint32_t sector_address)
     return -1;
 }
 
-int
-flash_erase(uint32_t address, uint32_t num_bytes)
-{
-    const struct flash_area_desc *area;
-    uint32_t end;
-    int i;
-
-    end = address + num_bytes;
-
-    for (i = 0; i < FLASH_NUM_AREAS; i++) {
-        area = flash_area_descs + i;
-
-        if (area->fad_offset >= end) {
-            return 0;
-        }
-
-        if (address >= area->fad_offset &&
-            address < area->fad_offset + area->fad_length) {
-
-            flash_erase_sector_id(area->fad_sector_id);
-        }
-    }
-
-    return 0;
-}
-
-int
-flash_init(void)
+static int
+stm32f4_flash_init(void)
 {
     int rc;