ADS131 Module - smartel99/NilaiTFO GitHub Wiki

Content

Limitations

In it's currently state, the Ads131Module imposes a few limitation, mostly defined by the hardware configuration of the chip. The interface assumes that:

  • The SPI mode is always Asynchronous Slave
  • The Device Words are always 24-bits wide
  • Hamming is always off
  • Frame Mode is always fixed (6 words long)
  • VREF is always 2.442V

These limitations will be fixed in a future version of the API.

Initialization

To instantiate an ADS131 module in your application, you must first configure a SPI peripheral in the CubeMX software, then instantiate a SpiModule. If you want to use interrupts on the DRDY signal or the DONE signal, you must active the interrupts after configuring the AdsModule!

void MyApplication::InitializeModules()
{
  SpiModule* spiModule = new SpiModule(&hspi1, "spi1");
  AddModule(spiModule);

  AddModule(new AdsModule(spiModule, "ads"));
}

To simplify access to this new instance, you can create a macro that will get that instance in the application:

#define ADS_MODULE    static_cast<AdsModule*>(MyApplication::GetModule("ads"))

This macro allows you to use the module like this:

float ch1 = ADS_MODULE->GetChannel1();

Configuration

To configure the AdsModule, you need to use the ADS::Config structure, which contains the following members:

ADS::Pins pins

Four GPIO needs to be configured in order to properly use the AdsModule: chipSelect, reset, dataReady and done. Each Pin consists of a pointer to the GPIO port and the pin number of that port.

ADS::SampleRate sampleRate

Defaults to ADS::SampleRate::Hz1000. The sample rate of the ADS131. Can take one of the following values:

  • Hz1000: 1kHz
  • Hz2000: 2kHz
  • Hz4000: 4kHz
  • Hz5120: 5.12kHz
  • Hz5333: 5.333kHz
  • Hz8000: 8kHz
  • Hz10240: 10.24kHz
  • Hz10667: 10.667kHz
  • Hz16000: 16kHz
  • Hz20480: 20.48kHz
  • Hz21333: 21.333kHz
  • Hz32000: 32kHz
  • Hz42667: 42.667kHz
  • Hz64000: 64kHz
  • Hz85333: 85.333kHz
  • Hz128000: 128kHz

ADS::Hamming hamming

Defaults to ADS::Hamming::Off Selects if the ADS131 uses hamming codes or not. Can be Enable or Disable.

ADS::WordSize wordSize

Can only be changed by hardware. Select the size of SPI words, in bits. Can be:

  • Size16 for 16 bits words
  • Size24 for 24 bits words
  • Size32 for 32 bits words

ADS::SpiMode spiMode

Can only be changed by hardware. The mode used by the ADS131 for SPI communication. Can be:

  • SynchronousMaster
  • SynchronousSlave
  • AsynchronousSlave

ADS::Threshold threshold

Defaults to ADS::Threshold::P95. Threshold that will trigger an error in the ADS131 if the input voltage goes above. Can be:

  • P95: Threshold is 95% of VDD
  • P92: Threshold is 92.5% of VDD
  • P87: Threshold is 87.5% of VDD
  • P85: Threshold is 85% of VDD
  • P80: Threshold is 80% of VDD
  • P75: Threshold is 75% of VDD
  • P70: Threshold is 70% of VDD

ADS::Delay highZDelay

Defaults to ADS::Delay::Ns12. This is the hold time between the last SCLK falling edge to DOUT 3-state. Can be:

  • Ns6: Between 6ns and 20ns
  • Ns8: Between 8ns and 27ns
  • Ns10: Between 10ns and 43ns
  • Ns12: Between 12ns and 47ns

ADS::Delay doneDelay

Defaults to ADS::Delay::Ns12. This is the propagation delay time between SCLK failing edge to DONE falling edge. Can be:

  • Ns6: Between 6ns and 21ns
  • Ns8: Between 8ns and 27ns
  • Ns10: Between 10ns and 32ns
  • Ns12: Between 12ns and 36ns

ADS::ClockSource clockSource

Defaults to ADS::ClockSource::CLKIN. The source of ICLK, the internal clock that the ADC operates on. Can be:

  • CLKIN: An external signal or a crystal oscillator connected to XTAL1/CLKIN and XTAL2
  • SCLK: The SCLK can be internally routed to be set as ICLK. This mode is only available in synchronous slave interface mode. CLKIN/XTAL1 must be connected to GND and XTAL2 must be unconnected.

ADS::Resolution resolution

Defaults to ADS::Resolution::HighRes. Allows the selection of a high performance mode and a low power mode. Can be:

  • LowRes: Low power mode, fMODlimited to 1.05MHz
  • HighRes: High resolution mode, fMOD limited to 4.25MHz

ADS::ClockDivision clockInDivision

Defaults to ADS::ClockDivision::Div2. Divider applied to FCLKIN to obtain FICLK. Can be:

  • Div2: FICLK = FCLKIN / 2
  • Div4: FICLK = FCLKIN / 4
  • Div6: FICLK = FCLKIN / 6
  • Div8: FICLK = FCLKIN / 8
  • Div10: FICLK = FCLKIN / 10
  • Div12: FICLK = FCLKIN / 12
  • Div14: FICLK = FCLKIN / 14

ADS::ClockDivision modulatorClockDivision

Defaults to ADS::ClockDivision::Div2. Divider applied to FICLK to create the modulator clock, MODCLK. Can be:

  • Div2: FICLK = FCLKIN / 2
  • Div4: FICLK = FCLKIN / 4
  • Div6: FICLK = FCLKIN / 6
  • Div8: FICLK = FCLKIN / 8
  • Div10: FICLK = FCLKIN / 10
  • Div12: FICLK = FCLKIN / 12
  • Div14: FICLK = FCLKIN / 14

ADS::Oversampling oversampling

Defaults to ADS::Oversampling::Ovr4096. The oversampling ratio is the ratio between the modulator frequency and the output data rate. Can be:

  • Ovr32: FDATA = FMOD / 32
  • Ovr48: FDATA = FMOD / 48
  • Ovr64: FDATA = FMOD / 64
  • Ovr96: FDATA = FMOD / 96
  • Ovr128: FDATA = FMOD / 128
  • Ovr192: FDATA = FMOD / 192
  • Ovr200: FDATA = FMOD / 200
  • Ovr256: FDATA = FMOD / 256 - Ovr384: FDATA = FMOD / 384
  • Ovr400: FDATA = FMOD / 400
  • Ovr512: FDATA = FMOD / 512
  • Ovr768: FDATA = FMOD / 768
  • Ovr800: FDATA = FMOD / 800
  • Ovr1024: FDATA = FMOD / 1024
  • Ovr2048: FDATA = FMOD / 2048
  • Ovr4096: FDATA = FMOD / 4096

ADS::Enable enable

Defaults to ADS::Enable::Enable. Use this member to enable or disable the four analog input channels. Can be:

  • Enable: Enable all four channels
  • Disable: Disable all four channels

ADS::DigitalGain ch1DigitalGain

Defaults to ADS::DigitalGain::Gain1. Controls the digital gain that is applied to the input channel 1. Note: The current implementation of the interface always assumes a gain of 1 when converting the value received to volts. Can be:

  • Gain1: Gain of 1x
  • Gain2: Gain of 2x
  • Gain4: Gain of 4x
  • Gain8: Gain of 8x
  • Gain16: Gain of 16x

ADS::DigitalGain ch2DigitalGain

Defaults to ADS::DigitalGain::Gain1. Controls the digital gain that is applied to the input channel 2. Note: The current implementation of the interface always assumes a gain of 1 when converting the value received to volts. Can be:

  • Gain1: Gain of 1x
  • Gain2: Gain of 2x
  • Gain4: Gain of 4x
  • Gain8: Gain of 8x
  • Gain16: Gain of 16x

ADS::DigitalGain ch3DigitalGain

Defaults to ADS::DigitalGain::Gain1. Controls the digital gain that is applied to the input channel 3. Note: The current implementation of the interface always assumes a gain of 1 when converting the value received to volts. Can be:

  • Gain1: Gain of 1x
  • Gain2: Gain of 2x
  • Gain4: Gain of 4x
  • Gain8: Gain of 8x
  • Gain16: Gain of 16x

ADS::DigitalGain ch4DigitalGain

Defaults to ADS::DigitalGain::Gain1. Controls the digital gain that is applied to the input channel 4. Note: The current implementation of the interface always assumes a gain of 1 when converting the value received to volts. Can be:

  • Gain1: Gain of 1x
  • Gain2: Gain of 2x
  • Gain4: Gain of 4x
  • Gain8: Gain of 8x
  • Gain16: Gain of 16x

ADS::InternalReferenceEnable internalReferenceEnable

Defaults to ADS::InternalReferenceEnable::Enable. Select between using the internal reference of the ADS131 or the external reference on the REFP/REFN pins. Can be:

  • Enable: Use the internal reference
  • Disable: Use the external reference

ADS::NegativeChargePumpEnable negativeChargePumpEnable

Defaults to ADS::NegativeChargePumpEnable::Disable. Enable an internal negative charge pump. This allows absolute input signals to go down to -1.5V when powered from a single 3.3V supply. Note: The current implementation of this interface always assumes that the negative charge pump is disabled. Can be:

  • Enable: Use the negative charge pump
  • Disable: Don't use the negative charge pump

ADS::ReferenceVoltage referenceVoltage

Defaults to ADS::ReferenceVoltage::Ref2V442. Select the voltage of the internal reference of the ADS131, if it being used. Note: The current implementation of this interface always assumes that the reference voltage is 2.442V. Can be:

  • Ref2V442: Reference voltage is 2.442V
  • Ref4V: Reference voltage is 4V

ADS::WatchdogEnable watchdogEnable

Defaults to ADS::WatchdogEnable::Disable. The ADS131 offers an integrated watchdog timer to protect the device from entering any unresponsive state. When enabled, the timer resets with each data frame when the CS signal transitions from high to low. If a timer reset does not take place and the watchdog timer expires after 500ms, the device assumes that an unresponsive state has occurred and enters the power-up state. Can be:

  • Enable
  • Disable

ADS::CrcMode crcMode

Defaults to ADS::CrcMode::All. Determine which words of the frame are used when computing the CRC. Can be:

  • All: All words of the frame are used
  • DeviceWord: Only the command word and the device words are used

ADS::FrameMode frameMode

Defaults to ADS::FrameMode::Dynamic. Set the number of device words per frame. In fixed-frame mode, there are always six device words for each data frame. In dynamic-frame mode, the number of device words per data frame is dependent on if the ADCs are enabled and if CRC data integrity is enabled. Note: The current implementation always assumes that the ADS131 is in Fixed-frame mode. Can be:

  • Fixed
  • Dynamic

ADS::CrcEnable crcEnable

Defaults to ADS::CrcEnable::Disable. Toggles the usage of the Cyclic Redundancy Check used by the ADS. Can be:

  • Enable
  • Disable

Configuring

Once you have your ADS::Config structure all ready, use the Configure method of AdsModule, which takes a reference to a ADS::Config object, and an optional Boolean flag that can be set to force the configuration of the ADS131 device.

void MyApplication::InitializeModules()
{
    // Instantiate the AdsModule.
    AddModule(new AdsModule(SPI1_MODULE, "ads"));
    
    // Use the default values of Config.
    ADS::Config adsConfig = ADS::Config();    
    
    // Set the Chip Select pin used by the ADS.
    adsConfig.pins.chipSelect.port = ADS131_CS_GPIO_Port;
    adsConfig.pins.chipSelect.pin = ADS131_CS_Pin;

    // Set the Data Ready pin used by the ADS.
    adsConfig.pins.dataReady.port = VOLT_DRDY_GPIO_Port;
    adsConfig.pins.dataReady.pin = VOLT_DRDY_Pin;

    // Set the Done pin used by the ADS.
    adsConfig.pins.done.port = VOLT_DONE_GPIO_Port;
    adsConfig.pins.done.pin = VOLT_DONE_Pin;

    // Set the Reset pin used by the ADS.
    adsConfig.pins.reset.port = VOLT_RST_GPIO_Port;
    adsConfig.pins.reset.pin = VOLT_RST_Pin;
        
    // Send the configuration to the ADS.
    ADS_MODULE->Configure(adsConfig, true);

    // Make sure that it was successfully configured.
    CEP_ASSERT(ADS_MODULE->IsActive() == true, "ADS Module not active!");
}

Usage

The utilization of the AdsModule once it is configure is pretty straight-forward. However, a simple twist needs to be done in order to use it well. The GetChannelX methods don't actually go read a channel on the ADS but instead return a cached value. In order to refresh these cached values, you must use the RefreshValues method, either during an interrupt caused by the DRDY signal, or in polling mode.

Interrupt-Driven Refresh

This example uses the EXTI9_5 interrupt vector, but literally any interrupt can be used.

stm32f4xx_it.c

// Declare the forwarding function here.
void Ads_RefreshValues(void);

// The interrupt that we should do the refresh in:
void EXTI9_5_IRQHandler(void)
{
  // Call the forwarding function.
  Ads_RefreshValues();

  // Let the HAL do what it has to do with the IRQ.
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_5);
}

someOtherFile.cpp

// Define the function that was declared in stm32f4xx_it.c:
// It is defined as extern "C" to allow it to be called from a file compiled in C.
extern "C" void Ads_RefreshValues(void)
{
  ADS_MODULE->RefreshValues();
}

Polling Mode

To make the program block and wait for a frame to be ready from the ADS131, simply specify a timeout when calling the RefreshValues method. It also returns the full frame, so you can directly work from those results, without having to call GetChannelX.

void SomeFunction()
{
  // Wait up to 100ms for the ADS131 to send a frame of data.
  AdsPacket frame = ADS_MODULE->RefreshValues(100);

  // Do some operation on channel 1.
  float ch1 = frame.channel1;
}
⚠️ **GitHub.com Fallback** ⚠️