Jetgpio 2.1
C library to manage the GPIO header of the Nvidia JETSON boards
Functions
jetgpio.h File Reference
#include <stddef.h>
#include <stdint.h>
Include dependency graph for jetgpio.h:

Go to the source code of this file.

Functions

int gpioInitialise (void)
 Initialises the library. gpioInitialise must be called before using the other library functions, it stores the status of all the relevant registers before using/modifying them. More...
 
void gpioTerminate (void)
 Terminates the library. This function restores the used registers to their previous state and releases memory. More...
 
int gpioSetMode (unsigned gpio, unsigned mode)
 Sets the GPIO mode, typically input or output. More...
 
int gpioRead (unsigned gpio)
 Reads the GPIO level, on or off, 0 or 1. Arduino style: digitalRead. Wheter a pin has been set as input or output it can be read with this function. More...
 
int gpioWrite (unsigned gpio, unsigned level)
 Sets the GPIO level, on or off. Arduino style: digitalWrite. More...
 
int gpioSetISRFunc (unsigned gpio, unsigned edge, unsigned debounce, unsigned long *timestamp, void(*f)())
 Registers a function to be called (a callback) whenever the specified. More...
 
int gpioSetPWMfrequency (unsigned gpio, unsigned frequency)
 Sets the frequency in hertz to be used for the GPIO. More...
 
int gpioPWM (unsigned gpio, unsigned dutycycle)
 Starts PWM on the GPIO, dutycycle between 0 (off) and range (fully on). Arduino style: analogWrite. More...
 
int i2cOpen (unsigned i2cBus, unsigned i2cFlags)
 This returns a handle for the device at the address on the I2C bus. More...
 
int i2cClose (unsigned handle)
 This closes the I2C device associated with the handle. More...
 
int i2cWriteByteData (unsigned handle, unsigned i2cAddr, unsigned i2cReg, unsigned bVal)
 This writes a single byte to the specified register of the device associated with handle. More...
 
int i2cReadByteData (unsigned handle, unsigned i2cAddr, unsigned i2cReg)
 This reads a single byte from the specified register of the device associated with handle. More...
 
int i2cWriteWordData (unsigned handle, unsigned i2cAddr, unsigned i2cReg, unsigned wVal)
 This writes two bytes to the specified consecutive register(s) of the device associated with handle. More...
 
int i2cReadWordData (unsigned handle, unsigned i2cAddr, unsigned i2cReg)
 This reads two bytes from the specified consecutive register(s) of the device associated with handle. More...
 
int spiOpen (unsigned spiChan, unsigned speed, unsigned mode, unsigned cs_delay, unsigned bits_word, unsigned lsb_first, unsigned cs_change)
 This function returns a handle for the SPI device on the channel. Data will be transferred at baud bits per second. The flags may be used to modify the default behaviour of a 4-wire operation, mode 0, active low chip select. There are 2 SPI channels called SPI1 & SPI2 on Nano and SPI0 & SPI1 on Orin Nano, Orin NX and Xavier NX. For Orin AGX there is only 1 SPI channel: SPI1 (pins 21, 19...so forth). The pins used are given in the following table. More...
 
int spiClose (unsigned handle)
 This functions closes the SPI device identified by the handle. More...
 
int spiXfer (unsigned handle, char *txBuf, char *rxBuf, unsigned len)
 This function transfers len bytes of data from txBuf to the SPI device associated with the handle. Simultaneously len bytes of data are read from the device and placed in rxBuf. More...
 
int extPeripheralRate (unsigned clk_name, unsigned rate)
 Orin family only. This functions changes the rate of one of the external peripheral clocks. There are 2 (pins 29 and 31) in Orin Nano and only one (pin 7) in Orin AGX. More...
 
int extPeripheralEnable (unsigned clk_name)
 Orin family only. This functions enables one of the external peripheral clocks. There are 2 (pins 29 and 31) in Orin Nano and only one (pin 7) in Orin AGX. More...
 
int extPeripheralDisable (unsigned clk_name)
 Orin family only. This functions disables one of the external peripheral clocks. There are 2 (pins 29 and 31) in Orin Nano and only one (pin 7) in Orin AGX. More...
 

Function Documentation

◆ extPeripheralDisable()

int extPeripheralDisable ( unsigned  clk_name)

Orin family only. This functions disables one of the external peripheral clocks. There are 2 (pins 29 and 31) in Orin Nano and only one (pin 7) in Orin AGX.

Parameters
clk_namepossible values are: EXTPERIPH3 -> pin 29 Orin Nano, EXTPERIPH4 -> pin 31 Orin Nano and pin 7 in Orin AGX
Returns
Returns 0 if OK, otherwise a negative number
extPeripheralDisable(EXTPERIPH3); // disabling clock routed to pin 29 in Orin Nano
int extPeripheralDisable(unsigned clk_name)
Orin family only. This functions disables one of the external peripheral clocks. There are 2 (pins 29...
Definition: orin.c:2837

◆ extPeripheralEnable()

int extPeripheralEnable ( unsigned  clk_name)

Orin family only. This functions enables one of the external peripheral clocks. There are 2 (pins 29 and 31) in Orin Nano and only one (pin 7) in Orin AGX.

Parameters
clk_namepossible values are: EXTPERIPH3 -> pin 29 Orin Nano, EXTPERIPH4 -> pin 31 Orin Nano and pin 7 in Orin AGX
Returns
Returns 0 if OK, otherwise a negative number
extPeripheralEnable(EXTPERIPH3); // enabling clock routed to pin 29 in Orin Nano
int extPeripheralEnable(unsigned clk_name)
Orin family only. This functions enables one of the external peripheral clocks. There are 2 (pins 29 ...
Definition: orin.c:2802

◆ extPeripheralRate()

int extPeripheralRate ( unsigned  clk_name,
unsigned  rate 
)

Orin family only. This functions changes the rate of one of the external peripheral clocks. There are 2 (pins 29 and 31) in Orin Nano and only one (pin 7) in Orin AGX.

Parameters
clk_namepossible values are: EXTPERIPH3 -> pin 29 Orin Nano, EXTPERIPH4 -> pin 31 Orin Nano and pin 7 in Orin AGX
ratenumber between 3200000 to 51000000, this is the value of the clock in Hz
Returns
Returns 0 if OK, otherwise a negative number
extPeripheralRate(EXTPERIPH3, 25000000); // setting up clock routed to pin 29 in Orin Nano to 25MHz
int extPeripheralRate(unsigned clk_name, unsigned rate)
Orin family only. This functions changes the rate of one of the external peripheral clocks....
Definition: orin.c:2768

◆ gpioInitialise()

int gpioInitialise ( void  )

Initialises the library. gpioInitialise must be called before using the other library functions, it stores the status of all the relevant registers before using/modifying them.

Returns
Returns 0 if OK, otherwise a negative number
if (gpioInitialise() < 0)
{
// jetgpio initialisation failed.
}
else
{
// jetgpio initialised okay.
}
int gpioInitialise(void)
Initialises the library. gpioInitialise must be called before using the other library functions,...
Definition: nano.c:185

◆ gpioPWM()

int gpioPWM ( unsigned  gpio,
unsigned  dutycycle 
)

Starts PWM on the GPIO, dutycycle between 0 (off) and range (fully on). Arduino style: analogWrite.

Parameters
gpioNano & Xavier NX: 32, 33 Orin Nano,NX: 15, 32, 33 Orin AGX: 13, 15, 18
dutycycle0-256 (0 to 100%)
Returns
Returns 0 if OK, a negative number otherwise
gpioPWM(32, 256); // Sets pin 32 full on.
gpioPWM(33, 128); // Sets pin 33 half on.
int gpioPWM(unsigned gpio, unsigned dutycycle)
Starts PWM on the GPIO, dutycycle between 0 (off) and range (fully on). Arduino style: analogWrite.
Definition: nano.c:2048

◆ gpioRead()

int gpioRead ( unsigned  gpio)

Reads the GPIO level, on or off, 0 or 1. Arduino style: digitalRead. Wheter a pin has been set as input or output it can be read with this function.

Parameters
gpio3-40, 7-40 on Xavier NX dev kit
Returns
Returns the GPIO level if OK, otherwise a negative number
printf("pin 24 is level %d", gpioRead(24));
int gpioRead(unsigned gpio)
Reads the GPIO level, on or off, 0 or 1. Arduino style: digitalRead. Wheter a pin has been set as inp...
Definition: nano.c:1424

◆ gpioSetISRFunc()

int gpioSetISRFunc ( unsigned  gpio,
unsigned  edge,
unsigned  debounce,
unsigned long *  timestamp,
void(*)()  f 
)

Registers a function to be called (a callback) whenever the specified.

GPIO interrupt occurs. This function will start a thread that will monitor the status of the interrupt.One function may be registered per GPIO.

Parameters
gpio3-40, 7-40 on Xavier NX dev kit
edgeRISING_EDGE, FALLING_EDGE, or EITHER_EDGE
debounce0-1000 useconds, to avoid bouncing specially on mechanical inputs
timestamptimestamp of the detected edge in nanoseconds in EPOCH format
fthe callback function, this will be execute if an edge is detected in the selected pin
Returns
Returns 0 if OK, otherwise a negative number
gpioSetISRFunc(3, RISING_EDGE, 1000, &timestamp, &callback); // Calls the 'callback' function when a rising edge is detected on pin 3, with a debouncing time of 1000 useconds
it also delivers the event timestamp on the "timestamp" variable in EPOCH format (nanoseconds)
int gpioSetISRFunc(unsigned gpio, unsigned edge, unsigned debounce, unsigned long *timestamp, void(*f)())
Registers a function to be called (a callback) whenever the specified.
Definition: nano.c:1778

◆ gpioSetMode()

int gpioSetMode ( unsigned  gpio,
unsigned  mode 
)

Sets the GPIO mode, typically input or output.

Parameters
gpio3-40, 7-40 on Xavier NX dev kit
modeJET_INPUT, JET_OUTPUT
Returns
Returns 0 if OK, -1 otherwise
gpioSetMode(17, JET_INPUT); // Sets pin 17 as input.
int gpioSetMode(unsigned gpio, unsigned mode)
Sets the GPIO mode, typically input or output.
Definition: nano.c:1004
gpioSetMode(18, JET_OUTPUT); // Sets pin 18 as output.

◆ gpioSetPWMfrequency()

int gpioSetPWMfrequency ( unsigned  gpio,
unsigned  frequency 
)

Sets the frequency in hertz to be used for the GPIO.

Parameters
gpioNano & Xavier NX: 32, 33 Orin Nano,NX: 15, 32, 33 Orin AGX: 13, 15, 18
frequency25Hz to 187 kHz on Nano and 50 Hz to 1593 kHz on Orin and Xavier
Returns
Returns 1 if OK, a negative number otherwise
gpioSetPWMfrequency(32, 10000); // Setting up PWM frequency=10kHz @ pin 32
int gpioSetPWMfrequency(unsigned gpio, unsigned frequency)
Sets the frequency in hertz to be used for the GPIO.
Definition: nano.c:2022

◆ gpioTerminate()

void gpioTerminate ( void  )

Terminates the library. This function restores the used registers to their previous state and releases memory.

Returns
Returns nothing
void gpioTerminate(void)
Terminates the library. This function restores the used registers to their previous state and release...
Definition: nano.c:701

◆ gpioWrite()

int gpioWrite ( unsigned  gpio,
unsigned  level 
)

Sets the GPIO level, on or off. Arduino style: digitalWrite.

Parameters
gpio3-40, 7-40 on Xavier NX dev kit
level0-1
Returns
Returns 0 if OK, otherwise a negative number
gpioWrite(24, 1); // Sets pin 24 high.
int gpioWrite(unsigned gpio, unsigned level)
Sets the GPIO level, on or off. Arduino style: digitalWrite.
Definition: nano.c:1521

◆ i2cClose()

int i2cClose ( unsigned  handle)

This closes the I2C device associated with the handle.

Parameters
handle>=0, as returned by a call to [i2cOpen]
Returns
Returns 0 if OK, otherwise a negative number
i2cClose(MPU6050); //Closing previously opened connection
int i2cClose(unsigned handle)
This closes the I2C device associated with the handle.
Definition: nano.c:2172

◆ i2cOpen()

int i2cOpen ( unsigned  i2cBus,
unsigned  i2cFlags 
)

This returns a handle for the device at the address on the I2C bus.

Parameters
i2cBus0 or 1, 0 are pins 27 (SDA) & 28 (SCL), 1 are pins 3(SDA) & 5(SCL) In Orin the pins are the same but the i2c devices are: 0->i2c-1 & 1->i2c-7 In Xavier NX the pins are the same but the i2c devices are: 0->i2c-1 & 1->i2c-8 Flags allow you to change the bus speed. NOTE - FLAGS WILL NOT CAUSE ANY EFFECT FOR MACHINES WITH JETPACK >= 6:
i2cFlags0 -> 100 kHz
i2cFlags1 -> 400 kHz
i2cFlags2 -> 1 MHz
Returns
Returns a handle with the I2C bus number being opened (>=0) if OK, otherwise a negative number
int MPU6050 = i2cOpen(0,0); //Opening a connection on pins 27/28 at speed 100kHz
int i2cOpen(unsigned i2cBus, unsigned i2cFlags)
This returns a handle for the device at the address on the I2C bus.
Definition: nano.c:2091

◆ i2cReadByteData()

int i2cReadByteData ( unsigned  handle,
unsigned  i2cAddr,
unsigned  i2cReg 
)

This reads a single byte from the specified register of the device associated with handle.

Parameters
handle>=0, as returned by a call to [i2cOpen]
i2cAddr0-0x7F, the I2C slave address
i2cReg0-255, the register to read
Returns
Returns the byte read (>=0) if OK, otherwise a negative number
gyro_x_H = i2cReadByteData(MPU6050, 0x68, 0x43); // getting register 0x43 out of opened connection MPU6050 with i2C address 0x68
int i2cReadByteData(unsigned handle, unsigned i2cAddr, unsigned i2cReg)
This reads a single byte from the specified register of the device associated with handle.
Definition: nano.c:2249

◆ i2cReadWordData()

int i2cReadWordData ( unsigned  handle,
unsigned  i2cAddr,
unsigned  i2cReg 
)

This reads two bytes from the specified consecutive register(s) of the device associated with handle.

Parameters
handle>=0, as returned by a call to [i2cOpen]
i2cAddr0-0x7F, the I2C slave address
i2cReg0-255, the register to read
Returns
Returns the word read (>=0) if OK, otherwise a negative number
gyro_x_H = i2cReadWordData(MPU6050, 0x68, 0x43); // getting register 0x43 and 0x44 out of opened connection MPU6050 with i2C address 0x68
int i2cReadWordData(unsigned handle, unsigned i2cAddr, unsigned i2cReg)
This reads two bytes from the specified consecutive register(s) of the device associated with handle.
Definition: nano.c:2344

◆ i2cWriteByteData()

int i2cWriteByteData ( unsigned  handle,
unsigned  i2cAddr,
unsigned  i2cReg,
unsigned  bVal 
)

This writes a single byte to the specified register of the device associated with handle.

Parameters
handle>=0, as returned by a call to [i2cOpen]
i2cAddr0-0x7F, the I2C slave address
i2cReg0-255, the register to write
bVal0-0xFF, the value to write
Returns
Returns 0 if OK, negative number otherwise
writestat = i2cWriteByteData(MPU6050, 0x68, 0x1B, 0x00); // writing 0x00 to register address 0x1B on opened chanel MPU6050 with i2C address 0x68
int i2cWriteByteData(unsigned handle, unsigned i2cAddr, unsigned i2cReg, unsigned bVal)
This writes a single byte to the specified register of the device associated with handle.
Definition: nano.c:2199

◆ i2cWriteWordData()

int i2cWriteWordData ( unsigned  handle,
unsigned  i2cAddr,
unsigned  i2cReg,
unsigned  wVal 
)

This writes two bytes to the specified consecutive register(s) of the device associated with handle.

Parameters
handle>=0, as returned by a call to [i2cOpen]
i2cAddr0-0x7F, the I2C slave address
i2cReg0-255, the register to write
wVal0-0xFFFF, the value to write
Returns
Returns 0 if OK, negative number otherwise
writestat = i2cWriteWordData(MPU6050, 0x68, 0x1B, 0x0000); // writing 0x0000 to register address 0x1B & 0x1C on opened chanel MPU6050 with i2C address 0x68
int i2cWriteWordData(unsigned handle, unsigned i2cAddr, unsigned i2cReg, unsigned wVal)
This writes two bytes to the specified consecutive register(s) of the device associated with handle.
Definition: nano.c:2294

◆ spiClose()

int spiClose ( unsigned  handle)

This functions closes the SPI device identified by the handle.

Parameters
handle>=0, as returned by a call to [spiOpen]
Returns
Returns 0 if OK, otherwise a negative number
spiClose(SPI_init); //closing previously opened spi port with handle SPI_init
int spiClose(unsigned handle)
This functions closes the SPI device identified by the handle.
Definition: nano.c:2547

◆ spiOpen()

int spiOpen ( unsigned  spiChan,
unsigned  speed,
unsigned  mode,
unsigned  cs_delay,
unsigned  bits_word,
unsigned  lsb_first,
unsigned  cs_change 
)

This function returns a handle for the SPI device on the channel. Data will be transferred at baud bits per second. The flags may be used to modify the default behaviour of a 4-wire operation, mode 0, active low chip select. There are 2 SPI channels called SPI1 & SPI2 on Nano and SPI0 & SPI1 on Orin Nano, Orin NX and Xavier NX. For Orin AGX there is only 1 SPI channel: SPI1 (pins 21, 19...so forth). The pins used are given in the following table.

Port MISO MOSI SCLK CS0 CS1
SPI1/SPI0 21 19 23 24 26
SPI2/SPI1 22 37 13 18 16
Parameters
spiChan0-1 (0 stands for SPI1 on Nano, SPI0 on Orin Nano and SPI1 on Orin AGX, 1 stands for SPI2 on Nano and SPI1 on Orin Nano)
speedup to 50M (beyond that expect problems, actually we are not going beyond that)
modedefines the SPI mode
Mode POL PHA
0 0 0
1 0 1
2 1 0
3 1 1
Parameters
cs_delaydelay if nonzero, how long to delay after the last bit transfer in us before optionally deselecting the device before the next transfer
bits_wordbits per word from 1 to 32 bits
lsb_firstis 1 if least significant bit first 0 otherwise
cs_changeis 1 to deselect device before starting the next transfer otherwise 0
Returns
Returns a handle (>=0) if OK, otherwise a negative number
SPI_init = spiOpen(1, 5000000, 0, 0, 8, 1, 1); // SPI2 (pins 22/37, 500 kHz, mode 0, cs delay 0, 8 bits/word,lsb first 1, cs change 1
int spiOpen(unsigned spiChan, unsigned speed, unsigned mode, unsigned cs_delay, unsigned bits_word, unsigned lsb_first, unsigned cs_change)
This function returns a handle for the SPI device on the channel. Data will be transferred at baud bi...
Definition: nano.c:2389

◆ spiXfer()

int spiXfer ( unsigned  handle,
char *  txBuf,
char *  rxBuf,
unsigned  len 
)

This function transfers len bytes of data from txBuf to the SPI device associated with the handle. Simultaneously len bytes of data are read from the device and placed in rxBuf.

Parameters
handle>=0, as returned by a call to [spiOpen]
txBufthe data bytes to write
rxBufthe received data bytes
lenthe number of bytes to transfer
Returns
Returns the number of bytes transferred if OK, otherwise a negative number.
spiXfer(SPI_init, tx, rx, 7); // transfers tx data with a data lenght of 7 words and receiving rx data from prevously opened connection with handle SPI_init
int spiXfer(unsigned handle, char *txBuf, char *rxBuf, unsigned len)
This function transfers len bytes of data from txBuf to the SPI device associated with the handle....
Definition: nano.c:2569