Omega2S+ gpiomux embedded C control
-
Hello,
I am working on a project with the Onion Omega2S+ that requires the use of pin 33 (GPIO 6/SPI_CS1) to be used as a GPIO. However, the gpiomux default is to use the pin as SPI_CS1.
The overall goal is to be able to persistently use that pin as a GPIO, even after reboot. Additionally, I will be triggering the GPIO toggling through an embedded C program with the sysfs system. I have two possible solution paths that I've thought of, but don't know if possible to implement:
Ideal solution—Is it possible to accomplish the
omega2-ctrl gpiomux set spi_cs1 gpio
command in a C program? Would asystem()
call work (something likesystem(omega2-ctrl gpiomux set spi_cs1 gpio)
)? Is that the best way to accomplish it?Secondary solution—If the above isn't possible, can I have the gpiomux command at least save my non-default setup so that it persists on reboot?
Thanks!
-
@teddylambert I am no expert on this, but if you check out the DTSi files in the build source and perhaps search the forums, I think you can default the pin registers using this method.
-
Hello @teddylambert
Ideal solution—Is it possible to accomplish the omega2-ctrl gpiomux set spi_cs1 gpio command in a C program?
Yes, if you want to learn how, have a look at the omega2-ctrl sources on github, in particular gpiomux.c and gpiomux.h
What it does is setting and clearing bits in the
GPIO1_MODE
andGPIO2_MODE
registers of the MT7688 SoC (see MT7688 Datasheet page 61ff).These registers are at 0x10000060 and 0x10000064 in the physical address space - so to access these from user space (any regular program runs in user space),
omega2-ctrl
needs to map that part of physical addresses into user space.
Thegpiomux_mmap_open
function ingpiomux.c
maps 0x10000000..0x100003FF into thegpio_mmap_reg
pointer, using the linuxmmap
function. After that, SoC registers can be accessed via that pointer (i.e.gpio_mmap_reg+0x60
points toGPIO1_MODE
).Would a system() call work (something like system(omega2-ctrl gpiomux set spi_cs1 gpio))? Is that the best way to accomplish it?
Yes, using
system
works too, and is much less code. So that might be the best way in your caseHowever, I mentioned all of the above because via
mmap
you could access all SoC registers, and do many more things (superpower, be careful!) not exposed by omega2-ctrl.Another reason to directly access SoC registers and bypass all Linux tools would be speed -
system()
is orders of magnitude slower (several milliseconds) because it involves forking a shell which in turn forks omega2-ctrl. That's fine for a one-time setup operation, but sometimes one needs nanosecond order speed, which is whatmmap
-ed direct access to SoC registers can deliver.