πŸ“¦ System.Device.Spi library for .NET nanoFramework.
ΠŸΠ΅Ρ€Π΅ΠΉΡ‚ΠΈ ΠΊ Ρ„Π°ΠΉΠ»Ρƒ
github-actions[bot] 55f3cfcc0c
Update 1 nuget dependencies
2021-05-25 01:10:35 +01:00
.github/workflows Work CI-CD 2021-05-04 10:52:50 +01:00
System.Device.Spi Update 3 nuget dependencies 2021-05-20 01:09:38 +01:00
Test/SpiHardwareUnitTests Update 1 nuget dependencies 2021-05-25 01:10:35 +01:00
assets initial commit 2021-03-22 21:51:15 +01:00
config Work CI-CD 2021-04-29 12:13:30 +01:00
.github_changelog_generator Fix repo name 2021-04-29 11:05:47 +01:00
.gitignore initial commit 2021-03-22 21:51:15 +01:00
CHANGELOG.md Update CHANGELOG for v1.0.0 2021-05-20 00:15:51 +00:00
LICENSE.md Rename license file 2021-04-29 11:05:31 +01:00
NuGet.Config initial commit 2021-03-22 21:51:15 +01:00
README.md Fix URLs and badges in readme 2021-04-29 11:33:04 +01:00
System.Device.Spi.sln Update 1 nuget dependencies 2021-05-06 01:47:28 +01:00
azure-pipelines.yml Work CI-CD 2021-04-29 12:05:38 +01:00
nanoFramework.System.Device.Spi.nuspec Update 3 NuGet dependencies 2021-05-19 12:06:09 +01:00
template.vssettings initial commit 2021-03-22 21:51:15 +01:00
version.json Update version.json 2021-04-13 00:23:43 +01:00

README.md

Quality Gate Status Reliability Rating License NuGet #yourfirstpr Discord

nanoFramework logo


Welcome to the .NET nanoFramework System.Device.Spi Library repository!

Build status

Component Build Status NuGet Package
System.Device.Spi Build Status NuGet
System.Device.Spi (preview) Build Status NuGet

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.