Update the ADXL345 interface following the current interface scheme

This commit is contained in:
Gregory Nutt 2014-12-16 11:45:28 -06:00
parent c9ca51fc8a
commit d818ab5e35
4 changed files with 65 additions and 54 deletions

View File

@ -98,8 +98,9 @@ struct kl_adxl345config_s
/* Additional private definitions only known to this driver */
ADXL345_HANDLE handle; /* The ADXL345 driver handle */
xcpt_t handler; /* The ADXL345 interrupt handler */
ADXL345_HANDLE handle; /* The ADXL345 driver handle */
adxl345_handler_t handler; /* The ADXL345 interrupt handler */
FAR void *arg; /* Argument to pass to the interrupt handler */
};
/****************************************************************************
@ -115,7 +116,8 @@ struct kl_adxl345config_s
* clear - Acknowledge/clear any pending GPIO interrupt
*/
static int adxl345_attach(FAR struct adxl345_config_s *state, xcpt_t isr);
static int adxl345_attach(FAR struct adxl345_config_s *state,
adxl345_handler_t handler, FAR void *arg);
static void adxl345_enable(FAR struct adxl345_config_s *state, bool enable);
static void adxl345_clear(FAR struct adxl345_config_s *state);
@ -146,12 +148,26 @@ static struct kl_adxl345config_s g_adxl345config =
.enable = adxl345_enable,
.clear = adxl345_clear,
},
.handler = NULL,
};
/****************************************************************************
* Private Functions
****************************************************************************/
/* This is the ADX1345 Interupt handler */
int adx1345_interrupt(int irq, FAR void *context)
{
/* Verify that we have a handler attached */
if (g_adxl345config.handler)
{
/* Yes.. forward with interrupt along with its argument */
g_adxl345config.handler(&g_adxl345config.config, g_adxl345config.arg);
}
return OK;
}
/* IRQ/GPIO access callbacks. These operations all hidden behind
* callbacks to isolate the ADXL345 driver from differences in GPIO
@ -159,19 +175,23 @@ static struct kl_adxl345config_s g_adxl345config =
*
* attach - Attach the ADXL345 interrupt handler to the GPIO interrupt
* enable - Enable or disable the GPIO interrupt
* clear - Acknowledge/clear any pending GPIO interrupt
* clear - Acknowledge/clear any pending GPIO interrupt
*/
static int adxl345_attach(FAR struct adxl345_config_s *state, xcpt_t isr)
static int adxl345_attach(FAR struct adxl345_config_s *state,
adxl345_handler_t handler, FAR void *arg)
{
FAR struct kl_adxl345config_s *priv = (FAR struct kl_adxl345config_s *)state;
ivdbg("Saving handler %p\n", isr);
snvdbg("Saving handler %p\n", isr);
DEBUGASSERT(priv);
/* Just save the handler. We will use it when interrupts are enabled */
/* Just save the handler and its argument. We will use it when interrupts
* are enabled
*/
priv->handler = isr;
priv->handler = handler;
priv->arg = arg;
return OK;
}
@ -191,7 +211,7 @@ static void adxl345_enable(FAR struct adxl345_config_s *state, bool enable)
/* Configure the interrupt using the SAVED handler */
kl_configgpio(GPIO_ADXL345_INT1);
(void)kl_gpioirqattach(GPIO_ADXL345_INT1, priv->handler);
(void)kl_gpioirqattach(GPIO_ADXL345_INT1, adx1345_interrupt);
kl_gpioirqenable(GPIO_ADXL345_INT1);
}
else
@ -237,14 +257,14 @@ int adxl345_archinitialize(int minor)
FAR struct spi_dev_s *dev;
int ret;
idbg("minor %d\n", minor);
sndbg("minor %d\n", minor);
DEBUGASSERT(minor == 0);
/* Check if we are already initialized */
if (!g_adxl345config.handle)
{
ivdbg("Initializing\n");
snvdbg("Initializing\n");
/* Configure the ADXL345 interrupt pin as an input */
@ -255,7 +275,7 @@ int adxl345_archinitialize(int minor)
dev = up_spiinitialize(CONFIG_ADXL345_SPIDEV);
if (!dev)
{
idbg("Failed to initialize SPI bus %d\n", CONFIG_ADXL345_SPIDEV);
sndbg("Failed to initialize SPI bus %d\n", CONFIG_ADXL345_SPIDEV);
return -ENODEV;
}
@ -265,7 +285,7 @@ int adxl345_archinitialize(int minor)
adxl345_instantiate(dev, (FAR struct adxl345_config_s *)&g_adxl345config);
if (!g_adxl345config.handle)
{
idbg("Failed to instantiate the ADXL345 driver\n");
sndbg("Failed to instantiate the ADXL345 driver\n");
return -ENODEV;
}
@ -274,8 +294,8 @@ int adxl345_archinitialize(int minor)
ret = adxl345_register(g_adxl345config.handle, CONFIG_ADXL345_DEVMINOR);
if (ret < 0)
{
idbg("Failed to register STMPE driver: %d\n", ret);
return -ENODEV;
sndbg("Failed to register ADX1345 driver: %d\n", ret);
return ret;
}
}

View File

@ -72,8 +72,6 @@ static ssize_t adxl345_read(FAR struct file *filep, FAR char *buffer,
* Private Data
****************************************************************************/
static struct adxl345_dev_s g_adxl345;
/* This the vtable that supports the character driver interface */
static const struct file_operations g_adxl345fops =
@ -281,18 +279,12 @@ static void adxl345_worker(FAR void *arg)
*
****************************************************************************/
static int adxl345_interrupt(int irq, FAR void *context)
static void adxl345_interrupt(FAR struct adxl345_config_s *config, FAR void *arg)
{
FAR struct adxl345_dev_s *priv;
FAR struct adxl345_config_s *config;
int ret;
FAR struct adxl345_dev_s *priv = (FAR struct adxl345_dev_s *)arg;
int ret;
/* Get a pointer the callbacks for convenience (and so the code is not so
* ugly).
*/
config = priv->config;
DEBUGASSERT(config != NULL);
DEBUGASSERT(priv && priv->config == config);
/* Disable further interrupts */
@ -320,7 +312,6 @@ static int adxl345_interrupt(int irq, FAR void *context)
/* Clear any pending interrupts and return success */
config->clear(config);
return OK;
}
/****************************************************************************
@ -391,19 +382,24 @@ static void adxl345_reset(FAR struct adxl345_dev_s *priv)
#ifdef CONFIG_ADXL345_SPI
ADXL345_HANDLE adxl345_instantiate(FAR struct spi_dev_s *dev,
FAR struct adxl345_config_s *config)
FAR struct adxl345_config_s *config)
#else
ADXL345_HANDLE adxl345_instantiate(FAR struct i2c_dev_s *dev,
FAR struct adxl345_config_s *config)
FAR struct adxl345_config_s *config)
#endif
{
FAR struct adxl345_dev_s *priv;
uint8_t regval;
int ret;
/* Use the one-and-only ADXL345 driver instance */
/* Allocate the ADXL345 driver instance */
priv = &g_adxl345;
priv = (FAR struct adxl345_dev_s *)kmm_zalloc(sizeof(struct adxl345_dev_s));
if (!priv)
{
sndbg("Failed to allocate the device structure!\n");
return NULL;
}
/* Initialize the device state structure */
@ -418,15 +414,16 @@ ADXL345_HANDLE adxl345_instantiate(FAR struct i2c_dev_s *dev,
* then just configure before sending data.
*/
#ifdef CONFIG_SPI_OWNBUS
/* Configure SPI for the ADXL345 */
#ifdef CONFIG_SPI_OWNBUS
/* Configure SPI for the ADXL345 */
SPI_SELECT(priv->spi, SPIDEV_GSENSOR, true);
SPI_SETMODE(priv->spi, SPIDEV_MODE3);
SPI_SETBITS(priv->spi, 8);
SPI_SETFREQUENCY(priv->spi, ADXL345_SPI_MAXFREQUENCY);
SPI_SELECT(priv->spi, SPIDEV_GSENSOR, false);
#endif
SPI_SELECT(priv->spi, SPIDEV_GSENSOR, true);
SPI_SETMODE(priv->spi, SPIDEV_MODE3);
SPI_SETBITS(priv->spi, 8);
SPI_SETFREQUENCY(priv->spi, ADXL345_SPI_MAXFREQUENCY);
SPI_SELECT(priv->spi, SPIDEV_GSENSOR, false);
#endif
#else
priv->i2c = dev;
@ -444,7 +441,7 @@ ADXL345_HANDLE adxl345_instantiate(FAR struct i2c_dev_s *dev,
ret = adxl345_checkid(priv);
if (ret < 0)
{
snlldbg("Wrong Device ID!\n");
sndbg("Wrong Device ID!\n");
return NULL;
}
@ -464,7 +461,7 @@ ADXL345_HANDLE adxl345_instantiate(FAR struct i2c_dev_s *dev,
/* Attach the ADXL345 interrupt handler. */
config->attach(config, adxl345_interrupt);
config->attach(config, (adxl345_handler_t)adxl345_interrupt, (FAR void *)priv);
/* Leave standby mode */
@ -478,6 +475,7 @@ ADXL345_HANDLE adxl345_instantiate(FAR struct i2c_dev_s *dev,
adxl345_putreg8(priv, ADXL345_INT_ENABLE, INT_DATA_READY);
/* Return our private data structure as an opaque handle */
return (ADXL345_HANDLE)priv;
}

View File

@ -241,7 +241,7 @@ struct ajoy_lowerhalf_s
CODE ajoy_buttonset_t (*al_buttons)(FAR const struct ajoy_lowerhalf_s *lower);
/* Enable interrupts on the selected set of joystick buttons. And empty
/* Enable interrupts on the selected set of joystick buttons. An empty
* set will disable all interrupts.
*/

View File

@ -292,7 +292,7 @@
* handler but rather from the context of the worker thread with interrupts enabled.
*/
typedef void (*adxl345_handler_t)(int pin);
typedef void (*adxl345_handler_t)(FAR struct adxl345_config_s *config, FAR void *arg);
/* A reference to a structure of this type must be passed to the ADXL345 driver when the
* driver is instantiated. This structure provides information about the configuration of the
@ -312,14 +312,6 @@ struct adxl345_config_s
#endif
uint32_t frequency; /* I2C or SPI frequency */
/* If multiple ADXL345 devices are supported, then an IRQ number must
* be provided for each so that their interrupts can be distinguished.
*/
#ifdef CONFIG_ADXL345_MULTIPLE
int irq; /* IRQ number received by interrupt handler. */
#endif
/* IRQ/GPIO access callbacks. These operations all hidden behind
* callbacks to isolate the ADXL345 driver from differences in GPIO
* interrupt handling by varying boards and MCUs.
@ -329,7 +321,8 @@ struct adxl345_config_s
* clear - Acknowledge/clear any pending GPIO interrupt
*/
int (*attach)(FAR struct adxl345_config_s *state, xcpt_t isr);
int (*attach)(FAR struct adxl345_config_s *state, adxl345_handler_t handler,
FAR void *arg);
void (*enable)(FAR struct adxl345_config_s *state, bool enable);
void (*clear)(FAR struct adxl345_config_s *state);
};