Skip to content

Commit

Permalink
Merge pull request #632 from WildernessLabs/peripheral-to-comms
Browse files Browse the repository at this point in the history
Rename bus peripheral classes, fields and references to communications
  • Loading branch information
adrianstevens authored Apr 24, 2023
2 parents 9650c96 + c03ff51 commit 3b14508
Show file tree
Hide file tree
Showing 106 changed files with 1,241 additions and 1,205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public abstract class ByteCommsSensorBase<UNIT> :
PollingSensorBase<UNIT>, IDisposable where UNIT : struct
{
/// <summary>
/// Peripheral object, i.e. an I2CPeripheral or SpiPeripheral
/// Bus communications object, i.e. an I2cCommunications or SpiCommunications
/// </summary>
protected IByteCommunications? Peripheral { get; set; }
protected IByteCommunications? BusComms { get; set; }

/// <summary>
/// The read buffer
Expand All @@ -37,7 +37,7 @@ protected ByteCommsSensorBase(
II2cBus i2cBus, byte address,
int readBufferSize = 8, int writeBufferSize = 8)
{
Peripheral = new I2cPeripheral(i2cBus, address, readBufferSize, writeBufferSize);
BusComms = new I2cCommunications(i2cBus, address, readBufferSize, writeBufferSize);
Init(readBufferSize, writeBufferSize);
}

Expand All @@ -60,7 +60,7 @@ protected ByteCommsSensorBase(
int writeBufferSize = 8,
ChipSelectMode chipSelectMode = ChipSelectMode.ActiveLow)
{
Peripheral = new SpiPeripheral(spiBus, chipSelect, busSpeed, busMode, readBufferSize, writeBufferSize, chipSelectMode);
BusComms = new SpiCommunications(spiBus, chipSelect, busSpeed, busMode, readBufferSize, writeBufferSize, chipSelectMode);
Init(readBufferSize, writeBufferSize);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
namespace Meadow.Hardware
{
/// <summary>
/// Defines a contract for a peripheral that communicates via the IIC/I2C
/// protocol.
/// Helper class for I2C communications, handles registers, endian, etc.
/// </summary>
public class I2cPeripheral : II2cPeripheral
public class I2cCommunications : II2cCommunications
{
/// <summary>
/// The I2C address
Expand All @@ -19,24 +18,24 @@ public class I2cPeripheral : II2cPeripheral
public II2cBus Bus { get; protected set; }

/// <summary>
/// Internal write buffer. Used in methods in which the buffers aren't
/// Internal write buffer - used in methods in which the buffers aren't
/// passed in.
/// </summary>
protected Memory<byte> WriteBuffer { get; }
/// <summary>
/// Internal read buffer. Used in methods in which the buffers aren't
/// Internal read buffer - used in methods in which the buffers aren't
/// passed in.
/// </summary>
protected Memory<byte> ReadBuffer { get; }

/// <summary>
/// Initializes a new instance of the I2cPeripheral class
/// Initializes a new instance of the I2cCommunications class
/// </summary>
/// <param name="bus">The II2cBus used for communication with the peripheral</param>
/// <param name="peripheralAddress">The address of the peripheral on the I2C bus</param>
/// <param name="readBufferSize">The size of the buffer used for reading data from the peripheral. Defaults to 8 bytes</param>
/// <param name="writeBufferSize">The size of the buffer used for writing data to the peripheral. Defaults to 8 bytes</param>
public I2cPeripheral(II2cBus bus, byte peripheralAddress, int readBufferSize = 8, int writeBufferSize = 8)
public I2cCommunications(II2cBus bus, byte peripheralAddress, int readBufferSize = 8, int writeBufferSize = 8)
{
Bus = bus;
Address = peripheralAddress;
Expand Down Expand Up @@ -122,7 +121,6 @@ public void Write(byte value)
/// <param name="value">Data to write into the register.</param>
public void WriteRegister(byte address, byte value)
{
// stuff the address and value into the write buffer
WriteBuffer.Span[0] = address;
WriteBuffer.Span[1] = value;
Bus.Write(Address, WriteBuffer.Span[0..2]);
Expand All @@ -136,9 +134,7 @@ public void WriteRegister(byte address, byte value)
/// <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)
{
// split the 16 bit ushort into two bytes
var bytes = BitConverter.GetBytes(value);
// call the helper method
WriteRegister(address, bytes, order);
}

Expand All @@ -150,9 +146,7 @@ public void WriteRegister(byte address, ushort value, ByteOrder order = ByteOrde
/// <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)
{
// split the 32 bit uint into four bytes
var bytes = BitConverter.GetBytes(value);
// call the helper method
WriteRegister(address, bytes, order);
}

Expand All @@ -164,9 +158,7 @@ public void WriteRegister(byte address, uint value, ByteOrder order = ByteOrder.
/// <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)
{
// split the 64 bit ulong into 8 bytes
var bytes = BitConverter.GetBytes(value);
// call the helper method
WriteRegister(address, bytes, order);
}

Expand All @@ -186,11 +178,9 @@ public void WriteRegister(byte address, Span<byte> writeBuffer, ByteOrder order
"amount of data to fix.");
}

// stuff the register address into the write buffer
// add the register address to the start of the write buffer
WriteBuffer.Span[0] = address;

// stuff the bytes into the write buffer (starting at `1` index,
// because `0` is the register address.
switch (order)
{
case ByteOrder.LittleEndian:
Expand All @@ -202,12 +192,10 @@ public void WriteRegister(byte address, Span<byte> writeBuffer, ByteOrder order
case ByteOrder.BigEndian:
for (int i = 0; i < writeBuffer.Length; i++)
{
// stuff them backwards
WriteBuffer.Span[i + 1] = writeBuffer[writeBuffer.Length - (i + 1)];
}
break;
}
// write it
Bus.Write(Address, WriteBuffer.Span[0..(writeBuffer.Length + 1)]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
namespace Meadow.Hardware
{
/// <summary>
/// Represents an SPI peripheral object
/// Helper class for SPI communications, handles registers, endian, etc.
/// This encapsulates and synchronizes the SPI bus and chip select ports
/// </summary>
public class SpiPeripheral : ISpiPeripheral
public class SpiCommunications : ISpiCommunications
{
/// <summary>
/// The SPI chip select port
Expand Down Expand Up @@ -46,7 +46,7 @@ public class SpiPeripheral : ISpiPeripheral
protected Memory<byte> ReadBuffer { get; }

/// <summary>
/// Creates a new SpiPeripheral instance
/// Creates a new SpiCommunications instance
/// </summary>
/// <param name="bus">The spi bus connected to the peripheral</param>
/// <param name="chipSelect">The chip select port</param>
Expand All @@ -55,7 +55,7 @@ public class SpiPeripheral : ISpiPeripheral
/// <param name="readBufferSize">The size of the read buffer in bytes</param>
/// <param name="writeBufferSize">The size of the write buffer in bytes</param>
/// <param name="csMode">The chip select mode, active high or active low</param>
public SpiPeripheral(
public SpiCommunications(
ISpiBus bus,
IDigitalOutputPort? chipSelect,
Frequency busSpeed,
Expand Down Expand Up @@ -153,7 +153,6 @@ public void Write(Span<byte> data)
/// <param name="value">Value to write</param>
public void WriteRegister(byte address, byte value)
{
// stuff the address and value into the write buffer
WriteBuffer.Span[0] = address;
WriteBuffer.Span[1] = value;
Bus.Write(ChipSelect, WriteBuffer.Span[0..2], chipSelectMode);
Expand All @@ -180,7 +179,6 @@ public void WriteRegister(byte address, ushort value, ByteOrder order = ByteOrde
/// <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)
{
// split the 32 bit ushort into four bytes
var bytes = BitConverter.GetBytes(value);
WriteRegister(address, bytes, order);
}
Expand All @@ -193,7 +191,6 @@ public void WriteRegister(byte address, uint value, ByteOrder order = ByteOrder.
/// <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)
{
// split the 64 bit ushort into eight bytes
var bytes = BitConverter.GetBytes(value);
WriteRegister(address, bytes, order);
}
Expand All @@ -216,8 +213,6 @@ public void WriteRegister(byte address, Span<byte> writeBuffer, ByteOrder order

WriteBuffer.Span[0] = address;

// stuff the bytes into the write buffer (starting at `1` index,
// because `0` is the register address.
switch (order)
{
case ByteOrder.LittleEndian:
Expand All @@ -229,7 +224,6 @@ public void WriteRegister(byte address, Span<byte> writeBuffer, ByteOrder order
case ByteOrder.BigEndian:
for (int i = 0; i < writeBuffer.Length; i++)
{
// stuff them backwards
WriteBuffer.Span[i + 1] = writeBuffer[writeBuffer.Length - (i + 1)];
}
break;
Expand Down Expand Up @@ -257,27 +251,14 @@ public void Exchange(Span<byte> writeBuffer, Span<byte> readBuffer, DuplexType d

if (duplex == DuplexType.Half)
{
// Todo: we should move this functionality deeper into the stack
// and have nuttx write the write buffer, then continue clocking out
// 0x00's until it's hit writeBuffer.Length + readBuffer.Lenght
// and ignore the input until it hits writeBuffer.Length, and then
// start writing directly into the readBuffer starting at 0.
// that will prevent all the allocations and copying we're doing
// here.

// clock in and clock out data means that the buffers have to be as
// long as both tx and rx together
int length = writeBuffer.Length + readBuffer.Length;
Span<byte> txBuffer = stackalloc byte[length];
Span<byte> rxBuffer = stackalloc byte[length];

// copy the write into tx
writeBuffer.CopyTo(txBuffer);

// write/read the data
Bus.Exchange(ChipSelect, txBuffer, rxBuffer, chipSelectMode);

// move the rx data into the read buffer, starting it at zero
rxBuffer[writeBuffer.Length..length].CopyTo(readBuffer);
}
else
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

using System;
using System.Threading;
using Meadow.Hardware;
using Meadow.Units;
using System;
using System.Threading;

namespace Meadow.Foundation.Audio.Radio
{
Expand All @@ -12,9 +12,9 @@ namespace Meadow.Foundation.Audio.Radio
public partial class Tea5767
{
/// <summary>
/// TEA5767 radio.
/// I2C Communication bus used to communicate with the peripheral
/// </summary>
private readonly II2cPeripheral i2cPeripheral;
protected readonly II2cCommunications i2cComms;

byte hiInjection;
readonly Memory<byte> writeBuffer = new byte[5];
Expand All @@ -32,7 +32,7 @@ public partial class Tea5767
/// <param name="address">Address of the bus on the I2C display.</param>
public Tea5767(II2cBus i2cBus, byte address = (byte)Address.Default)
{
i2cPeripheral = new I2cPeripheral(i2cBus, address);
i2cComms = new I2cCommunications(i2cBus, address);

InitTEA5767();
}
Expand Down Expand Up @@ -111,7 +111,7 @@ void SetFrequency(double frequency)

void TransmitData()
{
i2cPeripheral.Exchange(writeBuffer.Span, readBuffer.Span);
i2cComms.Exchange(writeBuffer.Span, readBuffer.Span);

Thread.Sleep(100);
}
Expand Down Expand Up @@ -155,7 +155,7 @@ public void SelectFrequency(Frequency frequency)

void ReadStatus()
{
i2cPeripheral.Read(readBuffer.Span);
i2cComms.Read(readBuffer.Span);
Thread.Sleep(100);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public partial class Ch1115 : IGraphicsDisplay, ISpiDevice
/// </summary>
public Frequency SpiBusSpeed
{
get => spiPeripheral.BusSpeed;
set => spiPeripheral.BusSpeed = value;
get => spiComms.BusSpeed;
set => spiComms.BusSpeed = value;
}

/// <summary>
Expand All @@ -61,14 +61,14 @@ public Frequency SpiBusSpeed
/// </summary>
public SpiClockConfiguration.Mode SpiBusMode
{
get => spiPeripheral.BusMode;
set => spiPeripheral.BusMode = value;
get => spiComms.BusMode;
set => spiComms.BusMode = value;
}

/// <summary>
/// SPI peripheral object
/// SPI Communication bus used to communicate with the peripheral
/// </summary>
readonly ISpiPeripheral spiPeripheral;
protected readonly ISpiCommunications spiComms;

readonly IDigitalOutputPort dataCommandPort;
readonly IDigitalOutputPort resetPort;
Expand Down Expand Up @@ -113,7 +113,7 @@ public Ch1115(ISpiBus spiBus,
this.dataCommandPort = dataCommandPort;
this.resetPort = resetPort;

spiPeripheral = new SpiPeripheral(spiBus, chipSelectPort, DefaultSpiBusSpeed, DefaultSpiBusMode);
spiComms = new SpiCommunications(spiBus, chipSelectPort, DefaultSpiBusSpeed, DefaultSpiBusMode);

imageBuffer = new Buffer1bpp(width, height);
pageBuffer = new byte[PageSize];
Expand Down Expand Up @@ -223,7 +223,7 @@ public void SetContrast(byte contrast)
private void SendCommand(byte command)
{
dataCommandPort.State = Command;
spiPeripheral.Write(command);
spiComms.Write(command);
}

/// <summary>
Expand All @@ -237,7 +237,7 @@ private void SendCommands(byte[] commands)
Array.Copy(commands, 0, data, 1, commands.Length);

dataCommandPort.State = Command;
spiPeripheral.Write(commands);
spiComms.Write(commands);
}

const int StartColumnOffset = 0;
Expand All @@ -257,7 +257,7 @@ public void Show()
dataCommandPort.State = Data;

Array.Copy(imageBuffer.Buffer, Width * page, pageBuffer, 0, PageSize);
spiPeripheral.Write(pageBuffer);
spiComms.Write(pageBuffer);
}
}

Expand Down Expand Up @@ -288,7 +288,7 @@ public void Show(int left, int top, int right, int bottom)
dataCommandPort.State = Data;

Array.Copy(imageBuffer.Buffer, Width * page, pageBuffer, 0, PageSize);
spiPeripheral.Write(pageBuffer);
spiComms.Write(pageBuffer);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected override void Initialize()
protected override void Command(byte value)
{
var data = new byte[] { 0x80, value };
i2cPeripheral.Write(data);
i2cComms.Write(data);
}

/// <summary>
Expand All @@ -77,7 +77,7 @@ protected override void Command(byte value)
protected override void Send(byte value, byte mode)
{
var data = new byte[] { 0x40, value };
i2cPeripheral.Write(data);
i2cComms.Write(data);
}
}
}
Loading

0 comments on commit 3b14508

Please sign in to comment.