2

I have the raspberry pi 3 model B and would like to communicate over i2c. I have heard that the bcm2835 and bcm2837 chip ar almost identical, so I should be able to use the bcm2835 library to communicate over i2c, yet I don't see anything happening.

  • I have 2 pull ups from 10k
  • this code shows me that I always get a NACK
  • a logic analyzer that shows me that the serial lines are always high and never go low

  • yes I did enable i2c in raspberry pi configurations > interfaces > i2c

Could anybody explain me what I am doing wrong?

EDIT:

- i2cdetect -y 1

    pi@raspberrypi:~ $ i2cdetect -y 1

         0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f

    00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 

    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 

    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 

    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 

    40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 

    50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 

    60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- -- 

    70: -- -- -- -- -- -- -- --                         

    pi@raspberrypi:~ $ 
  • This is the code I use to communicate, reason is always equal to 1 (=NACK):

    #include <bcm2835.h>
    
    #include <stdio.h>
    
    
    #define slaveAddress 0x68
    
    int main(int argc, char **argv) {
    
        char commands[1];
        uint8_t reason;
    
        bcm2835_init();
        bcm2835_i2c_begin();
    
        printf("i2c busy\n");
        bcm2835_i2c_setSlaveAddress(slaveAddress);
        bcm2835_i2c_set_baudrate(1000);
        bcm2835_delay(500);
    
        commands[0] = 0x35;
        reason = bcm2835_i2c_write(commands, 1);
    
        printf("reason: %d\n", reason);
        return 0;
    }
    

In the BCM2835 header file, I modified the base address from 0x20000000 to 0x3F00000000 (neither of them worked).

  • this is my setup

enter image description here

  • I tried to communicate with the module using the wiringPi library. That worked. So the issue has to do with my code somehow....
11
  • Comments are not for extended discussion; this conversation has been moved to chat. – goobering Apr 12 '17 at 11:48
  • 1
    it looks like you may have sda and scl reversed – Steve Robillard Apr 12 '17 at 12:34
  • @SteveRobillard sorry this is an "older" picture. In the meantime I already put them correctly. But the issue lays elsewhere. – qjbihftc Apr 12 '17 at 12:59
  • Please add a picture of the connections you are actually using, explain how you have enabled I2C, cut n' paste the output from i2cdetect -y 1, and include an SSCCE (not a link). – goldilocks Apr 12 '17 at 13:02
  • @goldilocks updated my answer – qjbihftc Apr 12 '17 at 13:49
1

First of all, your code is missing pin initialization

Do so by using void bcm2835_gpio_fsel(uint8_t pin, uint8_t mode)

Also consider your access level

If the library runs with any other effective UID (ie not root), then bcm2835_init() will attempt to open /dev/gpiomem, and, if successful, will only permit GPIO operations. In particular, bcm2835_spi_begin() and bcm2835_i2c_begin() will return false and all other non-gpio operations may fail silently or crash.

Check for bcm_init's return value if you are unsure. Nonzero means you are good to go (=root).

Modifying the base address to 0x3F00000000 should not be necessary as of the RPi3 (although it is correct that they do differ):

On RPI 2, the peripheral addresses are different and the bcm2835 library gets them from reading /proc/device-tree/soc/ranges. This is only availble with recent versions of the kernel on RPI 2.

(2 and 3 have the same base address)

This mechanism depends on the presence of /proc/device-tree/soc/ranges in your filesystem. So go check that and look up how to enable device-tree support in case it is not visible.

If you are done checking all of these and it is still not working, the next step is to consider void bcm2835_i2c_setClockDivider(uint16_t divider)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.