312 Views
October 05, 19
スライド概要
2019/10/5 .NET Conf 2019 meetup in AICHI
https://centerclr.connpass.com/event/143949/
SeeedKKの中の人。Microsoft MVP for Internet of Things。
.NET Conf 2019 meetup in AICHI 詳説? GPIO Support for Raspberry Pi 2019/10/5 Takashi Matsuoka
Takashi Matsuoka (@matsujirushi12) 「e」3つ 2017~ MVP for Windows Development Wio LTE Wio 3G Wio LTE M1/NB1(BG96) MT3620 DevBoard de:code 2019
What's new in .NET Core 3.0 https://docs.microsoft.com/en-us/dotnet/core/whats-new/dotnet-core-3-0
マイコンボードの外部インターフェース Ethernet GPIO/PWM HDMI I2C USB SPI USB UART
NETMF https://docs.microsoft.com/en-us/previous-versions/windows/embedded/ee434325%28v%3dmsdn.10%29
NETMF
TinyCLR, nanoFramework
UWP https://docs.microsoft.com/ja-jp/windows/iot-core/learn-about-hardware/pinmappings/pinmappingsrpi
.NET Core IoT Libraries https://github.com/dotnet/iot/blob/master/Documentation/roadmap.md
.NET Core IoT Libraries https://github.com/dotnet/iot/blob/master/Documentation/roadmap.md
.NET Core IoT Libraries – System.Device.Gpio ( ( ) ) ? sysfs (/sys/class/gpio) libgpiod (/dev/gpiochipX) https://github.com/dotnet/iot/blob/master/Documentation/roadmap.md
What's new in .NET Core 3.0 https://docs.microsoft.com/en-us/dotnet/core/whats-new/dotnet-core-3-0
フレームワークとAPI Interface UWP .NET Core IoT Libraries GPIO Windows.Devices.Gpio. GpioController System.Device.Gpio. GpioController PWM (LightningPwmProvider) System.Device.Pwm. PwmChannel I2C Windows.Devices.I2c. I2cController System.Device.I2c. I2cDevice SPI Windows.Devices.Spi. SpiDevice System.Device.Spi. SpiDevice UART .NET Framework System.IO.Ports. Windows.Devices.Serial SerialPort Communication. SerialDevice .NET Core 3.0 System.IO.Ports. SerialPort
APIs Summary
GPIO - .NET Core IoT Libraries using System.Device.Gpio; var controller = new GpioController(); controller.OpenPin(pinNumber, …); controller.SetPinMode(pinNumber, …); controller.Read(pinNumber, …); controller.Write(pinNumber, …); controller.WaitForEvent(pinNumber, …); controller.WaitForEventAsync(pinNumber, …); controller.RegisterCallbackForPinValueChangedEvent(pinNumber, …); controller.UnregisterCallbackForPinValueChangedEvent(pinNumber, …); controller.ClosePin(pinNumber, …);
PWM - .NET Core IoT Libraries using System.Device.Pwm; var channel = PwmChannel.Create(chip, channel); // var channel = new SoftwarePwmChannel(pinNumber); channel.Frequency = …; channel.DutyCyclePercentage = …; channel.Start(); channel.Stop();
I2C - .NET Core IoT Libraries using System.Device.I2c; var settings = new I2cConnectionSettings(busId, deviceAddress); var device = I2cDevice.Create(settings); device.ReadByte(); device.Read(…); device.WriteByte(…); device.Write(…); device.WriteRead(…);
SPI - .NET Core IoT Libraries using System.Device.Spi; var settings = new SpiConnectionSettings(busId, chipSelectLine); var device = SpiDevice.Create(settings); device.ReadByte(); device.Read(…); device.WriteByte(…); device.Write(…); device.TransferFullDuplex(…);
UART - .NET Core 3.0 using System.IO.Ports; var serial = new SerialPort(); serial.Open(); serial.BytesToRead; serial.Read(…); serial.ReadByte(…); serial.ReadLine(…); serial.Write(…); serial.WriteLine(…); … serial.Close();
Try
GPIO - Lチカ https://webofthings.org/2016/10/23/node-gpio-and-the-raspberry-pi/
GPIO - Lチカ $ # Install the .NET Core 3.0 SDK for Linux ARM32 $ dotnet new console -n blinky $ cd blinky $ dotnet add package System.Device.Gpio --source \ https://dotnetfeed.blob.core.windows.net/dotnet-iot/index.json
GPIO - Lチカ
using System;
using System.Threading;
using System.Device.Gpio;
…
const int pin = 21;
using (var controller = new GpioController())
{
controller.OpenPin(pin, PinMode.Output);
while (true)
{
Console.WriteLine("Blink!");
controller.Write(pin, PinValue.High);
Thread.Sleep(200);
controller.Write(pin, PinValue.Low);
Thread.Sleep(800);
}
}
…
https://gist.github.com/matsujirushi/af97cb7099d229a0b4c2a957e2ba5593
I2C - 加速度センサー ADXL345
I2C - 加速度センサー
class Program
{
const int BUS_ID = 1;
const int DEVICE_ADDRESS = 0x1d;
const byte REG_POWER_CTL = 0x2d;
const byte REG_DATAZ0 = 0x36;
static void Main(string[] args)
{
var settings = new I2cConnectionSettings(BUS_ID, DEVICE_ADDRESS);
var device = I2cDevice.Create(settings);
while (true)
{
device.WriteRegU8(REG_POWER_CTL, 0x08);
double gz = device.ReadRegI16(REG_DATAZ0) * 2.0 / (1024 - 1);
Console.WriteLine($"gz = {gz:f2}[G]");
Thread.Sleep(500);
}
}
}
https://gist.github.com/matsujirushi/f820f373f9d7089212106db0b1a79ac3
I2C - 加速度センサー
static class ExtensionMethods
{
public static void WriteRegU8(this I2cDevice device, byte reg, byte
data)
{
ReadOnlySpan<byte> writeBuf = stackalloc byte[] { reg, data };
device.Write(writeBuf);
}
public static short ReadRegI16(this I2cDevice device, byte reg)
{
Span<byte> readBuf = stackalloc byte[2];
device.WriteByte(reg);
device.Read(readBuf);
return BinaryPrimitives.ReadInt16LittleEndian(readBuf);
}
}
https://gist.github.com/matsujirushi/f820f373f9d7089212106db0b1a79ac3
Span<> .NET Core 3.0 2.2 2.1 Span<T>はマネージド ヒープではなく、スタックに割り当てられるref 構造体です。 stackalloc C# 7.3 .NET Framework 4.7.2 .NET Core 2.2 The stackalloc operator allocates a block of memory on the stack. Visual Studio 2017 15.7
I2C - 加速度センサー
I2C – クロックストレッチ ラズパイマガジン 2018年2月号
I2C – 温湿度センサー SHT-31
I2C – 温湿度センサー
class Program
{
const int BUS_ID = 1;
const int DEVICE_ADDRESS = 0x45;
static void Main(string[] args)
{
var settings = new I2cConnectionSettings(BUS_ID, DEVICE_ADDRESS);
var device = I2cDevice.Create(settings);
ReadOnlySpan<byte> writeBuf = stackalloc byte[] { 0x2c, 0x10 };
Span<byte> readBuf = stackalloc byte[6];
device.Write(writeBuf);
device.Read(readBuf);
}
}
https://gist.github.com/matsujirushi/f40db4b82cc5d9c2f15cbc9b2f2aeaf6
I2C – 温湿度センサー クロックストレッチ、正常に動作!!!
API Design and Implementation
GPIO - .NET Core IoT Libraries
using System;
using System.Threading;
using System.Device.Gpio;
…
const int pin = 21;
using (var controller = new GpioController())
{
controller.OpenPin(pin, PinMode.Output);
while (true)
{
Console.WriteLine("Blink!");
controller.Write(pin, PinValue.High);
Thread.Sleep(200);
controller.Write(pin, PinValue.Low);
Thread.Sleep(800);
}
}
…
GPIO – UWP using Windows.Devices.Gpio; var gpio = GpioController.GetDefault(); pin = gpio.OpenPin(LED_PIN); pinValue = GpioPinValue.High; pin.Write(pinValue); pin.SetDriveMode(GpioPinDriveMode.Output); https://github.com/microsoft/Windows-iotcore-samples/tree/develop/Samples/HelloBlinky/CS
.NET Design Reviews: GPIO https://github.com/dotnet/iot/blob/master/Documentation/README.md#design-reviews
.NET Core IoT Libraries – System.Device.Gpio https://github.com/dotnet/iot/blob/master/Documentation/roadmap.md
GPIO - .NET Core IoT Libraries https://github.com/dotnet/iot/tree/master/src/System.Device.Gpio
GPIO - .NET Core IoT Libraries /proc/cpuinfo BCM2835
GPIO - .NET Core IoT Libraries
GPIO - .NET Core IoT Libraries
RaspberryPi3Driver.Initialize()
int fileDescriptor = Interop.open("/dev/gpiomem", FileOpenFlags.O_RDWR |
FileOpenFlags.O_SYNC);
IntPtr mapPointer = Interop.mmap(IntPtr.Zero, Environment.SystemPageSize,
(MemoryMappedProtections.PROT_READ | MemoryMappedProtections.PROT_WRITE),
MemoryMappedFlags.MAP_SHARED, fileDescriptor, GpioRegisterOffset);
_registerViewPointer = (RegisterView*)mapPointer;
RaspberryPi3Driver.Write()
uint* registerPointer = (value == PinValue.High) ? &_registerViewPointer>GPSET[pinNumber / 32] : &_registerViewPointer->GPCLR[pinNumber / 32];
uint register = *registerPointer;
register = 1U << (pinNumber % 32);
*registerPointer = register;
GPIO - .NET Core IoT Libraries GpioController (.cs) partial GpioController (.Linux.cs) GpioController (.Windows.cs) Create instance GpioDriver UnixDriver RaspberryPi3Driver (.cs) RaspberryPi3Driver (.Linux.cs) RaspberryPi3Driver (.Windows.cs) LibGpiodDriver (.Linux.cs) SysFsDriver (.Linux.cs) Windows10Driver (.Windows.cs)
.NET Core IoT Libraries https://github.com/dotnet/iot/blob/master/Documentation/roadmap.md
.NET Core IoT Libraries – Iot.Device.* https://github.com/dotnet/iot/blob/master/src/devices/README.md
What's new in .NET Core 3.0 https://docs.microsoft.com/en-us/dotnet/core/whats-new/dotnet-core-3-0
Jetson Nano - GPIO/I2C • [GPIO] ピン番号を変更 • [GPIO/I2C] sudo … 21 -> 78 Unhandled exception. System.UnauthorizedAccessException: Setting a mode to a pin requires root permissions. ---> System.UnauthorizedAccessException: Access to the path '/sys/class/gpio/gpio78/direction' is denied. ---> System.IO.IOException: Permission denied
Coral DevBoard - GPIO/I2C • [GPIO] ピン番号を変更 • [GPIO/I2C] sudo … 21 -> 141 Unhandled exception. System.UnauthorizedAccessException: Opening pins requires root permissions. ---> System.UnauthorizedAccessException: Access to the path '/sys/class/gpio/export' is denied. ---> System.IO.IOException: Permission denied
I2C - Jetson Nano / Coral DevBoard
IDE https://code.visualstudio.com/docs/remote/ssh
まとめ • GPIO Support for Raspberry Pi → .NET Core IoT Libraries • .NET Core IoT Libraries • System.Device.* • Iot.Device.* … nuget … GPIO, PWM, I2C, SPI … Device Specific Classes • .NET Core 3.0 • System.IO.Ports … UART