GithubHelp home page GithubHelp logo

zhanggaoxing / rockchip-gpio-driver Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 645 KB

A full function(PULL-UP, PULL-DOWN) generic GPIO driver for Rockchip SoCs.

License: MIT License

C# 99.22% Dockerfile 0.78%
gpio rockchip

rockchip-gpio-driver's Introduction

Rockchip GPIO Driver for .NET

This project contains a full function(PULL-UP, PULL-DOWN) generic GPIO driver RockchipDriver for Rockchip SoCs and some special GPIO drivers like OrangePi4Driver, NanoPiR2sDriver.

Getting started

Special GPIO driver: OrangePi4Driver

using GpioController gpio = new GpioController(PinNumberingScheme.Board, new OrangePi4Driver());

// Open the GPIO pin.
gpio.OpenPin(7);
// Set the pin mode.
gpio.SetPinMode(7, PinMode.Output);
// Write a value to the pin.
gpio.Write(7, PinValue.High);

Generic GPIO driver: RockchipDriver

// 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 RockchipDriver(gpioRegisterAddresses: new uint[] { 0xFF72_0000, 0xFF73_0000, 0xFF78_0000, 0xFF78_8000, 0xFF79_0000 });

// Convert pin number to logical scheme.
int pinNumber = RockchipDriver.MapPinNumber(gpioNumber: 4, port: 'C', portNumber: 6);
gpio.OpenPin(pinNumber);
gpio.SetPinMode(pinNumber, PinMode.InputPullUp);
// Read current value of the pin.
PinValue value = gpio.Read(pinNumber);

Benchmark

Benchmarking with Orange Pi 4. 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 150 (Logical), and uses an oscilloscope to measure the average frequency of GPIO externally.

Drivers Language Library Version Average Frequency
RockchipDriver C# - 516 KHz
SysFsDriver C# System.Device.Gpio 1.3.0 4.27 KHz
LibGpiodDriver C# System.Device.Gpio 1.3.0
libgpiod 1.2-3
174 KHz
wiringOP C 35de015 584 KHz

Adding new drivers

For SoCs

  1. Inheriting RockchipDriver Class.
    public class Rk3328Driver : RockchipDriver { }
  2. Overriding the GPIO register addresses and adding GRF, CRU addresses.
    protected override uint[] GpioRegisterAddresses => new[] { 0xFF21_0000, 0xFF22_0000, 0xFF23_0000, 0xFF24_8000 };
    protected uint GeneralRegisterFiles => 0xFF10_0000;
    protected uint ClockResetUnit => 0xFF44_0000;        
  3. Overriding SetPinMode method.
    protected override void SetPinMode(int pinNumber, PinMode mode)
    {
        // TODO
        // You can refer to the corresponding datasheet.
        // Clock & Reset Unit (CRU) chapter is used to enable the GPIO function.
        // General Register Files (GRF) chapter is used to set pin pull up/down mode.
        // GPIO chapter is used to set pin direction and level.
    }

For Boards

  1. Inherit the corresponding SoC class.
    // For NanoPi R2S
    public class NanoPiR2sDriver : Rk3328Driver { }
  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[]
    {
        -1, -1, -1,  MapPinNumber(2, 'D', 1), -1, MapPinNumber(2, 'D', 0), -1,
        MapPinNumber(2, 'A', 2), MapPinNumber(3, 'A', 4), -1, MapPinNumber(3, 'A', 6)
    };
    
    protected override int PinCount => 5;
    
    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

Rockchip open source documents: http://opensource.rock-chips.com/wiki_Main_Page

Samples

Hardware required

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

Circuit

  • Switch 1 - Board Pin7
  • Switch 2 - GND

Run the sample

cd RockchipGpioDriver.Samples
dotnet publish -c release -r linux-arm64 -o YOUR_FOLDER
sudo dotnet YOUR_FOLDER/RockchipGpioDriver.Samples.dll

Run the sample with Docker

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

rockchip-gpio-driver's People

Contributors

zhanggaoxing avatar

Watchers

 avatar  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.