Jump to content

Orange Pi Zero, Python GPIO Library


Rafał Wolak

Recommended Posts

Hello i'm begginer in programing, but i'm learning slowly. I need use gpio pins in my Orange Pi Zero, but i can't find any library that work with my board. Google is not very usefull for me. I was using RPi.GPIO on my RPi projects, but this library don't work on OPi Zero. Can anyone link to working library for python? I need use only 6 output pins.

Link to comment
Share on other sites

Ok so if I see a project that uses WiringPi, how do I replace the wiringpi code in that project with the code from this library so that it does the same job?

 

Also what about if I need a library in C? (which one works?)

Link to comment
Share on other sites

In the link I provided, although it is a Python library, but under the hood, it still written in C.

So, for GPIO, you can use pyA20/gpio/gpio_lib.h and pyA20/gpio/gpio_lib.c directly.

For blink example of PiZero RED LED on PA17, here are the methods needed :

sunxi_gpio_init();
sunxi_gpio_set_cfgpin(SUNXI_GPA(17), SUNXI_GPIO_OUTPUT);
while(1) {
    sunxi_gpio_output(SUNXI_GPA(17), 1);
    sleep(1);
    sunxi_gpio_output(SUNXI_GPA(17), 0);
    sleep(1);
}
Link to comment
Share on other sites

So if we take for example this project (which is with wiringPi) -> https://github.com/rszimm/sprinklers_pi/blob/361946af2e972ac7df639859610d26adaf335624/core.cpp

 

I would replace #include <wiringPi.h> with #include <gpio_lib.h>

 

and then wiringPiSetup() with sunxi_gpio_init()

 

and then digitalWrite(ZoneToIOMap, (outState&(0x01<<i))?1:0);  to what?

 

And the pinMode stuff in core.cpp to what?

 

I'd really like to use this software with orange pi zero, I'd be really happy if you could help me.

 

I think I will use only OT_DIRECT_POS or  OT_DIRECT_NEG so maybe those pinmodes are not important dunno?

 

 

Also i found this cool project which uses Rpi.GPIO -> https://github.com/Dan-in-CA/SIP; how would I support here the Orange PI?

Link to comment
Share on other sites

Yes, the sunxi_gpio_init() should be called first.

Then, setting pinmode is done by sunxi_gpio_set_cfgpin().

sunxi_gpio_output() is the equivalent to digitalWrite().

The code in above link is a bit hard to understand without the schematic of the project.

It seems that it is talking to an external shift register with clock/data/latch, probably an 74HC595.

The OT_DIRECT_POS/OT_DIRECT_NEG seems to be equivalent of SUNXI_GPIO_OUTPUT/SUNXI_GPIO_INPUT.

Personally, I will re-write this code from scratch instead of trying to port it as is.

Link to comment
Share on other sites

I've watched some youtube review and the guys said he couldnt get i2c and spi to work on the orange pi zero...Did you test it already? Does it work?

 

Can you write a mini tutorial regarding those fex's etc.?

Link to comment
Share on other sites

I've watched some youtube review and the guys said he couldnt get i2c and spi to work on the orange pi zero...Did you test it already? Does it work?

 

I2C and SPI works since ages on Oranges. With legacy kernel and thanks to Martin's DT overlay work also with mainline kernel. Just use the Google powered forum search and enter 'spi i2c orange pi' (that's the first attempt if something doesn't work: use a search engine. And 'spi i2c orange pi' entered in any search engine will most probably also show numerous threads here in the forum with real use cases)

 

OPi Zero is just another compatible H2+/H3 device so the only question is how to make the pins accessible or to configure them correctly (the fex stuff -- no idea whether anyone already tested through whether the pin mappings are correct or not in the fex file we ship)

Link to comment
Share on other sites

So the default "Jessie server" OPI-0 image uses legacy kernel right?  

 

Yeah I was watching that video, that you linked...

 

Would be nice if there was a tutorial regarding this fex's and stuff...Its like alien language to me :D.

Link to comment
Share on other sites

If you are talking about this youtube https://m.youtube.com/watch?v=MzxLYGWQddA, the guy didn't even investigate ...

I've the I2C working, and for SPI I will probably need to tweak DT, since I'm working exclusively with Mainline.

(So, I can't provide recipe for FEX, since I don't use Legacy)

 

Hey Martin,

In a related question, i'm trying to get the GPIO / SPI working with pi zero.

I'm used to A13, so have experience with fex.

The objective is to move to mainline tho, but it's pretty hard to find good info what steps to take etc.

So my question is: for mainline zero GPIO / SPI access in kernel space(preferably) or user space, where can i find good guides / tutorials ?

Thanks in advance!

Link to comment
Share on other sites

In Mainline, the devices are defined my Device-Tree loaded by the kernel.

There is plenty of docs/tutorial that you can find on Google about DT.

Some for different platforms, but it doesn't matter if it to read and learn about DT.

 

For PiZero specific case, the SPIs are already defined in DT, but not visible in userspace until you add an SPIDEV overlay like the following one.

The /dev/spidev0.0 will appear and could be used by python library orangepi_PC_gpio_pyH3.

The /dev/spidev1.0 won't appear unless you disable led_status and uart3 since they are sharing the same pins.

 

 

// Enable the spidev interface
/dts-v1/;
/plugin/;

/ {
    compatible = "allwinner,sun8i-h3";

    fragment@0 {
        target-path = "/aliases";
        __overlay__ {
            /* Path to the SPI controller nodes */
            spi0 = "/soc@01c00000/spi@01c68000";
            spi1 = "/soc@01c00000/spi@01c69000";
        };
    };
    fragment@1 {
        target = <&spi0>;
        __overlay__ {
            pinctrl-names = "default";
            pinctrl-0 = <&spi0_pins_a>, <&spi0_cs0_pins_a>;
            status = "okay";
            #address-cells = <1>;
            #size-cells = <0>;
            spidev@0 {
                compatible = "spidev";
                reg = <0x0>;
                spi-max-frequency = <1000000>;
            };
        };
    };
    fragment@2 {
        target = <&spi1>;
        __overlay__ {
            pinctrl-names = "default";
            pinctrl-0 = <&spi1_pins_a>, <&spi1_cs0_pins_a>;
            status = "okay";
            #address-cells = <1>;
            #size-cells = <0>;
            spidev@0 {
                compatible = "spidev";
                reg = <0x0>;
                spi-max-frequency = <1000000>;
            };
        };
    };
};

 

Link to comment
Share on other sites

Okay great, this is very valuable.

I understand the spidev as it gets mounted and you can just use it like a file system.

 

I guess if i want to make a kernel module, i can just address them by bus 0 and bus 1 and use the spi kernel api to move further.

Am i right that i need to delete the 

            spidev@0 {
                compatible = "spidev";
                reg = <0x0>;
                spi-max-frequency = <1000000>;
            };

part for using it with my own kernel module ? Else it gets mounted to the spidev ?

 

Edit: Too fast. I will need to remove this piece :

// Enable the spidev interface
/dts-v1/;
/plugin/;
Edited by Ribster
Link to comment
Share on other sites

Right, you don't need my DT overlay shown in the spoiler if you are not using spidev in userspace.

In fact, you just need to replace in the main DT the status "disabled" by "okay".

For accessing the SPI itself in kernel-space, I'm not fluent with that, but you can take a look at other drivers present in the mainline.

Link to comment
Share on other sites

Of course ! you need to make sure they are properly set in Legacy Fex or Mainline DT as plain GPIO and remove the UART2 definition if present.

Same can be done with I2C and/or SPI if you don't need them.

Good day Martin, could you explain how to configure ports on OPi Zero as a GPIO ports? What i must to do?

Thanks in advance.

Link to comment
Share on other sites

Would be nice if someone could write step by step a tutorial for commands on how to enable gpio's and i2c for example.

Since as you see by these questions, we normal users (nonprogrammers), don't understand it. If there was such a tutorial one could then just link to it on subsequent questions.

But would really need to be detailed, with the bash commands.

Link to comment
Share on other sites

Would be nice if someone could write step by step a tutorial for commands on how to enable gpio's and i2c for example.

Since as you see by these questions, we normal users (nonprogrammers), don't understand it. If there was such a tutorial one could then just link to it on subsequent questions.

But would really need to be detailed, with the bash commands.

Since it is python on the A20, you can search the web for examples. They do exist.

Even in the official python docs. It's not that hard. You can always make a push request for that A20 python repo to add your examples once you figured it out.

https://pypi.python.org/pypi/pyA20

Link to comment
Share on other sites

Where did you read that pyA20 works on it? So I just flash armbian and then I use this python library and it works? I don't have to configure stuff inside fex?

Can someone post a fex compatible with this library and tell me how I use it?

Link to comment
Share on other sites

Hi Martin,

I have built and installed the GPIO library on my Orange Pi Zero that you suggested. But unfortunately I noticed that STATUS_LED pin is wrongly mapped after the install. I tried to build and install the library once again after the modification of the mapping.h but after the initialization of led >>>  led1 = port.STATUS_LED and print out >>> print led1 I am getting a wrong pin (15) instead of PA17. How shall I fix this problem? Thanks, Michal

Link to comment
Share on other sites

Michal so some pins work (for which the mapping is correct)?  Like pins 3, 5, 7 ,11, 13 and 15?  So with this lib I can do digital high and low?

 

I2c is not connected to this library right, it works standalone? Martinayotte which library would be good to test i2c (do you know if it works on opizero)?

 

Thanks guys for help!

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

Terms of Use - Privacy Policy - Guidelines