Erroneous i2c response from LSM303D to Netduino Plus

190 Views Asked by At

I have this module on a breadboard:

https://www.pololu.com/product/2127

+3V3 into Vdd, GND to Netduino GND, SDA to Netduino pin A4 and SCL to Netduino pin A5.

I'm attempting to use the NETMF i2c functions to simply read the response from the WHO_AM_I register. I should be seeing 0x49, instead I always see 0x73.

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

namespace HelloWorld
{
    public class Program
    {   
        static ushort I2C_ADDRESS = 0x3A>>1;//0011 101*
        static int I2C_CLOCK_RATE_KHZ = 400;
        static I2CDevice.Configuration LSM303D_I2C_CONFIG = new I2CDevice.Configuration(I2C_ADDRESS, I2C_CLOCK_RATE_KHZ);
        static I2CDevice LSM303D = new I2CDevice(LSM303D_I2C_CONFIG);

        public static void Main()
        {   
            byte[] WRITE_BUFFER;
            byte[] READ_BUFFER;
#region write
            WRITE_BUFFER = new byte[2];
            WRITE_BUFFER[0] = (byte)0x0F;

            I2CDevice.I2CTransaction[] writeTransaction = new I2CDevice.I2CTransaction[]
            {
                I2CDevice.CreateWriteTransaction(WRITE_BUFFER)
            };

            int bytesWritten = LSM303D.Execute(writeTransaction, 1000);

            while (bytesWritten < WRITE_BUFFER.Length)
            {
                byte[] temp = new byte[WRITE_BUFFER.Length - bytesWritten];
                Array.Copy(WRITE_BUFFER, bytesWritten, temp, 0, temp.Length);

                writeTransaction = new I2CDevice.I2CTransaction[]
            {
                I2CDevice.CreateWriteTransaction(temp)
            };

                bytesWritten += LSM303D.Execute(writeTransaction, 1000);
            }

            // make sure the data was sent
            if (bytesWritten != WRITE_BUFFER.Length)
            {
                throw new Exception("Could not write to device.");
            }
#endregion

#region read
            READ_BUFFER = new byte[2];
            I2CDevice.I2CTransaction[] readTransaction = new I2CDevice.I2CTransaction[]
            {
                I2CDevice.CreateReadTransaction(READ_BUFFER)
            };

            int bytesRead = LSM303D.Execute(readTransaction, 1000);

            // make sure the data was read
            if (bytesRead != READ_BUFFER.Length)
            {
                throw new Exception("Could not read from device.");
            }
#endregion
            Debug.Print("Response - "+READ_BUFFER[0].ToString()); 
        }   
    }
}

I've a feeling it's something very simple as I've got the same module talking to my pic18, though that uses bit banging i2c.

Any help would be greatly appreciated.

0

There are 0 best solutions below