Skip to content

Commit

Permalink
Merge pull request #1071 from WildernessLabs/ads1263_cleanup
Browse files Browse the repository at this point in the history
Cleanup and remove regions
  • Loading branch information
jorgedevs authored Sep 10, 2024
2 parents 83f081a + 8b08415 commit e99f0e9
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ public DigitalOutputPort(
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public enum Adc2Gain : byte
Gain_32 = 0x5,
/// <summary> 64 V/V (resulting in ±39 mV range assuming Internal reference) </summary>
Gain_64 = 0x6,
/// <summary> 128 V/V (resulting in ±19.5 mV range assuming Internal reference) </summary>
/// <summary> 128 V/V (resulting in ±19.5 mV range assuming Internal reference) </summary>
Gain_128 = 0x7,
}

Expand Down Expand Up @@ -267,4 +267,4 @@ public enum Register : byte
ADC2OSC1 = 0x1A,
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections;
using Meadow.Hardware;
using System.Collections;
using System.Collections.Generic;
using Meadow.Hardware;

namespace Meadow.Foundation.ICs.IOExpanders
{
Expand Down Expand Up @@ -37,35 +37,35 @@ public PinDefinitions(Ads1263 controller)
new List<IChannelInfo> { new AnalogChannelInfo(nameof(ADC2), 24, true, false), });

/// <summary> Pin representing GPIO0 (AIN3) </summary>
public IPin GPIO0 => new Pin(Controller, nameof(GPIO0), (byte)0x00,
public IPin GPIO0 => new Pin(Controller, nameof(GPIO0), (byte)0x00,
new List<IChannelInfo> { new DigitalChannelInfo(nameof(GPIO0), true, true, false, false, false) });

/// <summary> Pin representing GPIO1 (AIN4) </summary>
public IPin GPIO1 => new Pin(Controller, nameof(GPIO1), (byte)0x01,
public IPin GPIO1 => new Pin(Controller, nameof(GPIO1), (byte)0x01,
new List<IChannelInfo> { new DigitalChannelInfo(nameof(GPIO1), true, true, false, false, false) });

/// <summary> Pin representing GPIO2 (AIN5) </summary>
public IPin GPIO2 => new Pin(Controller, nameof(GPIO2), (byte)0x02,
public IPin GPIO2 => new Pin(Controller, nameof(GPIO2), (byte)0x02,
new List<IChannelInfo> { new DigitalChannelInfo(nameof(GPIO2), true, true, false, false, false) });

/// <summary> Pin representing GPIO3 (AIN6) </summary>
public IPin GPIO3 => new Pin(Controller, nameof(GPIO3), (byte)0x03,
public IPin GPIO3 => new Pin(Controller, nameof(GPIO3), (byte)0x03,
new List<IChannelInfo> { new DigitalChannelInfo(nameof(GPIO3), true, true, false, false, false) });

/// <summary> Pin representing GPIO4 (AIN7) </summary>
public IPin GPIO4 => new Pin(Controller, nameof(GPIO4), (byte)0x04,
public IPin GPIO4 => new Pin(Controller, nameof(GPIO4), (byte)0x04,
new List<IChannelInfo> { new DigitalChannelInfo(nameof(GPIO4), true, true, false, false, false) });

/// <summary> Pin representing GPIO5 (AIN8) </summary>
public IPin GPIO5 => new Pin(Controller, nameof(GPIO5), (byte)0x05,
public IPin GPIO5 => new Pin(Controller, nameof(GPIO5), (byte)0x05,
new List<IChannelInfo> { new DigitalChannelInfo(nameof(GPIO5), true, true, false, false, false) });

/// <summary> Pin representing GPIO6 (AIN9) </summary>
public IPin GPIO6 => new Pin(Controller, nameof(GPIO6), (byte)0x06,
public IPin GPIO6 => new Pin(Controller, nameof(GPIO6), (byte)0x06,
new List<IChannelInfo> { new DigitalChannelInfo(nameof(GPIO6), true, true, false, false, false) });

/// <summary> Pin representing GPIO7 (AINCOM) </summary>
public IPin GPIO7 => new Pin(Controller, nameof(GPIO7), (byte)0x07,
public IPin GPIO7 => new Pin(Controller, nameof(GPIO7), (byte)0x07,
new List<IChannelInfo> { new DigitalChannelInfo(nameof(GPIO7), true, true, false, false, false) });

/// <summary>
Expand All @@ -86,4 +86,4 @@ protected void InitAllPins()
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Meadow.Hardware;
using Meadow.Units;
using System.Linq;
using System;
using Meadow.Utilities;
using System;
using System.Linq;

namespace Meadow.Foundation.ICs.IOExpanders;

Expand All @@ -20,7 +20,6 @@ public partial class Ads1263 : IAnalogInputController, IDigitalInputOutputContro
private byte ioEnable, ioDir, ioOut, gain1, gain2;
private double vRef1, vRef2;

#region SPI
/// <summary>
/// Gets the underlying ISpiCommunications instance
/// </summary>
Expand All @@ -40,9 +39,8 @@ public partial class Ads1263 : IAnalogInputController, IDigitalInputOutputContro

/// <summary> Did we create the port(s) used by the peripheral </summary>
private readonly bool createdPort = false;

private readonly IDigitalOutputPort chipSelectPort;
#endregion

/// <summary>
/// object for using lock() while modifying GPIO outputs
Expand Down Expand Up @@ -89,8 +87,6 @@ internal Voltage GetADCReferenceVoltage(IPin pin)
var vRef = (byte)pin.Key switch { 0x00 => vRef1, _ => vRef2 };
return new Voltage(vRef, Voltage.UnitType.Volts);
}

#region Configuration
private void Initialize()
{
// read Device ID to confirm communications
Expand Down Expand Up @@ -127,19 +123,20 @@ public void ConfigureADC1(AdcSource positiveSource = AdcSource.AIN0, AdcSource n
double referenceVoltage = 5.0, Adc1ReferenceP positiveReference = Adc1ReferenceP.Internal, Adc1ReferenceN negativeReference = Adc1ReferenceN.Internal)
{
// Verify combinations of filter and data rate settings
if (filter == Adc1Filter.FIR && ((byte)rate > (byte)Adc1DataRate.SPS_20 || rate == Adc1DataRate.SPS_16p6))
if (filter == Adc1Filter.FIR && ((byte)rate > (byte)Adc1DataRate.SPS_20 || rate == Adc1DataRate.SPS_16p6))
throw new ArgumentException("FIR filter can only be used with 2.5, 5, 10, or 20 samples per second");
// Note: The three fastest data rates bypass the second filter stage, so filter may be ignored.

WriteRegister(Register.MODE1, (byte)filter << 5);
WriteRegister(Register.MODE2, (byte)gain << 4 | (byte)rate);
WriteRegister(Register.INPMUX, (byte)positiveSource << 4 | (byte)negativeSource);
WriteRegister(Register.REFMUX, (byte)positiveReference << 3 | (byte) negativeReference);
WriteRegister(Register.REFMUX, (byte)positiveReference << 3 | (byte)negativeReference);

gain1 = (byte)(1 << (byte)gain);
vRef1 = (positiveReference, negativeReference) switch {
vRef1 = (positiveReference, negativeReference) switch
{
(Adc1ReferenceP.Internal, Adc1ReferenceN.Internal) => 2.5,
(_,_) => referenceVoltage
(_, _) => referenceVoltage
};
}

Expand All @@ -154,7 +151,7 @@ public void ConfigureADC1(AdcSource positiveSource = AdcSource.AIN0, AdcSource n
/// <param name="reference">Enumeration specifying the positive and negative reference source</param>
public void ConfigureADC2(
AdcSource positiveSource = AdcSource.AIN0, AdcSource negativeSource = AdcSource.AIN1,
Adc2Gain gain = Adc2Gain.Gain_1, Adc2DataRate rate = Adc2DataRate.SPS_10,
Adc2Gain gain = Adc2Gain.Gain_1, Adc2DataRate rate = Adc2DataRate.SPS_10,
double referenceVoltage = 5.0, Adc2Reference reference = Adc2Reference.Internal)
{
WriteRegister(Register.ADC2CFG, (byte)rate << 6 | (byte)reference << 3 | (byte)gain);
Expand Down Expand Up @@ -192,9 +189,6 @@ public void WriteRegister(Register register, int data)
SpiComms.Write(command);
}

#endregion

#region AnalogInputPort
/// <inheritdoc />
public IAnalogInputPort CreateAnalogInputPort(IPin pin, int sampleCount, TimeSpan sampleInterval, Voltage referenceVoltage)
{
Expand Down Expand Up @@ -251,7 +245,7 @@ private Voltage ReadAnalog(IPin pin)
var gain = key switch { 0x00 => gain1, _ => gain2 };

// TODO: verify conversion/scaling
var result = (vRef / gain) * (rawValue / (double)0x7FFFFFFF);
var result = (vRef / gain) * (rawValue / (double)0x7FFFFFFF);

return new Voltage(result, Voltage.UnitType.Volts);
}
Expand All @@ -264,9 +258,6 @@ public static Temperature ConvertTempSensor(Voltage tempSensorVoltage)
return new Temperature(((tempSensorVoltage.Microvolts - 122400) / 420) + 25, Temperature.UnitType.Celsius);
}

#endregion

#region DigitalInputPort
/// <summary>
/// Creates a new DigitalInputPort using the specified GPIO pin
/// </summary>
Expand Down Expand Up @@ -294,9 +285,7 @@ private bool ReadPort(IPin pin)
var ioInputs = ReadRegister(Register.GPIODAT);
return BitHelpers.GetBitValue(ioInputs, (byte)pin.Key);
}
#endregion

#region DigitalOutputPort
/// <summary>
/// Creates a new DigitalOutputPort using the specified pin and initial state
/// </summary>
Expand Down Expand Up @@ -324,9 +313,7 @@ public IDigitalOutputPort CreateDigitalOutputPort(IPin pin, bool initialState =

return port;
}
#endregion

#region Digital
/// <summary>
/// Sets the GPIO configuration of a port using pre-cached information. This overload
/// assumes the pin has been pre-verified as valid.
Expand Down Expand Up @@ -382,9 +369,7 @@ private void PreValidatedWriteToPort(IPin pin, bool value)
WriteRegister(Register.GPIODAT, ioOut);
}
}
#endregion

#region IDisposable
/// <summary>
/// Is the object disposed
/// </summary>
Expand Down Expand Up @@ -413,5 +398,4 @@ protected virtual void Dispose(bool disposing)
IsDisposed = true;
}
}
#endregion
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Meadow.Foundation.ICs.ADC.Ads1263
# Meadow.Foundation.ICs.IOExpanders.Ads1263

**TI ADS1263 SPI analog to digital converter**
**TI ADS1263 SPI analog to digital converter, IO expander**

The **Ads1263** library is included in the **Meadow.Foundation.ICs.ADC.Ads1263** nuget package and is designed for the [Wilderness Labs](www.wildernesslabs.co) Meadow .NET IoT platform.
The **Ads1263** library is included in the **Meadow.Foundation.ICs.IOExpanders.Ads1263** nuget package and is designed for the [Wilderness Labs](www.wildernesslabs.co) Meadow .NET IoT platform.

This driver is part of the [Meadow.Foundation](https://developer.wildernesslabs.co/Meadow/Meadow.Foundation/) peripherals library, an open-source repository of drivers and libraries that streamline and simplify adding hardware to your C# .NET Meadow IoT applications.

Expand All @@ -14,7 +14,69 @@ To view all Wilderness Labs open-source projects, including samples, visit [gith

You can install the library from within Visual studio using the the NuGet Package Manager or from the command line using the .NET CLI:

`dotnet add package Meadow.Foundation.ICs.ADC.Ads1263`
`dotnet add package Meadow.Foundation.ICs.IOExpanders.Ads1263`
## Usage

```csharp
private Ads1263 ads1263;
private Ads1263.AnalogInputPort adc1, adc2;
private Ads1263.DigitalInputPort gpio0, gpio1;
private Ads1263.DigitalOutputPort gpio6, gpio7;

public override Task Initialize()
{
Resolver.Log.Info("Initialize...");

var spiBus = Device.CreateSpiBus();
ads1263 = new Ads1263(spiBus, Device.Pins.A05);

// Setup ADC1 on the default inputs
ads1263.ConfigureADC1(positiveSource: Ads1263.AdcSource.AIN0, negativeSource: Ads1263.AdcSource.AIN1);
adc1 = (Ads1263.AnalogInputPort)ads1263.CreateAnalogInputPort(ads1263.Pins.ADC1, 1, TimeSpan.Zero, 2.5.Volts());
adc1.StartConversions();
adc1.Updated += Adc1_Updated;
adc1.StartUpdating(TimeSpan.FromSeconds(5));

// Setup ADC2 to read the internal temperature
ads1263.ConfigureADC2(positiveSource: Ads1263.AdcSource.TempSensor, negativeSource: Ads1263.AdcSource.TempSensor);
adc2 = (Ads1263.AnalogInputPort)ads1263.CreateAnalogInputPort(ads1263.Pins.ADC2, 1, TimeSpan.Zero, 2.5.Volts());
adc2.StartConversions();
adc2.Updated += Adc2_Updated;
adc2.StartUpdating(TimeSpan.FromSeconds(5));

// Setup digital inputs and outputs
gpio0 = (Ads1263.DigitalInputPort)ads1263.CreateDigitalInputPort(ads1263.Pins.GPIO0);
gpio1 = (Ads1263.DigitalInputPort)ads1263.CreateDigitalInputPort(ads1263.Pins.GPIO1);
gpio6 = (Ads1263.DigitalOutputPort)ads1263.CreateDigitalOutputPort(ads1263.Pins.GPIO6);
gpio7 = (Ads1263.DigitalOutputPort)ads1263.CreateDigitalOutputPort(ads1263.Pins.GPIO7);

return base.Initialize();
}

private void Adc1_Updated(object sender, IChangeResult<Voltage> e)
{
Resolver.Log.Info($"ADC1 {e.New.Volts:N6} V");
}

private void Adc2_Updated(object sender, IChangeResult<Voltage> e)
{
Resolver.Log.Info($"ADC2 {e.New.Volts:N6} V = {(Ads1263.ConvertTempSensor(e.New)):N2} °C");
}

public override Task Run()
{
Resolver.Log.Info("Run...");

while (true)
{
gpio7.State = gpio0.State;
gpio6.State = !gpio1.State;

Thread.Sleep(TimeSpan.FromSeconds(1));
}
}

```
## How to Contribute

- **Found a bug?** [Report an issue](https://github.com/WildernessLabs/Meadow_Issues/issues)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Meadow;
using Meadow;
using Meadow.Devices;
using Meadow.Foundation.ICs.IOExpanders;
using Meadow.Units;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Ads1263_Sample
{
public class MeadowApp : App<F7FeatherV2>
{
//<!=SNIP=>

private Ads1263 ads1263;
private Ads1263.AnalogInputPort adc1, adc2;
private Ads1263.DigitalInputPort gpio0, gpio1;
Expand All @@ -24,7 +26,7 @@ public override Task Initialize()

// Setup ADC1 on the default inputs
ads1263.ConfigureADC1(positiveSource: Ads1263.AdcSource.AIN0, negativeSource: Ads1263.AdcSource.AIN1);
adc1 = (Ads1263.AnalogInputPort)ads1263.CreateAnalogInputPort(ads1263.Pins.ADC1, 1, TimeSpan.Zero, 2.5.Volts() );
adc1 = (Ads1263.AnalogInputPort)ads1263.CreateAnalogInputPort(ads1263.Pins.ADC1, 1, TimeSpan.Zero, 2.5.Volts());
adc1.StartConversions();
adc1.Updated += Adc1_Updated;
adc1.StartUpdating(TimeSpan.FromSeconds(5));
Expand Down Expand Up @@ -66,9 +68,8 @@ public override Task Run()

Thread.Sleep(TimeSpan.FromSeconds(1));
}

return base.Run();
}

//<!=SNOP=>
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ internal override void ReadDeviceInfo()
DeviceID = DeviceRevision = 0;
}

#region Enumerations
private enum Registers : byte
{
Config = 0x00,
Expand Down Expand Up @@ -240,9 +239,6 @@ public enum Mode
ContinuousAll = 0x7, // default at POR
}

#endregion

#region Shorthand
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void WriteRegister(Registers register, byte value) => BusComms.WriteRegister((byte)register, value, ByteOrder.BigEndian);

Expand All @@ -260,6 +256,4 @@ public enum Mode

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private ushort ReadRegisterAsUShort(Registers register) => BusComms.ReadRegisterAsUShort((byte)register, ByteOrder.BigEndian);
#endregion

}
Loading

0 comments on commit e99f0e9

Please sign in to comment.