diff --git a/drivers/spi/spi_slave_driver.c b/drivers/spi/spi_slave_driver.c index 880027f179..ccc8f0dd3a 100644 --- a/drivers/spi/spi_slave_driver.c +++ b/drivers/spi/spi_slave_driver.c @@ -195,6 +195,13 @@ static int spi_slave_open(FAR struct file *filep) /* Increment the count of open references on the driver */ + if (priv->crefs == 0) + { + SPIS_CTRLR_BIND(priv->ctrlr, (FAR struct spi_slave_dev_s *)priv, + CONFIG_SPI_SLAVE_DRIVER_MODE, + CONFIG_SPI_SLAVE_DRIVER_WIDTH); + } + priv->crefs++; DEBUGASSERT(priv->crefs > 0); @@ -246,6 +253,11 @@ static int spi_slave_close(FAR struct file *filep) DEBUGASSERT(priv->crefs > 0); priv->crefs--; + if (priv->crefs == 0) + { + SPIS_CTRLR_UNBIND(priv->ctrlr); + } + /* If the count has decremented to zero and the driver has been already * unlinked, then dispose of the driver resources. */ @@ -529,6 +541,7 @@ static int spi_slave_unlink(FAR struct inode *inode) if (priv->crefs <= 0) { + SPIS_CTRLR_UNBIND(priv->ctrlr); nxmutex_destroy(&priv->lock); kmm_free(priv); inode->i_private = NULL; @@ -798,10 +811,6 @@ int spi_slave_register(FAR struct spi_slave_ctrlr_s *ctrlr, int bus) kmm_free(priv); } - SPIS_CTRLR_BIND(priv->ctrlr, (FAR struct spi_slave_dev_s *)priv, - CONFIG_SPI_SLAVE_DRIVER_MODE, - CONFIG_SPI_SLAVE_DRIVER_WIDTH); - spiinfo("SPI Slave driver loaded successfully!\n"); return ret;