Giter VIP home page Giter VIP logo

system.device.spi's Introduction

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

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);

If you'll be controlling the state of the Chip Select line, you need to pass -1 to the second parameter of SpiConnectionSettings(...). When specifying a GPIO number the driver takes care of this for you by setting the appropriate state for the signal during the SPI transactions.

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.MaxClockFrequency)}: {spiBusInfo.MaxClockFrequency}");
Debug.WriteLine($"{nameof(spiBusInfo.MinClockFrequency)}: {spiBusInfo.MinClockFrequency}");

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 behaviour in our community. For more information see the .NET Foundation Code of Conduct.

.NET Foundation

This project is supported by the .NET Foundation.

system.device.spi's People

Contributors

ellerbach avatar github-actions[bot] avatar josesimoes avatar networkfusion avatar nfbot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.