Skip to content

Commit 7aac5f6

Browse files
authored
Add I2C slave sample (#373)
1 parent ce7fec8 commit 7aac5f6

11 files changed

Lines changed: 335 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Label="Globals">
4+
<NanoFrameworkProjectSystemPath>$(MSBuildExtensionsPath)\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
5+
</PropertyGroup>
6+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" />
7+
<PropertyGroup>
8+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
9+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
10+
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
11+
<ProjectGuid>ae6817bf-dd2a-4883-aba7-0a90751304b7</ProjectGuid>
12+
<OutputType>Exe</OutputType>
13+
<AppDesignerFolder>Properties</AppDesignerFolder>
14+
<FileAlignment>512</FileAlignment>
15+
<RootNamespace>Samples.I2c.MasterDevice</RootNamespace>
16+
<AssemblyName>I2cMasterDevice</AssemblyName>
17+
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
18+
</PropertyGroup>
19+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
20+
<ItemGroup>
21+
<Compile Include="Program.cs" />
22+
<Compile Include="Properties\AssemblyInfo.cs" />
23+
</ItemGroup>
24+
<ItemGroup>
25+
<Reference Include="mscorlib">
26+
<HintPath>..\packages\nanoFramework.CoreLibrary.1.15.5\lib\mscorlib.dll</HintPath>
27+
</Reference>
28+
<Reference Include="nanoFramework.Hardware.Esp32">
29+
<HintPath>..\packages\nanoFramework.Hardware.Esp32.1.6.19\lib\nanoFramework.Hardware.Esp32.dll</HintPath>
30+
</Reference>
31+
<Reference Include="nanoFramework.Runtime.Events">
32+
<HintPath>..\packages\nanoFramework.Runtime.Events.1.11.18\lib\nanoFramework.Runtime.Events.dll</HintPath>
33+
</Reference>
34+
<Reference Include="System.Device.I2c">
35+
<HintPath>..\packages\nanoFramework.System.Device.I2c.1.1.16\lib\System.Device.I2c.dll</HintPath>
36+
</Reference>
37+
</ItemGroup>
38+
<ItemGroup>
39+
<None Include="packages.config" />
40+
</ItemGroup>
41+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
42+
<ProjectExtensions>
43+
<ProjectCapabilities>
44+
<ProjectConfigurationsDeclaredAsItems />
45+
</ProjectCapabilities>
46+
</ProjectExtensions>
47+
</Project>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
using System;
7+
using System.Device.I2c;
8+
using System.Diagnostics;
9+
using System.Threading;
10+
using nanoFramework.Hardware.Esp32;
11+
12+
namespace Samples.I2c.MasterDevice
13+
{
14+
public class Program
15+
{
16+
public static void Main()
17+
{
18+
// Configure I2C GPIO pins
19+
Configuration.SetPinFunction(Gpio.IO18, DeviceFunction.I2C1_DATA);
20+
Configuration.SetPinFunction(Gpio.IO19, DeviceFunction.I2C1_CLOCK);
21+
22+
// create I2C device
23+
var myI2cDevice = I2cDevice.Create(new I2cConnectionSettings(
24+
1,
25+
0x10,
26+
I2cBusSpeed.FastMode));
27+
28+
// setup read buffer
29+
var buffer = new byte[2];
30+
31+
while (true)
32+
{
33+
try
34+
{
35+
// set address to read from
36+
if (myI2cDevice.Write(new byte[] { 0x22 }).BytesTransferred != 1)
37+
{
38+
Debug.WriteLine("Error writting to I2C device to set register address to read from");
39+
}
40+
else
41+
{
42+
if (myI2cDevice.Read(buffer).BytesTransferred != 2)
43+
{
44+
Debug.WriteLine("Error reading from I2C device");
45+
}
46+
else
47+
{
48+
49+
// expected buffer content is: 0xBE, 0xEF
50+
Debug.WriteLine($"Register content: {buffer[0]:X2} {buffer[1]:X2}");
51+
}
52+
}
53+
54+
// pause before a new read
55+
Thread.Sleep(1000);
56+
}
57+
catch (Exception ex)
58+
{
59+
Debug.WriteLine($"Error reading from I2C device: {ex.Message}");
60+
}
61+
}
62+
}
63+
}
64+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
using System.Reflection;
7+
using System.Runtime.InteropServices;
8+
9+
// General Information about an assembly is controlled through the following
10+
// set of attributes. Change these attribute values to modify the information
11+
// associated with an assembly.
12+
[assembly: AssemblyTitle("CSharp.I2cMasterDevice")]
13+
14+
// Setting ComVisible to false makes the types in this assembly not visible
15+
// to COM components. If you need to access a type in this assembly from
16+
// COM, set the ComVisible attribute to true on that type.
17+
[assembly: ComVisible(false)]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="nanoFramework.CoreLibrary" version="1.15.5" targetFramework="netnano1.0" />
4+
<package id="nanoFramework.Hardware.Esp32" version="1.6.19" targetFramework="netnano1.0" />
5+
<package id="nanoFramework.Runtime.Events" version="1.11.18" targetFramework="netnano1.0" />
6+
<package id="nanoFramework.System.Device.I2c" version="1.1.16" targetFramework="netnano1.0" />
7+
</packages>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.10.34928.147
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{1F0B41A0-B2F5-4986-95D9-8D91623B69C9}"
7+
ProjectSection(SolutionItems) = preProject
8+
README.md = README.md
9+
EndProjectSection
10+
EndProject
11+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "I2cMasterDevice", "I2cMasterDevice\I2cMasterDevice.nfproj", "{AE6817BF-DD2A-4883-ABA7-0A90751304B7}"
12+
EndProject
13+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "I2cSlaveDevice", "I2cSlaveDevice\I2cSlaveDevice.nfproj", "{EDFF1C28-7988-4360-A0EF-2008E57945C6}"
14+
EndProject
15+
Global
16+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
17+
Debug|Any CPU = Debug|Any CPU
18+
Release|Any CPU = Release|Any CPU
19+
EndGlobalSection
20+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
21+
{AE6817BF-DD2A-4883-ABA7-0A90751304B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22+
{AE6817BF-DD2A-4883-ABA7-0A90751304B7}.Debug|Any CPU.Build.0 = Debug|Any CPU
23+
{AE6817BF-DD2A-4883-ABA7-0A90751304B7}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
24+
{AE6817BF-DD2A-4883-ABA7-0A90751304B7}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{AE6817BF-DD2A-4883-ABA7-0A90751304B7}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{AE6817BF-DD2A-4883-ABA7-0A90751304B7}.Release|Any CPU.Deploy.0 = Release|Any CPU
27+
{EDFF1C28-7988-4360-A0EF-2008E57945C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
28+
{EDFF1C28-7988-4360-A0EF-2008E57945C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
29+
{EDFF1C28-7988-4360-A0EF-2008E57945C6}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
30+
{EDFF1C28-7988-4360-A0EF-2008E57945C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
31+
{EDFF1C28-7988-4360-A0EF-2008E57945C6}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{EDFF1C28-7988-4360-A0EF-2008E57945C6}.Release|Any CPU.Deploy.0 = Release|Any CPU
33+
EndGlobalSection
34+
GlobalSection(SolutionProperties) = preSolution
35+
HideSolutionNode = FALSE
36+
EndGlobalSection
37+
GlobalSection(ExtensibilityGlobals) = postSolution
38+
SolutionGuid = {5834D1F4-7FFC-4363-AA2D-8AA473993BCF}
39+
EndGlobalSection
40+
EndGlobal
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Label="Globals">
4+
<NanoFrameworkProjectSystemPath>$(MSBuildExtensionsPath)\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
5+
</PropertyGroup>
6+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" />
7+
<PropertyGroup>
8+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
9+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
10+
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
11+
<ProjectGuid>edff1c28-7988-4360-a0ef-2008e57945c6</ProjectGuid>
12+
<OutputType>Exe</OutputType>
13+
<AppDesignerFolder>Properties</AppDesignerFolder>
14+
<FileAlignment>512</FileAlignment>
15+
<RootNamespace>Samples.I2c.SlaveDevice</RootNamespace>
16+
<AssemblyName>I2cSlaveDevice</AssemblyName>
17+
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
18+
</PropertyGroup>
19+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
20+
<ItemGroup>
21+
<Compile Include="Program.cs" />
22+
<Compile Include="Properties\AssemblyInfo.cs" />
23+
</ItemGroup>
24+
<ItemGroup>
25+
<Reference Include="mscorlib">
26+
<HintPath>..\packages\nanoFramework.CoreLibrary.1.15.5\lib\mscorlib.dll</HintPath>
27+
</Reference>
28+
<Reference Include="nanoFramework.Hardware.Esp32">
29+
<HintPath>..\packages\nanoFramework.Hardware.Esp32.1.6.19\lib\nanoFramework.Hardware.Esp32.dll</HintPath>
30+
</Reference>
31+
<Reference Include="nanoFramework.Runtime.Events">
32+
<HintPath>..\packages\nanoFramework.Runtime.Events.1.11.18\lib\nanoFramework.Runtime.Events.dll</HintPath>
33+
</Reference>
34+
<Reference Include="System.Device.I2c.Slave">
35+
<HintPath>..\packages\nanoFramework.System.Device.I2c.Slave.1.0.10\lib\System.Device.I2c.Slave.dll</HintPath>
36+
</Reference>
37+
</ItemGroup>
38+
<ItemGroup>
39+
<None Include="packages.config" />
40+
</ItemGroup>
41+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
42+
<ProjectExtensions>
43+
<ProjectCapabilities>
44+
<ProjectConfigurationsDeclaredAsItems />
45+
</ProjectCapabilities>
46+
</ProjectExtensions>
47+
</Project>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
using System;
7+
using System.Device.I2c;
8+
using System.Diagnostics;
9+
using nanoFramework.Hardware.Esp32;
10+
11+
namespace Samples.I2c.SlaveDevice
12+
{
13+
public class Program
14+
{
15+
public static void Main()
16+
{
17+
Configuration.SetPinFunction(Gpio.IO18, DeviceFunction.I2C1_DATA);
18+
Configuration.SetPinFunction(Gpio.IO19, DeviceFunction.I2C1_CLOCK);
19+
20+
// create an I2C slave device on bus 1 with address 0x10
21+
var device = new I2cSlaveDevice(1, 0x10);
22+
23+
while (true)
24+
{
25+
// wait "forever" for a single byte
26+
try
27+
{
28+
if (device.ReadByte(out byte registerAddress, 500))
29+
{
30+
switch (registerAddress)
31+
{
32+
case 0x22:
33+
// reply back with 2 dummy bytes
34+
device.Write(new byte[] { 0xBE, 0xEF });
35+
36+
Debug.WriteLine($"Received message: {registerAddress:X2}");
37+
38+
break;
39+
}
40+
}
41+
}
42+
catch (Exception ex)
43+
{
44+
Debug.WriteLine("Timeout I2C device");
45+
}
46+
}
47+
}
48+
}
49+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
using System.Reflection;
7+
using System.Runtime.InteropServices;
8+
9+
// General Information about an assembly is controlled through the following
10+
// set of attributes. Change these attribute values to modify the information
11+
// associated with an assembly.
12+
[assembly: AssemblyTitle("CSharp.I2cSlaveDevice")]
13+
14+
// Setting ComVisible to false makes the types in this assembly not visible
15+
// to COM components. If you need to access a type in this assembly from
16+
// COM, set the ComVisible attribute to true on that type.
17+
[assembly: ComVisible(false)]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="nanoFramework.CoreLibrary" version="1.15.5" targetFramework="netnano1.0" />
4+
<package id="nanoFramework.Hardware.Esp32" version="1.6.19" targetFramework="netnano1.0" />
5+
<package id="nanoFramework.Runtime.Events" version="1.11.18" targetFramework="netnano1.0" />
6+
<package id="nanoFramework.System.Device.I2c.Slave" version="1.0.10" targetFramework="netnano1.0" />
7+
</packages>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# 🌶️🌶️ - I2C Master Slave sample
2+
3+
Shows how to use the [System.Device.I2c](http://docs.nanoframework.net/api/System.Device.I2c.html) [System.Device.I2c.Slave](http://docs.nanoframework.net/api/System.Device.I2c.Slave.html) APIs to connect two I2C devices.
4+
5+
## Hardware requirements
6+
7+
Two ESP32 devices board.
8+
The code sample is demonstrative of the use of the I2C and I2C slave APIs.
9+
10+
## Related topics
11+
12+
### Reference
13+
14+
- [System.Device.I2c](http://docs.nanoframework.net/api/System.Device.I2c.html)
15+
- [System.Device.I2c.Slave](http://docs.nanoframework.net/api/System.Device.I2c.Slave.html)
16+
17+
## Build the sample
18+
19+
1. Start Microsoft Visual Studio 2022 or Visual Studio 2019 (Visual Studio 2017 should be OK too) and select `File > Open > Project/Solution`.
20+
1. Starting in the folder where you unzipped the samples/cloned the repository, go to the subfolder for this specific sample. Double-click the Visual Studio Solution (.sln) file.
21+
1. Press `Ctrl+Shift+B`, or select `Build > Build Solution`.
22+
23+
## Run the sample
24+
25+
The next steps depend on whether you just want to deploy the sample or you want to both deploy and run it.
26+
27+
### Deploying the sample
28+
29+
- Select `Build > Deploy Solution`.
30+
31+
### Deploying and running the sample
32+
33+
- To debug the sample and then run it, press F5 or select `Debug > Start Debugging`.
34+
35+
> [!NOTE]
36+
>
37+
> **Important**: Before deploying or running the sample, please make sure your device is visible in the Device Explorer.
38+
>
39+
> **Tip**: To display the Device Explorer, go to Visual Studio menus: `View > Other Windows > Device Explorer`.

0 commit comments

Comments
 (0)