Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bmexxx bus abstractions #635

Merged
merged 4 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions Source/Meadow.Foundation.Core/Communications/I2cCommunications.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void Read(Span<byte> readBuffer)
/// </summary>
/// <param name="address"></param>
/// <param name="readBuffer"></param>
public void ReadRegister(byte address, Span<byte> readBuffer)
public virtual void ReadRegister(byte address, Span<byte> readBuffer)
{
WriteBuffer.Span[0] = address;
Bus.Exchange(Address, WriteBuffer.Span[0..1], readBuffer);
Expand All @@ -71,7 +71,7 @@ public void ReadRegister(byte address, Span<byte> readBuffer)
/// Read a register from the peripheral.
/// </summary>
/// <param name="address">Address of the register to read.</param>
public byte ReadRegister(byte address)
public virtual byte ReadRegister(byte address)
{
WriteBuffer.Span[0] = address;
Bus.Exchange(Address, WriteBuffer.Span[0..1], ReadBuffer.Span[0..1]);
Expand All @@ -84,7 +84,7 @@ public byte ReadRegister(byte address)
/// <param name="address">Register address of the low byte (the high byte will follow).</param>
/// <param name="order">Order of the bytes in the register (little endian is the default).</param>
/// <returns>Value read from the register.</returns>
public ushort ReadRegisterAsUShort(byte address, ByteOrder order = ByteOrder.LittleEndian)
public virtual ushort ReadRegisterAsUShort(byte address, ByteOrder order = ByteOrder.LittleEndian)
{
WriteBuffer.Span[0] = address;
Bus.Exchange(Address, WriteBuffer[0..1].Span, ReadBuffer[0..2].Span);
Expand All @@ -102,7 +102,7 @@ public ushort ReadRegisterAsUShort(byte address, ByteOrder order = ByteOrder.Lit
/// Write a single byte to the peripheral.
/// </summary>
/// <param name="value">Value to be written (8-bits).</param>
public void Write(byte value)
public virtual void Write(byte value)
{
WriteBuffer.Span[0] = value;
Bus.Write(Address, WriteBuffer.Span[0..1]);
Expand All @@ -112,14 +112,14 @@ public void Write(byte value)
/// Write an array of bytes to the peripheral.
/// </summary>
/// <param name="data">Values to be written.</param>
public void Write(Span<byte> data) => Bus.Write(Address, data);
public virtual void Write(Span<byte> data) => Bus.Write(Address, data);

/// <summary>
/// Write data a register in the peripheral.
/// </summary>
/// <param name="address">Address of the register to write to.</param>
/// <param name="value">Data to write into the register.</param>
public void WriteRegister(byte address, byte value)
public virtual void WriteRegister(byte address, byte value)
{
WriteBuffer.Span[0] = address;
WriteBuffer.Span[1] = value;
Expand All @@ -132,7 +132,7 @@ public void WriteRegister(byte address, byte value)
/// <param name="address">Address to write the first byte to.</param>
/// <param name="value">Value to be written (16-bits).</param>
/// <param name="order">Indicate if the data should be written as big or little endian.</param>
public void WriteRegister(byte address, ushort value, ByteOrder order = ByteOrder.LittleEndian)
public virtual void WriteRegister(byte address, ushort value, ByteOrder order = ByteOrder.LittleEndian)
{
var bytes = BitConverter.GetBytes(value);
WriteRegister(address, bytes, order);
Expand All @@ -144,7 +144,7 @@ public void WriteRegister(byte address, ushort value, ByteOrder order = ByteOrde
/// <param name="address">Address to write the first byte to.</param>
/// <param name="value">Value to be written.</param>
/// <param name="order">Indicate if the data should be written as big or little endian.</param>
public void WriteRegister(byte address, uint value, ByteOrder order = ByteOrder.LittleEndian)
public virtual void WriteRegister(byte address, uint value, ByteOrder order = ByteOrder.LittleEndian)
{
var bytes = BitConverter.GetBytes(value);
WriteRegister(address, bytes, order);
Expand All @@ -156,7 +156,7 @@ public void WriteRegister(byte address, uint value, ByteOrder order = ByteOrder.
/// <param name="address">Address to write the first byte to.</param>
/// <param name="value">Value to be written.</param>
/// <param name="order">Indicate if the data should be written as big or little endian.</param>
public void WriteRegister(byte address, ulong value, ByteOrder order = ByteOrder.LittleEndian)
public virtual void WriteRegister(byte address, ulong value, ByteOrder order = ByteOrder.LittleEndian)
{
var bytes = BitConverter.GetBytes(value);
WriteRegister(address, bytes, order);
Expand All @@ -168,7 +168,7 @@ public void WriteRegister(byte address, ulong value, ByteOrder order = ByteOrder
/// <param name="address">Address of the register to write to.</param>
/// <param name="writeBuffer">A buffer of byte values to be written.</param>
/// <param name="order">Indicate if the data should be written as big or little endian.</param>
public void WriteRegister(byte address, Span<byte> writeBuffer, ByteOrder order = ByteOrder.LittleEndian)
public virtual void WriteRegister(byte address, Span<byte> writeBuffer, ByteOrder order = ByteOrder.LittleEndian)
{
if (WriteBuffer.Length < writeBuffer.Length + 1)
{
Expand Down Expand Up @@ -207,7 +207,7 @@ public void WriteRegister(byte address, Span<byte> writeBuffer, ByteOrder order
/// <param name="duplex">An optional parameter that specifies the duplex type of the communication.
/// It defaults to half-duplex.</param>
/// <exception cref="ArgumentException">Thrown when duplex is set to full-duplex, as I2C only supports half-duplex communication</exception>
public void Exchange(Span<byte> writeBuffer, Span<byte> readBuffer, DuplexType duplex = DuplexType.Half)
public virtual void Exchange(Span<byte> writeBuffer, Span<byte> readBuffer, DuplexType duplex = DuplexType.Half)
{
if (duplex == DuplexType.Full) { throw new ArgumentException("I2C doesn't support full-duplex communications. Only half-duplex is available because it only has a single data line."); }

Expand Down
22 changes: 11 additions & 11 deletions Source/Meadow.Foundation.Core/Communications/SpiCommunications.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public SpiCommunications(
/// The number of bytes to be read is determined by the length of the
/// `readBuffer`.
/// </remarks>
public void Read(Span<byte> readBuffer)
public virtual void Read(Span<byte> readBuffer)
{
Bus.Read(ChipSelect, readBuffer, chipSelectMode);
}
Expand All @@ -90,7 +90,7 @@ public void Read(Span<byte> readBuffer)
/// </summary>
/// <param name="address">The register address</param>
/// <param name="readBuffer">The buffer to hold the data</param>
public void ReadRegister(byte address, Span<byte> readBuffer)
public virtual void ReadRegister(byte address, Span<byte> readBuffer)
{
WriteBuffer.Span[0] = address;
Bus.Exchange(ChipSelect, WriteBuffer.Span[0..readBuffer.Length], readBuffer, chipSelectMode);
Expand All @@ -101,7 +101,7 @@ public void ReadRegister(byte address, Span<byte> readBuffer)
/// </summary>
/// <param name="address">Address to read</param>
/// <returns>The byte read</returns>
public byte ReadRegister(byte address)
public virtual byte ReadRegister(byte address)
{
WriteBuffer.Span[0] = address;
Bus.Exchange(ChipSelect, WriteBuffer.Span[0..1], ReadBuffer.Span[0..1], chipSelectMode);
Expand All @@ -114,7 +114,7 @@ public byte ReadRegister(byte address)
/// <param name="address">Address of the read</param>
/// <param name="order">Endianness of the value read</param>
/// <returns>The value read</returns>
public ushort ReadRegisterAsUShort(byte address, ByteOrder order = ByteOrder.LittleEndian)
public virtual ushort ReadRegisterAsUShort(byte address, ByteOrder order = ByteOrder.LittleEndian)
{
ReadRegister(address, ReadBuffer[0..2].Span);
if (order == ByteOrder.LittleEndian)
Expand All @@ -141,7 +141,7 @@ public void Write(byte value)
/// Write a span of bytes to the peripheral.
/// </summary>
/// <param name="data">Data to be written.</param>
public void Write(Span<byte> data)
public virtual void Write(Span<byte> data)
{
Bus.Write(ChipSelect, data, chipSelectMode);
}
Expand All @@ -151,7 +151,7 @@ public void Write(Span<byte> data)
/// </summary>
/// <param name="address">The target write register address</param>
/// <param name="value">Value to write</param>
public void WriteRegister(byte address, byte value)
public virtual void WriteRegister(byte address, byte value)
{
WriteBuffer.Span[0] = address;
WriteBuffer.Span[1] = value;
Expand All @@ -164,7 +164,7 @@ public void WriteRegister(byte address, byte value)
/// <param name="address">The target write register address</param>
/// <param name="value">Value to write</param>
/// <param name="order">Endianness of the value to be written</param>
public void WriteRegister(byte address, ushort value, ByteOrder order = ByteOrder.LittleEndian)
public virtual void WriteRegister(byte address, ushort value, ByteOrder order = ByteOrder.LittleEndian)
{
// split the 16 bit ushort into two bytes
var bytes = BitConverter.GetBytes(value);
Expand All @@ -177,7 +177,7 @@ public void WriteRegister(byte address, ushort value, ByteOrder order = ByteOrde
/// <param name="address">Address to write the first byte to.</param>
/// <param name="value">Value to be written.</param>
/// <param name="order">Indicate if the data should be written as big or little endian.</param>
public void WriteRegister(byte address, uint value, ByteOrder order = ByteOrder.LittleEndian)
public virtual void WriteRegister(byte address, uint value, ByteOrder order = ByteOrder.LittleEndian)
{
var bytes = BitConverter.GetBytes(value);
WriteRegister(address, bytes, order);
Expand All @@ -189,7 +189,7 @@ public void WriteRegister(byte address, uint value, ByteOrder order = ByteOrder.
/// <param name="address">Address to write the first byte to.</param>
/// <param name="value">Value to be written.</param>
/// <param name="order">Indicate if the data should be written as big or little endian.</param>
public void WriteRegister(byte address, ulong value, ByteOrder order = ByteOrder.LittleEndian)
public virtual void WriteRegister(byte address, ulong value, ByteOrder order = ByteOrder.LittleEndian)
{
var bytes = BitConverter.GetBytes(value);
WriteRegister(address, bytes, order);
Expand All @@ -201,7 +201,7 @@ public void WriteRegister(byte address, ulong value, ByteOrder order = ByteOrder
/// <param name="address">Address of the register to write to.</param>
/// <param name="writeBuffer">A buffer of byte values to be written.</param>
/// <param name="order">Indicate if the data should be written as big or little endian.</param>
public void WriteRegister(byte address, Span<byte> writeBuffer, ByteOrder order = ByteOrder.LittleEndian)
public virtual void WriteRegister(byte address, Span<byte> writeBuffer, ByteOrder order = ByteOrder.LittleEndian)
{
if (WriteBuffer.Length < writeBuffer.Length + 1)
{
Expand Down Expand Up @@ -237,7 +237,7 @@ public void WriteRegister(byte address, Span<byte> writeBuffer, ByteOrder order
/// <param name="writeBuffer">The buffer holding the data to write</param>
/// <param name="readBuffer">The buffer to receieve data</param>
/// <param name="duplex">The duplex mode - half or full</param>
public void Exchange(Span<byte> writeBuffer, Span<byte> readBuffer, DuplexType duplex = DuplexType.Half)
public virtual void Exchange(Span<byte> writeBuffer, Span<byte> readBuffer, DuplexType duplex = DuplexType.Half)
{
if (Bus.Configuration.SpiMode != BusMode)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Meadow.Foundation.Sensors.Atmospheric
{
public partial class Bme280
{
internal enum Register : byte
{
ChipID = 0xd0,
Reset = 0xe0,
Humidity = 0xf2,
Status = 0xf3,
Measurement = 0xf4,
Configuration = 0xf5,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ public partial class Bme280 :
public Oversample HumiditySampleCount { get; set; } = Oversample.OversampleX8;

/// <summary>
/// Communication bus used to read and write to the BME280 sensor.
/// Communication bus used to read and write to the BME280 sensor
/// </summary>
private readonly Bme280Comms bme280Comms;
private readonly IByteCommunications bme280Comms;

/// <summary>
/// Compensation data from the sensor
Expand Down Expand Up @@ -119,8 +119,8 @@ public partial class Bme280 :
/// </summary>
public Frequency SpiBusSpeed
{
get => ((Bme280Spi)bme280Comms).spiComms.BusSpeed;
set => ((Bme280Spi)bme280Comms).spiComms.BusSpeed = value;
get => ((ISpiCommunications)bme280Comms).BusSpeed;
set => ((ISpiCommunications)bme280Comms).BusSpeed = value;
}

/// <summary>
Expand All @@ -133,8 +133,8 @@ public Frequency SpiBusSpeed
/// </summary>
public SpiClockConfiguration.Mode SpiBusMode
{
get => ((Bme280Spi)bme280Comms).spiComms.BusMode;
set => ((Bme280Spi)bme280Comms).spiComms.BusMode = value;
get => ((ISpiCommunications)bme280Comms).BusMode;
set => ((ISpiCommunications)bme280Comms).BusMode = value;
}

/// <summary>
Expand All @@ -144,7 +144,7 @@ public SpiClockConfiguration.Mode SpiBusMode
/// <param name="address">I2C address of the sensor (default = 0x77)</param>
public Bme280(II2cBus i2cBus, byte address = (byte)Addresses.Default)
{
bme280Comms = new Bme280I2C(i2cBus, address);
bme280Comms = new I2cCommunications(i2cBus, address);
configuration = new Configuration(); // here to avoid the warning
Initialize();
}
Expand All @@ -165,7 +165,7 @@ public Bme280(ISpiBus spiBus, IPin chipSelectPin) :
/// <param name="chipSelectPort">The port for the chip select pin</param>
public Bme280(ISpiBus spiBus, IDigitalOutputPort chipSelectPort)
{
bme280Comms = new Bme280Spi(spiBus, DefaultSpiBusSpeed, DefaultSpiBusMode, chipSelectPort);
bme280Comms = new SpiCommunications(spiBus, chipSelectPort, DefaultSpiBusSpeed, DefaultSpiBusMode);
configuration = new Configuration(); // here to avoid the warning
Initialize();
}
Expand Down Expand Up @@ -235,7 +235,7 @@ protected override void RaiseEventsAndNotify(IChangeResult<(Units.Temperature? T

(Units.Temperature Temperature, RelativeHumidity Humidity, Pressure Pressure) conditions;

bme280Comms.ReadRegisters(0xf7, readBuffer.Span[0..8]);
bme280Comms.ReadRegister(0xf7, readBuffer.Span[0..8]);

var adcTemperature = (readBuffer.Span[3] << 12) | (readBuffer.Span[4] << 4) | ((readBuffer.Span[5] >> 4) & 0x0f);
var tvar1 = (((adcTemperature >> 3) - (compensationData.T1 << 1)) * compensationData.T2) >> 11;
Expand Down Expand Up @@ -301,16 +301,16 @@ protected void UpdateConfiguration(Configuration configuration)
//
// Put to sleep to allow the configuration to be changed.
//
bme280Comms.WriteRegister(Bme280Comms.Register.Measurement, 0x00);
bme280Comms.WriteRegister((byte)Register.Measurement, 0x00);

var data = (byte)((((byte)configuration.Standby << 5) & 0xe0) | (((byte)configuration.Filter << 2) & 0x1c));
bme280Comms.WriteRegister(Bme280Comms.Register.Configuration, data);
bme280Comms.WriteRegister((byte)Register.Configuration, data);
data = (byte)((byte)configuration.HumidityOverSampling & 0x07);
bme280Comms.WriteRegister(Bme280Comms.Register.Humidity, data);
bme280Comms.WriteRegister((byte)Register.Humidity, data);
data = (byte)((((byte)configuration.TemperatureOverSampling << 5) & 0xe0) |
(((byte)configuration.PressureOversampling << 2) & 0x1c) |
((byte)configuration.Mode & 0x03));
bme280Comms.WriteRegister(Bme280Comms.Register.Measurement, data);
bme280Comms.WriteRegister((byte)Register.Measurement, data);
}

/// <summary>
Expand All @@ -321,7 +321,7 @@ protected void UpdateConfiguration(Configuration configuration)
/// </remarks>
public void Reset()
{
bme280Comms.WriteRegister(Bme280Comms.Register.Reset, 0xb6);
bme280Comms.WriteRegister((byte)Register.Reset, 0xb6);
UpdateConfiguration(configuration);
}

Expand All @@ -340,7 +340,7 @@ public void Reset()
protected void ReadCompensationData()
{
// read the temperature and pressure data into the internal read buffer
bme280Comms.ReadRegisters(0x88, readBuffer.Span[0..24]);
bme280Comms.ReadRegister(0x88, readBuffer.Span[0..24]);

// Temperature
compensationData.T1 = (ushort)(readBuffer.Span[0] + (readBuffer.Span[1] << 8));
Expand All @@ -361,10 +361,10 @@ protected void ReadCompensationData()
// non-sequential registers

// first one
bme280Comms.ReadRegisters(0xa1, readBuffer.Span[0..1]);
bme280Comms.ReadRegister(0xa1, readBuffer.Span[0..1]);
compensationData.H1 = readBuffer.Span[0];
// 2-6
bme280Comms.ReadRegisters(0xe1, readBuffer.Span[0..7]);
bme280Comms.ReadRegister(0xe1, readBuffer.Span[0..7]);
compensationData.H2 = (short)(readBuffer.Span[0] + (readBuffer.Span[1] << 8));
compensationData.H3 = readBuffer.Span[2];
compensationData.H4 = (short)((readBuffer.Span[3] << 4) + (readBuffer.Span[4] & 0xf));
Expand All @@ -378,7 +378,7 @@ protected void ReadCompensationData()
/// <returns></returns>
public byte GetChipID()
{
bme280Comms.ReadRegisters((byte)Bme280Comms.Register.ChipID, readBuffer.Span[0..1]);
bme280Comms.ReadRegister((byte)Register.ChipID, readBuffer.Span[0..1]);
return readBuffer.Span[0];
}

Expand Down

This file was deleted.

Loading