詳説 GPIO Support for Raspberry Pi

312 Views

October 05, 19

スライド概要

2019/10/5 .NET Conf 2019 meetup in AICHI
https://centerclr.connpass.com/event/143949/

profile-image

SeeedKKの中の人。Microsoft MVP for Internet of Things。

シェア

またはPlayer版

埋め込む »CMSなどでJSが使えない場合

(ダウンロード不可)

関連スライド

各ページのテキスト
1.

.NET Conf 2019 meetup in AICHI 詳説? GPIO Support for Raspberry Pi 2019/10/5 Takashi Matsuoka

2.

Takashi Matsuoka (@matsujirushi12) 「e」3つ 2017~ MVP for Windows Development Wio LTE Wio 3G Wio LTE M1/NB1(BG96) MT3620 DevBoard de:code 2019

3.

What's new in .NET Core 3.0 https://docs.microsoft.com/en-us/dotnet/core/whats-new/dotnet-core-3-0

4.

マイコンボードの外部インターフェース Ethernet GPIO/PWM HDMI I2C USB SPI USB UART

5.

NETMF https://docs.microsoft.com/en-us/previous-versions/windows/embedded/ee434325%28v%3dmsdn.10%29

6.

NETMF

7.

TinyCLR, nanoFramework

8.

UWP https://docs.microsoft.com/ja-jp/windows/iot-core/learn-about-hardware/pinmappings/pinmappingsrpi

9.

.NET Core IoT Libraries https://github.com/dotnet/iot/blob/master/Documentation/roadmap.md

10.

.NET Core IoT Libraries https://github.com/dotnet/iot/blob/master/Documentation/roadmap.md

11.

.NET Core IoT Libraries – System.Device.Gpio ( ( ) ) ? sysfs (/sys/class/gpio) libgpiod (/dev/gpiochipX) https://github.com/dotnet/iot/blob/master/Documentation/roadmap.md

12.

What's new in .NET Core 3.0 https://docs.microsoft.com/en-us/dotnet/core/whats-new/dotnet-core-3-0

13.

フレームワークと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

14.

APIs Summary

15.

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

16.

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

17.

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

18.

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

19.

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

20.

Try

21.

GPIO - Lチカ https://webofthings.org/2016/10/23/node-gpio-and-the-raspberry-pi/

22.

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

23.
[beta]
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

24.

I2C - 加速度センサー ADXL345

25.
[beta]
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

26.
[beta]
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

27.

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

28.

I2C - 加速度センサー

29.

I2C – クロックストレッチ ラズパイマガジン 2018年2月号

30.

I2C – 温湿度センサー SHT-31

31.
[beta]
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

32.

I2C – 温湿度センサー クロックストレッチ、正常に動作!!!

33.

API Design and Implementation

34.
[beta]
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);
}
}
…

35.

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

36.

.NET Design Reviews: GPIO https://github.com/dotnet/iot/blob/master/Documentation/README.md#design-reviews

37.

.NET Core IoT Libraries – System.Device.Gpio https://github.com/dotnet/iot/blob/master/Documentation/roadmap.md

38.

GPIO - .NET Core IoT Libraries https://github.com/dotnet/iot/tree/master/src/System.Device.Gpio

39.

GPIO - .NET Core IoT Libraries /proc/cpuinfo BCM2835

40.

GPIO - .NET Core IoT Libraries

41.
[beta]
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;

42.

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)

43.

.NET Core IoT Libraries https://github.com/dotnet/iot/blob/master/Documentation/roadmap.md

44.

.NET Core IoT Libraries – Iot.Device.* https://github.com/dotnet/iot/blob/master/src/devices/README.md

45.

What's new in .NET Core 3.0 https://docs.microsoft.com/en-us/dotnet/core/whats-new/dotnet-core-3-0

46.

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

47.

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

48.

I2C - Jetson Nano / Coral DevBoard

49.

IDE https://code.visualstudio.com/docs/remote/ssh

50.

まとめ • 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