title: Using Python smbus on a Raspberry Pi - confused with syntax
body: <p>I am trying to use python-smbus on a Raspberry Pi to communicate with an MMA7660 accelerometer chip using I2C. </p>
  
  <p>In the code below, I am reading registers 0x00, 0x01, 0x02 and 0x03 of the chip, and I am getting the exact same values for all. Looking at the values, and tilting the chip, I can see that they all correspond to register 0x00, the X value register.</p>
  
  <p>Output:</p>
  
  <pre><code>...
  1 1 1 2
  3 3 3 3
  1 1 1 1
  59 60 60 60
  51 51 51 51
  58 58 58 58
  3 3 3 3
  62 62 62 62
  58 58 58 58
  62 62 62 62
  ...
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>  import smbus
    import time
  
    bus = smbus.SMBus(1)
    # I2C address for MMA7660                                                     
    addr = 0x4C
    try:
      bus.write_byte_data(addr, 0x07, 0x00)
      bus.write_byte_data(addr, 0x06, 0x10)
      bus.write_byte_data(addr, 0x08, 0x00)
      bus.write_byte_data(addr, 0x07, 0x01)
    except IOError, err:
      print err
  
    while True:
      try:
        x = bus.read_byte_data(addr,0x00)
        y = bus.read_byte_data(addr,0x01)
        z = bus.read_byte_data(addr,0x02)
        tr = bus.read_byte_data(addr,0x03)
        print x, y, z, tr
        time.sleep(0.25)
      except:
        print 'exiting...'
        break
  </code></pre>
  
  <p>Am I doing something wrong with the smbus syntax? I did look at the documentation <a href="http://wiki.erazor-zone.de/wiki:linux:python:smbus:doc" rel="nofollow noreferrer">here</a>.</p>
  
  <p>I have verified that the chip works - I can communicate fine with it using an Arduino and setting the registers in the same order as above.</p>
  
  <p><strong>Update #1 (28 Jun 2013)</strong>:</p>
  
  <p>As per Sylvain's comment, I am attaching oscilloscope output for SDA/SCL lines for the following code:</p>
  
  <pre><code>bus.write_byte(addr, 0x01)
  print bus.read_byte(addr)
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/U1IA1.jpg" alt="enter image description here"></p>
  
  <p><strong>Update #2:</strong></p>
  
  <p>I guess there is a known problem with I2C on Raspberry Pi - there is no "Repeated Start".</p>
  
  <p><a href="https://raspberrypi.stackexchange.com/questions/7138/mma8452-i2c-module">https://raspberrypi.stackexchange.com/questions/7138/mma8452-i2c-module</a></p>
  
  <p>According to the Linux SMBus spec:</p>
  
  <pre><code>SMBus Read Byte:  i2c_smbus_read_byte_data()
  ============================================
  
  This reads a single byte from a device, from a designated register.
  The register is specified through the Comm byte.
  
  S Addr Wr [A] Comm [A] S Addr Rd [A] [Data] NA P
  </code></pre>
  
  <p>But when I tried it, the osciiloscope clearly shows a STOP (P) before the Repeated Start (S).</p>
  
  <p>So I guess I am out of luck for using I2C hardware on the Pi to talk to the MMA7760.</p>