From af7c57bcc2743ffe0097ed4ed02cee7663252fd8 Mon Sep 17 00:00:00 2001 From: josesimoes Date: Fri, 19 Jul 2024 15:55:35 +0100 Subject: [PATCH] Add I2C slave sample - Add sample projects for both slave and master devices. - Update main readme for I2C. --- .../I2cMasterDevice/I2cMasterDevice.nfproj | 47 ++++++++++++++ .../I2cMasterSlave/I2cMasterDevice/Program.cs | 64 +++++++++++++++++++ .../Properties/AssemblyInfo.cs | 17 +++++ .../I2cMasterDevice/packages.config | 7 ++ samples/I2C/I2cMasterSlave/I2cMasterSlave.sln | 40 ++++++++++++ .../I2cSlaveDevice/I2cSlaveDevice.nfproj | 47 ++++++++++++++ .../I2cMasterSlave/I2cSlaveDevice/Program.cs | 49 ++++++++++++++ .../I2cSlaveDevice/Properties/AssemblyInfo.cs | 17 +++++ .../I2cSlaveDevice/packages.config | 7 ++ samples/I2C/I2cMasterSlave/README.md | 39 +++++++++++ samples/I2C/README.md | 1 + 11 files changed, 335 insertions(+) create mode 100644 samples/I2C/I2cMasterSlave/I2cMasterDevice/I2cMasterDevice.nfproj create mode 100644 samples/I2C/I2cMasterSlave/I2cMasterDevice/Program.cs create mode 100644 samples/I2C/I2cMasterSlave/I2cMasterDevice/Properties/AssemblyInfo.cs create mode 100644 samples/I2C/I2cMasterSlave/I2cMasterDevice/packages.config create mode 100644 samples/I2C/I2cMasterSlave/I2cMasterSlave.sln create mode 100644 samples/I2C/I2cMasterSlave/I2cSlaveDevice/I2cSlaveDevice.nfproj create mode 100644 samples/I2C/I2cMasterSlave/I2cSlaveDevice/Program.cs create mode 100644 samples/I2C/I2cMasterSlave/I2cSlaveDevice/Properties/AssemblyInfo.cs create mode 100644 samples/I2C/I2cMasterSlave/I2cSlaveDevice/packages.config create mode 100644 samples/I2C/I2cMasterSlave/README.md diff --git a/samples/I2C/I2cMasterSlave/I2cMasterDevice/I2cMasterDevice.nfproj b/samples/I2C/I2cMasterSlave/I2cMasterDevice/I2cMasterDevice.nfproj new file mode 100644 index 00000000..ba5722db --- /dev/null +++ b/samples/I2C/I2cMasterSlave/I2cMasterDevice/I2cMasterDevice.nfproj @@ -0,0 +1,47 @@ + + + + $(MSBuildExtensionsPath)\nanoFramework\v1.0\ + + + + Debug + AnyCPU + {11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + ae6817bf-dd2a-4883-aba7-0a90751304b7 + Exe + Properties + 512 + Samples.I2c.MasterDevice + I2cMasterDevice + v1.0 + + + + + + + + + ..\packages\nanoFramework.CoreLibrary.1.15.5\lib\mscorlib.dll + + + ..\packages\nanoFramework.Hardware.Esp32.1.6.19\lib\nanoFramework.Hardware.Esp32.dll + + + ..\packages\nanoFramework.Runtime.Events.1.11.18\lib\nanoFramework.Runtime.Events.dll + + + ..\packages\nanoFramework.System.Device.I2c.1.1.16\lib\System.Device.I2c.dll + + + + + + + + + + + + \ No newline at end of file diff --git a/samples/I2C/I2cMasterSlave/I2cMasterDevice/Program.cs b/samples/I2C/I2cMasterSlave/I2cMasterDevice/Program.cs new file mode 100644 index 00000000..dd5bd4b6 --- /dev/null +++ b/samples/I2C/I2cMasterSlave/I2cMasterDevice/Program.cs @@ -0,0 +1,64 @@ +// +// Copyright (c) .NET Foundation and Contributors +// See LICENSE file in the project root for full license information. +// + +using System; +using System.Device.I2c; +using System.Diagnostics; +using System.Threading; +using nanoFramework.Hardware.Esp32; + +namespace Samples.I2c.MasterDevice +{ + public class Program + { + public static void Main() + { + // Configure I2C GPIO pins + Configuration.SetPinFunction(Gpio.IO18, DeviceFunction.I2C1_DATA); + Configuration.SetPinFunction(Gpio.IO19, DeviceFunction.I2C1_CLOCK); + + // create I2C device + var myI2cDevice = I2cDevice.Create(new I2cConnectionSettings( + 1, + 0x10, + I2cBusSpeed.FastMode)); + + // setup read buffer + var buffer = new byte[2]; + + while (true) + { + try + { + // set address to read from + if (myI2cDevice.Write(new byte[] { 0x22 }).BytesTransferred != 1) + { + Debug.WriteLine("Error writting to I2C device to set register address to read from"); + } + else + { + if (myI2cDevice.Read(buffer).BytesTransferred != 2) + { + Debug.WriteLine("Error reading from I2C device"); + } + else + { + + // expected buffer content is: 0xBE, 0xEF + Debug.WriteLine($"Register content: {buffer[0]:X2} {buffer[1]:X2}"); + } + } + + // pause before a new read + Thread.Sleep(1000); + } + catch (Exception ex) + { + Debug.WriteLine($"Error reading from I2C device: {ex.Message}"); + } + } + } + } +} diff --git a/samples/I2C/I2cMasterSlave/I2cMasterDevice/Properties/AssemblyInfo.cs b/samples/I2C/I2cMasterSlave/I2cMasterDevice/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..9797ee51 --- /dev/null +++ b/samples/I2C/I2cMasterSlave/I2cMasterDevice/Properties/AssemblyInfo.cs @@ -0,0 +1,17 @@ +// +// Copyright (c) .NET Foundation and Contributors +// See LICENSE file in the project root for full license information. +// + +using System.Reflection; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("CSharp.I2cMasterDevice")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] diff --git a/samples/I2C/I2cMasterSlave/I2cMasterDevice/packages.config b/samples/I2C/I2cMasterSlave/I2cMasterDevice/packages.config new file mode 100644 index 00000000..7ccef07c --- /dev/null +++ b/samples/I2C/I2cMasterSlave/I2cMasterDevice/packages.config @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/samples/I2C/I2cMasterSlave/I2cMasterSlave.sln b/samples/I2C/I2cMasterSlave/I2cMasterSlave.sln new file mode 100644 index 00000000..ef2b4af0 --- /dev/null +++ b/samples/I2C/I2cMasterSlave/I2cMasterSlave.sln @@ -0,0 +1,40 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.10.34928.147 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{1F0B41A0-B2F5-4986-95D9-8D91623B69C9}" + ProjectSection(SolutionItems) = preProject + README.md = README.md + EndProjectSection +EndProject +Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "I2cMasterDevice", "I2cMasterDevice\I2cMasterDevice.nfproj", "{AE6817BF-DD2A-4883-ABA7-0A90751304B7}" +EndProject +Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "I2cSlaveDevice", "I2cSlaveDevice\I2cSlaveDevice.nfproj", "{EDFF1C28-7988-4360-A0EF-2008E57945C6}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {AE6817BF-DD2A-4883-ABA7-0A90751304B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AE6817BF-DD2A-4883-ABA7-0A90751304B7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AE6817BF-DD2A-4883-ABA7-0A90751304B7}.Debug|Any CPU.Deploy.0 = Debug|Any CPU + {AE6817BF-DD2A-4883-ABA7-0A90751304B7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AE6817BF-DD2A-4883-ABA7-0A90751304B7}.Release|Any CPU.Build.0 = Release|Any CPU + {AE6817BF-DD2A-4883-ABA7-0A90751304B7}.Release|Any CPU.Deploy.0 = Release|Any CPU + {EDFF1C28-7988-4360-A0EF-2008E57945C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EDFF1C28-7988-4360-A0EF-2008E57945C6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EDFF1C28-7988-4360-A0EF-2008E57945C6}.Debug|Any CPU.Deploy.0 = Debug|Any CPU + {EDFF1C28-7988-4360-A0EF-2008E57945C6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EDFF1C28-7988-4360-A0EF-2008E57945C6}.Release|Any CPU.Build.0 = Release|Any CPU + {EDFF1C28-7988-4360-A0EF-2008E57945C6}.Release|Any CPU.Deploy.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {5834D1F4-7FFC-4363-AA2D-8AA473993BCF} + EndGlobalSection +EndGlobal diff --git a/samples/I2C/I2cMasterSlave/I2cSlaveDevice/I2cSlaveDevice.nfproj b/samples/I2C/I2cMasterSlave/I2cSlaveDevice/I2cSlaveDevice.nfproj new file mode 100644 index 00000000..41542a93 --- /dev/null +++ b/samples/I2C/I2cMasterSlave/I2cSlaveDevice/I2cSlaveDevice.nfproj @@ -0,0 +1,47 @@ + + + + $(MSBuildExtensionsPath)\nanoFramework\v1.0\ + + + + Debug + AnyCPU + {11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + edff1c28-7988-4360-a0ef-2008e57945c6 + Exe + Properties + 512 + Samples.I2c.SlaveDevice + I2cSlaveDevice + v1.0 + + + + + + + + + ..\packages\nanoFramework.CoreLibrary.1.15.5\lib\mscorlib.dll + + + ..\packages\nanoFramework.Hardware.Esp32.1.6.19\lib\nanoFramework.Hardware.Esp32.dll + + + ..\packages\nanoFramework.Runtime.Events.1.11.18\lib\nanoFramework.Runtime.Events.dll + + + ..\packages\nanoFramework.System.Device.I2c.Slave.1.0.10\lib\System.Device.I2c.Slave.dll + + + + + + + + + + + + \ No newline at end of file diff --git a/samples/I2C/I2cMasterSlave/I2cSlaveDevice/Program.cs b/samples/I2C/I2cMasterSlave/I2cSlaveDevice/Program.cs new file mode 100644 index 00000000..bf5397e2 --- /dev/null +++ b/samples/I2C/I2cMasterSlave/I2cSlaveDevice/Program.cs @@ -0,0 +1,49 @@ +// +// Copyright (c) .NET Foundation and Contributors +// See LICENSE file in the project root for full license information. +// + +using System; +using System.Device.I2c; +using System.Diagnostics; +using nanoFramework.Hardware.Esp32; + +namespace Samples.I2c.SlaveDevice +{ + public class Program + { + public static void Main() + { + Configuration.SetPinFunction(Gpio.IO18, DeviceFunction.I2C1_DATA); + Configuration.SetPinFunction(Gpio.IO19, DeviceFunction.I2C1_CLOCK); + + // create an I2C slave device on bus 1 with address 0x10 + var device = new I2cSlaveDevice(1, 0x10); + + while (true) + { + // wait "forever" for a single byte + try + { + if (device.ReadByte(out byte registerAddress, 500)) + { + switch (registerAddress) + { + case 0x22: + // reply back with 2 dummy bytes + device.Write(new byte[] { 0xBE, 0xEF }); + + Debug.WriteLine($"Received message: {registerAddress:X2}"); + + break; + } + } + } + catch (Exception ex) + { + Debug.WriteLine("Timeout I2C device"); + } + } + } + } +} diff --git a/samples/I2C/I2cMasterSlave/I2cSlaveDevice/Properties/AssemblyInfo.cs b/samples/I2C/I2cMasterSlave/I2cSlaveDevice/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..576ab4eb --- /dev/null +++ b/samples/I2C/I2cMasterSlave/I2cSlaveDevice/Properties/AssemblyInfo.cs @@ -0,0 +1,17 @@ +// +// Copyright (c) .NET Foundation and Contributors +// See LICENSE file in the project root for full license information. +// + +using System.Reflection; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("CSharp.I2cSlaveDevice")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] diff --git a/samples/I2C/I2cMasterSlave/I2cSlaveDevice/packages.config b/samples/I2C/I2cMasterSlave/I2cSlaveDevice/packages.config new file mode 100644 index 00000000..1b0e6532 --- /dev/null +++ b/samples/I2C/I2cMasterSlave/I2cSlaveDevice/packages.config @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/samples/I2C/I2cMasterSlave/README.md b/samples/I2C/I2cMasterSlave/README.md new file mode 100644 index 00000000..642b15a3 --- /dev/null +++ b/samples/I2C/I2cMasterSlave/README.md @@ -0,0 +1,39 @@ +# 🌶️🌶️ - I2C Master Slave sample + +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. + +## Hardware requirements + +Two ESP32 devices board. +The code sample is demonstrative of the use of the I2C and I2C slave APIs. + +## Related topics + +### Reference + +- [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) + +## Build the sample + +1. Start Microsoft Visual Studio 2022 or Visual Studio 2019 (Visual Studio 2017 should be OK too) and select `File > Open > Project/Solution`. +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. +1. Press `Ctrl+Shift+B`, or select `Build > Build Solution`. + +## Run the sample + +The next steps depend on whether you just want to deploy the sample or you want to both deploy and run it. + +### Deploying the sample + +- Select `Build > Deploy Solution`. + +### Deploying and running the sample + +- To debug the sample and then run it, press F5 or select `Debug > Start Debugging`. + +> [!NOTE] +> +> **Important**: Before deploying or running the sample, please make sure your device is visible in the Device Explorer. +> +> **Tip**: To display the Device Explorer, go to Visual Studio menus: `View > Other Windows > Device Explorer`. diff --git a/samples/I2C/README.md b/samples/I2C/README.md index fb06b2b8..58c10892 100644 --- a/samples/I2C/README.md +++ b/samples/I2C/README.md @@ -4,6 +4,7 @@ - [🌶️🌶️ - IES-SHIELD-GPS module](./System.Device.I2c/GPS) - [🌶️🌶️ - I2C Scanner sample](./NanoI2cScanner/) +- [🌶️🌶️ - I2C Master - Slave sample](./I2cMasterSlave/) ## Build the sample