ajuckler commented on code in PR #17376:
URL: https://github.com/apache/nuttx/pull/17376#discussion_r2558968463


##########
drivers/eeprom/spi_xx25xx.c:
##########
@@ -481,6 +484,253 @@ static void ee25xx_writepage(FAR struct ee25xx_dev_s 
*eedev,
   ee25xx_unlock(eedev);
 }
 
+/****************************************************************************
+ * Name: ee25xx_eraseall
+ *
+ * Description:
+ *   Erase all data on the device
+ *
+ * Input Parameters:
+ *   eedev - Device structure
+ *
+ ****************************************************************************/
+
+static int ee25xx_eraseall(FAR struct ee25xx_dev_s *eedev)
+{
+  DEBUGASSERT(eedev);
+  DEBUGASSERT(eedev->pgsize > 0);
+
+  if (eedev->readonly)
+    {
+      return -EACCES;
+    }
+
+  int ret = OK;
+
+  /* Devices with different page and sector sizes support a dedicated command
+   * for chip erasure
+   */
+
+  if (eedev->pgsize != eedev->secsize)
+    {
+      ret = nxmutex_lock(&eedev->lock);
+      if (ret < 0)
+        {
+          return ret;
+        }
+
+      ee25xx_writeenable(eedev, true);
+
+      ee25xx_lock(eedev);
+      SPI_SELECT(eedev->spi, SPIDEV_EEPROM(eedev->devid), true);
+
+      SPI_SEND(eedev->spi, EEP25XX_CMD_CE);
+
+      SPI_SELECT(eedev->spi, SPIDEV_EEPROM(eedev->devid), false);
+      ee25xx_unlock(eedev);
+
+      ee25xx_waitwritecomplete(eedev);
+
+      nxmutex_unlock(&eedev->lock);
+    }
+
+  /* If there is no dedicated command for erasure, write the entire memory to
+   * its default state
+   */
+
+  else
+    {
+      uint8_t *buf    = NULL;
+      off_t    offset = 0;
+
+      buf = kmm_malloc(eedev->pgsize);
+      if (buf == NULL)
+        {
+          ferr("ERROR: Failed to allocate memory for ee25xx eraseall\n");
+          return -ENOMEM;
+        }
+
+      (void)memset(buf, EE25XX_DUMMY, eedev->pgsize);
+
+      ret = nxmutex_lock(&eedev->lock);
+      if (ret < 0)
+        {
+          goto free_buffer;
+        }
+
+      for (offset = 0; offset < eedev->size; offset += eedev->pgsize)
+        {
+          ee25xx_writeenable(eedev, true);
+          ee25xx_writepage(eedev, offset, (char *)buf, eedev->pgsize);
+          ee25xx_waitwritecomplete(eedev);
+        }
+
+      nxmutex_unlock(&eedev->lock);
+
+free_buffer:
+      kmm_free(buf);
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: ee25xx_erasepage
+ *
+ * Description:
+ *   Erase 1 page of data
+ *
+ * Input Parameters:
+ *   eedev - Device structure
+ *   index - Index of the page to erase
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+static int ee25xx_erasepage(FAR struct ee25xx_dev_s *eedev,
+                            unsigned long index)
+{
+  DEBUGASSERT(eedev);
+  DEBUGASSERT(eedev->pgsize > 0);
+
+  if (eedev->readonly)
+    {
+      return -EACCES;
+    }
+
+  if (index >= (eedev->size / eedev->pgsize))
+    {
+      return -EFBIG;
+    }
+
+  int ret = OK;
+
+  /* Devices with different page and sector sizes support a dedicated command
+   * for page erasure
+   */
+
+  if (eedev->pgsize != eedev->secsize)
+    {
+      ret = nxmutex_lock(&eedev->lock);
+      if (ret < 0)
+        {
+          return ret;
+        }
+
+      ee25xx_writeenable(eedev, true);
+
+      ee25xx_lock(eedev);
+      SPI_SELECT(eedev->spi, SPIDEV_EEPROM(eedev->devid), true);
+
+      ee25xx_sendcmd(eedev->spi, EEP25XX_CMD_PE, eedev->addrlen,
+                     index * eedev->pgsize);
+
+      SPI_SELECT(eedev->spi, SPIDEV_EEPROM(eedev->devid), false);
+      ee25xx_unlock(eedev);
+
+      ee25xx_waitwritecomplete(eedev);
+
+      nxmutex_unlock(&eedev->lock);
+    }
+
+  /* If there is no dedicated command for erasure, write the page to its
+   * default state
+   */
+
+  else
+    {
+      uint8_t *buf = kmm_malloc(eedev->pgsize);
+
+      if (buf == NULL)
+        {
+          ferr("ERROR: Failed to allocate memory for ee25xx_erasepage\n");
+          return -ENOMEM;
+        }
+
+      (void)memset(buf, EE25XX_DUMMY, eedev->pgsize);
+
+      ret = nxmutex_lock(&eedev->lock);
+      if (ret < 0)
+        {
+          goto free_buffer;
+        }
+
+      ee25xx_writeenable(eedev, true);
+      ee25xx_writepage(eedev, index * eedev->pgsize, (char *)buf,
+                       eedev->pgsize);
+      ee25xx_waitwritecomplete(eedev);
+
+      nxmutex_unlock(&eedev->lock);
+
+free_buffer:
+      kmm_free(buf);
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: ee25xx_erasesector
+ *
+ * Description:
+ *   Erase 1 sector of data
+ *
+ * Input Parameters:
+ *   eedev - Device structure
+ *   index - Index of the sector to erase
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+static int ee25xx_erasesector(FAR struct ee25xx_dev_s *eedev,
+                              unsigned long index)
+{
+  DEBUGASSERT(eedev);
+  DEBUGASSERT(eedev->secsize > 0);
+
+  if (eedev->pgsize == eedev->secsize)
+    {
+      return ee25xx_erasepage(eedev, index);
+    }
+
+  if (eedev->readonly)
+    {
+      return -EACCES;
+    }
+
+  if (index >= (eedev->size / eedev->secsize))
+    {
+      return -EFBIG;
+    }
+
+  const int ret = nxmutex_lock(&eedev->lock);

Review Comment:
   Fixed, and all the other similar issues



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to