This repository contains the .NET nanoFramework System.Device.I2c.Slave class library.
Component | Build Status | NuGet Package |
---|---|---|
System.Device.I2c.Slave |
To instantiate a new I2C slave device call the constructor passing the device address in the parameter and the I2C hardware bus where this device will be exposed from. Like this:
// create an I2C slave device on bus 1 with address 0x10
var device = new I2cSlaveDevice(1, 0x10);
A common operation on an I2C device is to read the content of a specified address. The following code snippet reads a byte with the "register address" and returns the content as an array of two bytes. For simplicity no timeouts will be specified. Be aware that there are overloaded methods on the API that accept a timeout parameter in milliseconds.
byte registerAddress;
if(device.ReadByte(out registerAddress))
{
switch(registerAddress)
{
// (...)
// return dummy content for register 0x22
case 0x22:
device.Write(new byte[] { 0xBE, 0xEF});
break;
// (...)
}
}
From the I2C master end, assuming that's a .NET nanoFramework device, using the System.Device.I2c library, the code to perform the above operation on a slave device on I2C bus 1 would be like this:
// create I2C device
var myI2cDevice = I2cDevice.Create(new I2cConnectionSettings(1, 0x10, I2cBusSpeed.FastMode));
// setup read buffer
var buffer = new byte[2];
// set address to read from
myI2cDevice.Write(new byte[] { 0x22 });
myI2cDevice.Read(buffer);
// expected buffer content is: 0xBE, 0xEF
Console.Writeline($"Register content: {buffer[0]:X2} {buffer[1]:X2}")
For documentation, providing feedback, issues and finding out how to contribute please refer to the Home repo.
Join our Discord community here.
The list of contributors to this project can be found at CONTRIBUTORS.
The nanoFramework Class Libraries are licensed under the MIT license.
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.
This project is supported by the .NET Foundation.