You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ag...@apache.org on 2021/11/30 17:46:08 UTC

[incubator-nuttx] branch master updated: Support multiple SST26 devices on the same spi bus

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

aguettouche 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 437424d  Support multiple SST26 devices on the same spi bus
437424d is described below

commit 437424d2349d4a897dbe03b1e1c5f2e4e5ba3a6c
Author: Sebastien Lorquet <se...@lorquet.fr>
AuthorDate: Tue Nov 30 17:26:24 2021 +0100

    Support multiple SST26 devices on the same spi bus
---
 drivers/mtd/sst26.c     | 53 ++++++++++++++++++++++++++-----------------------
 include/nuttx/mtd/mtd.h |  3 ++-
 2 files changed, 30 insertions(+), 26 deletions(-)

diff --git a/drivers/mtd/sst26.c b/drivers/mtd/sst26.c
index f90af04..3be1d80 100644
--- a/drivers/mtd/sst26.c
+++ b/drivers/mtd/sst26.c
@@ -203,11 +203,12 @@ struct sst26_dev_s
 {
   struct mtd_dev_s mtd;      /* MTD interface */
   FAR struct spi_dev_s *dev; /* Saved SPI interface instance */
-  bool     lastwaswrite;
+  uint32_t npages;
+  uint16_t nsectors;
+  uint16_t devid;            /* SPI device ID to manage CS lines in board */
   uint8_t  sectorshift;
   uint8_t  pageshift;
-  uint16_t nsectors;
-  uint32_t npages;
+  bool     lastwaswrite;
 };
 
 /****************************************************************************
@@ -315,7 +316,7 @@ static inline int sst26_readid(struct sst26_dev_s *priv)
   /* Lock the SPI bus, configure the bus, and select this FLASH part. */
 
   sst26_lock(priv->dev);
-  SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
+  SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), true);
 
   /* Send the "Read ID (RDID)" command and read the first three ID bytes */
 
@@ -326,7 +327,7 @@ static inline int sst26_readid(struct sst26_dev_s *priv)
 
   /* De-select the FLASH and unlock the bus */
 
-  SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
+  SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), false);
   sst26_unlock(priv->dev);
 
   sstinfo("manufacturer: %02x memory: %02x capacity: %02x\n",
@@ -387,7 +388,7 @@ static void sst26_waitwritecomplete(struct sst26_dev_s *priv)
     {
       /* Select this FLASH part */
 
-      SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
+      SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), true);
 
       /* Send "Read Status Register (RDSR)" command */
 
@@ -401,7 +402,7 @@ static void sst26_waitwritecomplete(struct sst26_dev_s *priv)
 
       /* Deselect the FLASH */
 
-      SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
+      SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), false);
 
       /* Given that writing could take up to few tens of milliseconds,
        * and erasing could take more.  The following short delay in the
@@ -430,7 +431,7 @@ static void sst26_globalunlock(struct sst26_dev_s *priv)
 {
   /* Select this FLASH part */
 
-  SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
+  SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), true);
 
   /* Send "Global Unlock (ULBPR)" command */
 
@@ -438,7 +439,7 @@ static void sst26_globalunlock(struct sst26_dev_s *priv)
 
   /* Deselect the FLASH */
 
-  SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
+  SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), false);
 
   sstinfo("Device unlocked.\n");
 }
@@ -451,7 +452,7 @@ static void sst26_writeenable(struct sst26_dev_s *priv)
 {
   /* Select this FLASH part */
 
-  SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
+  SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), true);
 
   /* Send "Write Enable (WREN)" command */
 
@@ -459,7 +460,7 @@ static void sst26_writeenable(struct sst26_dev_s *priv)
 
   /* Deselect the FLASH */
 
-  SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
+  SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), false);
 
   sstinfo("Enabled\n");
 }
@@ -472,7 +473,7 @@ static void sst26_writedisable(struct sst26_dev_s *priv)
 {
   /* Select this FLASH part */
 
-  SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
+  SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), true);
 
   /* Send "Write Disable (WRDI)" command */
 
@@ -480,7 +481,7 @@ static void sst26_writedisable(struct sst26_dev_s *priv)
 
   /* Deselect the FLASH */
 
-  SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
+  SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), false);
 
   sstinfo("Disabled\n");
 }
@@ -505,7 +506,7 @@ static void sst26_sectorerase(struct sst26_dev_s *priv,
 
   /* Select this FLASH part */
 
-  SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
+  SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), true);
 
   /* Send the "Sector Erase (SE)" or "Block Erase (BE)" instruction
    * that was passed in as the erase type.
@@ -524,7 +525,7 @@ static void sst26_sectorerase(struct sst26_dev_s *priv,
 
   /* Deselect the FLASH */
 
-  SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
+  SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), false);
 
   sst26_waitwritecomplete(priv);
 
@@ -545,7 +546,7 @@ static inline int sst26_chiperase(struct sst26_dev_s *priv)
 
   /* Select this FLASH part */
 
-  SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
+  SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), true);
 
   /* Send the "Chip Erase (CE)" instruction */
 
@@ -553,7 +554,7 @@ static inline int sst26_chiperase(struct sst26_dev_s *priv)
 
   /* Deselect the FLASH */
 
-  SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
+  SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), false);
 
   sst26_waitwritecomplete(priv);
 
@@ -578,7 +579,7 @@ static inline void sst26_pagewrite(struct sst26_dev_s *priv,
 
   /* Select this FLASH part */
 
-  SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
+  SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), true);
 
   /* Send "Page Program (PP)" command */
 
@@ -596,7 +597,7 @@ static inline void sst26_pagewrite(struct sst26_dev_s *priv,
 
   /* Deselect the FLASH: Chip Select high */
 
-  SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
+  SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), false);
 
   sst26_waitwritecomplete(priv);
 
@@ -620,7 +621,7 @@ static inline void sst26_bytewrite(struct sst26_dev_s *priv,
 
   /* Select this FLASH part */
 
-  SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
+  SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), true);
 
   /* Send "Page Program (PP)" command */
 
@@ -639,7 +640,7 @@ static inline void sst26_bytewrite(struct sst26_dev_s *priv,
 
   /* Deselect the FLASH: Chip Select high */
 
-  SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
+  SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), false);
 
   sst26_waitwritecomplete(priv);
 
@@ -751,7 +752,7 @@ static ssize_t sst26_read(FAR struct mtd_dev_s *dev,
   /* Lock the SPI bus and select this FLASH part */
 
   sst26_lock(priv->dev);
-  SPI_SELECT(priv->dev, SPIDEV_FLASH(0), true);
+  SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), true);
 
   /* Send "Read from Memory " instruction */
 
@@ -773,7 +774,7 @@ static ssize_t sst26_read(FAR struct mtd_dev_s *dev,
 
   /* Deselect the FLASH and unlock the SPI bus */
 
-  SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false);
+  SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), false);
   sst26_unlock(priv->dev);
   sstinfo("return nbytes: %d\n", (int)nbytes);
   return nbytes;
@@ -947,7 +948,8 @@ static int sst26_ioctl(FAR struct mtd_dev_s *dev, int cmd, unsigned long arg)
  *
  ****************************************************************************/
 
-FAR struct mtd_dev_s *sst26_initialize_spi(FAR struct spi_dev_s *dev)
+FAR struct mtd_dev_s *sst26_initialize_spi(FAR struct spi_dev_s *dev,
+                                           uint16_t spi_devid)
 {
   FAR struct sst26_dev_s *priv;
   int ret;
@@ -978,10 +980,11 @@ FAR struct mtd_dev_s *sst26_initialize_spi(FAR struct spi_dev_s *dev)
       priv->mtd.ioctl  = sst26_ioctl;
       priv->mtd.name   = "sst26";
       priv->dev        = dev;
+      priv->devid      = spi_devid;
 
       /* Deselect the FLASH */
 
-      SPI_SELECT(dev, SPIDEV_FLASH(0), false);
+      SPI_SELECT(dev, SPIDEV_FLASH(priv->devid), false);
 
       /* Identify the FLASH chip and get its capacity */
 
diff --git a/include/nuttx/mtd/mtd.h b/include/nuttx/mtd/mtd.h
index 1a54a54..c7b820e 100644
--- a/include/nuttx/mtd/mtd.h
+++ b/include/nuttx/mtd/mtd.h
@@ -493,7 +493,8 @@ FAR struct mtd_dev_s *sst25xx_initialize(FAR struct spi_dev_s *dev);
  *
  ****************************************************************************/
 
-FAR struct mtd_dev_s *sst26_initialize_spi(FAR struct spi_dev_s *dev);
+FAR struct mtd_dev_s *sst26_initialize_spi(FAR struct spi_dev_s *dev,
+                                           uint16_t spi_devid);
 
 /****************************************************************************
  * Name: sst39vf_initialize