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

SpiPeripheral accepts SPI speed and mode #623

Merged
merged 2 commits into from
Apr 22, 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Meadow.Hardware;
using Meadow.Hardware;
using Meadow.Units;
using System;

namespace Meadow.Foundation
{
Expand Down Expand Up @@ -45,15 +46,21 @@ protected ByteCommsSensorBase(
/// </summary>
/// <param name="spiBus">SPI bus object</param>
/// <param name="chipSelect">Chip select port</param>
/// <param name="busSpeed">The SPI bus speed</param>
/// <param name="busMode">The SPI bus mode (0-3)</param>
/// <param name="readBufferSize">Read buffer size</param>
/// <param name="writeBufferSize">Write buffer size</param>
/// <param name="chipSelectMode">Chip select mode</param>
protected ByteCommsSensorBase(
ISpiBus spiBus, IDigitalOutputPort? chipSelect,
int readBufferSize = 8, int writeBufferSize = 8,
ISpiBus spiBus,
IDigitalOutputPort? chipSelect,
Frequency busSpeed,
SpiClockConfiguration.Mode busMode = SpiClockConfiguration.Mode.Mode0,
int readBufferSize = 8,
int writeBufferSize = 8,
ChipSelectMode chipSelectMode = ChipSelectMode.ActiveLow)
{
Peripheral = new SpiPeripheral(spiBus, chipSelect, readBufferSize, writeBufferSize, chipSelectMode);
Peripheral = new SpiPeripheral(spiBus, chipSelect, busSpeed, busMode, readBufferSize, writeBufferSize, chipSelectMode);
Init(readBufferSize, writeBufferSize);
}

Expand Down
60 changes: 41 additions & 19 deletions Source/Meadow.Foundation.Core/Communications/SpiPeripheral.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Meadow.Units;
using System;

namespace Meadow.Hardware
{
Expand All @@ -16,13 +17,23 @@ public class SpiPeripheral : ISpiPeripheral
/// <summary>
/// The chip select mode (active high or active low)
/// </summary>
ChipSelectMode chipSelectMode;
readonly ChipSelectMode chipSelectMode;

/// <summary>
/// the ISpiBus object
/// </summary>
public ISpiBus Bus { get; }

/// <summary>
/// SPI bus speed
/// </summary>
public Frequency BusSpeed { get; set; }

/// <summary>
/// SPI bus mode
/// </summary>
public SpiClockConfiguration.Mode BusMode { get; set; }

/// <summary>
/// Internal write buffer. Used in methods in which the buffers aren't
/// passed in.
Expand All @@ -39,18 +50,24 @@ public class SpiPeripheral : ISpiPeripheral
/// </summary>
/// <param name="bus">The spi bus connected to the peripheral</param>
/// <param name="chipSelect">The chip select port</param>
/// <param name="busSpeed">The SPI bus speed</param>
/// <param name="busMode">The SPI bus mode (0-3)</param>
/// <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(
ISpiBus bus,
IDigitalOutputPort? chipSelect,
Frequency busSpeed,
SpiClockConfiguration.Mode busMode = SpiClockConfiguration.Mode.Mode0,
int readBufferSize = 8, int writeBufferSize = 8,
ChipSelectMode csMode = ChipSelectMode.ActiveLow)
{
this.Bus = bus;
this.ChipSelect = chipSelect;
this.chipSelectMode = csMode;
Bus = bus;
BusMode = busMode;
BusSpeed = busSpeed;
ChipSelect = chipSelect;
chipSelectMode = csMode;
WriteBuffer = new byte[writeBufferSize];
ReadBuffer = new byte[readBufferSize];
}
Expand All @@ -65,7 +82,7 @@ public SpiPeripheral(
/// </remarks>
public void Read(Span<byte> readBuffer)
{
Bus.Read(this.ChipSelect, readBuffer, this.chipSelectMode);
Bus.Read(ChipSelect, readBuffer, chipSelectMode);
}

/// <summary>
Expand All @@ -76,7 +93,7 @@ public void Read(Span<byte> readBuffer)
public void ReadRegister(byte address, Span<byte> readBuffer)
{
WriteBuffer.Span[0] = address;
Bus.Exchange(this.ChipSelect, WriteBuffer.Span[0..readBuffer.Length], readBuffer, this.chipSelectMode);
Bus.Exchange(ChipSelect, WriteBuffer.Span[0..readBuffer.Length], readBuffer, chipSelectMode);
}

/// <summary>
Expand All @@ -87,7 +104,7 @@ public void ReadRegister(byte address, Span<byte> readBuffer)
public byte ReadRegister(byte address)
{
WriteBuffer.Span[0] = address;
Bus.Exchange(this.ChipSelect, WriteBuffer.Span[0..1], ReadBuffer.Span[0..1], this.chipSelectMode);
Bus.Exchange(ChipSelect, WriteBuffer.Span[0..1], ReadBuffer.Span[0..1], chipSelectMode);
return ReadBuffer.Span[0];
}

Expand Down Expand Up @@ -117,7 +134,7 @@ public ushort ReadRegisterAsUShort(byte address, ByteOrder order = ByteOrder.Lit
public void Write(byte value)
{
WriteBuffer.Span[0] = value;
Bus.Write(ChipSelect, WriteBuffer.Span[0..1], this.chipSelectMode);
Bus.Write(ChipSelect, WriteBuffer.Span[0..1], chipSelectMode);
}

/// <summary>
Expand All @@ -126,7 +143,7 @@ public void Write(byte value)
/// <param name="data">Data to be written.</param>
public void Write(Span<byte> data)
{
Bus.Write(this.ChipSelect, data, this.chipSelectMode);
Bus.Write(ChipSelect, data, chipSelectMode);
}

/// <summary>
Expand All @@ -139,7 +156,7 @@ 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], this.chipSelectMode);
Bus.Write(ChipSelect, WriteBuffer.Span[0..2], chipSelectMode);
}

/// <summary>
Expand All @@ -152,7 +169,6 @@ public void WriteRegister(byte address, ushort value, ByteOrder order = ByteOrde
{
// split the 16 bit ushort into two bytes
var bytes = BitConverter.GetBytes(value);
// call the helper method
WriteRegister(address, bytes, order);
}

Expand All @@ -166,7 +182,6 @@ public void WriteRegister(byte address, uint value, ByteOrder order = ByteOrder.
{
// split the 32 bit ushort into four bytes
var bytes = BitConverter.GetBytes(value);
// call the helper method
WriteRegister(address, bytes, order);
}

Expand All @@ -180,7 +195,6 @@ public void WriteRegister(byte address, ulong value, ByteOrder order = ByteOrder
{
// split the 64 bit ushort into eight bytes
var bytes = BitConverter.GetBytes(value);
// call the helper method
WriteRegister(address, bytes, order);
}

Expand All @@ -200,7 +214,6 @@ public void WriteRegister(byte address, Span<byte> writeBuffer, ByteOrder order
"amount of data to fix.");
}

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

// stuff the bytes into the write buffer (starting at `1` index,
Expand All @@ -221,8 +234,7 @@ public void WriteRegister(byte address, Span<byte> writeBuffer, ByteOrder order
}
break;
}
// write it
this.Bus.Write(this.ChipSelect, WriteBuffer.Span[0..(writeBuffer.Length + 1)], this.chipSelectMode);
Bus.Write(ChipSelect, WriteBuffer.Span[0..(writeBuffer.Length + 1)], chipSelectMode);
}

/// <summary>
Expand All @@ -233,6 +245,16 @@ public void WriteRegister(byte address, Span<byte> writeBuffer, ByteOrder order
/// <param name="duplex">The duplex mode - half or full</param>
public void Exchange(Span<byte> writeBuffer, Span<byte> readBuffer, DuplexType duplex = DuplexType.Half)
{
if (Bus.Configuration.SpiMode != BusMode)
{
Bus.Configuration.SetBusMode(BusMode);
}

if (Bus.Configuration.Speed != BusSpeed)
{
Bus.Configuration.Speed = BusSpeed;
}

if (duplex == DuplexType.Half)
{
// Todo: we should move this functionality deeper into the stack
Expand All @@ -253,14 +275,14 @@ public void Exchange(Span<byte> writeBuffer, Span<byte> readBuffer, DuplexType d
writeBuffer.CopyTo(txBuffer);

// write/read the data
Bus.Exchange(ChipSelect, txBuffer, rxBuffer, this.chipSelectMode);
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
{
Bus.Exchange(ChipSelect, writeBuffer, readBuffer, this.chipSelectMode);
Bus.Exchange(ChipSelect, writeBuffer, readBuffer, chipSelectMode);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ public override Task Initialize()

Resolver.Log.Info("Create Display with SPI...");

var config = new SpiClockConfiguration(Ssd1309.DefaultSpiBusSpeed, Ssd1309.DefaultSpiClockMode);

var bus = Device.CreateSpiBus(Device.Pins.SCK, Device.Pins.MOSI, Device.Pins.MISO, config);
var bus = Device.CreateSpiBus();

ssd1309 = new Ssd1309
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Meadow.Foundation.Displays.TextDisplayMenu;
using Meadow.Foundation.Graphics;
using Meadow.Foundation.Sensors.Buttons;
using Meadow.Hardware;
using Meadow.Peripherals.Displays;
using Meadow.Peripherals.Sensors.Buttons;
using System.IO;
Expand All @@ -27,9 +26,7 @@ public override Task Initialize()
{
Resolver.Log.Info("Initialize...");

var config = new SpiClockConfiguration(Ssd1309.DefaultSpiBusSpeed, Ssd1309.DefaultSpiClockMode);

var bus = Device.CreateSpiBus(Device.Pins.SCK, Device.Pins.MOSI, Device.Pins.MISO, config);
var bus = Device.CreateSpiBus();

ssd1309 = new Ssd1309
(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Meadow.Foundation.Graphics;
using Meadow.Foundation.Graphics.Buffers;
using Meadow.Hardware;
using Meadow.Units;
using System;
using System.Threading;

Expand All @@ -9,7 +10,7 @@ namespace Meadow.Foundation.Displays
/// <summary>
/// Provide an interface to the Ch1115 family of displays.
/// </summary>
public partial class Ch1115 : IGraphicsDisplay
public partial class Ch1115 : IGraphicsDisplay, ISpiDevice
{
/// <summary>
/// The display color mode - 1 bit per pixel monochrome
Expand All @@ -36,19 +37,47 @@ public partial class Ch1115 : IGraphicsDisplay
/// </summary>
public IPixelBuffer PixelBuffer => imageBuffer;

/// <summary>
/// The default SPI bus speed for the device
/// </summary>
public Frequency DefaultSpiBusSpeed => new Frequency(375, Frequency.UnitType.Kilohertz);

/// <summary>
/// The SPI bus speed for the device
/// </summary>
public Frequency SpiBusSpeed
{
get => spiPeripheral.BusSpeed;
set => spiPeripheral.BusSpeed = value;
}

/// <summary>
/// The default SPI bus mode for the device
/// </summary>
public SpiClockConfiguration.Mode DefaultSpiBusMode => SpiClockConfiguration.Mode.Mode0;

/// <summary>
/// The SPI bus mode for the device
/// </summary>
public SpiClockConfiguration.Mode SpiBusMode
{
get => spiPeripheral.BusMode;
set => spiPeripheral.BusMode = value;
}

/// <summary>
/// SPI peripheral object
/// </summary>
ISpiPeripheral spiPerihperal;
readonly ISpiPeripheral spiPeripheral;

IDigitalOutputPort dataCommandPort;
IDigitalOutputPort resetPort;
readonly IDigitalOutputPort dataCommandPort;
readonly IDigitalOutputPort resetPort;

const bool Data = true;
const bool Command = false;

Buffer1bpp imageBuffer;
byte[] pageBuffer;
readonly Buffer1bpp imageBuffer;
readonly byte[] pageBuffer;

/// <summary>
/// Create a new Ch1115 object
Expand Down Expand Up @@ -84,7 +113,7 @@ public Ch1115(ISpiBus spiBus,
this.dataCommandPort = dataCommandPort;
this.resetPort = resetPort;

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

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

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

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

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

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

Expand Down Expand Up @@ -259,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);
spiPerihperal.Write(pageBuffer);
spiPeripheral.Write(pageBuffer);
}
}

Expand Down
Loading