Julia interface for Raspberry Pi GPIO using /dev/gpiomem
.
/dev/gpiomem
provides userspace access to the BCM2835 GPIO control registers
(see BCM2835-ARM-Peripherals.pdf p89).
pkg> add https://github.com/notinaboat/PiGPIOMEM.jl
julia> using PiGPIOMEM
julia> pin7 = GPIOPin(7)
GPIOPin(7)
julia> pin7[]
false
julia> set_pullup(pin7)
julia> pin7[]
true
julia> set_output_mode(pin7)
julia> pin7[] = false
julia> pin7[]
false
julia> set(pin7)
julia> pin7[]
true
julia> clear(pin7)
julia> pin7[]
false
julia> pin17 = GPIOPin(17; output=true)
GPIOPin(17, output=true)
julia> pin17[] = true
julia> clear(pin7, pin17)
julia> pin7[], pin17[]
(false, false)
julia> set(pin7, pin17)
julia> pin7[], pin17[]
(true, true)
See src/PiGPIOMEM.jl
or online help:
julia> using PiGPIOMEM
help?> PiGPIOMEM
The implementation aims to minimise pin access overhead. The pin number is bounds checked only once at construction time and register addresses and bit masks are pre-computed.
The resulting compiled pin access functions are very small:
julia> code_native(set, [GPIOPin]; debuginfo=:none)
.text
ldr r1, [r0, #8]
ldr r0, [r0, #12]
str r1, [r0]
bx lr
julia> code_native(clear, [GPIOPin]; debuginfo=:none)
.text
ldr r1, [r0, #8]
ldr r0, [r0, #16]
str r1, [r0]
bx lr
julia> code_native(getindex, [GPIOPin]; debuginfo=:none)
.text
ldr r1, [r0, #8]
ldr r0, [r0, #20]
ldr r0, [r0]
ands r0, r1, r0
movne r0, #1
bx lr
The following test produces a 9.7MHz square wave on a Raspberry Pi Zero W.
julia> using LLVM
julia> @noinline nop() = LLVM.Interop.@asmcall("nop")
julia> toggle(pin) = while true
clear(pin)
nop()
nop()
set(pin)
nop()
nop()
end
toggle (generic function with 1 method)
julia> toggle(GPIOPin(21, output=true))
-
JuliaBerry/PiGPIO.jl -- uses the
pigpiod
daemon interface. -
notinaboat/PiGPIOC.jl --
ccall
wrappers for thepigpio
C Interface. -
ronisbr/BaremetalPi.jl -- uses
/dev/gpiomem
,/dev/spidevX.X
,/dev/i2c-X
etc.