5 Replies Latest reply on Dec 14, 2014 7:29 PM by qiki

    I2C doesn`t work on my edison arduino board with mraa interface

    qiki

      first of all sorry about my terrible english I am trying to use mraa C functions to communicate with my temperature&humidity sensor. The sensor actually is a MCU controlled system which introduce I2C interface to connect to other system. Its SCL and SDA feet are connected to A5 A4 header on my Arduino expansion board respectively. I wrote the following simple snippet to test how edison control the sensor as I2C master.

       

      #define I2C_BUS 6
      #define TRH_SENSOR_I2C_ADDR 0xB8
      
      mraa_init();
      mraa_i2c_context i2c_context = mraa_i2c_init(I2C_BUS);
      printf( "i2c bus initialization finished\n");
      
      
      
      
      if ( mraa_i2c_address( i2c_context, TRH_SENSOR_I2C_ADDR ) != MRAA_SUCCESS ) /* ALWAYS return MRAA_ERROR_INVALID_HANDLE */
           printf("can not find sensor!\n");
      
      uint8_t data_requirement_buffer[3];
      data_requirement_buffer[0] = TRH_SENSOR_FUN_CODE_READ_REG;
      data_requirement_buffer[1] = TRH_RH_REG_ADDR_H;
      data_requirement_buffer[2] = 4;
      if ( mraa_i2c_write( i2c_context, data_requirement_buffer, 3 )==MRAA_SUCCESS)
           printf("requirement has been send\n");
      
      ...
      
      
      
      

       

      When the program called mraa_i2c_address(), it always return MRAA_ERROR_INVALID_HANDLE and nothing happened. At line17 I use LA to analyze I2C signal, I found that no address is present.

      LA.jpg

      Now I have to dig into mraa source code but everything looks find. Is there anybody has successful experience of mraa i2c programming? This problem has troubled me for a few days

        • 1. Re: I2C doesn`t work on my edison arduino board with mraa interface
          arfoll

          Sorry about your troubles with libmraa. Can you give me the output of `journalctl -f` whilst you run your program?

           

          Because you're receiving an INVALID_HANDLE message that is probably becaues the mraa_i2c_init() function failed, make sure to chekc if i2c_context == NULL.

           

          What board are you on? Miniboard or arduino board? Make sure to update your libmraa library, the process is documented here: Upgrading Intel® IoT Developer Kit libraries – Intel® Edison or Intel® Galileo board | Intel® Developer Zone

          • 2. Re: I2C doesn`t work on my edison arduino board with mraa interface
            qiki

            thanks for reply I using arduino board. and yes, I have upgraded packages. Following image come from ' journalctrl -f '

            screenshot.jpg

            because debugged the program step by step, I pretty sure mraa_i2c_context instance is not null.

            • 3. Re: I2C doesn`t work on my edison arduino board with mraa interface
              arfoll

              So the next fun bit is that edison can only sink/source 3mA on it's IO pins and they already have 2x4.7k pulldowns on the arduino breakout. Try dropping to 3.3V by moving J9 to pins 2-3 instead of 1-2.

              • 4. Re: I2C doesn`t work on my edison arduino board with mraa interface
                KurtE

                Some other experiments I would do is also try starting your program again without the device connected to it and see if the address comes out correctly or not.  I had problems with an SRF10 ping sensor not working, but do have it working with a CMPS03 sensor.  Example output:

                Edison CMPS03 LA.jpg

                Note; with my last test (a few minutes ago), i used the SCL/SDA pins and not the A4/A5 (but I earlier confirmed by schematic as well as putting test probes on both that they are connected.

                 

                Code is pretty simple:

                #include "mraa.h"
                #include "math.h"
                #include <unistd.h>
                #define ADDRESS 0x60 //defines address of compass
                
                #define MAX_BUFFER_LENGTH 6
                
                int
                main(int argc, char **argv)
                {
                    mraa_init();
                    uint16_t direction = 0;
                    uint8_t rx_tx_buf[MAX_BUFFER_LENGTH];
                
                    mraa_i2c_context i2c;
                    i2c = mraa_i2c_init(0);
                
                    mraa_i2c_address(i2c, ADDRESS);
                    uint8_t bVer = mraa_i2c_read_byte_data(i2c, 0);   // read one byte
                    printf("CMPS03 Version: %d\n\r", bVer);
                    
                
                    for(;;) {
                        mraa_i2c_address(i2c, ADDRESS);
                        mraa_i2c_write_byte(i2c, 2);    // start read at byte 2
                
                        mraa_i2c_address(i2c, ADDRESS);
                        mraa_i2c_read(i2c, rx_tx_buf, 2);
                        direction = (rx_tx_buf[0] << 8) + rx_tx_buf[1];
                //        direction = mraa_i2c_read_word_data(i2c,  2);
                        printf("Heading : %d\n", direction) ;
                        usleep(250000);
                    }
                }
                
                

                 

                Note: this one inits for bus 0 which is an error and it sets it to default 6.

                 

                Dec 10 07:42:50 Edison libmraa[340]: libmraa initialised by user 0

                Dec 10 07:42:50 Edison libmraa[340]: libmraa initialised for platform 2

                Dec 10 07:42:50 Edison libmraa[340]: libmraa initialised by user 0

                Dec 10 07:42:50 Edison libmraa[340]: Invalid i2c bus, moving to default i2c bus

                ^C

                root@Edison:~/Raspberry_Pi/testCMPS03#

                 

                 

                (arfoll) - They have Pulldowns?  I would expect Pull ups...

                 

                Not sure if that helps or not.

                Kurt

                • 5. Re: I2C doesn`t work on my edison arduino board with mraa interface
                  qiki

                  I tried many many ways to figured out what is the problem of setting the sensor address, even rebuilded and reflashed image to slow bus speed down to 100KHz to feed the sensor, but all failed. So I had to contact the manufacturer and surprisingly they gave me a different slave address, that is why I can not address my sensor. The document in my hands is too old and manufacturer upgraded their products without notification.Thanks guys anyway, very appreciated.