Support multiple SST26 devices on the same spi bus

This commit is contained in:
Sebastien Lorquet 2021-11-30 17:26:24 +01:00 committed by Abdelatif Guettouche
parent 46c36e2603
commit 437424d234
2 changed files with 30 additions and 26 deletions

View File

@ -203,11 +203,12 @@ struct sst26_dev_s
{ {
struct mtd_dev_s mtd; /* MTD interface */ struct mtd_dev_s mtd; /* MTD interface */
FAR struct spi_dev_s *dev; /* Saved SPI interface instance */ 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 sectorshift;
uint8_t pageshift; uint8_t pageshift;
uint16_t nsectors; bool lastwaswrite;
uint32_t npages;
}; };
/**************************************************************************** /****************************************************************************
@ -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. */ /* Lock the SPI bus, configure the bus, and select this FLASH part. */
sst26_lock(priv->dev); 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 */ /* 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 */ /* 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); sst26_unlock(priv->dev);
sstinfo("manufacturer: %02x memory: %02x capacity: %02x\n", 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 */ /* 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 */ /* Send "Read Status Register (RDSR)" command */
@ -401,7 +402,7 @@ static void sst26_waitwritecomplete(struct sst26_dev_s *priv)
/* Deselect the FLASH */ /* 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, /* Given that writing could take up to few tens of milliseconds,
* and erasing could take more. The following short delay in the * 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 */ /* 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 */ /* Send "Global Unlock (ULBPR)" command */
@ -438,7 +439,7 @@ static void sst26_globalunlock(struct sst26_dev_s *priv)
/* Deselect the FLASH */ /* Deselect the FLASH */
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false); SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), false);
sstinfo("Device unlocked.\n"); sstinfo("Device unlocked.\n");
} }
@ -451,7 +452,7 @@ static void sst26_writeenable(struct sst26_dev_s *priv)
{ {
/* Select this FLASH part */ /* 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 */ /* Send "Write Enable (WREN)" command */
@ -459,7 +460,7 @@ static void sst26_writeenable(struct sst26_dev_s *priv)
/* Deselect the FLASH */ /* Deselect the FLASH */
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false); SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), false);
sstinfo("Enabled\n"); sstinfo("Enabled\n");
} }
@ -472,7 +473,7 @@ static void sst26_writedisable(struct sst26_dev_s *priv)
{ {
/* Select this FLASH part */ /* 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 */ /* Send "Write Disable (WRDI)" command */
@ -480,7 +481,7 @@ static void sst26_writedisable(struct sst26_dev_s *priv)
/* Deselect the FLASH */ /* Deselect the FLASH */
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false); SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), false);
sstinfo("Disabled\n"); sstinfo("Disabled\n");
} }
@ -505,7 +506,7 @@ static void sst26_sectorerase(struct sst26_dev_s *priv,
/* Select this FLASH part */ /* 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 /* Send the "Sector Erase (SE)" or "Block Erase (BE)" instruction
* that was passed in as the erase type. * that was passed in as the erase type.
@ -524,7 +525,7 @@ static void sst26_sectorerase(struct sst26_dev_s *priv,
/* Deselect the FLASH */ /* Deselect the FLASH */
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false); SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), false);
sst26_waitwritecomplete(priv); sst26_waitwritecomplete(priv);
@ -545,7 +546,7 @@ static inline int sst26_chiperase(struct sst26_dev_s *priv)
/* Select this FLASH part */ /* 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 */ /* Send the "Chip Erase (CE)" instruction */
@ -553,7 +554,7 @@ static inline int sst26_chiperase(struct sst26_dev_s *priv)
/* Deselect the FLASH */ /* Deselect the FLASH */
SPI_SELECT(priv->dev, SPIDEV_FLASH(0), false); SPI_SELECT(priv->dev, SPIDEV_FLASH(priv->devid), false);
sst26_waitwritecomplete(priv); sst26_waitwritecomplete(priv);
@ -578,7 +579,7 @@ static inline void sst26_pagewrite(struct sst26_dev_s *priv,
/* Select this FLASH part */ /* 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 */ /* 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 */ /* 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); sst26_waitwritecomplete(priv);
@ -620,7 +621,7 @@ static inline void sst26_bytewrite(struct sst26_dev_s *priv,
/* Select this FLASH part */ /* 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 */ /* 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 */ /* 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); 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 */ /* Lock the SPI bus and select this FLASH part */
sst26_lock(priv->dev); 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 */ /* 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 */ /* 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); sst26_unlock(priv->dev);
sstinfo("return nbytes: %d\n", (int)nbytes); sstinfo("return nbytes: %d\n", (int)nbytes);
return 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; FAR struct sst26_dev_s *priv;
int ret; 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.ioctl = sst26_ioctl;
priv->mtd.name = "sst26"; priv->mtd.name = "sst26";
priv->dev = dev; priv->dev = dev;
priv->devid = spi_devid;
/* Deselect the FLASH */ /* 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 */ /* Identify the FLASH chip and get its capacity */

View File

@ -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 * Name: sst39vf_initialize