đŸ’ģ
Programming Concept
  • 👨‍đŸ”ŦAbout Me
  • 📗C++ STL Concept 2023
    • 📖STL in CPP ( Part I)
    • 📖STL in CPP ( Part II)
    • 📖STL in CPP ( Part III )
    • 📖STL in CPP ( Part IV )
    • 📖STL in CPP ( Part V )
    • 📖STL in CPP ( Part VI )
    • 📖STL in CPP ( Part VII )
  • âœī¸Pointers in C / C++
    • 📖Pointers in details
  • 📘Strings in CPP 2023
  • 🕞Thread in C++ 2023
  • 📓Smart Pointer in C++ 2023
  • Basic C++ Topics
    • đŸ’ŧType Casting in C++ 2023
  • đŸ’ģAdvanced C++ Programming
    • 🎓Topics (Syllabus)
    • 🧑‍🎓Inheritance and Polymorphism
    • đŸ–Šī¸Exception Handling
    • 🏃Runtime Type Information
    • 📔An Overview of Templates
    • 📡Template in C++
  • đŸ’ģEmbedded Linux
    • đŸ–Ĩī¸Embedded Linux OS
      • Build Yocto Project Step By Step
      • How to install apt-get to the Yocto Project image
      • Installing gcc/g++ toolchain in yocto project
      • Installing GDB in yocto project
      • Installing ssh-server-dropbear for embedded linux in yocto project
      • Add Python To A Build
      • Add Boost To A Build
      • Adding Valgrind To Build
      • How To Add A Custom App To A Yocto Build
      • Raspberry Pi
    • đŸ“ļCommunication Protocols
      • âœī¸Working With I2C using c++
      • 📔SPI Implementation with source code in C++
      • 📓Linux Serial Ports Using C/C++
      • đŸ–ąī¸UART
      • đŸ–¨ī¸Universal GPIO Access
  • 🧑‍đŸ’ģQT QML
    • 📘QT QML Concept 2023
      • Why Qt Framework and QML
      • Sqlite3 for qtquick application
      • How To Install sqlite3 in windows 11 || Windows 10
      • Signals and Slots in Qt QML
      • Working with Signals and Slots in QML
      • Sorting QML ListModels
      • ListView In QML With Section Delegate
      • Registration of custom enum in QML
      • Registering a QML Type as a Singleton object
      • Using enumerations in QML without C ++
      • Registering a Singleton object to use “Static” methods in QML
      • Connecting JavaScript files to other JavaScript files in a Qt / QML project
      • Positioning in Qt QML with anchors
      • TextInput validators in Qt Qml
      • Qt Qml Abstract Item Model using C++
      • Qt QML List Model
      • How to Register your C++ Class as a QML Type
      • Jabra Speaker Connect Qt Project
      • Qt | One article summarizing QObject
  • â–ļī¸QT Concept
    • â„šī¸Icon button
    • đŸ’ģRegister QML Singletone in CP
Powered by GitBook
On this page

Was this helpful?

  1. Embedded Linux
  2. Communication Protocols

Working With I2C using c++

PreviousCommunication ProtocolsNextSPI Implementation with source code in C++

Last updated 1 year ago

Was this helpful?

I2C can be supported with the help of the SMBus protocol, which is described as a specific variant of the I2C bus. This protocol is available as . To use it, you need to configure your Raspberry Pi. In a terminal, run raspi-config, select 3 Interfacing Options and P5 I2C.

// SOURCE: https://www.kernel.org/doc/Documentation/i2c/dev-interface */
#include <linux/i2c-dev.h>
#include <i2c/smbus.h>

int file;
int adapter_nr = 2;
char filename[20];

snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
file = open(filename, O_RDWR);
if (file < 0) {
  exit(1);
}

  int addr = 0x40;

if (ioctl(file, I2C_SLAVE, addr) < 0) {
  exit(1);
}

__u8 reg = 0x10;
__s32 res;
char buf[10];

res = i2c_smbus_read_word_data(file, reg);
if (res < 0) {
  /* ERROR HANDLING: i2c transaction failed */
} else {
  /* res contains the read word */
}

buf[0] = reg;
buf[1] = 0x43;
buf[2] = 0x65;
if (write(file, buf, 3) != 3) {
  /* ERROR HANDLING: i2c transaction failed */
}
đŸ’ģ
đŸ“ļ
âœī¸
Linux Kernel module