Main Content

readRegister

R2026b

Read register of I2C device connected to NVIDIA Jetson

Since R2026a

    Description

    Add-On Required: This feature requires the MATLAB Coder Support Package for NVIDIA Jetson and NVIDIA DRIVE Platforms add-on.

    data = readRegister(i2cObj,regAddress) reads a single uint8 value from the register address, regAddress, of the I2C device i2cObj.

    example

    data = readRegister(i2cObj,regAddress,dataPrecision) reads the value from the register as the data type dataPrecision.

    example

    data = readRegister(___,registerAddressWidth=width) specifies the width of the register address in bytes. For devices that use two bytes for register addresses, specify width as "2Byte".

    example

    Examples

    collapse all

    This example shows how to read from a specified register of an I2C device connected to an NVIDIA® Jetson™ board.

    Create a jetson object named hwObj, and scan the I2C buses on the Jetson hardware by using the scanI2CBus function. In this example, the Jetson board has two I2C buses, i2c-1 and i2c-8. Each bus has a device at address 0x74.

    hwObj = jetson;
    addresses = scanI2CBus(hwObj);
    addresses{:}
    ans = 
    
      struct with fields:
    
            Bus: 'i2c-1'
        Devices: {'74'}
    
    
    ans = 
    
      struct with fields:
    
            Bus: 'i2c-8'
        Devices: {'74'}

    Create an i2cdev object, i2cObj, connecting to the device on the i2c-1 bus.

    i2cObj = i2cdev(hwObj,"i2c-1",0x74);

    Read from register 7 on the I2C device. In this example, the register returns 43. The value depends on the state of your hardware.

    readRegister(i2cObj,7)
    ans =
    
      uint8
    
       43

    If i2cObj is an i2cdev object, read the data from register 1 as an int16 value by entering this code. The actual value that readRegister returns depends on the state of your hardware. In this example, the function returns the int16 value -3.

    readRegister(i2cObj,1,"int16")
    ans =
    
      int16
    
       -3
    

    If the I2C device uses two bytes for register addresses, specify the width argument as "2Byte". For example, if i2cObj represents a connection to a device with two-byte addressing, enter this code to read from the address 3.

    readRegister(i2cObj,3,registerAddressWidth="2Byte")

    Input Arguments

    collapse all

    Connection to the I2C device, specified as an i2cdev object.

    Register address, specified as a nonnegative integer. The Jetson hardware sends the address by using the number of bytes specified in the width argument. The address can be at most 255 if width is "1Byte" and at most 65335 if width is "2Byte".

    Data type, specified as "uint8", "int8", "uint16", "int16", "uint32", "int32", "uint64", or "int64".

    Register address width, specified as "1Byte" or "2Byte". Specify the width based on how many bytes the I2C device uses for addresses.

    Output Arguments

    collapse all

    Data read from the I2C device, returned as a scalar. The dataPrecision argument determines the data type of the output data.

    Extended Capabilities

    expand all

    C/C++ Code Generation
    Generate C and C++ code using MATLAB® Coder™.

    GPU Code Generation
    Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

    Version History

    Introduced in R2026a

    expand all

    See Also

    Objects

    Functions