I2C

Demonstrates how to read and write data with I2C when programming with ADLX.

Command Prompts

Command Prompt Description
1 Display version.
2 Read.
3 Repeated read.
4 Write.
M/m Display the command prompt menu.
Q/q Terminate the application.

Sample Path

/Samples/CPP/I2C/I2C

Code

Copied!

//
// Copyright (c) 2021 - 2025 Advanced Micro Devices, Inc. All rights reserved.
//
//-------------------------------------------------------------------------------------------------


#include "SDK/ADLXHelper/Windows/Cpp/ADLXHelper.h"
#include "SDK/Include/II2C.h"
#include <iostream>

// Use ADLX namespace
using namespace adlx;

// ADLXHelper instance
// No outstanding interfaces from ADLX must exist when ADLX is destroyed.
// Use global variables to ensure validity of the interface.
static ADLXHelper g_ADLXHelp;
constexpr int DATA_SIZE = 2;

// Display version
void ShowVersion(const IADLXI2CPtr& i2c);

// Read
void Read(const IADLXI2CPtr& i2c);

// Repeated read
void RepeatedRead(const IADLXI2CPtr& i2c);

// Write
void Write(const IADLXI2CPtr& i2c);

// Capability
void Capability(const IADLXI2CPtr& i2c);

// Menu
void MainMenu();

// Menu control
void MenuControl(const IADLXI2CPtr& i2c);

// Wait for exit with error message
int WaitAndExit(const char* msg, const int retCode);

int main()
{
    // Define return code
    ADLX_RESULT  res = ADLX_FAIL ;

    // Initialize ADLX
    res = g_ADLXHelp.Initialize();
    if (ADLX_SUCCEEDED (res))
    {
        // GetGPUs
        IADLXGPUListPtr gpus;
        res = g_ADLXHelp.GetSystemServices()->GetGPUs(&gpus);

        if (!gpus->Empty() && ADLX_SUCCEEDED (res))
        {
            // Get GPU interface
            IADLXGPUPtr gpuInfo;
            adlx_uint index = 0;
            res = gpus->At(index, &gpuInfo);

            // Get I2C interface
            IADLXI2CPtr i2c;
            res = g_ADLXHelp.GetSystemServices()->GetI2C(gpuInfo, &i2c);
            if (ADLX_SUCCEEDED (res))
            {
                MainMenu();
                MenuControl(i2c);
            }
        }
        else
        {
            std::cout << "Failed to get the GPU list: " << res << std::endl;
        }
    }
    else
    {
        return WaitAndExit("ADLX initialization failed", 0);
    }

    // Destroy ADLX
    res = g_ADLXHelp.Terminate();
    std::cout << "Destroy ADLX res: " << res << std::endl;

    // Pause to see the print out
    system("pause");

    return 0;
}

void ShowVersion(const IADLXI2CPtr& i2c)
{
    adlx_int major;
    adlx_int minor;
    ADLX_RESULT  res = i2c->Version(&major, &minor);
    std::cout << "\\tI2C version, major: " << major << " , minor: " << minor << std::endl;
    std::cout << "\\tres is: " << res << std::endl;
}

void Read(const IADLXI2CPtr& i2c)
{
    // The following is hardware specific
    adlx_byte data[DATA_SIZE] = { 0 };
    ADLX_RESULT  res = i2c->Read(I2C_LINE_OEM2 , 100, 0x93, 0x30, DATA_SIZE, data);
    std::cout << "\\tI2C read info:\\n"
    << "\\t\\tline: I2C_LINE_OEM2\\n\\t\\tspeed: 100\\n\\t\\taddress: 0x93\\n\\t\\toffset: 0x30\\n\\t\\tdataSize: "
    << DATA_SIZE << "\\n\\t\\tdata: " << (int)data[0] << std::endl;
    std::cout << "\\tres is: " << res << std::endl;
}

void RepeatedRead(const IADLXI2CPtr& i2c)
{
    // The following is hardware specific
    adlx_byte data[DATA_SIZE] = { 0 };
    ADLX_RESULT  res = i2c->RepeatedStartRead(I2C_LINE_OEM2 , 100, 0x93, 0x30, DATA_SIZE, data);
    std::cout << "\\tI2C read info:\\n"
    << "\\t\\tline: I2C_LINE_OEM2\\n\\t\\tspeed: 100\\n\\t\\taddress: 0x93\\n\\t\\toffset: 0x30\\n\\t\\tdataSize: "
    << DATA_SIZE << "\\n\\t\\tdata: " << (int)data[0] << std::endl;
    std::cout << "\\tres is: " << res << std::endl;
}

void Write(const IADLXI2CPtr& i2c)
{
    // The following is hardware specific
    adlx_byte data[DATA_SIZE] = { 0 };
    data[0] = 176;
    ADLX_RESULT  res = i2c->Write(I2C_LINE_OEM2 , 100, 0x93, 0x30, DATA_SIZE, data);
    std::cout << "\\tI2C write info:\\n"
    << "\\t\\tline: I2C_LINE_OEM2\\n\\t\\tspeed: 100\\n\\t\\taddress: 0x93\\n\\t\\toffset: 0x30\\n\\t\\tdataSize: "
    << DATA_SIZE << "\\n\\t\\tdata: " << (int)data[0] << std::endl;
    std::cout << "\\tres is: " << res << std::endl;
}

void Capability(const IADLXI2CPtr& i2c)
{
    adlx_bool isSupported = false;
    ADLX_RESULT  res = i2c->IsSupported(I2C_LINE_OEM2 , 0x93, &isSupported);
    std::cout << "\\tI2C support info:\\n"
    << "\\t\\tline: I2C_LINE_OEM2\\n\\t\\taddress: 0x93\\n\\t\\tisSupported: " << isSupported
    << "\\n\\tres is: " << res << std::endl;
}

void MainMenu()
{
    std::cout << "\\tChoose one from the following options" << std::endl;

    std::cout << "\\t->Press 1 to display version" << std::endl;
    std::cout << "\\t->Press 2 to read" << std::endl;
    std::cout << "\\t->Press 3 to repeated read" << std::endl;
    std::cout << "\\t->Press 4 to write" << std::endl;
    std::cout << "\\t->Press 5 to display capability" << std::endl;

    std::cout << "\\t->Press Q/q to terminate the application" << std::endl;
    std::cout << "\\t->Press M/m to display main menu options" << std::endl;
}

void MenuControl(const IADLXI2CPtr& i2c)
{
    int num = 0;
    while ((num = getchar()) != 'q' && num != 'Q')
    {
        switch (num)
        {
            // Display version
        case '1':
            ShowVersion(i2c);
            break;

            // Read
        case '2':
            Read(i2c);
            break;

            // Repeated read
        case '3':
            RepeatedRead(i2c);
            break;

            // Write
        case '4':
            Write(i2c);
            break;

            // Capability
        case '5':
            Capability(i2c);
            break;

            // Display main menu options
        case 'm':
        case 'M':
            MainMenu();
            break;
        default:
            break;
        }
    }
}

// Wait for exit with error message
int WaitAndExit(const char* msg, const int retCode)
{
    // Printout the message and pause to see it before returning the desired code
    if (nullptr != msg)
        std::cout << msg << std::endl;

    system("pause");
    return retCode;
}

Related pages

  • Visit the Adlx product page for download links and more information.

Looking for more documentation on GPUOpen?

AMD GPUOpen software blogs

Our handy software release blogs will help you make good use of our tools, SDKs, and effects, as well as sharing the latest features with new releases.

GPUOpen Manuals

Don’t miss our manual documentation! And if slide decks are what you’re after, you’ll find 100+ of our finest presentations here.

AMD GPUOpen Performance Guides

The home of great performance and optimization advice for AMD RDNA™ 2 GPUs, AMD Ryzen™ CPUs, and so much more.

Getting started: AMD GPUOpen software

New or fairly new to AMD’s tools, libraries, and effects? This is the best place to get started on GPUOpen!

AMD GPUOpen Getting Started Development and Performance

Looking for tips on getting started with developing and/or optimizing your game, whether on AMD hardware or generally? We’ve got you covered!

AMD GPUOpen Technical blogs

Browse our technical blogs, and find valuable advice on developing with AMD hardware, ray tracing, Vulkan®, DirectX®, Unreal Engine, and lots more.

Find out more about our software!

AMD GPUOpen Effects - AMD FidelityFX technologies

Create wonder. No black boxes. Meet the AMD FidelityFX SDK!

AMD GPUOpen Samples

Browse all our useful samples. Perfect for when you’re needing to get started, want to integrate one of our libraries, and much more.

AMD GPUOpen developer SDKs

Discover what our SDK technologies can offer you. Query hardware or software, manage memory, create rendering applications or machine learning, and much more!

AMD GPUOpen Developer Tools

Analyze, Optimize, Profile, Benchmark. We provide you with the developer tools you need to make sure your game is the best it can be!