I2c communication between Atmel and Arduino.

Go To Last Post
16 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
The Atmega is the master and the Arduino is the slave in i2c. The code below is the code part for Master transmitter of Atmega.  The problem here is the TWI Address/commands,the second parameter. What must I mention for Arduino? In arduino , only the slave address is mentioned. If required I can add my code.

typedef struct {
/* ! TWI chip address to communicate with. */
char chip;
/* ! TWI address/commands to issue to the other chip (node). */
uint8_t addr[3];
/* ! Length of the TWI data address segment (1-3 bytes). */
int addr_length;
/* ! Where to find the data to be written. */
uint8_t *buffer;
/* ! How many bytes do we want to write. */
unsigned int length;

} twi_package_t; 
Last Edited: Fri. Apr 21, 2017 - 10:18 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
 Atmega is the master and the Arduino is the slave

They are both Atmel in case you didn't know. What is you question?

 

And this forum is only for Studio bugs so I'll move the thread.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

Last Edited: Fri. Apr 21, 2017 - 10:18 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It appears you're looking at the TWI driver code from the ASF.

 

Here's the Atmel ASF quick start guide for the TWI driver:

 

http://asf.atmel.com/docs/latest/common.components.crypto.sha204.example_client_host.atxmega128a1_xplain/html/xmega_twi_quickstart.html

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the corrections . 

They are both Atmel in case you didn't know

I cannot understand this statement because. My master is ATmega and my Slave is Arduino DUE (Atmel SAM3X8E ARM Cortex-M3) . The ASF wizard which is provided is for Xmega but mine is ATmega. I am new to the AVR controllers , so I dont know whether the API for Xmega can be used for ATmega !! When I started learning, the Atmel studio,I had the option to select the board so I really dont know if both(ATmega Xmega) can be used for my Atmega board ?? In my I2c ,The master AVR is programmed in Atmel studio and My slave is programmed in Arduino IDE. My question is,  I have included  code part earlier. It asks for the "slave address" - this is the general address we assign for slave(DUE board in my case)  and it asks for another parameter called "slave memory address" - it says at which address in the slave should I write my data. Arduino does not have any specification like this. So what should I do in regard to this ? 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As for your master code, there's no code, just a structure.  You need only send the slave address and the data. 

 

The slave side seems trivial in Arduino.

https://www.arduino.cc/en/Tutori...

Why do you not use Arduino for the master? What atmega are you using for the master? Do you realise the Arduino tools support quite a few devices - you don't need to have an Arduino board.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

 

Atmega used is Atmega128 RFR2. Because, the slave of Atmega has to check the status code everytime and acknowledgements everytime. So Atmega as Master was easy. Arduino master or slave it is very simple design to choose. I have attached the link of the example that I follow: (which I am using as reference to program ,nothing unique in my code) 

 

 

http://asf.atmel.com/docs/latest/mega.drivers.twi.master_example.atmega256rfr2_atmega256rfr2_xplained_pro/html/twi__master__example_8c.html

 

This is At256 but At128 is almost the same for twi driver module. Only this was available.

 

Thank you. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

pika wrote:
I am new to the AVR controllers

Do you have experience with any other microcontroller(s) ?

 

, so I dont know whether the API for Xmega can be used for ATmega !!

Assume NO unless specifically stated otherwise!

 

AIUI, Xmega peripherals (such as I2C) are not the same as ATmega.

 

 I really dont know if both(ATmega Xmega) can be used for my Atmega board ??

Almost certainly not.

 

I have included  code part earlier. It asks for the "slave address" - this is the general address we assign for slave(DUE board in my case)  and it asks for another parameter called "slave memory address"

Well, of course it does - because the example you say you're using is for talking to a memory device - specifically, an EEPROM!!

 

 

http://asf.atmel.com/docs/latest...

 

 

Arduino does not have any specification like this. So what should I do in regard to this ? 

  • Either choose a different Master example which more closely matches your Slave
     
  • Or modify your Slave so that it does look like a "memory" device

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

Yes, I have experience with PIC micro-controller and 8051. But not very much with communication protocols, something with interrupts ,timers, counters. . . 

 

Well, of course it does - because the example you say you're using is for talking to a memory device - specifically, an EEPROM!!

 

The point is Not only this specific example, but the general TWI driver also has the same format,here it is:

 http://asf.atmel.com/docs/latest/mega.drivers.twi.master_example.atmega256rfr2_atmega256rfr2_xplained_pro/html/twi__megarf_8c.html

 

 asking for a specific memory address, so I had got this doubt. To be honest, I thought , add the library and pass the parameters, and the output will be obtained.wink

 

Thank you for the above answers . I will try them. 

 

 

 

Last Edited: Mon. Apr 24, 2017 - 09:23 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Start with two Arduinos. Run one as Master, the other as Slave.
Then replace the Master with regular C code. e.g. Fleury.
.
Note that this strategy can work with most homework problems. Arduino has working code for almost everything.
If you develop on Arduino, you start with success. It is even used by professionals. e.g. write test code in Arduino.
.
Remember that Fleury uses 8-bit Slave address e.g. 0xA0 for 24Cxx. Arduino Wire.h uses 7-bit Slave e.g. 0x50.
.
David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
Remember that Fleury uses 8-bit Slave address e.g. 0xA0 for 24Cxx. Arduino Wire.h uses 7-bit Slave e.g. 0x50.

See: http://www.avrfreaks.net/comment...

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

pika wrote:
The point is Not only this specific example, but the general TWI driver also has the same format,here it is:

 http://asf.atmel.com/docs/latest/mega.drivers.twi.master_example.atmega256rfr2_atmega256rfr2_xplained_pro/html/twi__megarf_8c.html 

 

Given the specific mention of "atmega256rfr2_xplained_pro" in that URL, I rather doubt that this is a "general" driver: it looks very much like it is specifically for that particular board - and, thus, for the specific EEPROM on that particular board.

 

But, as David said, why not just use 2 Arduinos??

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote :But, as David said, why not just use 2 Arduinos??

 

 

like http://www.gammon.com.au/i2c (an arduino sends "hello world " to his/her slave; the latter displays it ; worked)

Another advantage of this link : you can find the I2C adress  of a device with a scanner -this is very comfortable -; code is given in "Reply 6" n (on rPI, comes from i2c-tools);

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I guess the other question is: why do you need two boards anyhow?

 

What are you doing that either one of the boards can't do on its on??

 

http://www.catb.org/~esr/faqs/sm...

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, I would try with two Arduinos. That would be better ..

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But why use two of anything?

 

Why can't this be done on just one board??

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Because that would be seriously difficult.

 

The assignment is for "separate Master and Slave".  

 

I generally suggest:

1.  One Master and One hardware Slave chip e.g. 24Cxxx.

2.  Then try Arduino Master and software Slave (on another Arduino)

3.  Then replace the Arduino Master with Fleury (or similar Master)

4.  Then replace the Arduino Slave with AVR311 (or similar TWI app note)

 

Every step has got something to compare with.

The OP only needs to go as far as (3).

 

I would expect that anyone could write a polled TWI Master.   An interrupt driven Slave is fairly tricky.

It is necessary to understand how Fleury is designed and written.    I see little point in writing it from scratch.

 

The main lesson to be learned is :  "how to use an App Note or Library example"

 

David.

Last Edited: Mon. Apr 24, 2017 - 04:16 PM