Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. signal generator segmentation fault
Forum Updated to NodeBB v4.3 + New Features

signal generator segmentation fault

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
10 Posts 3 Posters 581 Views 1 Watching
  • 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.
  • I Offline
    I Offline
    inexpert
    wrote on last edited by
    #1

    I want to program the hardware of my signalgenerator over i2c-Bus. Unfortunately I can only upload pictures of the program because my programs on the beaglebone are linux.
    Programs: form.h, form.cpp, i2ccwn.h, i2ccwn.cpp, main.cpp

    i2CCWn.cpp: row 19 MWindow*cw;//Object
    row 108 void I2CCWn:: writeFb(){
    row 109. writeRegister (OLATA, cw->get_Fb() );

    The call "get_Fb() )" leads to error "Segmentation fault".
    Only when I write the function in program "form.cpp" for instance:
    unsigned char MWindow::get_Fb() {
    return 12;
    }
    give 12 over i2c-Bus out.

    Please help me since I am no expert in programming.;)

    thumbnail_image (3).jpg thumbnail_image (2).jpg thumbnail_image (1).jpg thumbnail_image (5).jpg thumbnail_image (4).jpg thumbnail_image (6).jpg thumbnail_image (14).jpg thumbnail_image (11).jpg thumbnail_image (13).jpg thumbnail_image (12).jpg

    jsulmJ 1 Reply Last reply
    0
    • I inexpert

      I want to program the hardware of my signalgenerator over i2c-Bus. Unfortunately I can only upload pictures of the program because my programs on the beaglebone are linux.
      Programs: form.h, form.cpp, i2ccwn.h, i2ccwn.cpp, main.cpp

      i2CCWn.cpp: row 19 MWindow*cw;//Object
      row 108 void I2CCWn:: writeFb(){
      row 109. writeRegister (OLATA, cw->get_Fb() );

      The call "get_Fb() )" leads to error "Segmentation fault".
      Only when I write the function in program "form.cpp" for instance:
      unsigned char MWindow::get_Fb() {
      return 12;
      }
      give 12 over i2c-Bus out.

      Please help me since I am no expert in programming.;)

      thumbnail_image (3).jpg thumbnail_image (2).jpg thumbnail_image (1).jpg thumbnail_image (5).jpg thumbnail_image (4).jpg thumbnail_image (6).jpg thumbnail_image (14).jpg thumbnail_image (11).jpg thumbnail_image (13).jpg thumbnail_image (12).jpg

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

      @inexpert Please post code as text and not pictures of your screen!

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

      1 Reply Last reply
      0
      • I Offline
        I Offline
        inexpert
        wrote on last edited by
        #3

        I am very sorry that I put the code in the wrong place without any comment, but since I am nearly 80 years old it´s really hard for me to handle the internet stuff. So here another time the code and I hope that someone can help me with my programming problem.

        main.cpp
        
        #include "form.h"
        #include <QApplication>
        #include "ui_menuwindow.h"
        
        int main(int argc, char*argv[] ) {
        QApplication app(argc, argv);
        MWindow window;
        window.show();
        return app.exec();
        }
        
        form.h
        
        /media/ubuntu/rootfs/home/debian/jauk/Signalgenerator/CW/CW
        #ifndef FORM_H
        #define FORM_H
        #include <QMainWindow>
        #include <QMessageBox>
        #include "ui_cwwindow.h"
        #include "ui_menuwindow.h"
        
        const long fosc = 171798692 ; //Quartzoscillator frequency
        
        class MWindow : public QMainWindow, private Ui::CwWindow {
        
        Q_OBJECT
        
        public:
        MWindow(); // constructor
        ~MWindow() {} // destructor
        
        double get_FR();
        unsigned char get_Fb();
        long long get_YIGFr();
        unsigned int get_YIGDA();
        unsigned int get_YIGFilter();
        unsigned char get_Ndiv();
        //double get_FrDDS();
        long get_Phase();
        unsigned char get_Koswitch();
        unsigned char get_Att();
        unsigned int get_Pow();
        
        private slots:
        void analyseDialog();
        void updatef();
        void updatep();
        
        private:
        double FR ; // input frequency [Hz]
        double P; // output power [dBm] or [mW]
        unsigned char Fb ; // frequency range
        long long YIGFr ; //requiered YIG frequency
        unsigned int YIGDA ; //requiered YIG D/A
        unsigned int YIGFilter ; //requiered YIG Filter
        unsigned char Ndiv ; //divider factor for PLL SDI
        //double FrDDS ; //requiered DDS frequency = YIGFR/Ndiv -fosc (18667400.00Hz <= FrDDS <= 28201308.00Hz)
        long Phase ; //decimal value of 32-bit frequency tuning word for DDS AD9851 = 25*FrDDS
        unsigned char Koswitch ; //koaxswitch 3 to 8 decoder HCT 238
        unsigned char Att ; //60dB attenuator step 10dB
        unsigned int Pow; // input power range fine (0 - 10dB) SDI
        
        int decimals ;
        double minimum;
        double maximum;
        
        };
        
        #endif
        
        form.cpp
        
        #include "form.h"
        #include "i2ccwn.h"
        #include <math.h>
        
        //Constructor
        
        MWindow::MWindow() {
        
        setupUi(this);
        
              //Signal-Slot connection
        connect( pushButton_2, SIGNAL( clicked() ),
        this, SLOT(analyseDialog() ) );
        
        connect( pushButton, SIGNAL( clicked() ),
        qApp, SLOT(quit() ) );
        
        connect( pushButton_2, SIGNAL( clicked() ),
        this, SLOT(updatef() ) );
        
        connect( pushButton_2, SIGNAL( clicked() ),
        this, SLOT(updatep() ) );
        }
        
        void MWindow::analyseDialog() {
        if(comboBox->currentText() == "GHz" ) {
        doubleSpinBox->setMaximum ( 31.999999999 );
        doubleSpinBox->setDecimals( 9 );
        }
        
        else if(comboBox->currentText() == "MHz" ) {
        doubleSpinBox->setMaximum ( 31999.999999 );
        doubleSpinBox->setDecimals( 6 );
        }
        
        else if(comboBox->currentText() == "KHz" ) {
        doubleSpinBox->setMaximum ( 31999999.999 );
        doubleSpinBox->setDecimals( 3 );
        
        }
        
        else if(comboBox->currentText() == "Hz" ) {
        doubleSpinBox->setMaximum ( 31999999999 );
        doubleSpinBox->setDecimals( 0 );
        }
        
        if( comboBox_2->currentText() == "dBm" ) {
        doubleSpinBox_2-> setDecimals( 2 ) ;
        doubleSpinBox_2-> setMinimum( -50.00 ) ;
        doubleSpinBox_2-> setMaximum( 20.00 ) ;
        }
        
        else if( comboBox_2->currentText() == "mW" ) {
          doubleSpinBox_2-> setDecimals( 5 ) ; 
          doubleSpinBox_2-> setMinimum( 0.00001 ) ;
          doubleSpinBox_2-> setMaximum( 100.00000 ) ;
        }
        
        } // End of "analyseDialog()"
        
        double MWindow::get_FR() {
        if(comboBox->currentText() == "GHz" ) {
        FR = doubleSpinBox->value() * 1000000000;
        return FR;
        }
        
        else if(comboBox->currentText() == "MHz" ) {
        FR = doubleSpinBox->value() * 1000000;
        return FR;
        }
        
        else if(comboBox->currentText() == "KHz" ) {
        FR = doubleSpinBox->value() * 1000;
        return FR;
        }
        
        else if(comboBox->currentText() == "Hz" ) {
        FR = doubleSpinBox->value();
        return FR;
        }
        } // End of "get_FR"
        
        unsigned char MWindow::get_Fb() {
        
        //get_FR(); Segmentation fault
        //cw->get_FR(); -------"----------
        //this->get_FR(); -------"----------
        
        if( FR < 62500000 ) { "
        Fb = 0; "
        return Fb; "
        }
        
        else if( (FR >= 62500000E+6) && (FR < 125000000 ) ) {
        Fb = ( FR < 88000000 ) ?1 : 2;
        return Fb;
        }
        
        else if( (FR >= 125000000) && (FR < 250000000 ) ) {
        Fb = ( FR < 177000000 ) ?3 : 4;
        return Fb;
        }
        
        else if( (FR >= 250000000) && (FR < 500000000 ) ) {
        Fb = ( FR < 353000000 ) ?5 : 6;
        return Fb;
        }
        
        else if( (FR >= 500000000) && (FR < 1000000000 ) ) {
        Fb = ( FR < 707000000 ) ?7 : 8;
        return Fb;
        }
        
        else if( (FR >= 1000000000) && (FR < 2000000000 ) ) {
        Fb = ( FR < 1400000000 ) ?9 : 10;
        return Fb;
        }
        
        else if( (FR >= 2000000000) && (FR < 4000000000 ) ) {
        Fb = ( FR < 2800000000 ) ?11 : 12;
        return Fb;
        }
        
        else if( (FR >= 4000000000) && (FR < 8000000000 ) ) {
        Fb = ( FR < 5600000000 ) ?13 : 14;
        return Fb;
        }
        
        else if( (FR >= 8000000000) && (FR < 16000000000 ) ) {
        Fb = ( FR < 11200000000 ) ?15 : 16;
        return Fb;
        }
        
        else if( (FR >= 16000000000) && (FR < 32000000000 ) ) {
        Fb = ( FR < 22400000000 ) ?17 : 18;
        return Fb;
        }
        
        // Fb = 12;
        // returnFb; Segmentation fault
        
        return 12;  // OK without 'Segmentation fault' ...gibt '12' über I2C-Bus aus
        } //End of "get_Fb"
        
        unsigned char MWindow::get_Att() {
        if( (comboBox_2->currentText() == "dBm" ) ) {
        if( (doubleSpinBox_2-> value() > 10.0 ) && (doubleSpinBox_2-> value() <= 20.0 ) ) {
        Att = 0 ;
        return Att;
        }
        else if( (doubleSpinBox_2-> value() > 0.0 ) && (doubleSpinBox_2-> value() <= 10.0 ) ) {
        Att = 10 ;
        return Att;
        }
        
         else if( (doubleSpinBox_2-> value() > -10.0 ) && (doubleSpinBox_2-> value() <= 0.0 ) ) {
           Att = 20 ;
           return Att;
         }
        
         else if( (doubleSpinBox_2-> value() > -20.0 ) && (doubleSpinBox_2-> value() <= -10.0 ) ) {
           Att = 30 ;
           return Att;
         }
        
         else if( (doubleSpinBox_2-> value() > -30.0 ) && (doubleSpinBox_2-> value() <= -20.0 ) ) {
           Att = 40 ;
           return Att;
         }
        
         else if( (doubleSpinBox_2-> value() > -40.0 ) && (doubleSpinBox_2-> value() <= -30.0 ) ) {
           Att = 50 ;
           return Att;
         }
        
         else if( (doubleSpinBox_2-> value() > -50.0 ) && (doubleSpinBox_2-> value() <= -40.0 ) ) {
           Att = 60 ;
           return Att;
         }
        }
        
        else if( (comboBox_2->currentText() == "mW" ) ) {
        if( (doubleSpinBox_2-> value() > 10.0 ) && (doubleSpinBox_2-> value() <= 100.0 ) ) {
        Att = 0 ;
        return Att;
        }
        
         else if( (doubleSpinBox_2-> value() > 1.0 ) && (doubleSpinBox_2-> value() <= 10.0 ) ) {
           Att = 10 ;
           return Att;
         }
        
         else if( (doubleSpinBox_2-> value() > 0.1 ) && (doubleSpinBox_2-> value() <= 1.0 ) ) {
           Att = 20 ;
           return Att;
         }
        
         else if( (doubleSpinBox_2-> value() > 0.01 ) && (doubleSpinBox_2-> value() <= 0.1 ) ) {
           Att = 30 ;
           return Att;
         }
        
         else if( (doubleSpinBox_2-> value() > 0.001 ) && (doubleSpinBox_2-> value() <= 0.01 ) ) {
           Att = 40 ;
           return Att;
         }
        
         else if( (doubleSpinBox_2-> value() > 0.0001 ) && (doubleSpinBox_2-> value() <= 0.001 ) ) {
           Att = 50 ;
           return Att;
         }
        
         else if( (doubleSpinBox_2-> value() > 0.00001 ) && (doubleSpinBox_2-> value() <= 0.0001 ) ) {
           Att = 60 ;
           return Att;
         }
        }
        
        } //End of "get_Att()"
        
        unsigned int MWindow::get_Pow() {
        if( (comboBox_2->currentText() == "dBm" ) ) {
        if( (doubleSpinBox_2-> value() > 10.0 ) && (doubleSpinBox_2-> value() <= 20.0 ) ) {
        Pow = ( (doubleSpinBox_2-> value()-10.0) / 10) * 65536 ;
        return Pow;
        }
        
         else if( (doubleSpinBox_2-> value() > 0.0 ) && (doubleSpinBox_2-> value() <= 10.0 ) ) {
           Pow = ( (doubleSpinBox_2-> value()-0.0 ) / 10) * 6/7 * 65536 ;
           return Pow;
         }
        
         else if( (doubleSpinBox_2-> value() > -10.0 ) && (doubleSpinBox_2-> value() <= 0.0 ) ) {
           Pow = ( (doubleSpinBox_2-> value()+10.0 ) / 10) * 5/7 * 65536 ;
           return Pow;
          }
        
         else if( (doubleSpinBox_2-> value() > -20.0 ) && (doubleSpinBox_2-> value() <= -10.0 ) ) {
           Pow = ( (doubleSpinBox_2-> value()+20.0 ) / 10) * 4/7 * 65536 ;
           return Pow;
          }
        
         else if( (doubleSpinBox_2-> value() > -30.0 ) && (doubleSpinBox_2-> value() <= -20.0 ) ) {
           Pow = ( (doubleSpinBox_2-> value()+30.0 ) / 10) * 3/7 * 65536 ;
           return Pow;
          }
        
         else if( (doubleSpinBox_2-> value() > -40.0 ) && (doubleSpinBox_2-> value() <= -30.0 ) ) {
           Pow = ( (doubleSpinBox_2-> value()+40.0 ) / 10) * 2/7 * 65536 ;
           return Pow;
          }
        
         else if( (doubleSpinBox_2-> value() > -50.0 ) && (doubleSpinBox_2-> value() <= -40.0 ) ) {
           Pow = ( (doubleSpinBox_2-> value()+50.0 ) / 10) * 1/7 * 65536 ;
           return Pow;
          }
        }
        
        else if( (comboBox_2->currentText() == "mW" ) ) {
        if( (doubleSpinBox_2-> value() > 10.0 ) && (doubleSpinBox_2-> value() <= 100.0 ) ) {
        // Pow = (log10(doubleSpinBox_2-> value() ) -log10(10.0) ) * 65536.0 ;
        Pow = log10(doubleSpinBox_2-> value() / 10.0 ) * 65536.0 ;
        return Pow;
        }
        
         else if( (doubleSpinBox_2-> value() > 1.0 ) && (doubleSpinBox_2-> value() <= 10.0 ) ) {
        // Pow = (log10(doubleSpinBox_2-> value() ) -log10(1.0) ) * 65536.0 ;
        Pow = log10(doubleSpinBox_2-> value() / 1.0 ) * 6/7 * 65536.0 ;
        return Pow;
        }
        
         else if( (doubleSpinBox_2-> value() > 0.1 ) && (doubleSpinBox_2-> value() <= 1.0 ) ) {
        // Pow = (log10(doubleSpinBox_2-> value() ) -log10(0.1) ) * 5/7 * 65536.0 ;
        Pow = log10(doubleSpinBox_2-> value() / 0.1 ) * 5/7 * 65536.0 ;
        return Pow;
        }
        
         else if( (doubleSpinBox_2-> value() > 0.01 ) && (doubleSpinBox_2-> value() <= 0.1 ) ) {
        // Pow = (log10(doubleSpinBox_2-> value() ) -log10(0.01) ) * 4/7 * 65536.0 ;
        Pow = log10(doubleSpinBox_2-> value() / 0.01 ) * 4/7 * 65536.0 ;
        return Pow;
        }
        
         else if( (doubleSpinBox_2-> value() > 0.001 ) && (doubleSpinBox_2-> value() <= 0.01 ) ) {
        // Pow = (log10(doubleSpinBox_2-> value() ) -log10(0.001) ) * 3/7 * 65536.0 ;
        Pow = log10(doubleSpinBox_2-> value() / 0.001 ) * 3/7 * 65536.0
        Pow = log10(doubleSpinBox_2-> value() / 0.0001 ) * 2/7 * 65536.0 ;
        return Pow;
        }
        
         else if( (doubleSpinBox_2-> value() > 0.00001 ) && (doubleSpinBox_2-> value() <= 0.0001 ) ) {
        // Pow = (log10(doubleSpinBox_2-> value() ) -log10(0.00001) ) * 1/7 * 65536.0 ;
        Pow = log10(doubleSpinBox_2-> value() / 0.00001 ) * 1/7 * 65536.0 ;
        return Pow;
        }
        
        }
        
        } // End of "get Pow"
        
        long long MWindow::get_YIGFr() {
        if( (FR >= 62500000) && (FR < 125000000 ) ) {
        YIGFr = FR * 64;
        return YIGFr ;
        }
        
        else if( (FR >= 125000000) && (FR < 250000000 ) ) {
        YIGFr = FR * 32;
        return YIGFr ;
        }
        
        else if( (FR >= 250000000) && (FR < 500000000 ) ) {
        YIGFr = FR * 16;
        return YIGFr ;
        }
        
        else if( (FR >= 500000000) && (FR < 1000000000 ) ) {
        YIGFr = FR * 8;
        return YIGFr ;
        }
        
        else if( (FR >= 1000000000) && (FR < 2000000000 ) ) {
        YIGFr = FR * 4;
        return YIGFr ;
        }
        
        else if( (FR >= 2000000000) && (FR < 4000000000 ) ) {
        YIGFr = FR * 2;
        return YIGFr ;
        }
        
        else if( (FR >= 4000000000) && (FR < 8000000000 ) ) {
        YIGFr = FR ;
        return YIGFr ;
        }
        
        else if( (FR >= 8000000000) && (FR < 16000000000 ) ) {
        YIGFr = FR / 2;
        return YIGFr ;
        }
        
        else if( (FR >= 16000000000) && (FR < 32000000000 ) ) {
        YIGFr = FR / 4;
        return YIGFr ;
        }
        
        } // End of "get_YIGFr()
        
        unsigned int MWindow::get_YIGDA() {
        YIGDA = (YIGFr/4000000000 -1) * 65536 ;
        return YIGDA ;
        } // End of "get_YIGDA()"
        
        unsigned int MWindow::get_YIGFilter() {
        YIGFilter = (YIGFr/4000000000 -1) * 65536 ;
        return YIGFilter ;
        } // End of "get_YIGFilter()"
        
        unsigned char MWindow::get_Koswitch() {
        if ( FR < 10000000 ) {
        Koswitch = 0b00000000 ;
        return Koswitch;
        }
        
        else if( ( FR >= 10000000) && ( FR < 62500000) ) {
        Koswitch = 0b00100000 ;
        return Koswitch;
        }
        
        else if( ( FR >= 62500000) && ( FR < 4000000000) ) {
        Koswitch = 0b01000000 ;
        return Koswitch;
        }
        
        else if( ( FR >= 4000000000) && ( FR < 8000000000) ) {
        Koswitch = 0b01100000 ;
        return Koswitch;
        }
        
        else if( ( FR >= 8000000000) && ( FR < 16000000000) ) {
        Koswitch = 0b10000000 ;
        return Koswitch;
        }
        
        else if( ( FR >= 16000000000) && ( FR < 32000000000) ) {
        Koswitch = 0b10100000 ;
        return Koswitch;
        }
        
        } //End of "get_Koswitch()"
        
        unsigned char MWindow::get_Ndiv() {
        int n={0},FrDDS={0};
        if( YIGFr > 6000000000) {
        // (( n >= 31 ) && ( n <= 40 ) );
        n = 31;
        }
        
        else if( YIGFr <= 6000000000 ) {
        // (( n >= 20 ) && ( n <= 30 ) );
        n = 20;
        }
        do {
        FrDDS = YIGFr/n - fosc ;
        Ndiv = n++ ;
        }
        while (!( FrDDS >= 18677400 && FrDDS <= 28201308 ) );
        if (n > 41) {
        return 1;
        }
        return Ndiv;
        
        } // End of "get_Ndiv()"
        
        long MWindow::get_Phase() {
        Phase = 25 * (YIGFr / Ndiv - fosc) ;
        return Phase;
        
        } //End of "get_Phase()"
        
        //---------------SLOT Definition------------------
        
        void MWindow::updatef(double) {
        
        I2CCWn m_i2ccwn(2, 0x20); //object
        //I2CCWn m_i2ccwnb; //object for default constructor call
        
        if(get_FR() !>= doubleSpinBox->value() ) {
        FR = doubleSpinBox->value() ;
        m_i2ccwn.writeFb();
        }
        }
        
        void MWindow::updatep(double) {
        
        I2CCWn m_i2ccwn(2, 0x20); //object
        // m_i2ccwn.writeAtt(); //Segmentation fault
        
        // if( att != Att) {
        // Att = att;
        // m_i2ccwn.writeAtt();
        // }
        
        }
        
        i2ccwn.h
        
        #ifndef I2C_H_
        #define I2C_H_
        
        #include <QObject>
        #define BBB_I2C_1 "/dev/i2c-1"
        #define BBB_I2C_2 "/dev/i2c-2"
        
        class I2CCWn{
        private:
        unsigned int bus;
        unsigned int device;
        int file;
        
        public:
        I2CCWn(unsigned int bus, unsigned int device); //constructor declaration
        
            I2CCWn();                                       //default constructor declaration
        
            int open();
        // virtual int write(unsigned char value);
        // virtual unsigned char readRegister(unsigned int registerAddress);
        // virtual unsigned char* readRegisters(unsigned int number, unsigned int fromAddress=0);
        // virtual int writeRegister(unsigned int registerAddress, unsigned char value);
        int writeRegister(unsigned int registerAddress, unsigned char value);
        // virtual void debugDumpRegisters(unsigned int number = 0xff);
        virtual void close();
        virtual ~I2CCWn();
        
            void writeAtt();
            void writePow();         //SDI
            void writeFb();
            void writeYIGDA();
            void writeYIGFilter();
            void writeKoswitch();
            void writeNdiv();        //SDI
            void writePhase();
        // virtual ~I2CCWn();
        
        };
        
        //} /* namespace exploringBB */
        
        #endif /* I2C_H_ */
        
        i2ccwn.cpp
        
        #include "i2ccwn.h"
        #include "form.h"
        
        #include <iostream>
        #include <unistd.h>
        #include <stdio.h>
        #include <sstream>
        #include <fcntl.h>
        #include <iomanip>
        #include <sys/ioctl.h>
        #include <linux/i2c.h>
        #include <linux/i2c-dev.h>
        using namespace std;
        
        #define HEX(x) setw(2) << setfill('0') << hex << (int)(x)
        
        class MWindow;
        MWindow* cw; // Object
        //MWindow cw; // Object---Must construct a QApplication before a QPaintDevice
        
        #define IODIRA 0x00 // Input/Output -Direction Register A
        #define IODIRB 0x01 // Input/Output -Direction Register B
        #define OLATA 0x14 // Output Latch A
        #define OLATB 0x15 // Output Latch B
        
        // constructor
        //I2CCWn::I2CCWn (unsigned int bus = 2, unsigned int device = 0x20) {
        I2CCWn::I2CCWn (unsigned int bus, unsigned int device) {
        
        bus = bus;
        device = device;
        file = -1;
        open();
        writeRegister(IODIRA, 0x00); // 0x00 sets all 8bits of Input/Output -Direction Register A to Output
        writeRegister(IODIRB, 0x00); // 0x00 sets all 8bits of Input/Output -Direction Register B to Output
        }
        
        // constructor
        I2CCWn::I2CCWn() {
        // QObject (parent),
        bus= 2;
        device= 0x28; //device 0x20 or 0x28
        file= -1;
        open();
        writeRegister(IODIRA, 0x00);
        writeRegister(IODIRB, 0x00);
        }
        
        // Method open
        
        int I2CCWn::open(){
        string name = BBB_I2C_2;
        
        if((this->file=::open(name.c_str(), O_RDWR)) < 0){
        perror("I2C: failed to open the bus\n");
        return 1;
        }
        
        if(ioctl(this->file, I2C_SLAVE, this->device) < 0){
        perror("I2C: Failed to connect to the device\n");
        return 1;
        }
        return 0;
        }
        
        int I2CCWn::writeRegister(unsigned int registerAddress, unsigned char value) {
        unsigned char buffer[2];
        buffer[0] = registerAddress;
        buffer[1] = value;
        if(::write(this->file, buffer, 2)!=2) {
        perror("I2C: Failed write to the device\n");
        return 1;
        }
        return 0;
        }
        
        //Method to output Att on I2C
        
        void I2CCWn:: writeAtt () {
        writeRegister (OLATA,cw->get_Att());
        writeRegister (OLATB, 0x0f);
        writeRegister (OLATB, 0x10);
        }
        
        /*
        //Method to output Pow on I2C/SPI
        
        void I2CCWn:: writePow () {
        device = 0x28;
        writeRegister (0x28,0xf0,0x05); //Bridge SC18IS602B MSB_First, SPI-Clock 0 quit, Data clocked on trailing edge SPI
        device = 0x20; //SPI clock rate 461kHz
        writeRegister (OLATB, 0x15); //Synch AD5660-2 set
        device = 0x28;
        writeRegister (0x28,0x01,0x00,cw->get_Pow() ) i; //input Pow-data
        // writeRegister (0x28,0x01,0x00,cw.get_Pow() ) i; //input Pow-data
        device = 0x20;
        writeRegister (OLATB, 0x16); //Synch AD5660-2 set 1
        }
        */
        
        //Method to output Fb on I2C
        
        void I2CCWn:: writeFb () {
        writeRegister (OLATA,cw->get_Fb());
        writeRegister (OLATB, 0x0d);
        writeRegister (OLATB, 0x0e);
        }
        
        //Method to output YIGDA on I2C
        
        void I2CCWn:: writeYIGDA () {
        writeRegister (OLATA,cw->get_YIGDA()>>8 ); //Output high Byte
        writeRegister (OLATB, 0x01);
        writeRegister (OLATB, 0x02);
        writeRegister (OLATA,cw->get_YIGDA() & 0x00ff ) ; //Output low Byte
        writeRegister (OLATB, 0x01);
        writeRegister (OLATB, 0x02);
        writeRegister (OLATB, 0x03);
        writeRegister (OLATB, 0x02);
        }
        
        //Method to output YIGFilter on I2C
        
        void I2CCWn:: writeYIGFilter () {
        writeRegister (OLATA,cw->get_YIGDA()>>8 ); //Output high Byte
        writeRegister (OLATB, 0x04);
        writeRegister (OLATB, 0x05);
        writeRegister (OLATA,cw->get_YIGDA() & 0x00ff ) ; //Output low Byte
        writeRegister (OLATB, 0x04);
        writeRegister (OLATB, 0x05);
        writeRegister (OLATB, 0x06);
        writeRegister (OLATB, 0x05);
        
        }
        
        //Method to output Koswitch on I2C
        
        void I2CCWn:: writeKoswitch () {
        writeRegister (OLATB,cw->get_Koswitch()); //Output Koswitch
        }
        
        //Method to output Ndiv on I2C/SDI
        
        //Method to output Phase on I2C
        
        void I2CCWn::writePhase () {
        writeRegister (OLATA, 0x00); //W0 Output for 6x REFCLK Multiplyer disabled
        writeRegister (OLATB, 0x08);
        writeRegister (OLATB, 0x07);
        writeRegister (OLATA,cw->get_Phase()>>24 ); //W1
        writeRegister (OLATA,cw.get_Phase()>>24 ); //W1
        writeRegister (OLATB, 0x08);
        writeRegister (OLATB, 0x07);
        writeRegister (OLATA,cw->get_Phase()>>16 & 0x00ff); //W2
        writeRegister (OLATA,cw.get_Phase()>>16 & 0x00ff); //W2
        writeRegister (OLATB, 0x08);
        writeRegister (OLATB, 0x07);
        writeRegister (OLATA,cw->get_Phase()>>8 & 0x0000ff); //W3
        writeRegister (OLATA,cw.get_Phase()>>8 & 0x0000ff); //W3
        writeRegister (OLATB, 0x08);
        writeRegister (OLATB, 0x07);
        writeRegister (OLATA,cw->get_Phase() & 0x000000ff); //W4
        writeRegister (OLATA,cw.get_Phase() & 0x000000ff); //W4
        writeRegister (OLATB, 0x08);
        writeRegister (OLATB, 0x09);
        writeRegister (OLATB, 0x07);
        }
        
        void I2CCWn::close(){
        ::close(this->file);
        this->file = -1;
        }
        
        I2CCWn:: ~I2CCWn() {
        if(file!=-1) this->close();
        }
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          The first thing I see is that you have an encapsulation issue .

          Your class that manages your i2c devices should not know anything regarding the widgets. Its role is to communicate with the device(s) and not to change the GUI.

          Your MainWindow is the one that will pilot the devices through that other class and will keep things regarding the GUI in sync with whatever comes from the device.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • I Offline
            I Offline
            inexpert
            wrote on last edited by
            #5

            Hi,
            Thanks a lot for your Swift reply. Based on Your feedback, I Will try to solve the Issue. However, it is probably going to take me a while.

            SGaistS 1 Reply Last reply
            0
            • I inexpert

              Hi,
              Thanks a lot for your Swift reply. Based on Your feedback, I Will try to solve the Issue. However, it is probably going to take me a while.

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @inexpert said in signal generator segmentation fault:

              However, it is probably going to take me a while.

              No pressure, do it at your own pace.

              I would recommend getting your I2C class working independently from the GUI first with some unit tests to ensure things keep working and then build your user interface using that class.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • I Offline
                I Offline
                inexpert
                wrote on last edited by
                #7

                Hi,
                Thanks. I already checked my i2c class and it is working as it should.

                1 Reply Last reply
                0
                • I Offline
                  I Offline
                  inexpert
                  wrote on last edited by
                  #8

                  I cannot find a way to bring the data 'Fb' ( return value of the method 'MWindow get_Fb() )
                  into the i2c class :
                  void I2CCWn : : writeFb() {
                  writeRegister (OLATA, Fb);
                  +
                  +
                  can you help me, please

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    It should be the other way around: the method should be writeFb(char Fb) and you call it in MWindow.

                    In your application the main class is MWindow, so it should have an object of type I2CCWn and call its methods with adequate parameter. As suggested before, I2CCWn should be completely independent so that if you use it through your MWindow class or decide to build a command line tool with it, you do not have to change it.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1
                    • I Offline
                      I Offline
                      inexpert
                      wrote on last edited by
                      #10

                      three cheers !!!Following your advice the i2c output works.
                      You are a very competent expert and I want to thank you very much for your efforts.
                      I hope you don`t mind if I contact you again in the future regarding my work on my signal generator
                      (menue, modulation etc.).
                      Best regards!

                      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