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

Netduino Plus - I2C not working


  • Please log in to reply
15 replies to this topic

#1 HAMZA

HAMZA

    Member

  • Members
  • PipPip
  • 12 posts

Posted 27 November 2012 - 05:43 PM

Hi

i have Netduino Plus that i updated last week with the new firmware 4.2 because the I2C don't work in firmware 4.1, and i tried again but the same error again happen (see the attachement).

i use this code that i found here
http://www.tinyclr.c...m/topic?id=9074


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 i2C
{
public class Program
{
static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);

public static void Main()
{

i2c();

while (true)
{
led.Write(true);
Thread.Sleep(10);
led.Write(false);
Thread.Sleep(1500);
}

//Thread.Sleep(Timeout.Infinite);
}


static void i2c()
{
// i2c configuration(address of my eeprom , clock speed)
I2CDevice myi2c = new I2CDevice(new I2CDevice.Configuration(0xA2, 400));

// to store the received value from eeprom
byte[] readBuffer = new byte[1];
byte[] address = new byte[] { 1 };

// Reading data stored in the selected address register
I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[1];

xActions[0] = I2CDevice.CreateWriteTransaction(address);
myi2c.Execute(xActions, 1000);

xActions[0] = I2CDevice.CreateReadTransaction(readBuffer);
myi2c.Execute(xActions, 1000);


// print the received data
Debug.Print("address:" + address[0].ToString() + "data:" + readBuffer[0].ToString());

// go to the next address
address[0]++;

led.Write(true);
Thread.Sleep(1000);
led.Write(false);
}

}
}




there are any solution of this problem ?

Attached Files



#2 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 27 November 2012 - 06:05 PM

i added a little more complex sample of how i do it. it works perfectly on my mini. maybe you have to change some small things on the ic_ class part, depends on what eeprom your using.

Attached Files



#3 HAMZA

HAMZA

    Member

  • Members
  • PipPip
  • 12 posts

Posted 27 November 2012 - 06:41 PM

i added a little more complex sample of how i do it.
it works perfectly on my mini.

maybe you have to change some small things on the ic_ class part, depends on what eeprom your using.


thank you NooM
i will try it in my board with my eeprom(24C04) ^_^

#4 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 27 November 2012 - 08:38 PM

good luck. i just read the datasheet, it only uses one byte as address, but youd have to change that

#5 HAMZA

HAMZA

    Member

  • Members
  • PipPip
  • 12 posts

Posted 27 November 2012 - 11:23 PM

no , it don't work :P . i wrote the i2c code in two different methods and it giving me the same error.

Attached Files



#6 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 27 November 2012 - 11:29 PM

try lock(youri2cdevice) and set the timeout lower. also, do you have pullup resistors on your i2c lines? 4.7kohms are fine. also try 100khz instead of 400 when talking to your device. you can change that when you found out what was wrong.

#7 HAMZA

HAMZA

    Member

  • Members
  • PipPip
  • 12 posts

Posted 28 November 2012 - 12:48 PM

try lock(youri2cdevice) and set the timeout lower.

also, do you have pullup resistors on your i2c lines? 4.7kohms are fine.
also try 100khz instead of 400 when talking to your device. you can change that when you found out what was wrong.


i have pullup my eeprom with 1k resistors.
i have locked my "myi2c" object but its still not working.

when i try to read the value of the selected register ,this message appears in output windows

Une exception de première chance de type 'System.ArgumentException' s'est produite dans Microsoft.SPOT.Hardware.dll.
Une exception non gérée du type 'System.ArgumentException' s'est produite dans Microsoft.SPOT.Hardware.dll


with some traduction, it will be somthig like this :)

A first chance exception of type 'System.ArgumentException' occurred in Microsoft.SPOT.Hardware.dll.
An unhandled exception of type 'System.ArgumentException' occurred in Microsoft.SPOT.Hardware.dll

Attached Files



#8 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 28 November 2012 - 01:00 PM

You are using invalid slave address, 0xA2 includes the R/W bit which is not part of the slave address in .NET MF (max. value is 0x7F, hence the exception), try passing 0x51 instead (assuming A2 = 0, A1 = 0, A0 = 1).

#9 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 28 November 2012 - 01:09 PM

Additional note: The memory requires repeated start condition for random read, you'd need special method for that (you have to pass 'word address' via internalAddress parameter).

#10 HAMZA

HAMZA

    Member

  • Members
  • PipPip
  • 12 posts

Posted 29 November 2012 - 02:14 AM

You are using invalid slave address, 0xA2 includes the R/W bit which is not part of the slave address in .NET MF (max. value is 0x7F, hence the exception), try passing 0x51 instead (assuming A2 = 0, A1 = 0, A0 = 1).



i haven't thinking about this trick, thank you :lol: .
now it work, i can read data from my eeprom, but i can't write on it anything.

address: 0 data: 72
address: 1 data: 101
address: 2 data: 108
address: 3 data: 108
address: 4 data: 111
.......

Attached Files



#11 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 29 November 2012 - 07:49 AM

Unfortunately, I don't have this particular device to check it, but I think the correct method to write a byte is to create just one write transaction of two byte buffer (address, data):

// Write transaction (up to 8 or 16 bytes, depending on the page size)
var xWrite = I2CDevice.CreateWriteTransaction(new [] { RegisterNum[0], Value[0] });
myi2c.Execute(new I2CDevice.I2CTransaction[] { xWrite }, 100);

// Write cycle time is 10 ms
Thread.Sleep(10000);

// Random read transaction requires repeated start condition, but CreateWriteTransaction(...)
// followed by CreateReadTransaction(...) generates two separate transactions,
// you have to use CreateReadTransaction from http://forums.netduino.com/index.php?showtopic=944
var data = new byte[8];
uint startAddressToReadFrom = 0;
var xRead = CreateReadTransaction(data, startAddressToReadFrom, 1);
myi2c.Execute(new I2CDevice.I2CTransaction[] { xRead }, 100);
Note: The code is written from head, may not even compile.

#12 HAMZA

HAMZA

    Member

  • Members
  • PipPip
  • 12 posts

Posted 29 November 2012 - 12:55 PM

Unfortunately, I don't have this particular device to check it, but I think the correct method to write a byte is to create just one write transaction of two byte buffer (address, data):

// Write transaction (up to 8 or 16 bytes, depending on the page size)
var xWrite = I2CDevice.CreateWriteTransaction(new [] { RegisterNum[0], Value[0] });
myi2c.Execute(new I2CDevice.I2CTransaction[] { xWrite }, 100);

// Write cycle time is 10 ms
Thread.Sleep(10000);

// Random read transaction requires repeated start condition, but CreateWriteTransaction(...)
// followed by CreateReadTransaction(...) generates two separate transactions,
// you have to use CreateReadTransaction from http://forums.netduino.com/index.php?showtopic=944
var data = new byte[8];
uint startAddressToReadFrom = 0;
var xRead = CreateReadTransaction(data, startAddressToReadFrom, 1);
myi2c.Execute(new I2CDevice.I2CTransaction[] { xRead }, 100);
Note: The code is written from head, may not even compile.


it work now, thanks :)
you too NooM, thanks for your help :)

#13 GlenS

GlenS

    Member

  • Members
  • PipPip
  • 23 posts
  • LocationTamworth, UK

Posted 29 November 2012 - 01:49 PM

Hi,

Just been thinking about this repeatable read fix (link). Can this be applied in all cases, or will the code have to be used on a device by device basis?

many thanks

Glen

#14 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 29 November 2012 - 02:13 PM

Just been thinking about this repeatable read fix (link). Can this be applied in all cases, or will the code have to be used on a device by device basis?

Basically, it is device-specific. When there is "repeated start condition" mentioned in the device communication protocol description, or it has some kind of addressing scheme (register + data, address + data), those special methods are usually required.

#15 GlenS

GlenS

    Member

  • Members
  • PipPip
  • 23 posts
  • LocationTamworth, UK

Posted 29 November 2012 - 05:59 PM

Basically, it is device-specific. When there is "repeated start condition" mentioned in the device communication protocol description, or it has some kind of addressing scheme (register + data, address + data), those special methods are usually required.



Thanks CW2

#16 thrghost

thrghost

    New Member

  • Members
  • Pip
  • 1 posts

Posted 01 December 2013 - 06:06 PM

i have eeprom 24c64 from atmel and it require ,i can't use the last code  but i can read from it only and can't write any help?






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.