This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 2af9b57  boards/arm/samv7: define MTD progmem partitions at board level
2af9b57 is described below

commit 2af9b574f60af1e9cd40175e97286bd07f5fe001
Author: Petro Karashchenko <petro.karashche...@gmail.com>
AuthorDate: Fri Mar 18 15:33:56 2022 +0100

    boards/arm/samv7: define MTD progmem partitions at board level
    
    Signed-off-by: Petro Karashchenko <petro.karashche...@gmail.com>
---
 boards/arm/samv7/common/include/board_progmem.h    |  25 +++-
 boards/arm/samv7/common/src/sam_progmem.c          | 150 +++++++--------------
 boards/arm/samv7/same70-qmtech/src/sam_bringup.c   |  40 +++++-
 boards/arm/samv7/same70-xplained/src/sam_bringup.c |  44 +++++-
 boards/arm/samv7/samv71-xult/src/sam_bringup.c     |  39 +++++-
 5 files changed, 179 insertions(+), 119 deletions(-)

diff --git a/boards/arm/samv7/common/include/board_progmem.h 
b/boards/arm/samv7/common/include/board_progmem.h
index 65381cc..c07df50 100644
--- a/boards/arm/samv7/common/include/board_progmem.h
+++ b/boards/arm/samv7/common/include/board_progmem.h
@@ -22,15 +22,31 @@
 #define __BOARDS_ARM_SAMV7_COMMON_INCLUDE_BOARD_PROGMEM_H
 
 /****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stddef.h>
+
+#include <nuttx/mtd/mtd.h>
+
+#ifndef __ASSEMBLY__
+
+/****************************************************************************
  * Public Types
  ****************************************************************************/
 
+struct mtd_partition_s
+{
+  size_t            offset;    /* Partition offset from the beginning of MTD */
+  size_t            size;      /* Partition size in bytes */
+  const char       *devpath;   /* Partition device path */
+  struct mtd_dev_s *mtd;       /* Pointer to allocated MTD partition */
+};
+
 /****************************************************************************
  * Public Data
  ****************************************************************************/
 
-#ifndef __ASSEMBLY__
-
 #undef EXTERN
 #if defined(__cplusplus)
 #define EXTERN extern "C"
@@ -52,6 +68,8 @@ extern "C"
  *
  * Input Parameters:
  *   minor - The starting minor number for progmem MTD partitions.
+ *   table - Progmem MTD partition table
+ *   count - Number of element in progmem MTD partition table
  *
  * Returned Value:
  *   Zero is returned on success.  Otherwise, a negated errno value is
@@ -59,7 +77,8 @@ extern "C"
  *
  ****************************************************************************/
 
-int board_progmem_init(int minor);
+int board_progmem_init(int minor, struct mtd_partition_s *table,
+                       size_t count);
 
 #undef EXTERN
 #if defined(__cplusplus)
diff --git a/boards/arm/samv7/common/src/sam_progmem.c 
b/boards/arm/samv7/common/src/sam_progmem.c
index 2c42a19..cf38772 100644
--- a/boards/arm/samv7/common/src/sam_progmem.c
+++ b/boards/arm/samv7/common/src/sam_progmem.c
@@ -42,50 +42,6 @@
 #include "board_progmem.h"
 
 /****************************************************************************
- * Pre-processor Definitions
- ****************************************************************************/
-
-#define ARRAYSIZE(x)                (sizeof((x)) / sizeof((x)[0]))
-
-/****************************************************************************
- * Private Types
- ****************************************************************************/
-
-struct mcuboot_partition_s
-{
-  uint32_t    offset;          /* Partition offset from the beginning of MTD */
-  uint32_t    size;            /* Partition size in bytes */
-  const char *devpath;         /* Partition device path */
-};
-
-/****************************************************************************
- * Private Data
- ****************************************************************************/
-
-static FAR struct mtd_dev_s *g_samv7_progmem_mtd;
-
-#if defined(CONFIG_SAMV7_PROGMEM_OTA_PARTITION)
-static const struct mcuboot_partition_s g_mcuboot_partition_table[] =
-{
-  {
-    .offset  = CONFIG_SAMV7_OTA_PRIMARY_SLOT_OFFSET,
-    .size    = CONFIG_SAMV7_OTA_SLOT_SIZE,
-    .devpath = CONFIG_SAMV7_OTA_PRIMARY_SLOT_DEVPATH
-  },
-  {
-    .offset  = CONFIG_SAMV7_OTA_SECONDARY_SLOT_OFFSET,
-    .size    = CONFIG_SAMV7_OTA_SLOT_SIZE,
-    .devpath = CONFIG_SAMV7_OTA_SECONDARY_SLOT_DEVPATH
-  },
-  {
-    .offset  = CONFIG_SAMV7_OTA_SCRATCH_OFFSET,
-    .size    = CONFIG_SAMV7_OTA_SCRATCH_SIZE,
-    .devpath = CONFIG_SAMV7_OTA_SCRATCH_DEVPATH
-  }
-};
-#endif /* CONFIG_SAMV7_PROGMEM_OTA_PARTITION */
-
-/****************************************************************************
  * Private Functions
  ****************************************************************************/
 
@@ -146,7 +102,6 @@ static int sam_progmem_register_driver(int minor, FAR 
struct mtd_dev_s *mtd,
   return ret;
 }
 
-#if defined(CONFIG_SAMV7_PROGMEM_OTA_PARTITION)
 /****************************************************************************
  * Name: sam_progmem_alloc_mtdpart
  *
@@ -154,6 +109,7 @@ static int sam_progmem_register_driver(int minor, FAR 
struct mtd_dev_s *mtd,
  *   Allocate an MTD partition from FLASH.
  *
  * Input Parameters:
+ *   mtd        - The MTD device to be partitioned
  *   mtd_offset - MTD Partition offset from the base address in FLASH.
  *   mtd_size   - Size for the MTD partition.
  *
@@ -162,19 +118,20 @@ static int sam_progmem_register_driver(int minor, FAR 
struct mtd_dev_s *mtd,
  *
  ****************************************************************************/
 
-static struct mtd_dev_s *sam_progmem_alloc_mtdpart(uint32_t mtd_offset,
-                                                   uint32_t mtd_size)
+static struct mtd_dev_s *sam_progmem_alloc_mtdpart(FAR struct mtd_dev_s *mtd,
+                                                   size_t mtd_offset,
+                                                   size_t mtd_size)
 {
-  uint32_t blocks;
-  uint32_t startblock;
+  ssize_t startblock;
+  size_t  blocks;
 
   ASSERT((mtd_offset + mtd_size) <= up_progmem_neraseblocks() *
           up_progmem_pagesize(0));
   ASSERT((mtd_offset % up_progmem_pagesize(0)) == 0);
   ASSERT((mtd_size % up_progmem_pagesize(0)) == 0);
 
-  finfo("\tMTD offset = 0x%" PRIx32 "\n", mtd_offset);
-  finfo("\tMTD size = 0x%" PRIx32 "\n", mtd_size);
+  finfo("\tMTD offset = 0x%zx\n", mtd_offset);
+  finfo("\tMTD size = %zu\n", mtd_size);
 
   startblock = up_progmem_getpage(mtd_offset);
   if (startblock < 0)
@@ -184,51 +141,10 @@ static struct mtd_dev_s 
*sam_progmem_alloc_mtdpart(uint32_t mtd_offset,
 
   blocks = mtd_size / up_progmem_pagesize(0);
 
-  return mtd_partition(g_samv7_progmem_mtd, startblock, blocks);
+  return mtd_partition(mtd, startblock, blocks);
 }
 
 /****************************************************************************
- * Name: init_mcuboot_partitions
- *
- * Description:
- *   Initialize partitions that are dedicated to firmware MCUBOOT update.
- *
- * Input Parameters:
- *   minor - The starting minor number for progmem MTD partitions.
- *
- * Returned Value:
- *   Zero on success; a negated errno value on failure.
- *
- ****************************************************************************/
-
-static int init_mcuboot_partitions(int minor)
-{
-  FAR struct mtd_dev_s *mtd;
-  int ret = OK;
-
-  for (int i = 0; i < ARRAYSIZE(g_mcuboot_partition_table); ++i)
-    {
-      const struct mcuboot_partition_s *part = &g_mcuboot_partition_table[i];
-      mtd = sam_progmem_alloc_mtdpart(part->offset, part->size);
-
-      if (mtd == NULL)
-        {
-          ferr("ERROR: create MTD OTA partition %s", part->devpath);
-          continue;
-        }
-
-      ret = sam_progmem_register_driver(minor + i, mtd, part->devpath);
-      if (ret < 0)
-        {
-          return ret;
-        }
-    }
-
-  return ret;
-}
-#endif /* CONFIG_SAMV7_PROGMEM_OTA_PARTITION */
-
-/****************************************************************************
  * Public Functions
  ****************************************************************************/
 
@@ -240,6 +156,8 @@ static int init_mcuboot_partitions(int minor)
  *
  * Input Parameters:
  *   minor - The starting minor number for progmem MTD partitions.
+ *   table - Progmem MTD partition table
+ *   count - Number of element in progmem MTD partition table
  *
  * Returned Value:
  *   Zero is returned on success.  Otherwise, a negated errno value is
@@ -247,8 +165,11 @@ static int init_mcuboot_partitions(int minor)
  *
  ****************************************************************************/
 
-int board_progmem_init(int minor)
+int board_progmem_init(int minor, struct mtd_partition_s *table,
+                       size_t count)
 {
+  FAR struct mtd_dev_s *progmem_mtd;
+  size_t i;
   int ret = OK;
 
   /* Initialize the SAMV7 FLASH programming memory library */
@@ -257,25 +178,44 @@ int board_progmem_init(int minor)
 
   /* Create an instance of the SAMV7 FLASH program memory device driver */
 
-  g_samv7_progmem_mtd = progmem_initialize();
-  if (g_samv7_progmem_mtd == NULL)
+  progmem_mtd = progmem_initialize();
+  if (progmem_mtd == NULL)
     {
       return -EFAULT;
     }
 
-#if defined(CONFIG_SAMV7_PROGMEM_OTA_PARTITION)
-  ret = init_mcuboot_partitions(minor);
-  if (ret < 0)
+  if (table == NULL || count == 0)
     {
-      return ret;
+      ret = sam_progmem_register_driver(minor, progmem_mtd, NULL);
     }
-#else
-  ret = sam_progmem_register_driver(minor, g_samv7_progmem_mtd, NULL);
-  if (ret < 0)
+  else
     {
-      return ret;
+      for (i = 0; i < count; ++i)
+        {
+          struct mtd_partition_s *part = &table[i];
+
+          part->mtd = sam_progmem_alloc_mtdpart(progmem_mtd, part->offset,
+                                                part->size);
+          if (part->mtd != NULL)
+            {
+              if (part->devpath != NULL)
+                {
+                  ret = sam_progmem_register_driver(minor + i, part->mtd,
+                                                    part->devpath);
+                  if (ret < 0)
+                    {
+                      break;
+                    }
+                }
+            }
+          else
+            {
+              ferr("Failed to allocate MTD partition %d", i);
+              ret = -ENOMEM;
+              break;
+            }
+        }
     }
-#endif
 
   return ret;
 }
diff --git a/boards/arm/samv7/same70-qmtech/src/sam_bringup.c 
b/boards/arm/samv7/same70-qmtech/src/sam_bringup.c
index 7759acd..4b432d2 100644
--- a/boards/arm/samv7/same70-qmtech/src/sam_bringup.c
+++ b/boards/arm/samv7/same70-qmtech/src/sam_bringup.c
@@ -72,11 +72,44 @@
  * Pre-processor Definitions
  ****************************************************************************/
 
+#define ARRAY_SIZE(x)   (sizeof(x) / sizeof((x)[0]))
+
 #define NSECTORS(n) \
   (((n)+CONFIG_SAME70QMTECH_ROMFS_ROMDISK_SECTSIZE-1) / \
    CONFIG_SAME70QMTECH_ROMFS_ROMDISK_SECTSIZE)
 
 /****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+#if defined(CONFIG_SAMV7_PROGMEM_OTA_PARTITION)
+static struct mtd_partition_s g_mtd_partition_table[] =
+{
+  {
+    .offset  = CONFIG_SAMV7_OTA_PRIMARY_SLOT_OFFSET,
+    .size    = CONFIG_SAMV7_OTA_SLOT_SIZE,
+    .devpath = CONFIG_SAMV7_OTA_PRIMARY_SLOT_DEVPATH
+  },
+  {
+    .offset  = CONFIG_SAMV7_OTA_SECONDARY_SLOT_OFFSET,
+    .size    = CONFIG_SAMV7_OTA_SLOT_SIZE,
+    .devpath = CONFIG_SAMV7_OTA_SECONDARY_SLOT_DEVPATH
+  },
+  {
+    .offset  = CONFIG_SAMV7_OTA_SCRATCH_OFFSET,
+    .size    = CONFIG_SAMV7_OTA_SCRATCH_SIZE,
+    .devpath = CONFIG_SAMV7_OTA_SCRATCH_DEVPATH
+  }
+};
+
+static const size_t g_mtd_partition_table_size =
+    ARRAY_SIZE(g_mtd_partition_table);
+#else
+#  define g_mtd_partition_table         NULL
+#  define g_mtd_partition_table_size    0
+#endif /* CONFIG_SAMV7_PROGMEM_OTA_PARTITION */
+
+/****************************************************************************
  * Private Functions
  ****************************************************************************/
 
@@ -195,7 +228,8 @@ int sam_bringup(void)
 #ifdef HAVE_PROGMEM_CHARDEV
   /* Initialize the SAME70 FLASH programming memory library */
 
-  ret = board_progmem_init(PROGMEM_MTD_MINOR);
+  ret = board_progmem_init(PROGMEM_MTD_MINOR, g_mtd_partition_table,
+                           g_mtd_partition_table_size);
   if (ret < 0)
     {
       syslog(LOG_ERR, "ERROR: Failed to initialize progmem: %d\n", ret);
@@ -227,8 +261,8 @@ int sam_bringup(void)
   ret = sam_dacdev_initialize();
   if (ret < 0)
     {
-      syslog(LOG_ERR,
-             "ERROR: Initialization of the DAC module failed: %d\n", ret);
+      syslog(LOG_ERR, "ERROR: Initialization of the DAC module failed: %d\n",
+             ret);
     }
 #endif
 
diff --git a/boards/arm/samv7/same70-xplained/src/sam_bringup.c 
b/boards/arm/samv7/same70-xplained/src/sam_bringup.c
index 72cda9d..83dd694 100644
--- a/boards/arm/samv7/same70-xplained/src/sam_bringup.c
+++ b/boards/arm/samv7/same70-xplained/src/sam_bringup.c
@@ -65,11 +65,44 @@
  * Pre-processor Definitions
  ****************************************************************************/
 
+#define ARRAY_SIZE(x)   (sizeof(x) / sizeof((x)[0]))
+
 #define NSECTORS(n) \
   (((n)+CONFIG_SAME70XPLAINED_ROMFS_ROMDISK_SECTSIZE-1) / \
    CONFIG_SAME70XPLAINED_ROMFS_ROMDISK_SECTSIZE)
 
 /****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+#if defined(CONFIG_SAMV7_PROGMEM_OTA_PARTITION)
+static struct mtd_partition_s g_mtd_partition_table[] =
+{
+  {
+    .offset  = CONFIG_SAMV7_OTA_PRIMARY_SLOT_OFFSET,
+    .size    = CONFIG_SAMV7_OTA_SLOT_SIZE,
+    .devpath = CONFIG_SAMV7_OTA_PRIMARY_SLOT_DEVPATH
+  },
+  {
+    .offset  = CONFIG_SAMV7_OTA_SECONDARY_SLOT_OFFSET,
+    .size    = CONFIG_SAMV7_OTA_SLOT_SIZE,
+    .devpath = CONFIG_SAMV7_OTA_SECONDARY_SLOT_DEVPATH
+  },
+  {
+    .offset  = CONFIG_SAMV7_OTA_SCRATCH_OFFSET,
+    .size    = CONFIG_SAMV7_OTA_SCRATCH_SIZE,
+    .devpath = CONFIG_SAMV7_OTA_SCRATCH_DEVPATH
+  }
+};
+
+static const size_t g_mtd_partition_table_size =
+    ARRAY_SIZE(g_mtd_partition_table);
+#else
+#  define g_mtd_partition_table         NULL
+#  define g_mtd_partition_table_size    0
+#endif /* CONFIG_SAMV7_PROGMEM_OTA_PARTITION */
+
+/****************************************************************************
  * Private Functions
  ****************************************************************************/
 
@@ -97,8 +130,8 @@ static void sam_i2c_register(int bus)
       ret = i2c_register(i2c, bus);
       if (ret < 0)
         {
-          syslog(LOG_ERR,
-                 "ERROR: Failed to register I2C%d driver: %d\n", bus, ret);
+          syslog(LOG_ERR, "ERROR: Failed to register I2C%d driver: %d\n",
+                 bus, ret);
           sam_i2cbus_uninitialize(i2c);
         }
     }
@@ -256,7 +289,8 @@ int sam_bringup(void)
 #ifdef HAVE_PROGMEM_CHARDEV
   /* Initialize the SAME70 FLASH programming memory library */
 
-  ret = board_progmem_init(PROGMEM_MTD_MINOR);
+  ret = board_progmem_init(PROGMEM_MTD_MINOR, g_mtd_partition_table,
+                           g_mtd_partition_table_size);
   if (ret < 0)
     {
       syslog(LOG_ERR, "ERROR: Failed to initialize progmem: %d\n", ret);
@@ -340,8 +374,8 @@ int sam_bringup(void)
   ret = sam_dacdev_initialize();
   if (ret < 0)
     {
-      syslog(LOG_ERR,
-             "ERROR: Initialization of the DAC module failed: %d\n", ret);
+      syslog(LOG_ERR, "ERROR: Initialization of the DAC module failed: %d\n",
+             ret);
     }
 #endif
 
diff --git a/boards/arm/samv7/samv71-xult/src/sam_bringup.c 
b/boards/arm/samv7/samv71-xult/src/sam_bringup.c
index 03fe366..8e82ab1 100644
--- a/boards/arm/samv7/samv71-xult/src/sam_bringup.c
+++ b/boards/arm/samv7/samv71-xult/src/sam_bringup.c
@@ -32,14 +32,13 @@
 #include <errno.h>
 #include <debug.h>
 
-#include <nuttx/fs/fs.h>
-
 #ifdef CONFIG_USBMONITOR
 #  include <nuttx/usb/usbmonitor.h>
 #endif
 
 #include <nuttx/drivers/drivers.h>
 #include <nuttx/drivers/ramdisk.h>
+#include <nuttx/fs/fs.h>
 #include <nuttx/fs/nxffs.h>
 #include <nuttx/i2c/i2c_master.h>
 #include <nuttx/video/fb.h>
@@ -91,11 +90,44 @@
  * Pre-processor Definitions
  ****************************************************************************/
 
+#define ARRAY_SIZE(x)   (sizeof(x) / sizeof((x)[0]))
+
 #define NSECTORS(n) \
   (((n)+CONFIG_SAMV71XULT_ROMFS_ROMDISK_SECTSIZE-1) / \
    CONFIG_SAMV71XULT_ROMFS_ROMDISK_SECTSIZE)
 
 /****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+#if defined(CONFIG_SAMV7_PROGMEM_OTA_PARTITION)
+static struct mtd_partition_s g_mtd_partition_table[] =
+{
+  {
+    .offset  = CONFIG_SAMV7_OTA_PRIMARY_SLOT_OFFSET,
+    .size    = CONFIG_SAMV7_OTA_SLOT_SIZE,
+    .devpath = CONFIG_SAMV7_OTA_PRIMARY_SLOT_DEVPATH
+  },
+  {
+    .offset  = CONFIG_SAMV7_OTA_SECONDARY_SLOT_OFFSET,
+    .size    = CONFIG_SAMV7_OTA_SLOT_SIZE,
+    .devpath = CONFIG_SAMV7_OTA_SECONDARY_SLOT_DEVPATH
+  },
+  {
+    .offset  = CONFIG_SAMV7_OTA_SCRATCH_OFFSET,
+    .size    = CONFIG_SAMV7_OTA_SCRATCH_SIZE,
+    .devpath = CONFIG_SAMV7_OTA_SCRATCH_DEVPATH
+  }
+};
+
+static const size_t g_mtd_partition_table_size =
+    ARRAY_SIZE(g_mtd_partition_table);
+#else
+#  define g_mtd_partition_table         NULL
+#  define g_mtd_partition_table_size    0
+#endif /* CONFIG_SAMV7_PROGMEM_OTA_PARTITION */
+
+/****************************************************************************
  * Private Functions
  ****************************************************************************/
 
@@ -453,7 +485,8 @@ int sam_bringup(void)
 #ifdef HAVE_PROGMEM_CHARDEV
   /* Initialize the SAMV71 FLASH programming memory library */
 
-  ret = board_progmem_init(PROGMEM_MTD_MINOR);
+  ret = board_progmem_init(PROGMEM_MTD_MINOR, g_mtd_partition_table,
+                           g_mtd_partition_table_size);
   if (ret < 0)
     {
       syslog(LOG_ERR, "ERROR: Failed to initialize progmem: %d\n", ret);

Reply via email to