#define ADS1298_MAX_CHANNELS 8 #define ADS1298_BITS_PER_SAMPLE 24 #define ADS1298_CLK_RATE_HZ 2048000 #define ADS1298_CLOCKS_TO_USECS(x) \
(DIV_ROUND_UP((x) * MICROHZ_PER_HZ, ADS1298_CLK_RATE_HZ)) /* * Read/write register commands require 4 clocks to decode, for speeds above * 2x the clock rate, this would require extra time between the command byte and * the data. Much simpler is to just limit the SPI transfer speed while doing * register access.
*/ #define ADS1298_SPI_BUS_SPEED_SLOW ADS1298_CLK_RATE_HZ /* For reading and writing registers, we need a 3-byte buffer */ #define ADS1298_SPI_CMD_BUFFER_SIZE 3 /* Outputs status word and 'n' 24-bit samples, plus the command byte */ #define ADS1298_SPI_RDATA_BUFFER_SIZE(n) (((n) + 1) * 3 + 1) #define ADS1298_SPI_RDATA_BUFFER_SIZE_MAX \
ADS1298_SPI_RDATA_BUFFER_SIZE(ADS1298_MAX_CHANNELS)
struct ads1298_private { conststruct ads1298_chip_info *chip_info; struct spi_device *spi; struct regulator *reg_avdd; struct regulator *reg_vref; struct clk *clk; struct regmap *regmap; struct completion completion; struct iio_trigger *trig; struct spi_transfer rdata_xfer; struct spi_message rdata_msg;
spinlock_t irq_busy_lock; /* Handshake between SPI and DRDY irqs */ /* * rdata_xfer_busy increments when a DRDY occurs and decrements when SPI * completion is reported. Hence its meaning is: * 0 = Waiting for DRDY interrupt * 1 = SPI transfer in progress * 2 = DRDY during SPI transfer, start another transfer on completion * >2 = Multiple DRDY during transfer, lost rdata_xfer_busy - 2 samples
*/ unsignedint rdata_xfer_busy;
/* Temporary storage for demuxing data after SPI transfer */
u32 bounce_buffer[ADS1298_MAX_CHANNELS];
/* Buffer used for incoming SPI data */
u8 rx_buffer[ADS1298_SPI_RDATA_BUFFER_SIZE_MAX]; /* Contains the RDATA command and zeroes to clock out */
u8 tx_buffer[ADS1298_SPI_RDATA_BUFFER_SIZE_MAX];
};
/* Three bytes per sample in RX buffer, starting at offset 4 */ #define ADS1298_OFFSET_IN_RX_BUFFER(index) (3 * (index) + 4)
staticint ads1298_read_one(struct ads1298_private *priv, int chan_index)
{ int ret;
/* Enable the channel */
ret = regmap_update_bits(priv->regmap, ADS1298_REG_CHnSET(chan_index),
ADS1298_MASK_CH_PD, 0); if (ret) return ret;
/* Enable single-shot mode, so we don't need to send a STOP */
ret = regmap_update_bits(priv->regmap, ADS1298_REG_CONFIG4,
ADS1298_MASK_CONFIG4_SINGLE_SHOT,
ADS1298_MASK_CONFIG4_SINGLE_SHOT); if (ret) return ret;
reinit_completion(&priv->completion);
ret = ads1298_write_cmd(priv, ADS1298_CMD_START); if (ret < 0) {
dev_err(&priv->spi->dev, "CMD_START error: %d\n", ret); return ret;
}
/* Cannot take longer than 40ms (250Hz) */
ret = wait_for_completion_timeout(&priv->completion, msecs_to_jiffies(50)); if (!ret) return -ETIMEDOUT;
return 0;
}
staticint ads1298_get_samp_freq(struct ads1298_private *priv, int *val)
{ unsignedlong rate; unsignedint cfg; int ret;
ret = regmap_read(priv->regmap, ADS1298_REG_CONFIG1, &cfg); if (ret) return ret;
if (priv->clk)
rate = clk_get_rate(priv->clk); else
rate = ADS1298_CLK_RATE_HZ; if (!rate) return -EINVAL;
/* Data rate shift depends on HR/LP mode */ if (cfg & ADS1298_MASK_CONFIG1_HR)
rate >>= ADS1298_SHIFT_DR_HR; else
rate >>= ADS1298_SHIFT_DR_LP;
staticvoid ads1298_rdata_unmark_busy(struct ads1298_private *priv)
{ /* Notify we're no longer waiting for the SPI transfer to complete */
guard(spinlock_irqsave)(&priv->irq_busy_lock);
priv->rdata_xfer_busy = 0;
}
staticint ads1298_update_scan_mode(struct iio_dev *indio_dev, constunsignedlong *scan_mask)
{ struct ads1298_private *priv = iio_priv(indio_dev); unsignedint val; int ret; int i;
/* Make the interrupt routines start with a clean slate */
ads1298_rdata_unmark_busy(priv);
/* Configure power-down bits to match scan mask */ for (i = 0; i < indio_dev->num_channels; i++) {
val = test_bit(i, scan_mask) ? 0 : ADS1298_MASK_CH_PD;
ret = regmap_update_bits(priv->regmap, ADS1298_REG_CHnSET(i),
ADS1298_MASK_CH_PD, val); if (ret) return ret;
}
if (priv->rdata_xfer_busy > 1) { /* * DRDY interrupt occurred before SPI completion. Start a new * SPI transaction now to retrieve the data that wasn't latched * into the ADS1298 chip's transfer buffer yet.
*/
spi_async(priv->spi, &priv->rdata_msg); /* * If more than one DRDY took place, there was an overrun. Since * the sample is already lost, reset the counter to 1 so that * we will wait for a DRDY interrupt after this SPI transaction.
*/
priv->rdata_xfer_busy = 1;
} else { /* No pending data, wait for DRDY */
priv->rdata_xfer_busy = 0;
}
}
/* Called from SPI completion interrupt handler */ staticvoid ads1298_rdata_complete(void *context)
{ struct iio_dev *indio_dev = context; struct ads1298_private *priv = iio_priv(indio_dev); int scan_index;
u32 *bounce = priv->bounce_buffer;
if (!iio_buffer_enabled(indio_dev)) { /* * for a single transfer mode we're kept in direct_mode until * completion, avoiding a race with buffered IO.
*/
ads1298_rdata_unmark_busy(priv);
complete(&priv->completion); return;
}
/* Demux the channel data into our bounce buffer */
iio_for_each_active_channel(indio_dev, scan_index) { conststruct iio_chan_spec *scan_chan =
&indio_dev->channels[scan_index]; const u8 *data = priv->rx_buffer + scan_chan->address;
*bounce++ = get_unaligned_be24(data);
}
/* rx_buffer can be overwritten from this point on */
ads1298_rdata_release_busy_or_restart(priv);
/* Device initializes into RDATAC mode, which we don't want */
ret = ads1298_write_cmd(priv, ADS1298_CMD_SDATAC); if (ret) return ret;
ret = regmap_read(priv->regmap, ADS1298_REG_ID, &val); if (ret) return ret;
/* Fill in name and channel count based on what the chip told us */
indio_dev->num_channels = 4 + 2 * (val & ADS1298_MASK_ID_CHANNELS); switch (val & ADS1298_MASK_ID_FAMILY) { case ADS1298_ID_FAMILY_ADS129X:
suffix = ""; break; case ADS1298_ID_FAMILY_ADS129XR:
suffix = "r"; break; default: return dev_err_probe(dev, -ENODEV, "Unknown ID: 0x%x\n", val);
}
indio_dev->name = devm_kasprintf(dev, GFP_KERNEL, "ads129%u%s",
indio_dev->num_channels, suffix); if (!indio_dev->name) return -ENOMEM;
/* Enable internal test signal, double amplitude, double frequency */
ret = regmap_write(priv->regmap, ADS1298_REG_CONFIG2,
ADS1298_MASK_CONFIG2_RESERVED |
ADS1298_MASK_CONFIG2_INT_TEST |
ADS1298_MASK_CONFIG2_TEST_AMP |
ADS1298_MASK_CONFIG2_TEST_FREQ_FAST); if (ret) return ret;
val = ADS1298_MASK_CONFIG3_RESERVED; /* Must write 1 always */ if (!priv->reg_vref) { /* Enable internal reference */
val |= ADS1298_MASK_CONFIG3_PWR_REFBUF; /* Use 4V VREF when power supply is at least 4.4V */ if (regulator_get_voltage(priv->reg_avdd) >= 4400000)
val |= ADS1298_MASK_CONFIG3_VREF_4V;
} return regmap_write(priv->regmap, ADS1298_REG_CONFIG3, val);
}
indio_dev = devm_iio_device_alloc(dev, sizeof(*priv)); if (!indio_dev) return -ENOMEM;
priv = iio_priv(indio_dev);
/* Reset to be asserted before enabling clock and power */
reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); if (IS_ERR(reset_gpio)) return dev_err_probe(dev, PTR_ERR(reset_gpio), "Cannot get reset GPIO\n");
/* VREF can be supplied externally, otherwise use internal reference */
priv->reg_vref = devm_regulator_get_optional(dev, "vref"); if (IS_ERR(priv->reg_vref)) { if (PTR_ERR(priv->reg_vref) != -ENODEV) return dev_err_probe(dev, PTR_ERR(priv->reg_vref), "Failed to get vref regulator\n");
priv->reg_vref = NULL;
} else {
ret = regulator_enable(priv->reg_vref); if (ret) return ret;
ret = devm_add_action_or_reset(dev, ads1298_reg_disable, priv->reg_vref); if (ret) return ret;
}
priv->clk = devm_clk_get_optional_enabled(dev, "clk"); if (IS_ERR(priv->clk)) return dev_err_probe(dev, PTR_ERR(priv->clk), "Failed to get clk\n");
priv->reg_avdd = devm_regulator_get(dev, "avdd"); if (IS_ERR(priv->reg_avdd)) return dev_err_probe(dev, PTR_ERR(priv->reg_avdd), "Failed to get avdd regulator\n");
ret = regulator_enable(priv->reg_avdd); if (ret) return dev_err_probe(dev, ret, "Failed to enable avdd regulator\n");
ret = devm_add_action_or_reset(dev, ads1298_reg_disable, priv->reg_avdd); if (ret) return ret;
if (reset_gpio) { /* * Deassert reset now that clock and power are active. * Minimum reset pulsewidth is 2 clock cycles.
*/
fsleep(ADS1298_CLOCKS_TO_USECS(2));
gpiod_set_value_cansleep(reset_gpio, 0);
} else {
ret = ads1298_write_cmd(priv, ADS1298_CMD_RESET); if (ret) return dev_err_probe(dev, ret, "RESET failed\n");
} /* Wait 18 clock cycles for reset command to complete */
fsleep(ADS1298_CLOCKS_TO_USECS(18));
ret = ads1298_init(indio_dev); if (ret) return dev_err_probe(dev, ret, "Init failed\n");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.