cl gpio
1.1.0A library for the Linux GPIO kernel module as used on hobby kits such as the Raspberry Pi
Table of Contents
About cl-gpio
This is a bindings library for the Linux GPIO kernel module as described on https://www.kernel.org/doc/Documentation/gpio/sysfs.txt. It provides both primitive access and more sophisticated constructs to work with interrupts and such.
How To
Enumerate a list of all available GPIO pins on your system:
(gpio:pins)
If you have GPIO pins on your system and GPIO:PINS returns NIL, then try to export them first:
;; Replace 26 with how many pins you have:
(loop for i from 0 to 26
do (gpio:export i))
GPIO:PINS should now return the pins' statuses:
(gpio:pins) =>
(#<CL-GPIO:PIN 0 :OUT> #<CL-GPIO:PIN 1 :IN> #<CL-GPIO:PIN 10 :IN>
#<CL-GPIO:PIN 11 :IN> #<CL-GPIO:PIN 12 :IN> #<CL-GPIO:PIN 13 :IN>
#<CL-GPIO:PIN 14 :IN> #<CL-GPIO:PIN 15 :IN> #<CL-GPIO:PIN 16 :IN>
#<CL-GPIO:PIN 17 :IN> #<CL-GPIO:PIN 18 :IN> #<CL-GPIO:PIN 19 :IN>
#<CL-GPIO:PIN 2 :IN> #<CL-GPIO:PIN 20 :IN> #<CL-GPIO:PIN 21 :IN>
#<CL-GPIO:PIN 22 :IN> #<CL-GPIO:PIN 23 :IN> #<CL-GPIO:PIN 24 :IN>
#<CL-GPIO:PIN 25 :IN> #<CL-GPIO:PIN 26 :IN> #<CL-GPIO:PIN 3 :IN>
#<CL-GPIO:PIN 4 :IN> #<CL-GPIO:PIN 5 :IN> #<CL-GPIO:PIN 6 :IN>
#<CL-GPIO:PIN 7 :IN> #<CL-GPIO:PIN 8 :IN> #<CL-GPIO:PIN 9 :IN>)
You can then access the direction, edge, and active-low of each pin:
(setf (gpio:direction 0) :out)
(gpio:active-low 0)
If you try to read or set a PIN's value, its direction is automatically adjusted as necessary:
(gpio:value 0)
(setf (gpio:value 0) T)
On SBCL you can also wait for values:
(progn (gpio:await-value 0)
(format T "Whoah, 0's edge is ~a to ~:[0~;1~]" (edge 0) (value 0)))
Or even install handlers:
(defun pin-value-handler (pin value)
(format T "~& ~a changed value to ~a." pin value))
(gpio:with-pin-handler (#'pin-value-handler 0 :falling)
(format T "Waiting for a change on 0...")
(loop (sleep 0.001)))
Naturally you'll have to refer to your particular board/system's specification to be able to tell which pins are supposed to be used for what. For the Raspberry Pi 2/3/4, it would be:
(Source)
System Information
Definition Index
-
CL-GPIO
- ORG.SHIRAKUMO.GPIO
- GPIO
No documentation provided.-
EXTERNAL STRUCTURE PIN
Representative type to encapsulate a GPIO pin. This will cache current pin properties. Note that it will not be updated automatically should changes to the pin occur from elsewhere in the system. Only updates done through this high-level interface will be tracked. See MAKE-PIN See ENSURE-PIN See NAME See CHIP See DIRECTION See EDGE See ACTIVE-LOW See VALUE
-
EXTERNAL FUNCTION ACTIVE-LOW
- PIN
- &REST
Accesses the pin's I/O active-low. If the pin does not yet exist or is not exported, it will be. The system GPIO's value is not adjusted if the cached value is already the same as the value attempted to be set with this. See ENSURE-PIN See CL-GPIO-LLI:ACTIVE-LOW See PIN-ACTIVE-LOW
-
EXTERNAL FUNCTION (SETF ACTIVE-LOW)
- ACTIVE-LOW
- PIN
- &REST
No documentation provided. -
EXTERNAL FUNCTION ALL-PINS
Returns a list of PIN instances for all pins on the system. See CL-GPIO-LLI:AVAILABLE-PINS See ENSURE-PIN
-
EXTERNAL FUNCTION AWAIT-VALUE
- PIN
- &OPTIONAL
- TIMEOUT
- &REST
Wait until the pin has a value that we can read. If TIMEOUT is specified and reached before a value becomes accessible, NIL is returned. Otherwise, true is returned. This function is available on the following implementations: * SBCL See ENSURE-PIN
-
EXTERNAL FUNCTION CALL-WITH-PIN-HANDLER
- FUNCTION
- HANDLER
- PIN
- &OPTIONAL
- EDGE
- ACTIVE-LOW
- &REST
Make the HANDLER function be called if the PIN changes value during the evaluation of FUNCTION. The HANDLER is called with the corresponding PIN instance and the new value as arguments. This function is available on the following implementations: * SBCL See ENSURE-PIN See EDGE See ACTIVE-LOW
-
EXTERNAL FUNCTION CHIP
- PIN
- &REST
Returns the pin's chip device name. See PIN-CHIP
-
EXTERNAL FUNCTION DIRECTION
- PIN
- &REST
Accesses the pin's I/O direction. If the pin does not yet exist or is not exported, it will be. The system GPIO's value is not adjusted if the cached value is already the same as the value attempted to be set with this. See ENSURE-PIN See CL-GPIO-LLI:DIRECTION See PIN-DIRECTION
-
EXTERNAL FUNCTION (SETF DIRECTION)
- DIRECTION
- PIN
- &REST
No documentation provided. -
EXTERNAL FUNCTION EDGE
- PIN
- &REST
Accesses the pin's I/O edge. If the pin does not yet exist or is not exported, it will be. The system GPIO's value is not adjusted if the cached value is already the same as the value attempted to be set with this. See ENSURE-PIN See CL-GPIO-LLI:EDGE See PIN-EDGE
-
EXTERNAL FUNCTION (SETF EDGE)
- EDGE
- PIN
- &REST
No documentation provided. -
EXTERNAL FUNCTION ENSURE-PIN
- PIN
- &OPTIONAL
- REFRESH
- &REST
Ensure to get a PIN instance in return. Accepts either a pin's name/id number or a pin instance. If REFRESH is true, a fresh PIN instance is returned that has its values taken from the system's GPIO values. See *PIN-CACHE* See MAKE-PIN See PIN
-
EXTERNAL FUNCTION EXPORT
- &REST
- PINS
- &REST
Export the specified pins and return a list of according PIN instances. See ENSURE-PIN See CL-GPIO-LLI:EXPORT-PIN
-
EXTERNAL FUNCTION NAME
- PIN
- &REST
Returns the pin's name or ID. See PIN-NAME
-
EXTERNAL FUNCTION PINS
Returns a list of available/exported PIN instances. See CL-GPIO-LLI:EXPORTED-PINS See ENSURE-PIN
-
EXTERNAL FUNCTION UNEXPORT
- &REST
- PINS
- &REST
Unexport the specified pins and invalidate their cache. See *PIN-CACHE* See CL-GPIO-LLI:UNEXPORT-PIN
-
EXTERNAL FUNCTION VALUE
- PIN
- &REST
Accesses the pin's I/O value. If the pin does not yet exist or is not exported, it will be. The pin's I/O direction is automatically adjusted if necessary depending on whether the value is read or set by setf. The value returned by this is never cached. See ENSURE-PIN See CL-GPIO-LLI:VALUE
-
EXTERNAL FUNCTION (SETF VALUE)
- VALUE
- PIN
- &REST
No documentation provided. -
EXTERNAL MACRO WITH-PIN-HANDLER
- HANDLER
- PIN
- &OPTIONAL
- EDGE
- ACTIVE-LOW
- &REST
- &BODY
- BODY
- &REST
Shorthand to call a handler function on PIN value change during the evaluation of BODY. See CALL-WITH-PIN-HANDLER
-
CL-GPIO-LLI
- ORG.SHIRAKUMO.GPIO.LLI
No documentation provided.-
EXTERNAL SPECIAL-VARIABLE *GPIO-ROOT*
The root directory of the GPIO system devices. Should be /sys/class/gpio/
-
EXTERNAL FUNCTION ACTIVE-LOW
- PIN
- &REST
Accessor to whether the GPIO pin has an active low. The value should be either NIL or T.
-
EXTERNAL FUNCTION (SETF ACTIVE-LOW)
- VALUE
- PIN
- &REST
No documentation provided. -
EXTERNAL FUNCTION AVAILABLE-PINS
Return a list of all available GPIO pins on the system. Note that the pins are not necessarily accessible; they may need to be exported first.
-
EXTERNAL FUNCTION BASE
- CHIP
- &REST
Return the GPIO chip's base pin number.
-
EXTERNAL FUNCTION CHIP-FILE
- CHIP
- SUB
- &REST
Returns a file for the specified GPIO chip. See GPIO-FILE
-
EXTERNAL FUNCTION CHIP-PINS
- CHIP
- &REST
Return a list of GPIO pin numbers on the chip.
-
EXTERNAL FUNCTION CHIPS
Returns a list of known GPIO chips.
-
EXTERNAL FUNCTION DIRECTION
- PIN
- &REST
Accessor to the GPIO pin's direction. The value should be either :IN or :OUT.
-
EXTERNAL FUNCTION (SETF DIRECTION)
- VALUE
- PIN
- &REST
No documentation provided. -
EXTERNAL FUNCTION EDGE
- PIN
- &REST
Accessor to the GPIO pin's interrupt edge. The value should be one of :NONE :RISING :FALLING :BOTH
-
EXTERNAL FUNCTION (SETF EDGE)
- VALUE
- PIN
- &REST
No documentation provided. -
EXTERNAL FUNCTION EXPORT-PIN
- &REST
- PINS
- &REST
Export the specified pins so that they may be accessed from userspace.
-
EXTERNAL FUNCTION EXPORTED-PINS
Returns a list of GPIO pins that are accessible.
-
EXTERNAL FUNCTION GPIO-FILE
- SUB
- &REST
Returns an absolute path to the requested gpio file. See *GPIO-ROOT*
-
EXTERNAL FUNCTION LABEL
- CHIP
- &REST
Return the GPIO chip's label.
-
EXTERNAL FUNCTION NGPIO
- CHIP
- &REST
Return the number of GPIO pins on the chip.
-
EXTERNAL FUNCTION PIN-FILE
- PIN
- SUB
- &REST
Returns a file for the specified GPIO pin. See GPIO-FILE
-
EXTERNAL FUNCTION UNEXPORT-PIN
- &REST
- PINS
- &REST
Unexport the specified pins so that they can no longer be accessed from userspace.
-
EXTERNAL FUNCTION VALUE
- PIN
- &REST
Accessor to the GPIO pin's value. When reading, the pin's direction should be :IN When setting, the pin's direction should be :OUT The value should be either NIL or T.
-
EXTERNAL FUNCTION (SETF VALUE)
- VALUE
- PIN
- &REST
No documentation provided.