(08-29-2016, 05:25 AM)MarkHaysHarris777 Wrote: For the sake of this discussion, lets keep it to PC4.

... I am going to post a script below written by longsleep which converts Pine GP names to their Pine GPIO number. There are three ways to number the pins on the PI bus (let's just keep the discussion there for now).

Board numbers:  the actual physical number of the pin on the pcb... 

BCM numbers:  the broadcom GPIO numbering scheme

Pine GPIO:  the numbers of the GPIO pins from the Pine perspective.

The following script will convert the name of the pin (in this case PC4) to its Pine GPIO number.  Place the code in a script called sysfs_gpio.sh   chmod it with 0754,  and run it with  ./sysfs_gpio.sh PC4   ... it will return 68.


import sys
import string

def convert(value):
    value = value.upper()
    alp = value[1]
    idx = string.ascii_uppercase.index(alp)
    num = int(value[2:], 10)
    res = idx * 32 + num
    return res

if __name__ == "__main__":
    args = sys.argv[1:]
    if not args:
        print("Usage: %s <pin>" % sys.argv[0])

    print("%d" % convert(args[0]))

Try activating the pin(32) , GPIO(12), PineGPIO(68) using sysfs, or using RPi.GPIO-PineA64.

If it works you know that the hardware is not at fault.  If not, there may be something wrong on your board. If it works with sysfs, then try again with your C coding.  Thank you for sharing;  I have not used C to access the GPIO on the PineA64-- very interesting.  Hopefully you will get enough of a hint about the PineA64 numbering with pin 68, PC4, GPIO(12),  pin(32)....

edit:  try using 0x44   which is the hex for 68

Hello Mark

First thank you for your help Smile

However what is the problem? I already know about the possibility to access GPIOs via sysfs. I know when I export pin 68 via gpio class I can then set its functionality and control its output value and this works well.

Event this laberling is in this link I use for refering pins on PI 2 connector


Even on the bottom is recalculation of pins and even link to some forum's thread about this issue. Wink

However, 1 thing I did not understand that pin labeling. I get the point pin(32) is actual physical pin on PI 2 connector of PINE64 board, PineGPIO(68) is gpio used by sysfs when we access it via gpio class. But what is GPIO(12) labeling?

Right now I am going to my actual issue I want to solve.

I want to control GPIOs through my program written in C code or any other programming languages.
What I want to test is direct memory access when I map /dev/mem file image to virtual memory then I would able to directly write/read GPIO registers

For this I open /dev/mem

fd = open("/dev/mem", O_RDWR | O_SYNC);

then I use given file descriptor "fd" in map function and map it to the address "p_mem" in virtual memory

p_mem = mmap(NULL, page_size * 2, PROT_READ | PROT_WRITE, MAP_SHARED, fd, BASE_ADDR);

I use BASE_ADDR which is this value 0x01C20000 and it points to CCU register. I cannot directly start mapping from the address 0x01C20800 which is starting address of PIO registers, because this one is not aligned to page sizes and I get error.

Thus, after mapping it and getting back void pointer p_mem I recast it to unsigned long pointer as we use 64bit architecture and assign it to unsigned long pointer variable named "gpio"

gpio = (volatile unsigned long *) p_mem;

In turn, I use this address and add to it offset GPIO_REG_OFFSET which value is 0x800 and this along with gpio pointer should give me the starting address of PIO register 0x01C20800. Then I also add offset 0x48 which should give me the address of PC configure register 0 (0x01C20848) where the configuration of pin PC4 is located.

*(gpio + GPIO_REG_OFFSET + 0x48) &= ~(7 << 16);
*(gpio + GPIO_REG_OFFSET + 0x48) |= (1 << 16);

By default all data values in register are set to 1 what means all pins have disabled functionality. That is the reason that in code above I first shift 3 zeros starting from position 16, thus, 16-18 are set to zeros after this line. Because for PC4 there are 3 configuration bits and if we want to configure it to the output function we have to set it to 001 value. See manual page 377


In the second line I already set bit 16th to 1 what finally gives me value 001 for PC4 pin what in turn should set it to output function. Wink

Next I write to PC Data register (above manual reference page 380) by adding its offset 0x58 to PIO address, actually the same way as described in code above. Wink. And then write into this register value 0xff what should set first 8 pins and with it also pin PC4.

*(gpio + GPIO_REG_OFFSET + 0x58) |= 0xff;

In this case LED connected to PC4 (pin 32 on PI 2 connector of the board) should light. Unfortunately, it does not. So I am not sure where I actually do mistakes. Maybe that manual is not for this release of Allwinner A64 chip or I access pins incorrectly. I also tried to figure out, how I could utilize the value 68 (used for sysfs) in my code.

Any help in this way would be very appreciated. Big Grin

Thank you Mark and also to others.                                           Martin.

Messages In This Thread
C GPIO - by igna09 - 06-10-2016, 12:41 PM
RE: C GPIO - by martinayotte - 06-10-2016, 01:52 PM
RE: C GPIO - by igna09 - 06-10-2016, 02:16 PM
RE: C GPIO - by martinayotte - 06-10-2016, 03:10 PM
RE: C GPIO - by martind1983 - 08-24-2016, 09:55 AM
RE: C GPIO - by igna09 - 06-11-2016, 02:51 AM
RE: C GPIO - by martinayotte - 08-25-2016, 07:35 AM
RE: C GPIO - by martind1983 - 08-25-2016, 08:24 AM
RE: C GPIO - by MarkHaysHarris777 - 08-25-2016, 11:17 AM
RE: C GPIO - by martind1983 - 08-29-2016, 01:30 AM
RE: C GPIO - by martind1983 - 08-29-2016, 03:50 AM
RE: C GPIO - by MarkHaysHarris777 - 08-29-2016, 05:25 AM
RE: C GPIO - by martind1983 - 08-31-2016, 03:47 AM
RE: C GPIO - by pfeerick - 08-31-2016, 04:14 AM
RE: C GPIO - by martind1983 - 08-31-2016, 04:36 AM
RE: C GPIO - by MarkHaysHarris777 - 08-29-2016, 05:04 PM
RE: C GPIO - by MarkHaysHarris777 - 08-31-2016, 04:09 AM
RE: C GPIO - by KnReLe - 08-31-2016, 07:27 AM
RE: C GPIO - by martind1983 - 09-01-2016, 05:08 AM
RE: C GPIO - by MarkHaysHarris777 - 09-01-2016, 05:29 AM
RE: C GPIO - by martind1983 - 09-01-2016, 08:04 AM
RE: C GPIO - by KnReLe - 09-01-2016, 08:52 AM
RE: C GPIO - by martind1983 - 09-01-2016, 09:06 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  RPi.GPIO python module for Pine A64/A64+ aquilegia 98 105,200 12-15-2022, 08:40 PM
Last Post: Fadazo
  fm transmitter with gpio weasel18 2 3,792 09-10-2019, 04:28 AM
Last Post: desai_amogh
  How to use dts or other setup to declare gpio pin Interrupt (e.g. a button)? dkebler 1 2,908 06-12-2019, 10:37 AM
Last Post: dkebler
Lightbulb Sample GPIO codes highlighting RPi.GPIO-PineA64 and the PI bus MarkHaysHarris777 6 9,026 06-07-2019, 12:37 AM
Last Post: tllim
Star GPIO, SPI and I2C C++ Lib databit 7 9,172 02-04-2019, 05:45 AM
Last Post: Jeff R
Information Howto: Controlling Pine64 GPIO via the filesystem (sysfs) on linux pfeerick 4 10,294 01-24-2019, 03:36 AM
Last Post: Fifth
  GPIO and SPI SamR1 20 25,993 03-15-2018, 10:32 AM
Last Post: jomoengineer
Question GPIO shockr 7 12,325 03-11-2018, 01:52 AM
Last Post: jomoengineer
  Read GPIO problem shworker 14 16,976 08-17-2017, 01:21 PM
Last Post: martinayotte
  GPIO fiq capability joseph 3 4,925 11-10-2016, 06:07 PM
Last Post: joseph

Forum Jump:

Users browsing this thread: 3 Guest(s)