You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2022/08/10 15:50:16 UTC

[GitHub] [incubator-nuttx] pkarashchenko commented on a diff in pull request #6829: drivers/mtd:init commit of power-loss resilient cfg

pkarashchenko commented on code in PR #6829:
URL: https://github.com/apache/incubator-nuttx/pull/6829#discussion_r942601486


##########
drivers/mtd/mtd_nvs.c:
##########
@@ -0,0 +1,2241 @@
+/****************************************************************************
+ * drivers/mtd/mtd_nvs.c
+ *
+ * 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.
+ *
+ * NVS: non volatile storage in flash
+ *
+ * Copyright (c) 2018 Laczen
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <crc8.h>
+#include <debug.h>
+#include <errno.h>
+#include <sys/poll.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/mtd/mtd.h>
+#include <nuttx/mtd/configdata.h>
+#include <nuttx/mtd/nvs_priv.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define CONFIG_MTD_NVS_WRITE_BLOCK_SIZE 4
+#define NVS_WRITE_BLOCK_SIZE            CONFIG_MTD_NVS_WRITE_BLOCK_SIZE
+#define MIN(a, b)                       ((a) > (b) ? (b) : (a))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* MTD NVS opeation api */
+
+static int     mtdnvs_open(FAR struct file *filep);
+static int     mtdnvs_close(FAR struct file *filep);
+static ssize_t mtdnvs_read(FAR struct file *filep, FAR char *buffer,
+                           size_t buflen);
+static int     mtdnvs_ioctl(FAR struct file *filep, int cmd,
+                            unsigned long arg);
+static int     mtdnvs_poll(FAR struct file *filep, struct pollfd *fds,
+                           bool setup);
+
+/* Nvs external api, called in mtdnvs_ioctl() and mtdnvs_register() */
+
+/* Nvs internal api */
+
+/* Nvs flash operation api */
+
+/* Basic flash operation api */
+
+static ssize_t flash_read(struct mtd_dev_s *mtd, off_t offset,
+                          void *data, size_t len);
+static ssize_t flash_write(struct mtd_dev_s *mtd, off_t offset,
+                           const uint8_t *data, size_t len);
+static int flash_erase(struct mtd_dev_s *mtd, off_t offset, size_t len);

Review Comment:
   ```suggestion
   static ssize_t flash_read(FAR struct mtd_dev_s *mtd, off_t offset,
                             FAR void *data, size_t len);
   static ssize_t flash_write(FAR struct mtd_dev_s *mtd, off_t offset,
                              FAR const uint8_t *data, size_t len);
   static int flash_erase(FAR struct mtd_dev_s *mtd, off_t offset, size_t len);
   ```



##########
drivers/mtd/mtd_nvs.c:
##########
@@ -0,0 +1,2241 @@
+/****************************************************************************
+ * drivers/mtd/mtd_nvs.c
+ *
+ * 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.
+ *
+ * NVS: non volatile storage in flash
+ *
+ * Copyright (c) 2018 Laczen
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <crc8.h>
+#include <debug.h>
+#include <errno.h>
+#include <sys/poll.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/mtd/mtd.h>
+#include <nuttx/mtd/configdata.h>
+#include <nuttx/mtd/nvs_priv.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define CONFIG_MTD_NVS_WRITE_BLOCK_SIZE 4
+#define NVS_WRITE_BLOCK_SIZE            CONFIG_MTD_NVS_WRITE_BLOCK_SIZE
+#define MIN(a, b)                       ((a) > (b) ? (b) : (a))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* MTD NVS opeation api */
+
+static int     mtdnvs_open(FAR struct file *filep);
+static int     mtdnvs_close(FAR struct file *filep);
+static ssize_t mtdnvs_read(FAR struct file *filep, FAR char *buffer,
+                           size_t buflen);
+static int     mtdnvs_ioctl(FAR struct file *filep, int cmd,
+                            unsigned long arg);
+static int     mtdnvs_poll(FAR struct file *filep, struct pollfd *fds,
+                           bool setup);
+
+/* Nvs external api, called in mtdnvs_ioctl() and mtdnvs_register() */
+
+/* Nvs internal api */
+
+/* Nvs flash operation api */
+
+/* Basic flash operation api */
+
+static ssize_t flash_read(struct mtd_dev_s *mtd, off_t offset,
+                          void *data, size_t len);
+static ssize_t flash_write(struct mtd_dev_s *mtd, off_t offset,
+                           const uint8_t *data, size_t len);
+static int flash_erase(struct mtd_dev_s *mtd, off_t offset, size_t len);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct file_operations mtdnvs_fops =
+{
+  mtdnvs_open,  /* open */
+  mtdnvs_close, /* close */
+  mtdnvs_read,  /* read */
+  NULL,         /* write */
+  NULL,         /* seek */
+  mtdnvs_ioctl, /* ioctl */
+  mtdnvs_poll   /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  , NULL        /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+uint32_t fnv_32_str(const char *str)

Review Comment:
   ```suggestion
   uint32_t fnv_32_str(FAR const char *str)
   ```



##########
drivers/mtd/mtd_nvs.c:
##########
@@ -0,0 +1,2241 @@
+/****************************************************************************
+ * drivers/mtd/mtd_nvs.c
+ *
+ * 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.
+ *
+ * NVS: non volatile storage in flash
+ *
+ * Copyright (c) 2018 Laczen
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <crc8.h>
+#include <debug.h>
+#include <errno.h>
+#include <sys/poll.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/mtd/mtd.h>
+#include <nuttx/mtd/configdata.h>
+#include <nuttx/mtd/nvs_priv.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define CONFIG_MTD_NVS_WRITE_BLOCK_SIZE 4
+#define NVS_WRITE_BLOCK_SIZE            CONFIG_MTD_NVS_WRITE_BLOCK_SIZE
+#define MIN(a, b)                       ((a) > (b) ? (b) : (a))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* MTD NVS opeation api */
+
+static int     mtdnvs_open(FAR struct file *filep);
+static int     mtdnvs_close(FAR struct file *filep);
+static ssize_t mtdnvs_read(FAR struct file *filep, FAR char *buffer,
+                           size_t buflen);
+static int     mtdnvs_ioctl(FAR struct file *filep, int cmd,
+                            unsigned long arg);
+static int     mtdnvs_poll(FAR struct file *filep, struct pollfd *fds,
+                           bool setup);
+
+/* Nvs external api, called in mtdnvs_ioctl() and mtdnvs_register() */
+
+/* Nvs internal api */
+
+/* Nvs flash operation api */
+
+/* Basic flash operation api */
+
+static ssize_t flash_read(struct mtd_dev_s *mtd, off_t offset,
+                          void *data, size_t len);
+static ssize_t flash_write(struct mtd_dev_s *mtd, off_t offset,
+                           const uint8_t *data, size_t len);
+static int flash_erase(struct mtd_dev_s *mtd, off_t offset, size_t len);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct file_operations mtdnvs_fops =
+{
+  mtdnvs_open,  /* open */
+  mtdnvs_close, /* close */
+  mtdnvs_read,  /* read */
+  NULL,         /* write */
+  NULL,         /* seek */
+  mtdnvs_ioctl, /* ioctl */
+  mtdnvs_poll   /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  , NULL        /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+uint32_t fnv_32_str(const char *str)
+{
+  unsigned char *s = (unsigned char *)str;
+  uint32_t hval = 2166136261;
+
+  /* FNV-1 hash each octet in the buffer */
+
+  while (*s)
+    {
+      /* multiply by the 32 bit FNV magic prime mod 2^32 */
+
+      hval *= 0x01000193;
+
+      /* xor the bottom with the current octet */
+
+      hval ^= (uint32_t)*s++;
+    }
+
+  return hval;
+}
+
+#ifdef CONFIG_MTD_NVS_LOOKUP_CACHE
+
+static inline size_t nvs_lookup_cache_pos(uint32_t id)
+{
+  size_t pos;
+
+#if CONFIG_NVS_LOOKUP_CACHE_SIZE <= UINT8_MAX
+  /* CRC8-CCITT is used for ATE checksums and it also acts well as a hash
+   * function, so it can be a good choice from the code size perspective.
+   * However, other hash functions can be used as well if proved better
+   * performance.
+   */
+
+  pos = crc8_ccitt(CRC8_CCITT_INITIAL_VALUE, &id, sizeof(id));
+#else
+  pos = crc16_ccitt(0xffff, (const uint8_t *)&id, sizeof(id));
+#endif
+
+  return pos % CONFIG_NVS_LOOKUP_CACHE_SIZE;
+}
+
+static int nvs_lookup_cache_rebuild(struct nvs_fs *fs)

Review Comment:
   ```suggestion
   static int nvs_lookup_cache_rebuild(FAR struct nvs_fs *fs)
   ```



##########
drivers/mtd/mtd_nvs.c:
##########
@@ -0,0 +1,2241 @@
+/****************************************************************************
+ * drivers/mtd/mtd_nvs.c
+ *
+ * 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.
+ *
+ * NVS: non volatile storage in flash
+ *
+ * Copyright (c) 2018 Laczen
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <crc8.h>
+#include <debug.h>
+#include <errno.h>
+#include <sys/poll.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/mtd/mtd.h>
+#include <nuttx/mtd/configdata.h>
+#include <nuttx/mtd/nvs_priv.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define CONFIG_MTD_NVS_WRITE_BLOCK_SIZE 4
+#define NVS_WRITE_BLOCK_SIZE            CONFIG_MTD_NVS_WRITE_BLOCK_SIZE
+#define MIN(a, b)                       ((a) > (b) ? (b) : (a))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* MTD NVS opeation api */
+
+static int     mtdnvs_open(FAR struct file *filep);
+static int     mtdnvs_close(FAR struct file *filep);
+static ssize_t mtdnvs_read(FAR struct file *filep, FAR char *buffer,
+                           size_t buflen);
+static int     mtdnvs_ioctl(FAR struct file *filep, int cmd,
+                            unsigned long arg);
+static int     mtdnvs_poll(FAR struct file *filep, struct pollfd *fds,
+                           bool setup);
+
+/* Nvs external api, called in mtdnvs_ioctl() and mtdnvs_register() */
+
+/* Nvs internal api */
+
+/* Nvs flash operation api */
+
+/* Basic flash operation api */
+
+static ssize_t flash_read(struct mtd_dev_s *mtd, off_t offset,
+                          void *data, size_t len);
+static ssize_t flash_write(struct mtd_dev_s *mtd, off_t offset,
+                           const uint8_t *data, size_t len);
+static int flash_erase(struct mtd_dev_s *mtd, off_t offset, size_t len);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct file_operations mtdnvs_fops =
+{
+  mtdnvs_open,  /* open */
+  mtdnvs_close, /* close */
+  mtdnvs_read,  /* read */
+  NULL,         /* write */
+  NULL,         /* seek */
+  mtdnvs_ioctl, /* ioctl */
+  mtdnvs_poll   /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  , NULL        /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+uint32_t fnv_32_str(const char *str)
+{
+  unsigned char *s = (unsigned char *)str;
+  uint32_t hval = 2166136261;
+
+  /* FNV-1 hash each octet in the buffer */
+
+  while (*s)
+    {
+      /* multiply by the 32 bit FNV magic prime mod 2^32 */
+
+      hval *= 0x01000193;
+
+      /* xor the bottom with the current octet */
+
+      hval ^= (uint32_t)*s++;
+    }
+
+  return hval;
+}
+
+#ifdef CONFIG_MTD_NVS_LOOKUP_CACHE
+
+static inline size_t nvs_lookup_cache_pos(uint32_t id)
+{
+  size_t pos;
+
+#if CONFIG_NVS_LOOKUP_CACHE_SIZE <= UINT8_MAX
+  /* CRC8-CCITT is used for ATE checksums and it also acts well as a hash
+   * function, so it can be a good choice from the code size perspective.
+   * However, other hash functions can be used as well if proved better
+   * performance.
+   */
+
+  pos = crc8_ccitt(CRC8_CCITT_INITIAL_VALUE, &id, sizeof(id));
+#else
+  pos = crc16_ccitt(0xffff, (const uint8_t *)&id, sizeof(id));
+#endif
+
+  return pos % CONFIG_NVS_LOOKUP_CACHE_SIZE;
+}
+
+static int nvs_lookup_cache_rebuild(struct nvs_fs *fs)
+{
+  int rc;
+  uint32_t addr;
+  uint32_t ate_addr;
+  uint32_t *cache_entry;

Review Comment:
   ```suggestion
     FAR uint32_t *cache_entry;
   ```



##########
drivers/mtd/mtd_nvs.c:
##########
@@ -0,0 +1,2241 @@
+/****************************************************************************
+ * drivers/mtd/mtd_nvs.c
+ *
+ * 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.
+ *
+ * NVS: non volatile storage in flash
+ *
+ * Copyright (c) 2018 Laczen
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <crc8.h>
+#include <debug.h>
+#include <errno.h>
+#include <sys/poll.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/mtd/mtd.h>
+#include <nuttx/mtd/configdata.h>
+#include <nuttx/mtd/nvs_priv.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define CONFIG_MTD_NVS_WRITE_BLOCK_SIZE 4
+#define NVS_WRITE_BLOCK_SIZE            CONFIG_MTD_NVS_WRITE_BLOCK_SIZE
+#define MIN(a, b)                       ((a) > (b) ? (b) : (a))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* MTD NVS opeation api */
+
+static int     mtdnvs_open(FAR struct file *filep);
+static int     mtdnvs_close(FAR struct file *filep);
+static ssize_t mtdnvs_read(FAR struct file *filep, FAR char *buffer,
+                           size_t buflen);
+static int     mtdnvs_ioctl(FAR struct file *filep, int cmd,
+                            unsigned long arg);
+static int     mtdnvs_poll(FAR struct file *filep, struct pollfd *fds,
+                           bool setup);
+
+/* Nvs external api, called in mtdnvs_ioctl() and mtdnvs_register() */
+
+/* Nvs internal api */
+
+/* Nvs flash operation api */
+
+/* Basic flash operation api */
+
+static ssize_t flash_read(struct mtd_dev_s *mtd, off_t offset,
+                          void *data, size_t len);
+static ssize_t flash_write(struct mtd_dev_s *mtd, off_t offset,
+                           const uint8_t *data, size_t len);
+static int flash_erase(struct mtd_dev_s *mtd, off_t offset, size_t len);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct file_operations mtdnvs_fops =
+{
+  mtdnvs_open,  /* open */
+  mtdnvs_close, /* close */
+  mtdnvs_read,  /* read */
+  NULL,         /* write */
+  NULL,         /* seek */
+  mtdnvs_ioctl, /* ioctl */
+  mtdnvs_poll   /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  , NULL        /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+uint32_t fnv_32_str(const char *str)
+{
+  unsigned char *s = (unsigned char *)str;
+  uint32_t hval = 2166136261;
+
+  /* FNV-1 hash each octet in the buffer */
+
+  while (*s)

Review Comment:
   Optional
   ```suggestion
     while (*s != 0)
   ```



##########
include/nuttx/mtd/nvs_priv.h:
##########
@@ -0,0 +1,103 @@
+/****************************************************************************
+ * include/nuttx/mtd/nvs_priv.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_MTD_NVS_PRIV_H_
+#define __INCLUDE_NUTTX_MTD_NVS_PRIV_H_
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+#include <assert.h>
+#include <nuttx/compiler.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+/* MASKS AND SHIFT FOR ADDRESSES
+ * an address in nvs is an uint32_t where:
+ *   high 2 bytes represent the sector number
+ *   low 2 bytes represent the offset in a sector
+ */
+#define ADDR_SECT_MASK           0xFFFF0000
+#define ADDR_SECT_SHIFT          16
+#define ADDR_OFFS_MASK           0x0000FFFF
+
+/* Status return values */
+
+#define NVS_STATUS_NOSPACE       1
+
+#define NVS_BLOCK_SIZE           32
+
+/**
+ * @brief Non-volatile Storage File system structure
+ *
+ * @param offset File system offset in flash
+ * @param ate_wra: Allocation table entry write address. Addresses are stored
+ * as uint32_t: high 2 bytes are sector, low 2 bytes are offset in sector,
+ * @param data_wra: Data write address.
+ * @param sector_size File system is divided into sectors each sector
+ * should be multiple of pagesize
+ * @param sector_count Amount of sectors in the file systems
+ * @param write_block_size Alignment size
+ * @param nvs_lock Mutex
+ * @param flash_device Flash Device
+ */
+
+struct nvs_fs
+{
+  struct   mtd_dev_s *mtd;   /* mtd device */
+  off_t    offset;           /* filesystem offset in flash */
+  uint32_t ate_wra;          /* next alloc table entry write address */
+  uint32_t data_wra;         /* next data write address */
+  uint16_t sector_size;      /* filesystem is divided into sectors,
+                              * sector size should be multiple of pagesize
+                              */
+  uint16_t page_size;        /* page size */
+  uint16_t sector_count;     /* amount of sectors in the filesystem */
+  bool     ready;            /* is the filesystem initialized ? */
+
+  uint32_t step_addr;        /* for traverse */
+  sem_t    nvs_lock;
+};
+
+/* Allocation Table Entry */
+
+begin_packed_struct struct nvs_ate
+{
+  uint32_t id;         /* data id */
+  uint16_t offset;     /* data offset within sector */
+  uint16_t len;        /* data len within sector */
+  uint16_t key_len;    /* key string len */
+  uint8_t  part;       /* part of a multipart data - future extension */
+  uint8_t  crc8;       /* crc8 check of the ate entry */
+  uint8_t  expired;    /* 0xFF-newest entry, others-old entry */
+  uint8_t  reserved;   /* for future extension */
+  uint16_t reserved2;  /* for future extension */
+} end_packed_struct;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __INCLUDE_NUTTX_MTD_NVS_PRIV_H_ */

Review Comment:
   ```suggestion
   #endif /* __INCLUDE_NUTTX_MTD_NVS_PRIV_H */
   ```



##########
drivers/mtd/mtd_nvs.c:
##########
@@ -0,0 +1,2241 @@
+/****************************************************************************
+ * drivers/mtd/mtd_nvs.c
+ *
+ * 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.
+ *
+ * NVS: non volatile storage in flash
+ *
+ * Copyright (c) 2018 Laczen
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <crc8.h>
+#include <debug.h>
+#include <errno.h>
+#include <sys/poll.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/mtd/mtd.h>
+#include <nuttx/mtd/configdata.h>
+#include <nuttx/mtd/nvs_priv.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define CONFIG_MTD_NVS_WRITE_BLOCK_SIZE 4
+#define NVS_WRITE_BLOCK_SIZE            CONFIG_MTD_NVS_WRITE_BLOCK_SIZE
+#define MIN(a, b)                       ((a) > (b) ? (b) : (a))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* MTD NVS opeation api */
+
+static int     mtdnvs_open(FAR struct file *filep);
+static int     mtdnvs_close(FAR struct file *filep);
+static ssize_t mtdnvs_read(FAR struct file *filep, FAR char *buffer,
+                           size_t buflen);
+static int     mtdnvs_ioctl(FAR struct file *filep, int cmd,
+                            unsigned long arg);
+static int     mtdnvs_poll(FAR struct file *filep, struct pollfd *fds,
+                           bool setup);
+
+/* Nvs external api, called in mtdnvs_ioctl() and mtdnvs_register() */
+
+/* Nvs internal api */
+
+/* Nvs flash operation api */
+
+/* Basic flash operation api */

Review Comment:
   Why do we need this comments? I mean especially some that are not followed by any APIs



##########
drivers/mtd/mtd_nvs.c:
##########
@@ -0,0 +1,2241 @@
+/****************************************************************************
+ * drivers/mtd/mtd_nvs.c
+ *
+ * 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.
+ *
+ * NVS: non volatile storage in flash
+ *
+ * Copyright (c) 2018 Laczen
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <crc8.h>
+#include <debug.h>
+#include <errno.h>
+#include <sys/poll.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/mtd/mtd.h>
+#include <nuttx/mtd/configdata.h>
+#include <nuttx/mtd/nvs_priv.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define CONFIG_MTD_NVS_WRITE_BLOCK_SIZE 4
+#define NVS_WRITE_BLOCK_SIZE            CONFIG_MTD_NVS_WRITE_BLOCK_SIZE
+#define MIN(a, b)                       ((a) > (b) ? (b) : (a))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* MTD NVS opeation api */
+
+static int     mtdnvs_open(FAR struct file *filep);
+static int     mtdnvs_close(FAR struct file *filep);
+static ssize_t mtdnvs_read(FAR struct file *filep, FAR char *buffer,
+                           size_t buflen);
+static int     mtdnvs_ioctl(FAR struct file *filep, int cmd,
+                            unsigned long arg);
+static int     mtdnvs_poll(FAR struct file *filep, struct pollfd *fds,

Review Comment:
   ```suggestion
   static int     mtdnvs_poll(FAR struct file *filep, FAR struct pollfd *fds,
   ```



##########
drivers/mtd/mtd_nvs.c:
##########
@@ -0,0 +1,2241 @@
+/****************************************************************************
+ * drivers/mtd/mtd_nvs.c
+ *
+ * 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.
+ *
+ * NVS: non volatile storage in flash
+ *
+ * Copyright (c) 2018 Laczen
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <crc8.h>
+#include <debug.h>
+#include <errno.h>
+#include <sys/poll.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/mtd/mtd.h>
+#include <nuttx/mtd/configdata.h>
+#include <nuttx/mtd/nvs_priv.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define CONFIG_MTD_NVS_WRITE_BLOCK_SIZE 4
+#define NVS_WRITE_BLOCK_SIZE            CONFIG_MTD_NVS_WRITE_BLOCK_SIZE
+#define MIN(a, b)                       ((a) > (b) ? (b) : (a))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* MTD NVS opeation api */
+
+static int     mtdnvs_open(FAR struct file *filep);
+static int     mtdnvs_close(FAR struct file *filep);
+static ssize_t mtdnvs_read(FAR struct file *filep, FAR char *buffer,
+                           size_t buflen);
+static int     mtdnvs_ioctl(FAR struct file *filep, int cmd,
+                            unsigned long arg);
+static int     mtdnvs_poll(FAR struct file *filep, struct pollfd *fds,
+                           bool setup);
+
+/* Nvs external api, called in mtdnvs_ioctl() and mtdnvs_register() */
+
+/* Nvs internal api */
+
+/* Nvs flash operation api */
+
+/* Basic flash operation api */
+
+static ssize_t flash_read(struct mtd_dev_s *mtd, off_t offset,
+                          void *data, size_t len);
+static ssize_t flash_write(struct mtd_dev_s *mtd, off_t offset,
+                           const uint8_t *data, size_t len);
+static int flash_erase(struct mtd_dev_s *mtd, off_t offset, size_t len);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct file_operations mtdnvs_fops =
+{
+  mtdnvs_open,  /* open */
+  mtdnvs_close, /* close */
+  mtdnvs_read,  /* read */
+  NULL,         /* write */
+  NULL,         /* seek */
+  mtdnvs_ioctl, /* ioctl */
+  mtdnvs_poll   /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  , NULL        /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+uint32_t fnv_32_str(const char *str)
+{
+  unsigned char *s = (unsigned char *)str;

Review Comment:
   ```suggestion
     FAR unsigned char *s = (FAR unsigned char *)str;
   ```



##########
drivers/mtd/mtd_nvs.c:
##########
@@ -0,0 +1,2241 @@
+/****************************************************************************
+ * drivers/mtd/mtd_nvs.c
+ *
+ * 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.
+ *
+ * NVS: non volatile storage in flash
+ *
+ * Copyright (c) 2018 Laczen
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <crc8.h>
+#include <debug.h>
+#include <errno.h>
+#include <sys/poll.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/mtd/mtd.h>
+#include <nuttx/mtd/configdata.h>
+#include <nuttx/mtd/nvs_priv.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define CONFIG_MTD_NVS_WRITE_BLOCK_SIZE 4
+#define NVS_WRITE_BLOCK_SIZE            CONFIG_MTD_NVS_WRITE_BLOCK_SIZE
+#define MIN(a, b)                       ((a) > (b) ? (b) : (a))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* MTD NVS opeation api */
+
+static int     mtdnvs_open(FAR struct file *filep);
+static int     mtdnvs_close(FAR struct file *filep);
+static ssize_t mtdnvs_read(FAR struct file *filep, FAR char *buffer,
+                           size_t buflen);
+static int     mtdnvs_ioctl(FAR struct file *filep, int cmd,
+                            unsigned long arg);
+static int     mtdnvs_poll(FAR struct file *filep, struct pollfd *fds,
+                           bool setup);
+
+/* Nvs external api, called in mtdnvs_ioctl() and mtdnvs_register() */
+
+/* Nvs internal api */
+
+/* Nvs flash operation api */
+
+/* Basic flash operation api */
+
+static ssize_t flash_read(struct mtd_dev_s *mtd, off_t offset,
+                          void *data, size_t len);
+static ssize_t flash_write(struct mtd_dev_s *mtd, off_t offset,
+                           const uint8_t *data, size_t len);
+static int flash_erase(struct mtd_dev_s *mtd, off_t offset, size_t len);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct file_operations mtdnvs_fops =
+{
+  mtdnvs_open,  /* open */
+  mtdnvs_close, /* close */
+  mtdnvs_read,  /* read */
+  NULL,         /* write */
+  NULL,         /* seek */
+  mtdnvs_ioctl, /* ioctl */
+  mtdnvs_poll   /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  , NULL        /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+uint32_t fnv_32_str(const char *str)
+{
+  unsigned char *s = (unsigned char *)str;
+  uint32_t hval = 2166136261;
+
+  /* FNV-1 hash each octet in the buffer */
+
+  while (*s)
+    {
+      /* multiply by the 32 bit FNV magic prime mod 2^32 */
+
+      hval *= 0x01000193;
+
+      /* xor the bottom with the current octet */
+
+      hval ^= (uint32_t)*s++;
+    }
+
+  return hval;
+}
+
+#ifdef CONFIG_MTD_NVS_LOOKUP_CACHE
+
+static inline size_t nvs_lookup_cache_pos(uint32_t id)
+{
+  size_t pos;
+
+#if CONFIG_NVS_LOOKUP_CACHE_SIZE <= UINT8_MAX
+  /* CRC8-CCITT is used for ATE checksums and it also acts well as a hash
+   * function, so it can be a good choice from the code size perspective.
+   * However, other hash functions can be used as well if proved better
+   * performance.
+   */
+
+  pos = crc8_ccitt(CRC8_CCITT_INITIAL_VALUE, &id, sizeof(id));
+#else
+  pos = crc16_ccitt(0xffff, (const uint8_t *)&id, sizeof(id));
+#endif
+
+  return pos % CONFIG_NVS_LOOKUP_CACHE_SIZE;
+}
+
+static int nvs_lookup_cache_rebuild(struct nvs_fs *fs)
+{
+  int rc;
+  uint32_t addr;
+  uint32_t ate_addr;
+  uint32_t *cache_entry;
+  struct nvs_ate ate;
+
+  memset(fs->lookup_cache, 0xff, sizeof(fs->lookup_cache));

Review Comment:
   should we have `0xff` to be a define or erased value?



##########
include/nuttx/mtd/nvs_priv.h:
##########
@@ -0,0 +1,103 @@
+/****************************************************************************
+ * include/nuttx/mtd/nvs_priv.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_MTD_NVS_PRIV_H_
+#define __INCLUDE_NUTTX_MTD_NVS_PRIV_H_

Review Comment:
   ```suggestion
   #ifndef __INCLUDE_NUTTX_MTD_NVS_PRIV_H
   #define __INCLUDE_NUTTX_MTD_NVS_PRIV_H
   ```



##########
drivers/mtd/mtd_nvs.c:
##########
@@ -0,0 +1,2241 @@
+/****************************************************************************
+ * drivers/mtd/mtd_nvs.c
+ *
+ * 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.
+ *
+ * NVS: non volatile storage in flash
+ *
+ * Copyright (c) 2018 Laczen
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <crc8.h>
+#include <debug.h>
+#include <errno.h>
+#include <sys/poll.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/mtd/mtd.h>
+#include <nuttx/mtd/configdata.h>
+#include <nuttx/mtd/nvs_priv.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define CONFIG_MTD_NVS_WRITE_BLOCK_SIZE 4
+#define NVS_WRITE_BLOCK_SIZE            CONFIG_MTD_NVS_WRITE_BLOCK_SIZE
+#define MIN(a, b)                       ((a) > (b) ? (b) : (a))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* MTD NVS opeation api */
+
+static int     mtdnvs_open(FAR struct file *filep);
+static int     mtdnvs_close(FAR struct file *filep);
+static ssize_t mtdnvs_read(FAR struct file *filep, FAR char *buffer,
+                           size_t buflen);
+static int     mtdnvs_ioctl(FAR struct file *filep, int cmd,
+                            unsigned long arg);
+static int     mtdnvs_poll(FAR struct file *filep, struct pollfd *fds,
+                           bool setup);
+
+/* Nvs external api, called in mtdnvs_ioctl() and mtdnvs_register() */
+
+/* Nvs internal api */
+
+/* Nvs flash operation api */
+
+/* Basic flash operation api */
+
+static ssize_t flash_read(struct mtd_dev_s *mtd, off_t offset,
+                          void *data, size_t len);
+static ssize_t flash_write(struct mtd_dev_s *mtd, off_t offset,
+                           const uint8_t *data, size_t len);
+static int flash_erase(struct mtd_dev_s *mtd, off_t offset, size_t len);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct file_operations mtdnvs_fops =
+{
+  mtdnvs_open,  /* open */
+  mtdnvs_close, /* close */
+  mtdnvs_read,  /* read */
+  NULL,         /* write */
+  NULL,         /* seek */
+  mtdnvs_ioctl, /* ioctl */
+  mtdnvs_poll   /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  , NULL        /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+uint32_t fnv_32_str(const char *str)
+{
+  unsigned char *s = (unsigned char *)str;
+  uint32_t hval = 2166136261;
+
+  /* FNV-1 hash each octet in the buffer */
+
+  while (*s)
+    {
+      /* multiply by the 32 bit FNV magic prime mod 2^32 */
+
+      hval *= 0x01000193;
+
+      /* xor the bottom with the current octet */
+
+      hval ^= (uint32_t)*s++;
+    }
+
+  return hval;
+}
+
+#ifdef CONFIG_MTD_NVS_LOOKUP_CACHE
+
+static inline size_t nvs_lookup_cache_pos(uint32_t id)
+{
+  size_t pos;
+
+#if CONFIG_NVS_LOOKUP_CACHE_SIZE <= UINT8_MAX
+  /* CRC8-CCITT is used for ATE checksums and it also acts well as a hash
+   * function, so it can be a good choice from the code size perspective.
+   * However, other hash functions can be used as well if proved better
+   * performance.
+   */
+
+  pos = crc8_ccitt(CRC8_CCITT_INITIAL_VALUE, &id, sizeof(id));
+#else
+  pos = crc16_ccitt(0xffff, (const uint8_t *)&id, sizeof(id));
+#endif
+
+  return pos % CONFIG_NVS_LOOKUP_CACHE_SIZE;
+}
+
+static int nvs_lookup_cache_rebuild(struct nvs_fs *fs)
+{
+  int rc;
+  uint32_t addr;
+  uint32_t ate_addr;
+  uint32_t *cache_entry;
+  struct nvs_ate ate;
+
+  memset(fs->lookup_cache, 0xff, sizeof(fs->lookup_cache));
+  addr = fs->ate_wra;
+
+  while (true)
+    {
+      /* Make a copy of 'addr' as it will be advanced by nvs_pref_ate() */
+
+      ate_addr = addr;
+      rc = nvs_prev_ate(fs, &addr, &ate);
+
+      if (rc)
+        {
+          return rc;
+        }
+
+      cache_entry = &fs->lookup_cache[nvs_lookup_cache_pos(ate.id)];
+
+      if (ate.id != 0xffffffff && *cache_entry == NVS_LOOKUP_CACHE_NO_ADDR &&
+          nvs_ate_valid(fs, &ate))
+        {
+          *cache_entry = ate_addr;
+        }
+
+      if (addr == fs->ate_wra)
+        {
+          break;
+        }
+    }
+
+  return 0;
+}
+
+static void nvs_lookup_cache_invalidate(struct nvs_fs *fs, uint32_t sector)

Review Comment:
   ```suggestion
   static void nvs_lookup_cache_invalidate(FAR struct nvs_fs *fs, uint32_t sector)
   ```
   here and other places



##########
include/nuttx/mtd/nvs_priv.h:
##########
@@ -0,0 +1,103 @@
+/****************************************************************************
+ * include/nuttx/mtd/nvs_priv.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_MTD_NVS_PRIV_H_
+#define __INCLUDE_NUTTX_MTD_NVS_PRIV_H_
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+#include <assert.h>
+#include <nuttx/compiler.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+/* MASKS AND SHIFT FOR ADDRESSES
+ * an address in nvs is an uint32_t where:
+ *   high 2 bytes represent the sector number
+ *   low 2 bytes represent the offset in a sector
+ */
+#define ADDR_SECT_MASK           0xFFFF0000
+#define ADDR_SECT_SHIFT          16
+#define ADDR_OFFS_MASK           0x0000FFFF
+
+/* Status return values */
+
+#define NVS_STATUS_NOSPACE       1
+
+#define NVS_BLOCK_SIZE           32
+
+/**
+ * @brief Non-volatile Storage File system structure
+ *
+ * @param offset File system offset in flash
+ * @param ate_wra: Allocation table entry write address. Addresses are stored
+ * as uint32_t: high 2 bytes are sector, low 2 bytes are offset in sector,
+ * @param data_wra: Data write address.
+ * @param sector_size File system is divided into sectors each sector
+ * should be multiple of pagesize
+ * @param sector_count Amount of sectors in the file systems
+ * @param write_block_size Alignment size
+ * @param nvs_lock Mutex
+ * @param flash_device Flash Device
+ */
+
+struct nvs_fs
+{
+  struct   mtd_dev_s *mtd;   /* mtd device */

Review Comment:
   ```suggestion
     FAR struct   mtd_dev_s *mtd;   /* mtd device */
   ```



##########
drivers/mtd/mtd_nvs.c:
##########
@@ -0,0 +1,2241 @@
+/****************************************************************************
+ * drivers/mtd/mtd_nvs.c
+ *
+ * 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.
+ *
+ * NVS: non volatile storage in flash
+ *
+ * Copyright (c) 2018 Laczen
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <crc8.h>
+#include <debug.h>
+#include <errno.h>
+#include <sys/poll.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/mtd/mtd.h>
+#include <nuttx/mtd/configdata.h>
+#include <nuttx/mtd/nvs_priv.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define CONFIG_MTD_NVS_WRITE_BLOCK_SIZE 4
+#define NVS_WRITE_BLOCK_SIZE            CONFIG_MTD_NVS_WRITE_BLOCK_SIZE
+#define MIN(a, b)                       ((a) > (b) ? (b) : (a))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* MTD NVS opeation api */
+
+static int     mtdnvs_open(FAR struct file *filep);
+static int     mtdnvs_close(FAR struct file *filep);
+static ssize_t mtdnvs_read(FAR struct file *filep, FAR char *buffer,
+                           size_t buflen);
+static int     mtdnvs_ioctl(FAR struct file *filep, int cmd,
+                            unsigned long arg);
+static int     mtdnvs_poll(FAR struct file *filep, struct pollfd *fds,
+                           bool setup);
+
+/* Nvs external api, called in mtdnvs_ioctl() and mtdnvs_register() */
+
+/* Nvs internal api */
+
+/* Nvs flash operation api */
+
+/* Basic flash operation api */
+
+static ssize_t flash_read(struct mtd_dev_s *mtd, off_t offset,
+                          void *data, size_t len);
+static ssize_t flash_write(struct mtd_dev_s *mtd, off_t offset,
+                           const uint8_t *data, size_t len);
+static int flash_erase(struct mtd_dev_s *mtd, off_t offset, size_t len);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct file_operations mtdnvs_fops =
+{
+  mtdnvs_open,  /* open */
+  mtdnvs_close, /* close */
+  mtdnvs_read,  /* read */
+  NULL,         /* write */
+  NULL,         /* seek */
+  mtdnvs_ioctl, /* ioctl */
+  mtdnvs_poll   /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  , NULL        /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+uint32_t fnv_32_str(const char *str)
+{
+  unsigned char *s = (unsigned char *)str;
+  uint32_t hval = 2166136261;
+
+  /* FNV-1 hash each octet in the buffer */
+
+  while (*s)
+    {
+      /* multiply by the 32 bit FNV magic prime mod 2^32 */
+
+      hval *= 0x01000193;
+
+      /* xor the bottom with the current octet */
+
+      hval ^= (uint32_t)*s++;
+    }
+
+  return hval;
+}
+
+#ifdef CONFIG_MTD_NVS_LOOKUP_CACHE
+
+static inline size_t nvs_lookup_cache_pos(uint32_t id)
+{
+  size_t pos;
+
+#if CONFIG_NVS_LOOKUP_CACHE_SIZE <= UINT8_MAX
+  /* CRC8-CCITT is used for ATE checksums and it also acts well as a hash
+   * function, so it can be a good choice from the code size perspective.
+   * However, other hash functions can be used as well if proved better
+   * performance.
+   */
+
+  pos = crc8_ccitt(CRC8_CCITT_INITIAL_VALUE, &id, sizeof(id));
+#else
+  pos = crc16_ccitt(0xffff, (const uint8_t *)&id, sizeof(id));

Review Comment:
   ```suggestion
     pos = crc16_ccitt(0xffff, (FAR const uint8_t *)&id, sizeof(id));
   ```



##########
include/nuttx/mtd/nvs_priv.h:
##########
@@ -0,0 +1,103 @@
+/****************************************************************************
+ * include/nuttx/mtd/nvs_priv.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_MTD_NVS_PRIV_H_
+#define __INCLUDE_NUTTX_MTD_NVS_PRIV_H_
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+#include <assert.h>

Review Comment:
   ```suggestion
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org