Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to interfcae i2c bus of a chip on my hardware to qt application
Forum Updated to NodeBB v4.3 + New Features

How to interfcae i2c bus of a chip on my hardware to qt application

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 886 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ananthavidhya
    wrote on last edited by
    #1

    Hello Everyone,
    I recently working on developing qt application for accessing a rtc chip whose i2c address is 0x24. For this chip we need to fuse a register to set date and time through command line. that what i am trying to replace with gui in which if i press a button i2c registers have to write data like as below which i am doing in command line
    i2cset -f -y 0 0x24 0x10 0xb1 && i2cset -f -y 0 0x24 0x10 0xfe && i2cset -f -y 0 0x24 0x10 0xa3
    at the same time if i press status button it should read the i2c register equivalent to below command
    i2cget -f -y 0 0x24 0x05
    To am trying out to achive this in better way ..but i am strucked in middle can some one help me with good reference of how to achive this ?
    or basic way to control read and write using qt gui methods.
    Thanks in advance
    A..N.V.Lavanya

    jsulmJ 1 Reply Last reply
    0
    • A ananthavidhya

      Hello Everyone,
      I recently working on developing qt application for accessing a rtc chip whose i2c address is 0x24. For this chip we need to fuse a register to set date and time through command line. that what i am trying to replace with gui in which if i press a button i2c registers have to write data like as below which i am doing in command line
      i2cset -f -y 0 0x24 0x10 0xb1 && i2cset -f -y 0 0x24 0x10 0xfe && i2cset -f -y 0 0x24 0x10 0xa3
      at the same time if i press status button it should read the i2c register equivalent to below command
      i2cget -f -y 0 0x24 0x05
      To am trying out to achive this in better way ..but i am strucked in middle can some one help me with good reference of how to achive this ?
      or basic way to control read and write using qt gui methods.
      Thanks in advance
      A..N.V.Lavanya

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @ananthavidhya There is nothing in Qt to access i2c, but you can use libraries see https://forum.qt.io/topic/114267/qt-functions-for-i2c

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • A Offline
        A Offline
        ananthavidhya
        wrote on last edited by
        #3

        i used direct kernel drivers related i2c application. But unfortunately my code is not working as i expected instead it disturbed some other i2c bus address of emmc chip . I am really have no idea about how that happened.
        sincerely seeking for help in proceeding further.
        Prior Thanks
        A.N.V.Lavanya

        here i am attaching my mainwindow.cpp file. any sort of help is needy

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        //*****************
        #include <QTimer>
        #include <QDateTime>
        #include <QDate>
        //*****************
        
        //******i2c handling c++ code*******
        #include <linux/i2c-dev.h>
        
        #include <sys/stat.h>
        #include <linux/i2c.h>
        
        //#include <i2c/smbus.h>
        #include <fcntl.h>// for opening file
        #include <sys/ioctl.h> //IOCTL FUNCTION
        #include <unistd.h>//READ AND WRITE FUNCTIONS
        int file;
        #include <QDebug>
        
        int i2c_fd = -1;
        typedef unsigned char   u8;
        
        //***********************************
        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
             ui->setupUi(this);
        //   showTime();
             QTimer *timer=new QTimer(this);
             connect(timer ,SIGNAL(timeout()),this,SLOT(showTime()));
             timer->start();
             showDate();
             int nI2COpenResault = i2c_init(0x24);
         ui->textEdit->append(QString("I2C Open result:%1").arg(nI2COpenResault));
        }
        int MainWindow::i2c_init(int addr)
        {
              QString sFilename = "/dev/i2c-0";
              QByteArray ba = sFilename.toLocal8Bit();
              const char *filename = ba.data();
        //        int adapter_nr = 0;
        //        char filename[20];
        //        snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
        //        file = open(filename, O_RDWR);
        //        if(file < 0)
        //        {
        //            qDebug() << "Open i2c-0 file failed.";
        //            return file;
        //        }
        //        int ret = ioctl(file, I2C_SLAVE, addr);
        //        if(ret < 0)
        //        {
        //            qDebug() << "Open Buss failed.";
        //            return ret;
        //        }
        //        return ret;
        
            if ((i2c_fd = open(filename, O_RDWR)) < 0) {
                    char err[200];
                    sprintf(err, "open('%s') in i2c_init", filename);
                    perror(err);
                    return -1;
                }
                return i2c_fd;
        }
        int MainWindow::I2C_Close()
        {
            if(file == -1)
                return -1;
            ::close(file);
            return 0;
        }
        
        MainWindow::~MainWindow()
        {
            delete ui;
            I2C_Close();
        }
        
        
        
        void MainWindow::showTime()
        {
            QTime time=QTime::currentTime();
              QString timetext=time.toString("hh : mm : ss");
            //blink the  colon at every sec
            if((time.second() % 2) == 0)
        //    {
        //        timetext[3] = ' ';
        //        timetext[8] = ' ';
        //    }
            ui->label->setText(timetext);
        }
        void MainWindow::showDate()
        {
            QDate date = QDate::currentDate();
        
            /////////////////////////////////////////
            //       Format        Result          //
            //    dd.MM.yyyy      21.05.2001       //
            //    ddd MMMM d yy   Tue May 21 01    //
            //    hh:mm:ss.zzz	  14:13:09.120     //
            //    hh:mm:ss.z	  14:13:09.12      //
            //    h:m:s ap	      2:13:9 pm        //
            //    H:m:s a         14:16:0 pm       //
            /////////////////////////////////////////
        
        
            QString datetext=date.toString("dd : MM : yyyy");
            ui->label_2->setText(datetext);
        
        }
        
        void MainWindow::on_Status_Read_clicked()
        {
                //i2cget -f -y 0 0x24 0x05  == 0x28 before fuse burn; 0xa8 after fuse burn;
        
        //        char *rbuf = new char;
        
        //        char cWriteData[2] = {ui->addr1->text().toInt(), ui->addr2->text().toInt()};
        //        int nWriteResult = I2C_Write(cWriteData, 2); // 需先寫入2byte欲讀出資料之位址
        //        usleep(1000);
        //        int nReadResult = I2C_Read(rbuf, ui->read_length->text().toInt());
        //        ui->textEdit->append(QString("ReadWrite Result:%1").arg(nWriteResult));
        //        ui->textEdit->append(QString("Read Result:%1").arg(nReadResult));
        //        ui->textEdit->append(QString("Read Data:%1").arg((int)*rbuf));
        //        delete rbuf;
               int  Result;
              Result = I2C_Read(0x24,0x05, unsigned char *result);
               ui->data->setText(QString(Result));
        }
        void MainWindow::on_i2creg_write_clicked()
        {
        
                 char cWriteData[2];
                cWriteData[0] = 0x05;
                cWriteData[1] = ui->data->text().toInt();
                int nResult = I2C_Write(cWriteData, 2);
                ui->textEdit->append(QString("write Result:%1").arg(nResult));
        }
        
        int MainWindow::I2C_Write(u8 slave_addr, u8 reg, u8 data);
        {
        
        
            int retval;
                u8 outbuf[2];
        
                struct i2c_msg msgs[1];
                struct i2c_rdwr_ioctl_data msgset[1];
        
                outbuf[0] = reg;
                outbuf[1] = data;
        
                msgs[0].addr = slave_addr;
                msgs[0].flags = 0;
                msgs[0].len = 2;
                msgs[0].buf = outbuf;
        
                msgset[0].msgs = msgs;
                msgset[0].nmsgs = 1;
        
                if (ioctl(i2c_fd, I2C_RDWR, &msgset) < 0) {
                    perror("ioctl(I2C_RDWR) in i2c_write");
                    return -1;
                }
        
                return 0;
            }
        
        }
        
        int MainWindow::I2C_Read(unsigned char slave_addr, unsigned char reg, unsigned char *result)
        {
        
        
            //*************************************//
            int retval;
                unsigned char outbuf[1], inbuf[1];
                struct i2c_msg msgs[2];
                struct i2c_rdwr_ioctl_data msgset[1];
        
                msgs[0].addr = slave_addr;
                msgs[0].flags = 0;
                msgs[0].len = 1;
                msgs[0].buf = outbuf;
        
                msgs[1].addr = slave_addr;
                msgs[1].flags = I2C_M_RD | I2C_M_NOSTART;
                msgs[1].len = 1;
                msgs[1].buf = inbuf;
        
                msgset[0].msgs = msgs;
                msgset[0].nmsgs = 2;
        
                outbuf[0] = reg;
        
                inbuf[0] = 0;
        
                *result = 0;
                if (ioctl(i2c_fd, I2C_RDWR, &msgset) < 0) {
                    perror("ioctl(I2C_RDWR) in i2c_read");
                    return -1;
                }
        
                *result = inbuf[0];
                return 0;
        }
        void MainWindow::update_date_time()
        {
        //        QDateEdit *dateEdit = new QDateEdit(this);
        //        QTimeEdit *timeEdit = new QTimeEdit(this);
        //    ui->daeTimeEdit->setDateTime(QDateTime::currentDateTime());
        //        ui->dateEdit->displayFormat(hh );
                ui->label->setText(ui->timeEdit->text());
                ui->label->update();
                ui->label_2->setText(ui->dateEdit->text());
                ui->label_2->update();
        
        
        }
        
        
        void MainWindow::on_set_date_clicked()
        {
        
        //    QDateTime dateTimeEdit=new QDateTimeEdit(QDateTime::currentDateTime(),this);
        //    dateTimeEdit->setDisplayFormat("yyyy.MMM.dd hh:mm");
        //    exec=new QProcess(this)
        //    exec->start("touch", QStringList() << "-t" <<dateTimeEdit->dateTime().toString("yyyyMMddhhmm")<<file_path);
        //    exec->waitForFinished();
                  QTime newModifiedTime = ui->timeEdit->time();
                    QDate newModifiedDate=ui->dateEdit->date();
        //        QDateTime newCreationDate=TimeEdit_Creation->dateTime();
        
        
        //        system("date -s \"date");
        //           system("date -s \"newModifiedTime newModifiedDate");
        //        system("hwclock -w");
        //        system("reboot");
                update_date_time();
        }
        
        
        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved