Git Product home page Git Product logo

sunxi-gpio-driver's Introduction

Sunxi GPIO Driver for .NET

sunxi represents the family of ARM SoCs from Allwinner Technology. This project contains a full function(PULL-UP, PULL-DOWN) generic GPIO driver SunxiDriver for Allwinner SoCs and some special GPIO drivers like OrangePiZeroDriver, OrangePiLite2Driver.

dotnet/iot#1130

Getting started

Special GPIO driver: OrangePiZeroDriver

// For Orange Pi Zero
using GpioController gpio = new GpioController(PinNumberingScheme.Board, new OrangePiZeroDriver());

// Open the GPIO pin.
gpio.OpenPin(7);
// Set the pin mode.
gpio.SetPinMode(7, PinMode.InputPullUp);
// Read current value of the pin.
PinValue value = gpio.Read(7);

Generic GPIO driver: SunxiDriver

// Beacuse this is a generic driver, the pin scheme can only be Logical.
// The base addresses can be found in the corresponding SoC datasheet.
using GpioController gpio = new GpioController(PinNumberingScheme.Logical, new SunxiDriver(cpuxPortBaseAddress: 0x01C20800, cpusPortBaseAddress: 0x01F02C00));

// Convert pin number to logical scheme.
int pinNumber = SunxiDriver.MapPinNumber(portController: 'A', port: 10);
gpio.OpenPin(pinNumber);
gpio.SetPinMode(pinNumber, PinMode.Output);
// Write a value to the pin.
gpio.Write(pinNumber, PinValue.High);

Benchmark

Benchmarking with Orange Pi Zero. The operating system is Armbian buster, Linux kernel version is 5.10.16, and .NET version is 5.0.3. The test uses different GPIO drivers to quickly switch the state of GPIO 6 (Logical), and uses an oscilloscope to measure the average frequency of GPIO externally.

Drivers Language Library Version Average Frequency
SunxiDriver C# - 185 KHz
SysFsDriver C# System.Device.Gpio 1.3.0 692 Hz
LibGpiodDriver C# System.Device.Gpio 1.3.0
libgpiod 1.2-3
81 KHz
wiringOP C 35de015 1.10 MHz

Adding new drivers

For SoCs

  1. Inheriting SunxiDriver Class.
    // For Allwinner H2+/H3
    public class Sun8iw7p1Driver : SunxiDriver { }
  2. Overriding the GPIO base addresses.
    protected override int CpuxPortBaseAddress => 0x01C20800;
    protected override int CpusPortBaseAddress => 0x01F02C00;

For Boards

  1. Inherit the corresponding SoC class.
    public class OrangePiZeroDriver : Sun8iw7p1Driver { }
  2. Overriding the mapping method for converting a board pin number to the driver's logical numbering scheme.
    // Mapping from board pins to logic pins.
    private static readonly int[] _pinNumberConverter = new int[27]
    {
        -1, -1, -1, MapPinNumber('A', 12), -1, MapPinNumber('A', 11), -1, MapPinNumber('A', 6), MapPinNumber('G', 6), -1,
        MapPinNumber('G', 7), MapPinNumber('A', 1), MapPinNumber('A', 7), MapPinNumber('A', 0), -1, MapPinNumber('A', 3),
        MapPinNumber('A', 19), -1, MapPinNumber('A', 18), MapPinNumber('A', 15), -1, MapPinNumber('A', 16), MapPinNumber('A', 2),
        MapPinNumber('A', 14), MapPinNumber('A', 13), -1, MapPinNumber('A', 10)
    };
    
    protected override int PinCount => 17;
    
    protected internal override int ConvertPinNumberToLogicalNumberingScheme(int pinNumber)
    {
        int num = _pinNumberConverter[pinNumber];
        return num != -1 ? num : 
            throw new ArgumentException($"Board (header) pin {pinNumber} is not a GPIO pin on the {GetType().Name} device.", nameof(pinNumber));
    }

References

The wiki of the linux-sunxi community: https://linux-sunxi.org/Main_Page

Sunxi GPIO Driver's Sample

Hardware required

  • Orange Pi Zero
  • Switch
  • Male/Female Jumper Wires

Circuit

  • Switch 1 - Board Pin7 (GPIO 6)
  • Switch 2 - GND

Run the sample

cd SunxiGpioDriver.Samples
dotnet publish -c release -r linux-arm -o YOUR_FOLDER
sudo dotnet YOUR_FOLDER/SunxiGpioDriver.Samples.dll

Run the sample with Docker

docker build -t sunxi-sample -f Dockerfile .
docker run --rm -it --privileged=true --device=/dev/mem sunxi-sample

sunxi-gpio-driver's People

Contributors

zhanggaoxing avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.