0 background
The Jetson TX1, TX2, AGX Xavier and Nano development boards contain a 40 pin GPIO connector, similar to the 40 pin connector in Raspberry Pi. NV officially gives a GPIO library, which is a Python version, and The RPi.GPIO Library of Raspberry Pi is the same. This paper introduces the content and usage of the library.
1 installation method
It can be installed directly through pip
sudo pip install Jetson.GPIO
Download sample code
git clone https://github.com/NVIDIA/jetson-gpio.git
In the Jetson GPIO project
- The lib/python path contains Python modules that implement all library functions. Gpio.py module is the main component that will be imported into the application and provide the required API, and gpio_event.py and gpio_pin_data.py is referenced by gpio.py and cannot be called directly by applications
- The samples path lists some application examples. The specific meanings are as follows
-
simple_input.py: this application uses the BCM pin numbering mode and reads the value at pin 12 of the 40 pin connector and prints the value to the screen.
-
simple_out.py: this application uses the BCM pin numbering mode of Raspberry Pi and outputs alternating high and low values on BCM pin 18 (or BOARD pin 12) every 2 seconds.
-
button_led.py: this application uses the BOARD pin number. It requires a button connected to pin 18 and GND, a pull-up resistor connecting pin 18 to 3V3, and an LED and current limiting resistor connected to pin 12. The application reads the button status and keeps the LED on for 1 second at a time, and the button is pressed.
-
button_event.py: this application uses the BOARD pin number. It requires a button connected to pin 18 and GND, a pull-up resistor connecting the button to 3V3, and an LED and current limiting resistor connected to pin 12. The application executes with a button_led.py has the same function, but performs blocking and waiting for button press events instead of constantly checking the value of the pin to reduce CPU utilization.
-
button_interrupt.py: this application uses the BOARD pin number. It requires a button connected to pin 18 and GND, a pull-up resistor connecting the button to 3V3, an LED and current limiting resistor connected to pin 12, and a second LED and current limiting resistor connected to pin 13. The application flashes slowly. Only when the button is pressed, the first LED flashes continuously and quickly, and the second LED flashes 5 times.
2 API functions
2.1 setting mode
For Jetson series boards, we need to set the mode to BOARD. We don't care about other modes
GPIO.setmode(GPIO.BOARD)
You can use the method below to get whether the setting is successful
mode = GPIO.getmode()
The results will be gpio.board, gpio.bcm, gpio.cvm and gpio.tegra_ One of SOC or None
2.2 input pin
Before calling the gpio pin, the first step is to define the input or output direction.
Set pin to input direction
GPIO.setup(channel, GPIO.IN)
Among them, channel is the specific pin number, such as 30, 25, etc
Read the input value of the pin
state = GPIO.input(channel)
The return value is GPIO.LOW or GPIO.HIGH, i.e. low signal or high signal
2.3 output pin
Set pin to output direction
GPIO.setup(channel, GPIO.OUT)
It is also supported to specify whether the default output is high or low
GPIO.setup(channel, GPIO.OUT, initial=GPIO.HIGH)
Of course, it also supports setting the direction of multiple pins at the same time, such as
channels = [18, 12, 13] GPIO.setup(channels, GPIO.OUT)
After definition, set the output value of the pin to support multiple modes
channels = [18, 12, 13] # or use tuples GPIO.output(channels, GPIO.HIGH) # or GPIO.LOW # set first channel to HIGH and rest to LOW GPIO.output(channel, (GPIO.LOW, GPIO.HIGH, GPIO.HIGH))
2.4 inspection direction
You can use the interface below to check whether a pin is an input pin or an output pin
GPIO.gpio_function(channel)
The return value is GPIO.IN or GPIO.OUT
2.5 clear pin function
After the program runs, it is best to have a clear operation to restore the pin to the default state
GPIO.cleanup()
If you do not want to clear the status of all pins, you can also choose to clear the specified pins
GPIO.cleanup(chan1) # cleanup only chan1 GPIO.cleanup([chan1, chan2]) # cleanup only chan1 and chan2 GPIO.cleanup((chan1, chan2)) # does the same operation as previous statement
3 interrupt function
When monitoring the status of input pins, in order to prevent non-stop polling and occupying cpu resources, API provides three interfaces to improve
3.1 wait_for_edge()
This function blocks the calling thread until the provided edge is detected, such as
GPIO.wait_for_edge(channel, GPIO.RISING)
The second parameter represents the change signal to be detected and supports GPIO.RISING, GPIO.FALLING or GPIO.BOTH, and rising edge trigger, falling edge trigger or both. In addition, the timeout can be set
# timeout is in milliseconds GPIO.wait_for_edge(channel, GPIO.RISING, timeout=500)
The return of the function is the detected signal, or it returns None after timeout
3.2 event_detected()
This function can be used to periodically check whether events have occurred since the last call. This function can be set and called as follows
# set rising edge detection on the channel GPIO.add_event_detect(channel, GPIO.RISING) run_other_code() if GPIO.event_detected(channel): do_something()
Similarly, several trigger modes such as rising edge, falling edge or rising and falling edge are also supported
3.3 callback function
The callback function is executed in the new thread, so the main program and callback function can run concurrently to respond to events, such as
# define callback function def callback_fn(channel): print("Callback called from channel %s" % channel) # add rising edge detection GPIO.add_event_detect(channel, GPIO.RISING, callback=callback_fn)
You can also specify multiple callback functions
def callback_one(channel): print("First Callback") def callback_two(channel): print("Second Callback") GPIO.add_event_detect(channel, GPIO.RISING) GPIO.add_event_callback(channel, callback_one) GPIO.add_event_callback(channel, callback_two)
However, it should be noted that multiple callback functions will be executed sequentially rather than simultaneously, which is related to the threading mechanism of python. To prevent multiple calls to the callback function by multiple events, you can choose to set the de dithering time:
# bouncetime set in milliseconds GPIO.add_event_detect(channel, GPIO.RISING, callback=callback_fn, bouncetime=200)
If you do not need to detect changes, you can use the clear function
GPIO.remove_event_detect(channel)
4 PWM function
samples/simple_pwm.py shows how to use PWM function. There are two points to note:
(1) Not every development board has PWM function. Jetson Nano supports 2 PWM channels and Jetson AGX Xavier supports 3 PWM channels. Jetson TX1 and TX2 do not support any PWM channels.
(2) The jetson board does not turn on the PWM function by default. It needs to be configured with the jetson IO tool to enable the PWM function. Refer to< NVIDIA Jetson SPI function enable >Make settings
5 C + + interface
In addition to the official Python interface, there is also the GPIO API of C + + interface. The functions are the same as those of python, the use method is similar, and the installation method is the same
git clone https://github.com/pjueon/JetsonGPIO cd JetsonGPIO/build make all sudo make install
Compilation method
g++ -o your_program_name your_source_code.cpp -lpthread -lJetsonGPIO
The API interface implementation methods described above will not be repeated here, but can be viewed directly Project description that will do