i2c with atmega (new to i2c)

Go To Last Post
18 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm familiar a bit with i2c but I'm not sure why I do not see data. I want to communicate with a thing called the wii-mote. It uses  i2c to talk to its peripherals. I'm trying to emulate a peripheral with an atmega32u4. From what I have read the master should init the communication and I should see a low clock followed by a low SDA. I tried using the chip to pull up on the two lines and after no luck there I used a pair of 4k7 pullups to 5v. Both lines high is a free bus I was pretty sure. Watching the actual device and its peripheral I see the communication start. When I plug in my emulated peripheral I do not see any activity. I'm wondering if the master is waiting for something before it begins? If so what could it be waiting for? Based on what I have gathered on the net I should see the master attempting to talk even if there is no peripheral but I most certainly do not see that.

 

I also have read the 3 to 5 TTL can be fussy so I put things on the scope and the two pullups are holding at 2.7. Maybe this is my issue but most designed do not even use pull ups.

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

Unfortunately, you've written a ton of words, but told us little. This is something you continue to do. Stop, think carefully about the problem then go about describing your setup and what you've observed.

 

If you don't understand i2c - then the first stop is to find the philips/NXP i2c specification. i2c is pretty straightforward, but if you don't understand the basics, you'll go around in circles.

 

From your description, I'm not sure who is the master and what the slave(s) are. You've told us there is a wii-mote and a mega32u4.

 

As for 'init the communication' - in i2c speak this is the start sequence. The master sends the start sequence to acquire the bus. It will look for the bus being high, then going low in the correct sequence. Then the master sends the address and r/w bit. It expects the slave to response with an acknowlege. Therefore both master and slave need to respond appropriately. From this point data can be transferred. The master then relinquishes the bus by sending a stop sequence.

 

i2c bus demands pullup resistors! They are not an option. I2c only actively pulls the bus low, it does not source current. There may be parasitic pullups in the circuit, but you are well advised to follow the recommendations. Level translation can be a challenge. The usual common gate mosfet configuration works well. Of course, the devil is in the details. The mosfets have to be the right way around and the resistor values determine the achievable speed. The bus levels are not TTL - so be careful using that term.

 

I would suggest getting yourself the little Saleae logic clone logic analysers. This makes decoding i2c much easier. If your scope does i2c decode, then that also helps.

 

 

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

As for what you will see, you should, at a very minimum, see the master send the 7 bit address, followed by the read/write bit, then a wait for the acknowledge by the slave. But, YOU Have to tell the master to issue a START, which is what this is. You also need a pull-up on both the clock line and the data line. 4.7K should be OK for 100KHz clock rate and short distances (on the  same board). If there is a cable, you might have to reduce the pull-up resistors to 2.2K or even 1.5K. Internal mcu pull-ups tend to be pretty wimpy (say 20K to 40K) and are not useful for much except a nil bus. 

 

If you don't see that start condition followed by address, then something is wrong with the master.

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

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

My issue most of the time and will always be explaining. It's just something I'm no good at.

 

Few answers.

 

- I have studied the i2s bus to the point I'm sure its not my misunderstanding.

- The master is this wii-mote and it has devices you connect to it (slaves).

- my mega32u4 is going to act as one of the slaves. That is my plan.

- I have the Saleae logic , this is what I'm using to see the signal.

" The master sends the start sequence to acquire the bus. " This is what I'm not seeing. Both lines are high (2.7v) according to the analyzer. This is why I asked if that could be my problem. What I'm wondering is what it takes to get the master to start talking. It never pulls low, lines remain high.

 

"Level translation can be a challenge. " - Thus far I have read the same but many have no issues making i2c comm work with a wii-mote, although typical the atmega is the mast in my case its the slave.

 

 

 

Last Edited: Wed. Mar 4, 2020 - 12:34 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi jim..

 

"But, YOU Have to tell the master to issue a START, which is what this is. " - The slave needs to tell the master to send the init?

 

 

"If you don't see that start condition followed by address, then something is wrong with the master." The master works with its slave but when I try to act as a slave I never see the init. How do I tell the master to init, I missed that part in what you said? You said "YOU",  Everything I read said the master starts the communication. What do you mean by "YOU"?

 

 

 

 

 

Last Edited: Wed. Mar 4, 2020 - 12:39 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Everything is managed by the master. You, as the operator, need to do what ever it takes to get the master to send a start condition. There is nothing the slave can do UNLESS there is some sort of back channel signaling system, such as an interrupt line, to request "service". 

 

So, YOU need to find out what that procedure is, because YOU need to do something to cause the start to be generated. And YOU also have to find out what kind of address the master sends, and what expects from slave. For example, the master MIGHT send an all-call address, expecting the slave to send back its own address after the all-call ack. OR, it might expect the slave to have a specific address. YOU have to discover these things.

 

Jim

 

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

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

So you're talking about this:

https://wiibrew.org/wiki/Wiimote...

 

From what i can gather, the wii-mote is not going to magically talk to slave devices unless there's some code elsewhere to make it so.

 

You really make it hard for us to help you - tell us up front what you're doing. Instead, I had to Google to get half an idea.

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

Everything is managed by the master. You, as the operator, need to do what ever it takes to get the master to send a start condition. There is nothing the slave can do UNLESS there is some sort of back channel signaling system, such as an interrupt line, to request "service". 

 Yeah so that is the way I understood how it works. So there is nothing I can do here and yet nothing is happening. This system does not have additional lines, only SDL and SCA. As long as my pullups are there, I should see a start condition on the analyzer but I do not. I understand that I need to corresponds with the master here but if the master never begins the communication there is nothing to do.

 

 

 

"the wii-mote is not going to magically talk to slave devices", That would suggest the slave starts the communication, the opposite of what I was thinking.

 

 

I have attached what I see when the master and device talk. I was under the impression the master sends that setup write to 0x00. So I looped my code waiting, I would have expected to see the part I circled in yellow. I only see the two lines high.

 

 

Kartman, if my posts bother you so much why help? I'm not twisting anyone's arm here. Of course my question is vague, I come here when I do not understand things, not when I do.

 

 

 

 

 

 

Attachment(s): 

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

I am totally confused. You say that you see nothing, yet there is a perfectly good start condition and sending address 0x00. You have the logic analyzer setup  improperly labeled, though. SDA is DATA and SCL is CLOCK. What you have labeled "latch" is clock. What you have labeled "clock" is data. Nobody has issued an ACK (e.g. there is a NAK). After the NAK, I think there is a repeated start. I did not try to figure out what happens after that.

 

Address 0x00 can be used as an all-call address. The first thing that has to happen, after that, is that some slave needs to issue an ACK. What has to happen next depends on what the master expects. It MIGHT be the slave's 7 bit address, but somehow, YOU need to find out.

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

Last Edited: Wed. Mar 4, 2020 - 02:50 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm trying to help you, help yourself. 

http://www.catb.org/esr/faqs/sma...

 

'the quality of the question determines the quality of the answer' - so, do you want a good answer?

 

You could have showed the logic analyser pic and told use what you want to do first up - only at post #8 are starting to get somewhere.

 

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

you "I am totally confused. You say that you see nothing, yet there is a perfectly good start condition and sending address 0x00. " -

me " I have attached what I see when the master and device talk."

 

Right so I attached the "good" signal. This is with the current slave and device do.

 

 

My task is to emulate the device, and when I look at the wires they are always high not like what I see in the image.

 

Sure Kartman I see that now, but was too confused at first. Typically I find if I start with lots of text and images things go south, if I give tidbits things go better. Though yeah, still working on the art of explaining. Never was any good at it, thx for helping.

 

 

 

Last Edited: Wed. Mar 4, 2020 - 10:25 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok, so images then.

 

top is the real system, button is my attempt to emulate it. Once I enter my endless loop and based on what I think I know, I should see the master do something.

Attachment(s): 

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

You’ve still not told us what you want to do!
My interpretation is you want to emulate a wiimote extension. Yes or no?

From my basic googling, the wiimote can be master and slave, but is usually the master. Also what i’m not clear on is how the wiimote determines if there is an extension connected - does it send out a broadcast address and expect a response? Or does the game query the wiimote which then expects to talk to a specific device? Looking at the examples from the link I gave earlier, it seems this might be the case. It could be both.
If you want to emulate a specific extension, then sniff the comms. You’ve probably done this already. This should give you an idea of what is expected. The. i would get an Arduino and mock up some code to talk to the extension. If you get this to work then you have a means of testing your emulation. I used this technique recently when I was doing a midi interface to a rhythm box- the dsp and front panel used i2c and i needed to tap into that.

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

"My interpretation is you want to emulate a wiimote extension. Yes or no? " Yes.

 

" From my basic googling, the wiimote can be master and slave, but is usually the master " Yes but it's a slave to Bluetooth not i2c. My intention is to work with it in the master mode. 

 

 " Also what i’m not clear on is how the wiimote determines if there is an extension connected " So this is what I apparently am failing to to ask. This is what I have been try to ask and what I can not figure out. Now, perhaps my entire problem was that I took things for granted. I was nearly %100 sure this was normal i2c talk and the reason I figure it was not important to mention much about the wii-mote. For all I knew/know its just a normal i2c. So the question should be, how does a normal i2c master know when a attachment is there. Is there such rule? - guessing not.  Most gaming peripheral send a routine check. Like the Play station (isp), it sends out a 1 and waits for a reply. I was assuming this would be similar. More importantly and why I confused the hell out of you, is I figured this was a normal i2c question. Sorry, I clearly was wrong.....

 

Now that attached screen shot is the point at where I plugged in the real device to the wii-mote. I have no idea who initialed that command. My next go it it, I will see if I can initiate the command from the device end and see if it replies. I was pretty damn certain the master needs to first comm to all devices on the bus and the device must wait for a wake up. 

 

I may try the  Arduino  idea but I'm thinking ill get this without.

 

BTW: I found a project that does kind of what I want to do here. It is a bit more involved then what I want but I'm trying to study the concept here. If you have a moment to take a glance and can tell from his code what its doing, let me know. 

https://www.raphnet.net/electron...

 

 

his init does this

static void twi_slave_init(unsigned char addr)
{
    // initialize stuff
    twi_reg_addr = 0;

    // set slave address
    TWAR = addr << 1;
    
    // enable twi module, acks, and twi interrupt
    TWCR = _BV(TWIE) | _BV(TWEA);
}

 

followed by this

 

void wm_start(void)
{
    if (!wm_started) {
        // Start I2C
        TWCR |= _BV(TWEN);
        wm_started = 1;
    }
}

 

 

Does not look like he is sending anything there. Also take note this schematic has no pull-ups either. I'm still confused by that. Why does no example use pullups? The wii-mote does not have anything pulling up internally and I think relying on the MCU 20k is a bad idea. 

 

 

 

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

Seems like you are going in circles (or squares)....I would monetarily abandon this for some simple learnings... just try hooking up some I2C device at the same voltage level.

 

Dealing with multiple voltage translations opens up a whole bunch of other possible troubles, any of which can make the whole scheme mysteriously fail, especially if you've never even had basic I2c working at all.

If you can't get something working (maybe a DAC or LED chip), at the same voltage, there is even less hope (none) using multivoltage.

 

When you finally have single voltage working, convert that to multivoltage.  You will probably find NEW troubles with your conversion circuits, transistor wiring, or other anomalies.  At least you will know the software is good & can concentrate on the bidirectional voltage translation.

 

Once you have multivoltage figured out (working) you will be on semi-solid ground to attempt a similar connection with your desired device.   Taking things step-by-step gets you there in one sane piece.

 

If you try learning everything in one giant swoop, you will probably take 3 times as long.

 

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

S_K_U_N_X wrote:

...a pair of 4k7 pullups to 5v. ...so I put things on the scope and the two pullups are holding at 2.7...

 

Can you clarify this? Your pullups go to +5V but you only see 2.7V on SCL and SDA? So you are running your slave, ie mega, on 5V? This would imply that the on-chip protection diodes in whatever chip the master uses are clamping to the supply rail in the master.

 

But, more fundamentally, why run your mega at 5V? Just run it at 3V3 and the need for level shifting disappears.

 

Your problem is that 2.7V will not be seen by your mega, if it is running on 5V, as a logic high. The datasheet says that the two-wire interface needs the input high voltage to be greater than 0.7 Vcc, so 3.5V.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

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

Took a long break from the project and came back to it, problem hit my right away. The wii-mote uses a battery pack (in the shape of batteries)  but also one can use batteries. It didnt even occur to me to check that and sure enough the batteries make it work and show the voltage I was looking for. Also for anyone that may hit this via searching... My opening topic asked " I'm wondering if the master is waiting for something before it begins? " If you look at the pinout for this wii-mote online some go so far as to point out there is a device detect pin. Its active at about 2.2v. This is how the host knows its connected.

 

I also ended up level shifting and the TTL is around 2.2v itself. Everything now works. I'm not sure the shifting is needed but I'd rather use it that way.

 

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

You keep on using the term ‘TTL’ in the wrong context. Do you know what ‘TTL’ means?
https://en.m.wikipedia.org/wiki/Transistor–transistor_logic
How does that line up with your use of the term? Maybe you might think I’m being picky, but look what happened when Bush used the word ‘interwebs’.