From cb0e8454d704be616344b229a030e556c51345bb Mon Sep 17 00:00:00 2001 From: Simon Filgis Date: Sun, 14 Apr 2024 11:07:37 +0200 Subject: [PATCH] enable support for multible devices of mcp48xx Instead of one static dev and priv, allocate a dedicated one each time the init is invoked. --- drivers/analog/mcp48xx.c | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/drivers/analog/mcp48xx.c b/drivers/analog/mcp48xx.c index 10092984d2..19b0174783 100644 --- a/drivers/analog/mcp48xx.c +++ b/drivers/analog/mcp48xx.c @@ -106,8 +106,6 @@ static int mcp48xx_ioctl(FAR struct dac_dev_s *dev, int cmd, * Private Data ****************************************************************************/ -static struct mcp48xx_dev_s g_devpriv; - static const struct dac_ops_s g_dacops = { mcp48xx_reset, /* ao_reset */ @@ -118,12 +116,6 @@ static const struct dac_ops_s g_dacops = mcp48xx_ioctl /* ao_ioctl */ }; -static struct dac_dev_s g_dacdev = -{ - &g_dacops, /* ad_ops */ - &g_devpriv /* ad_priv */ -}; - /**************************************************************************** * Private Functions ****************************************************************************/ @@ -337,15 +329,35 @@ FAR struct dac_dev_s *mcp48xx_initialize(FAR struct spi_dev_s *spi, uint32_t spidev) { FAR struct mcp48xx_dev_s *priv; + FAR struct dac_dev_s *dacdev; int i; /* Sanity check */ DEBUGASSERT(spi != NULL); + /* Initialize the DAC device structure */ + + priv = kmm_malloc(sizeof(struct mcp48xx_dev_s)); + if (priv == NULL) + { + aerr("ERROR: Failed to allocate mcp48xx_dev_s instance\n"); + free(priv); + return NULL; + } + + dacdev = kmm_malloc(sizeof(struct dac_dev_s)); + if (dacdev == NULL) + { + aerr("ERROR: Failed to allocate dac_dev_s instance\n"); + return NULL; + } + + dacdev->ad_ops = &g_dacops; + dacdev->ad_priv = priv; + /* Initialize the MCP48XX device structure */ - priv = (FAR struct mcp48xx_dev_s *)g_dacdev.ad_priv; priv->spi = spi; priv->spidev = spidev; @@ -356,7 +368,7 @@ FAR struct dac_dev_s *mcp48xx_initialize(FAR struct spi_dev_s *spi, priv->cmd[i] = MCP48XX_SHDN | MCP48XX_GA; } - return &g_dacdev; + return dacdev; } #endif /* CONFIG_MCP48XX */