55f3cfcc0c | ||
---|---|---|
.github/workflows | ||
System.Device.Spi | ||
Test/SpiHardwareUnitTests | ||
assets | ||
config | ||
.github_changelog_generator | ||
.gitignore | ||
CHANGELOG.md | ||
LICENSE.md | ||
NuGet.Config | ||
README.md | ||
System.Device.Spi.sln | ||
azure-pipelines.yml | ||
nanoFramework.System.Device.Spi.nuspec | ||
template.vssettings | ||
version.json |
README.md
Welcome to the .NET nanoFramework System.Device.Spi Library repository!
Build status
Component | Build Status | NuGet Package |
---|---|---|
System.Device.Spi | ||
System.Device.Spi (preview) |
Usage
To create a SpiDevice, you need to follow this pattern:
SpiDevice spiDevice;
SpiConnectionSettings connectionSettings;
// Note: the ChipSelect pin should be adjusted to your device, here 12
// You can adjust as well the bus, here 1 for SPI1
connectionSettings = new SpiConnectionSettings(1, 12);
spiDevice = SpiDevice.Create(connectionSettings);
SpiConnectionSettings and SpiBusInfo
The SpiConnectionSettings
contains the key elements needed to setup properly the hardware SPI device. Once created, those elements can't be adjusted.
connectionSettings.ChipSelectLineActiveState = PinValue.High; // Default is active Low
connectionSettings.ClockFrequency = 1_000_000;
connectionSettings.DataFlow = DataFlow.LsbFirst; // Default is MSB
To get the default bus values, especially the bus min and max frequencies, the maximum number of ChipSelect, you can use SpiBusInfo
.
SpiBusInfo spiBusInfo = SpiDevice.GetBusInfo(1);
Debug.WriteLine($"{nameof(spiBusInfo.ChipSelectLineCount)}: {spiBusInfo.ChipSelectLineCount}");
Debug.WriteLine($"{nameof(spiBusInfo.MaxClockFrequency)}: {spiBusInfo.MaxClockFrequency}");
Debug.WriteLine($"{nameof(spiBusInfo.MinClockFrequency)}: {spiBusInfo.MinClockFrequency}");
Debug.WriteLine($"{nameof(spiBusInfo.SupportedDataBitLengths)}: ");
foreach(var data in spiBusInfo.SupportedDataBitLengths)
{
Debug.WriteLine($" {data}");
}
Reading and Writing
You can read, write and do a full transfer. You have the opportunity to use either a SpanByte
, either a ushort
array.
Important: in both cases, the data bit length will be automatically adjusted. So you can use both SpanByte
and ushort
array at the same time.
You can write a SpanByte
like this:
SpanByte writeBuffer = new byte[2] { 42, 84 };
spiDevice.Write(writeBuffer);
You can write a ushort
array like this:
ushort[] writeBuffer = new ushort[2] { 4200, 8432 };
spiDevice.Write(writeBuffer);
You can as well write a single byte:
spiDevice.WriteByte(42);
Read operations are similar:
SpanByte readBuffer = new byte[2];
// This will read 2 bytes
spiDevice.Read(readBuffer);
ushort[] readUshort = new ushort[4];
// This will read 4 ushort
spiDevice.Read(readUshort);
// read 1 byte
byte readMe = spiDevice.ReadByte();
For full transfer, you need to have 2 arrays of the same size and perform a full duplex transfer:
SpanByte writeBuffer = new byte[4] { 0xAA, 0xBB, 0xCC, 0x42 };
SpanByte readBuffer = new byte[4];
spiDevice.TransferFullDuplex(writeBuffer, readBuffer);
// Same for ushirt arrays:
ushort[] writeBuffer = new ushort[4] { 0xAABC, 0x00BB, 0xCC00, 0x4242 };
ushort[] readBuffer = new ushort[4];
spiDevice.TransferFullDuplex(writeBuffer, readBuffer);
Feedback and documentation
For documentation, providing feedback, issues and finding out how to contribute please refer to the Home repo.
Join our Discord community here.
Credits
The list of contributors to this project can be found at CONTRIBUTORS.
License
The nanoFramework Class Libraries are licensed under the MIT license.
Code of Conduct
This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behavior in our community. For more information see the .NET Foundation Code of Conduct.
.NET Foundation
This project is supported by the .NET Foundation.