Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

I2C error


  • Please log in to reply
5 replies to this topic

#1 Lord Wheezy

Lord Wheezy

    New Member

  • Members
  • Pip
  • 3 posts

Posted 03 October 2011 - 03:12 AM

I get the following error when ever I try to execute the I2CDevice.Execute function.

#### Exception System.ArgumentException - 0xfd000000 (1) ####
#### Message:
#### Microsoft.SPOT.Hardware.I2CDevice::Execute [IP: 0000] ####
#### Phaser_6140.Program::Main [IP: 0106] ####
A first chance exception of type 'System.ArgumentException' occurred in Microsoft.SPOT.Hardware.dll
Exception was thrown: System.ArgumentException
Microsoft.SPOT.Hardware.I2CDevice::Execute


My code is as follows:

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace Phaser_6140
{
    public class Program
    {
        public static void Main()
        {
            // write your code here
            OutputPort SDA = new OutputPort(Pins.GPIO_PIN_A4, false);
            OutputPort SCK = new OutputPort(Pins.GPIO_PIN_A5, false);
            SDA.Dispose();
            SCK.Dispose();

            InputPort button = new InputPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled);
            OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
            bool pushState = false;
            bool pushStatePrev = false;
            bool ledState = false;
            bool ledBlinking = false;
            uint blinkCounter = 0;
            uint blinkcounterLimit = 1000;
            ushort writeAddress = 0xA0;
            ushort readAddress = 0xA1;
            int clockRateKhz = 100;
            I2CDevice.Configuration writeConfig = new I2CDevice.Configuration(writeAddress, clockRateKhz);
            I2CDevice.Configuration readConfig = new I2CDevice.Configuration(readAddress, clockRateKhz);
            I2CDevice eeprom = new I2CDevice(writeConfig);

            byte[] writeBuffer = new byte[1];
            writeBuffer[0] = 0;
            byte[] readBuffer = new byte[256];
            // create an i2c read transaction to be sent to the device.
            I2CDevice.I2CTransaction[] writeXAction = new I2CDevice.I2CTransaction[1];
            writeXAction[0] = I2CDevice.CreateWriteTransaction(writeBuffer);
            I2CDevice.I2CTransaction[] readXAction = new I2CDevice.I2CTransaction[1];
            readXAction[0] = I2CDevice.CreateReadTransaction(readBuffer);

            while (true)
            {
                try
                {
                    pushStatePrev = pushState;
                    pushState = button.Read();
                    led.Write(ledState);

                    if (ledBlinking)
                    {
                        if (blinkCounter > blinkcounterLimit)
                        {
                            blinkCounter = 0;
                            ledState = !ledState;
                        }
                        blinkCounter++;
                    }

                    if (!pushState && pushStatePrev)
                    {


                        Debug.Print("Change LED State");

                        //the i2c data is received here from the device.
                        int writeTransferred = eeprom.Execute(writeXAction, 1000);
                        eeprom.Config = readConfig;
                        //int readTransferred = eeprom.Execute(readXAction, 1000);

                        //make sure the data was received.
                        //if (transferred != readBuffer.Length)
                        //    throw new Exception("Could not read from device.");
                        //else

                        ledBlinking = true;

                        string buff = "";

                        for (int i = 0; i < 256; i++)
                        {
                            buff += readBuffer[i].ToString();
                        }

                        Debug.Print(buff);
                    }
                }
                catch (Exception e)
                {
                    Debug.Print(e.Message);
                    if (e.InnerException != null)
                    {
                        Debug.Print(e.InnerException.Message);
                    }
                    Debug.Print(e.StackTrace);
                }
            }


        }


    }
}

Any suggestions?

#2 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 03 October 2011 - 06:10 AM

ushort writeAddress = 0xA0;
ushort readAddress = 0xA1;

The current I2C implementation supports only 7-bit slave address, so 0xA0 is out of range. You'd probably need to use 0x50 address and only one configuration - setting Read/Write bit is handled automatically depending on the type of transaction.

#3 Stefan W.

Stefan W.

    Advanced Member

  • Members
  • PipPipPip
  • 153 posts

Posted 03 October 2011 - 10:56 AM

Out of curiosity - what is the purpose of this?
            // write your code here
            OutputPort SDA = new OutputPort(Pins.GPIO_PIN_A4, false);
            OutputPort SCK = new OutputPort(Pins.GPIO_PIN_A5, false);
            SDA.Dispose();
            SCK.Dispose();

I believe that no discovery of fact, however trivial, can be wholly useless to the race, and that no trumpeting of falsehood, however virtuous in intent, can be anything but vicious.
-- H.L. Mencken, "What I Believe"

#4 Lord Wheezy

Lord Wheezy

    New Member

  • Members
  • Pip
  • 3 posts

Posted 04 October 2011 - 01:08 AM

The current I2C implementation supports only 7-bit slave address, so 0xA0 is out of range. You'd probably need to use 0x50 address and only one configuration - setting Read/Write bit is handled automatically depending on the type of transaction.


That did it thanks, CW2!

#5 Lord Wheezy

Lord Wheezy

    New Member

  • Members
  • Pip
  • 3 posts

Posted 04 October 2011 - 01:14 AM

Out of curiosity - what is the purpose of this?

            // write your code here
            OutputPort SDA = new OutputPort(Pins.GPIO_PIN_A4, false);
            OutputPort SCK = new OutputPort(Pins.GPIO_PIN_A5, false);
            SDA.Dispose();
            SCK.Dispose();


In one of the other I2C related posts It was suggested that setting and resetting the pins for SDK and SCL might help. I want to say it was one of the moderators, Chris Walker, made the suggestion. But I could be wrong and I lost track of the thread.

#6 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 04 October 2011 - 05:43 AM

In one of the other I2C related posts It was suggested that setting and resetting the pins for SDK and SCL might help. I want to say it was one of the moderators, Chris Walker, made the suggestion. But I could be wrong and I lost track of the thread.

It was suggested here:
http://forums.netdui...2299#entry12299

[Also, just for fun...try creating OutputPorts for pins A4/A5 and then disposing them right before you open up the I2C connection. If that fixes the issue, then there's a glitch in the firmware.]


"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.