2016-08-07 17:43:48 +02:00
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
* drivers/analog/ads1255.c
|
2011-08-19 16:05:05 +02:00
|
|
|
*
|
2016-05-26 19:32:26 +02:00
|
|
|
* Copyright (C) 2010, 2016 Gregory Nutt. All rights reserved.
|
2011-08-19 16:05:05 +02:00
|
|
|
* Copyright (C) 2011 Li Zhuoyi. All rights reserved.
|
|
|
|
* Author: Li Zhuoyi <lzyy.cn@gmail.com>
|
2016-05-26 19:32:26 +02:00
|
|
|
* Gregory Nutt <gnutt@nuttx.org>
|
2011-08-19 16:05:05 +02:00
|
|
|
*
|
|
|
|
* This file is a part of NuttX:
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Gregory Nutt. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2016-08-07 17:43:48 +02:00
|
|
|
****************************************************************************/
|
2011-08-19 16:05:05 +02:00
|
|
|
|
2016-05-26 21:42:35 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-08-19 16:05:05 +02:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <errno.h>
|
2016-05-26 19:32:26 +02:00
|
|
|
#include <assert.h>
|
2011-08-19 16:05:05 +02:00
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/arch.h>
|
2016-05-26 21:42:35 +02:00
|
|
|
#include <nuttx/wqueue.h>
|
2017-10-06 18:15:01 +02:00
|
|
|
#include <nuttx/signal.h>
|
2013-07-01 16:11:54 +02:00
|
|
|
#include <nuttx/spi/spi.h>
|
2011-08-19 16:05:05 +02:00
|
|
|
#include <nuttx/analog/adc.h>
|
|
|
|
|
|
|
|
#if defined(CONFIG_ADC_ADS1255)
|
|
|
|
|
2016-05-26 21:42:35 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-08-19 16:05:05 +02:00
|
|
|
#define ADS125X_BUFON 0x02
|
|
|
|
#define ADS125X_BUFOFF 0x00
|
|
|
|
|
|
|
|
#define ADS125X_PGA1 0x00
|
|
|
|
#define ADS125X_PGA2 0x01
|
|
|
|
#define ADS125X_PGA4 0x02
|
|
|
|
#define ADS125X_PGA8 0x03
|
|
|
|
#define ADS125X_PGA16 0x04
|
|
|
|
#define ADS125X_PGA32 0x05
|
|
|
|
#define ADS125X_PGA64 0x06
|
|
|
|
|
2015-03-02 17:18:25 +01:00
|
|
|
#define ADS125X_RDATA 0x01 /* Read Data */
|
|
|
|
#define ADS125X_RDATAC 0x03 /* Read Data Continuously */
|
|
|
|
#define ADS125X_SDATAC 0x0F /* Stop Read Data Continuously */
|
|
|
|
#define ADS125X_RREG 0x10 /* Read from REG */
|
|
|
|
#define ADS125X_WREG 0x50 /* Write to REG */
|
|
|
|
#define ADS125X_SELFCAL 0xF0 /* Offset and Gain Self-Calibration */
|
|
|
|
#define ADS125X_SELFOCAL 0xF1 /* Offset Self-Calibration */
|
|
|
|
#define ADS125X_SELFGCAL 0xF2 /* Gain Self-Calibration */
|
|
|
|
#define ADS125X_SYSOCAL 0xF3 /* System Offset Calibration */
|
|
|
|
#define ADS125X_SYSGCAL 0xF4 /* System Gain Calibration */
|
|
|
|
#define ADS125X_SYNC 0xFC /* Synchronize the A/D Conversion */
|
|
|
|
#define ADS125X_STANDBY 0xFD /* Begin Standby Mode */
|
|
|
|
#define ADS125X_RESET 0xFE /* Reset to Power-Up Values */
|
|
|
|
#define ADS125X_WAKEUP 0xFF /* Completes SYNC and Exits Standby Mode */
|
2011-08-19 16:05:05 +02:00
|
|
|
|
|
|
|
#ifndef CONFIG_ADS1255_FREQUENCY
|
2015-03-02 17:18:25 +01:00
|
|
|
# define CONFIG_ADS1255_FREQUENCY 1000000
|
2011-08-19 16:05:05 +02:00
|
|
|
#endif
|
|
|
|
#ifndef CONFIG_ADS1255_MUX
|
2015-03-02 17:18:25 +01:00
|
|
|
# define CONFIG_ADS1255_MUX 0x01
|
2011-08-19 16:05:05 +02:00
|
|
|
#endif
|
|
|
|
#ifndef CONFIG_ADS1255_CHMODE
|
2015-03-02 17:18:25 +01:00
|
|
|
# define CONFIG_ADS1255_CHMODE 0x00
|
2011-08-19 16:05:05 +02:00
|
|
|
#endif
|
|
|
|
#ifndef CONFIG_ADS1255_BUFON
|
2015-03-02 17:18:25 +01:00
|
|
|
# define CONFIG_ADS1255_BUFON 1
|
2011-08-19 16:05:05 +02:00
|
|
|
#endif
|
|
|
|
#ifndef CONFIG_ADS1255_PGA
|
2015-03-02 17:18:25 +01:00
|
|
|
# define CONFIG_ADS1255_PGA ADS125X_PGA2
|
2011-08-19 16:05:05 +02:00
|
|
|
#endif
|
|
|
|
#ifndef CONFIG_ADS1255_SPS
|
2015-03-02 17:18:25 +01:00
|
|
|
# define CONFIG_ADS1255_SPS 50
|
2011-08-19 16:05:05 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
2016-05-26 21:42:35 +02:00
|
|
|
* Private Types
|
2011-08-19 16:05:05 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2016-05-26 19:32:26 +02:00
|
|
|
struct ads1255_dev_s
|
2011-08-19 16:05:05 +02:00
|
|
|
{
|
2016-05-26 21:42:35 +02:00
|
|
|
FAR struct spi_dev_s *spi; /* Cached SPI device reference */
|
2022-03-26 21:51:22 +01:00
|
|
|
int devno;
|
|
|
|
const uint8_t *mux;
|
|
|
|
uint16_t sps;
|
2014-02-11 01:08:49 +01:00
|
|
|
uint8_t channel;
|
|
|
|
uint8_t pga;
|
|
|
|
int irq;
|
2022-03-26 21:51:22 +01:00
|
|
|
FAR const struct adc_callback_s *cb;
|
|
|
|
struct work_s work;
|
|
|
|
bool buf;
|
2011-08-19 16:05:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
2016-05-26 21:42:35 +02:00
|
|
|
* Private Function Prototypes
|
2011-08-19 16:05:05 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2016-05-26 22:00:33 +02:00
|
|
|
static void adc_lock(FAR struct spi_dev_s *spi);
|
|
|
|
static void adc_unlock(FAR struct spi_dev_s *spi);
|
|
|
|
|
2011-08-19 16:05:05 +02:00
|
|
|
/* ADC methods */
|
|
|
|
|
2016-05-26 19:32:26 +02:00
|
|
|
static int adc_bind(FAR struct adc_dev_s *dev,
|
|
|
|
FAR const struct adc_callback_s *callback);
|
2011-08-19 16:05:05 +02:00
|
|
|
static void adc_reset(FAR struct adc_dev_s *dev);
|
|
|
|
static int adc_setup(FAR struct adc_dev_s *dev);
|
|
|
|
static void adc_shutdown(FAR struct adc_dev_s *dev);
|
|
|
|
static void adc_rxint(FAR struct adc_dev_s *dev, bool enable);
|
|
|
|
static int adc_ioctl(FAR struct adc_dev_s *dev, int cmd, unsigned long arg);
|
2016-05-26 21:42:35 +02:00
|
|
|
|
|
|
|
/* Interrupt handling */
|
|
|
|
|
|
|
|
static void adc_worker(FAR void *arg);
|
2017-02-27 13:27:56 +01:00
|
|
|
static int adc_interrupt(int irq, void *context, FAR void *arg);
|
2011-08-19 16:05:05 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
2016-05-26 21:42:35 +02:00
|
|
|
* Private Data
|
2011-08-19 16:05:05 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct adc_ops_s g_adcops =
|
|
|
|
{
|
2022-03-26 21:51:22 +01:00
|
|
|
adc_bind, /* ao_bind */
|
|
|
|
adc_reset, /* ao_reset */
|
|
|
|
adc_setup, /* ao_setup */
|
|
|
|
adc_shutdown, /* ao_shutdown */
|
|
|
|
adc_rxint, /* ao_rxint */
|
|
|
|
adc_ioctl /* ao_read */
|
2011-08-19 16:05:05 +02:00
|
|
|
};
|
|
|
|
|
2016-05-26 19:32:26 +02:00
|
|
|
static struct ads1255_dev_s g_adcpriv =
|
2011-08-19 16:05:05 +02:00
|
|
|
{
|
2022-03-26 21:51:22 +01:00
|
|
|
NULL, 0, (const uint8_t [])
|
2014-02-11 01:08:49 +01:00
|
|
|
{
|
2015-10-10 18:41:00 +02:00
|
|
|
CONFIG_ADS1255_MUX, 0
|
2014-02-11 01:08:49 +01:00
|
|
|
},
|
2022-03-26 21:51:22 +01:00
|
|
|
CONFIG_ADS1255_SPS, 0, 0, CONFIG_ADS1255_IRQ
|
2011-08-19 16:05:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct adc_dev_s g_adcdev =
|
|
|
|
{
|
2022-03-26 21:51:22 +01:00
|
|
|
&g_adcops, /* ad_ops */
|
|
|
|
&g_adcpriv /* ad_priv */
|
2011-08-19 16:05:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static uint8_t getspsreg(uint16_t sps)
|
|
|
|
{
|
2022-03-26 21:51:22 +01:00
|
|
|
static const uint16_t sps_tab[] =
|
2014-02-11 01:08:49 +01:00
|
|
|
{
|
2014-11-25 20:15:09 +01:00
|
|
|
3, 7, 12, 20, 27, 40, 55, 80,
|
|
|
|
300, 750, 1500, 3000, 5000, 10000, 20000, 65535,
|
2014-02-11 01:08:49 +01:00
|
|
|
};
|
2019-12-05 22:13:55 +01:00
|
|
|
|
2022-03-26 21:51:22 +01:00
|
|
|
static const uint8_t sps_reg[] =
|
2014-02-11 01:08:49 +01:00
|
|
|
{
|
2014-11-25 20:15:09 +01:00
|
|
|
0x03, 0x13, 0x23, 0x33, 0x43, 0x53, 0x63, 0x72,
|
|
|
|
0x82, 0x92, 0xa1, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0,
|
2014-02-11 01:08:49 +01:00
|
|
|
};
|
2019-12-05 22:13:55 +01:00
|
|
|
|
2014-02-11 01:08:49 +01:00
|
|
|
int i;
|
|
|
|
|
2014-11-25 20:15:09 +01:00
|
|
|
for (i = 0; i < 16; i++)
|
2011-08-19 16:05:05 +02:00
|
|
|
{
|
2014-11-25 20:15:09 +01:00
|
|
|
if (sps < sps_tab[i])
|
2014-02-11 01:08:49 +01:00
|
|
|
{
|
2014-11-25 20:15:09 +01:00
|
|
|
return sps_reg[i];
|
2014-02-11 01:08:49 +01:00
|
|
|
}
|
2011-08-19 16:05:05 +02:00
|
|
|
}
|
2014-02-11 01:08:49 +01:00
|
|
|
|
2014-11-25 20:15:09 +01:00
|
|
|
return sps_reg[15];
|
2011-08-19 16:05:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2016-05-26 19:32:26 +02:00
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-05-26 22:00:33 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: adc_lock
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Lock and configure the SPI bus.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void adc_lock(FAR struct spi_dev_s *spi)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(spi, true);
|
2016-05-26 22:00:33 +02:00
|
|
|
SPI_SETMODE(spi, SPIDEV_MODE1);
|
|
|
|
SPI_SETBITS(spi, 8);
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_HWFEATURES(spi, 0);
|
2016-05-26 22:00:33 +02:00
|
|
|
SPI_SETFREQUENCY(spi, CONFIG_ADS1255_FREQUENCY);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: adc_unlock
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Unlock the SPI bus.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void adc_unlock(FAR struct spi_dev_s *spi)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(spi, false);
|
2016-05-26 22:00:33 +02:00
|
|
|
}
|
|
|
|
|
2016-05-26 19:32:26 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: adc_bind
|
|
|
|
*
|
|
|
|
* Description:
|
2020-03-10 13:25:22 +01:00
|
|
|
* Bind the upper-half driver callbacks to the lower-half implementation.
|
|
|
|
* This must be called early in order to receive ADC event notifications.
|
2016-05-26 19:32:26 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int adc_bind(FAR struct adc_dev_s *dev,
|
|
|
|
FAR const struct adc_callback_s *callback)
|
|
|
|
{
|
|
|
|
FAR struct ads1255_dev_s *priv = (FAR struct ads1255_dev_s *)dev->ad_priv;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv != NULL);
|
|
|
|
priv->cb = callback;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: adc_reset
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Reset the ADC device. Called early to initialize the hardware. This
|
|
|
|
* is called, before ao_setup() and on error conditions.
|
|
|
|
*
|
2011-08-19 16:05:05 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void adc_reset(FAR struct adc_dev_s *dev)
|
|
|
|
{
|
2016-05-26 19:32:26 +02:00
|
|
|
FAR struct ads1255_dev_s *priv = (FAR struct ads1255_dev_s *)dev->ad_priv;
|
|
|
|
FAR struct spi_dev_s *spi;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv != NULL && priv->spi != NULL);
|
|
|
|
spi = priv->spi;
|
2014-02-11 01:08:49 +01:00
|
|
|
|
2016-05-26 22:00:33 +02:00
|
|
|
adc_lock(spi);
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(1000);
|
2016-05-26 22:00:33 +02:00
|
|
|
|
2014-02-11 01:08:49 +01:00
|
|
|
SPI_SELECT(spi, priv->devno, true);
|
2020-03-10 13:25:22 +01:00
|
|
|
SPI_SEND(spi, ADS125X_WREG + 0x03); /* WRITE SPS REG */
|
2015-10-10 18:41:00 +02:00
|
|
|
SPI_SEND(spi, 0x00); /* count=1 */
|
|
|
|
SPI_SEND(spi, 0x63);
|
2014-02-11 01:08:49 +01:00
|
|
|
SPI_SELECT(spi, priv->devno, false);
|
2016-05-26 22:00:33 +02:00
|
|
|
|
|
|
|
adc_unlock(spi);
|
2011-08-19 16:05:05 +02:00
|
|
|
}
|
|
|
|
|
2016-05-26 19:32:26 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: adc_setup
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Configure the ADC. This method is called the first time that the ADC
|
|
|
|
* device is opened. This will occur when the port is first opened.
|
2020-03-10 13:25:22 +01:00
|
|
|
* This setup includes configuring and attaching ADC interrupts. Interrupts
|
2016-05-26 19:32:26 +02:00
|
|
|
* are all disabled upon return.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2011-08-19 16:05:05 +02:00
|
|
|
|
2016-05-26 19:32:26 +02:00
|
|
|
static int adc_setup(FAR struct adc_dev_s *dev)
|
2011-08-19 16:05:05 +02:00
|
|
|
{
|
2016-05-26 19:32:26 +02:00
|
|
|
FAR struct ads1255_dev_s *priv = (FAR struct ads1255_dev_s *)dev->ad_priv;
|
|
|
|
FAR struct spi_dev_s *spi;
|
|
|
|
int ret;
|
2014-02-11 01:08:49 +01:00
|
|
|
|
2016-05-26 19:32:26 +02:00
|
|
|
DEBUGASSERT(priv != NULL && priv->spi != NULL);
|
|
|
|
spi = priv->spi;
|
|
|
|
|
2017-02-27 13:27:56 +01:00
|
|
|
ret = irq_attach(priv->irq, adc_interrupt, NULL);
|
2014-02-11 01:08:49 +01:00
|
|
|
if (ret == OK)
|
2011-08-19 16:05:05 +02:00
|
|
|
{
|
2016-05-26 22:00:33 +02:00
|
|
|
adc_lock(spi);
|
|
|
|
|
2014-02-11 01:08:49 +01:00
|
|
|
SPI_SELECT(spi, priv->devno, true);
|
2015-10-10 18:41:00 +02:00
|
|
|
SPI_SEND(spi, ADS125X_WREG); /* WRITE REG from 0 */
|
|
|
|
SPI_SEND(spi, 0x03); /* count=4+1 */
|
2014-02-11 01:08:49 +01:00
|
|
|
if (priv->buf)
|
|
|
|
{
|
2015-10-10 18:41:00 +02:00
|
|
|
SPI_SEND(spi, ADS125X_BUFON); /* REG0 STATUS BUFFER ON */
|
2014-02-11 01:08:49 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-10-10 18:41:00 +02:00
|
|
|
SPI_SEND(spi, ADS125X_BUFOFF);
|
2014-02-11 01:08:49 +01:00
|
|
|
}
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
SPI_SEND(spi, priv->mux[0]);
|
|
|
|
SPI_SEND(spi, priv->pga); /* REG2 ADCON PGA=2 */
|
|
|
|
SPI_SEND(spi, getspsreg(priv->sps));
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(1000);
|
2015-10-10 18:41:00 +02:00
|
|
|
SPI_SEND(spi, ADS125X_SELFCAL);
|
2014-02-11 01:08:49 +01:00
|
|
|
SPI_SELECT(spi, priv->devno, false);
|
2016-05-26 22:00:33 +02:00
|
|
|
|
|
|
|
adc_unlock(spi);
|
2014-02-11 01:08:49 +01:00
|
|
|
up_enable_irq(priv->irq);
|
2011-08-19 16:05:05 +02:00
|
|
|
}
|
2014-02-11 01:08:49 +01:00
|
|
|
|
|
|
|
return ret;
|
2011-08-19 16:05:05 +02:00
|
|
|
}
|
|
|
|
|
2016-05-26 19:32:26 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: adc_shutdown
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Disable the ADC. This method is called when the ADC device is closed.
|
|
|
|
* This method reverses the operation the setup method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2011-08-19 16:05:05 +02:00
|
|
|
|
|
|
|
static void adc_shutdown(FAR struct adc_dev_s *dev)
|
|
|
|
{
|
2016-05-26 19:32:26 +02:00
|
|
|
FAR struct ads1255_dev_s *priv = (FAR struct ads1255_dev_s *)dev->ad_priv;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv != NULL);
|
|
|
|
|
2014-02-11 01:08:49 +01:00
|
|
|
up_disable_irq(priv->irq);
|
|
|
|
irq_detach(priv->irq);
|
2011-08-19 16:05:05 +02:00
|
|
|
}
|
|
|
|
|
2016-05-26 19:32:26 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: adc_rxint
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Call to enable or disable RX interrupts
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2011-08-19 16:05:05 +02:00
|
|
|
|
|
|
|
static void adc_rxint(FAR struct adc_dev_s *dev, bool enable)
|
|
|
|
{
|
2016-05-26 19:32:26 +02:00
|
|
|
FAR struct ads1255_dev_s *priv = (FAR struct ads1255_dev_s *)dev->ad_priv;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv != NULL);
|
|
|
|
|
2014-02-11 01:08:49 +01:00
|
|
|
if (enable)
|
|
|
|
{
|
|
|
|
up_enable_irq(priv->irq);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
up_disable_irq(priv->irq);
|
|
|
|
}
|
2011-08-19 16:05:05 +02:00
|
|
|
}
|
|
|
|
|
2016-05-26 19:32:26 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: adc_ioctl
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* All ioctl calls will be routed through this method
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2011-08-19 16:05:05 +02:00
|
|
|
|
2016-05-26 19:32:26 +02:00
|
|
|
static int adc_ioctl(FAR struct adc_dev_s *dev, int cmd, unsigned long arg)
|
2011-08-19 16:05:05 +02:00
|
|
|
{
|
2016-06-16 20:33:32 +02:00
|
|
|
_err("ERROR: Fix me; Not Implemented\n");
|
2014-02-11 01:08:49 +01:00
|
|
|
return 0;
|
2011-08-19 16:05:05 +02:00
|
|
|
}
|
|
|
|
|
2016-05-26 19:32:26 +02:00
|
|
|
/****************************************************************************
|
2016-05-26 21:42:35 +02:00
|
|
|
* Name: adc_worker
|
2016-05-26 19:32:26 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2016-05-26 21:42:35 +02:00
|
|
|
* ADC interrupt work
|
2016-05-26 19:32:26 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-05-26 21:42:35 +02:00
|
|
|
static void adc_worker(FAR void *arg)
|
2011-08-19 16:05:05 +02:00
|
|
|
{
|
2016-05-26 21:42:35 +02:00
|
|
|
FAR struct ads1255_dev_s *priv = (FAR struct ads1255_dev_s *)arg;
|
2016-05-26 19:32:26 +02:00
|
|
|
FAR struct spi_dev_s *spi;
|
2014-02-11 01:08:49 +01:00
|
|
|
unsigned char buf[4];
|
|
|
|
unsigned char ch;
|
|
|
|
|
2016-05-26 19:32:26 +02:00
|
|
|
DEBUGASSERT(priv != NULL && priv->spi != NULL);
|
|
|
|
spi = priv->spi;
|
|
|
|
|
|
|
|
/* REVISIT: Cannot perform SPI operations from an interrupt handler!
|
|
|
|
* Need to use the high priority work queue.
|
|
|
|
*/
|
|
|
|
|
2016-05-26 22:00:33 +02:00
|
|
|
adc_lock(spi);
|
|
|
|
|
2014-02-11 01:08:49 +01:00
|
|
|
SPI_SELECT(spi, priv->devno, true);
|
2015-10-10 18:41:00 +02:00
|
|
|
SPI_SEND(spi, ADS125X_RDATA);
|
2014-02-11 01:08:49 +01:00
|
|
|
up_udelay(10);
|
2015-10-10 18:41:00 +02:00
|
|
|
buf[3] = SPI_SEND(spi, 0xff);
|
|
|
|
buf[2] = SPI_SEND(spi, 0xff);
|
|
|
|
buf[1] = SPI_SEND(spi, 0xff);
|
|
|
|
buf[0] = 0;
|
2014-02-11 01:08:49 +01:00
|
|
|
|
|
|
|
priv->channel++;
|
|
|
|
ch = priv->mux[priv->channel];
|
2015-10-10 18:41:00 +02:00
|
|
|
if (ch == 0)
|
2011-08-19 16:05:05 +02:00
|
|
|
{
|
2015-10-10 18:41:00 +02:00
|
|
|
priv->channel = 0;
|
2014-02-11 01:08:49 +01:00
|
|
|
ch = priv->mux[0];
|
2011-08-19 16:05:05 +02:00
|
|
|
}
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
SPI_SEND(spi, ADS125X_WREG + 0x01);
|
|
|
|
SPI_SEND(spi, 0x00);
|
|
|
|
SPI_SEND(spi, ch);
|
|
|
|
SPI_SEND(spi, ADS125X_SYNC);
|
2014-02-11 01:08:49 +01:00
|
|
|
up_udelay(2);
|
2015-10-10 18:41:00 +02:00
|
|
|
SPI_SEND(spi, ADS125X_WAKEUP);
|
2014-02-11 01:08:49 +01:00
|
|
|
SPI_SELECT(spi, priv->devno, false);
|
2011-08-19 16:05:05 +02:00
|
|
|
|
2016-05-26 22:00:33 +02:00
|
|
|
adc_unlock(spi);
|
|
|
|
|
2016-05-26 19:32:26 +02:00
|
|
|
/* Verify that the upper-half driver has bound its callback functions */
|
|
|
|
|
|
|
|
if (priv->cb != NULL)
|
|
|
|
{
|
|
|
|
/* Perform the data received callback */
|
|
|
|
|
|
|
|
DEBUGASSERT(priv->cb->au_receive != NULL);
|
2024-08-25 01:21:12 +02:00
|
|
|
priv->cb->au_receive(&g_adcdev, priv->channel, *(FAR int32_t *)buf);
|
2016-05-26 19:32:26 +02:00
|
|
|
}
|
|
|
|
|
2016-05-26 21:42:35 +02:00
|
|
|
/* Re-enable ADC interrupts */
|
|
|
|
|
|
|
|
up_enable_irq(priv->irq);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: adc_interrupt
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* ADC interrupt handler
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-08-25 01:21:12 +02:00
|
|
|
static int adc_interrupt(int irq, FAR void *context, FAR void *arg)
|
2016-05-26 21:42:35 +02:00
|
|
|
{
|
2020-03-10 13:25:22 +01:00
|
|
|
FAR struct ads1255_dev_s *priv =
|
|
|
|
(FAR struct ads1255_dev_s *)g_adcdev.ad_priv;
|
2016-05-26 21:42:35 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(priv != NULL);
|
|
|
|
|
|
|
|
/* Disable further ADC interrupts until the worker thread has executed. */
|
|
|
|
|
|
|
|
up_disable_irq(priv->irq);
|
|
|
|
|
|
|
|
/* Schedule the ADC work for the worker thread. Whent he sample has been
|
|
|
|
* processed, the ADC interrupt will be re-enabled.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGVERIFY(work_queue(HPWORK, &priv->work, adc_worker, priv, 0));
|
2014-02-11 01:08:49 +01:00
|
|
|
return OK;
|
2011-08-19 16:05:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: up_ads1255initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the selected adc port
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2015-03-02 17:18:25 +01:00
|
|
|
* Port number (for hardware that has multiple adc interfaces)
|
2011-08-19 16:05:05 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2015-03-02 17:18:25 +01:00
|
|
|
* Valid can device structure reference on success; a NULL on failure
|
2011-08-19 16:05:05 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-03-02 17:18:25 +01:00
|
|
|
FAR struct adc_dev_s *up_ads1255initialize(FAR struct spi_dev_s *spi,
|
|
|
|
unsigned int devno)
|
2011-08-19 16:05:05 +02:00
|
|
|
{
|
2020-03-10 13:25:22 +01:00
|
|
|
FAR struct ads1255_dev_s *priv =
|
|
|
|
(FAR struct ads1255_dev_s *)g_adcdev.ad_priv;
|
2016-05-26 19:32:26 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(spi != NULL);
|
2011-08-19 16:05:05 +02:00
|
|
|
|
2014-02-11 01:08:49 +01:00
|
|
|
/* Driver state data */
|
2011-08-19 16:05:05 +02:00
|
|
|
|
2014-02-11 01:08:49 +01:00
|
|
|
priv->spi = spi;
|
|
|
|
priv->devno = devno;
|
|
|
|
return &g_adcdev;
|
2011-08-19 16:05:05 +02:00
|
|
|
}
|
|
|
|
#endif
|