I2C Slave, detecting start and stop conditions?

Technical questions regarding the XTC tools and programming with XMOS.
Post Reply
microman
Junior Member
Posts: 7
Joined: Sat Apr 17, 2010 6:22 pm
Contact:

I2C Slave, detecting start and stop conditions?

Post by microman »

Hello,

I am just embarking on my first XMOS project, I am much more familiar with programming standard micro's and I'm still getting familiar with the xc language. I would like to implement an I2C slave. The bit I'm not sure how to do is detect start or stop conditions such that it resets my internal I2C state machine to the correct state.

Any help would be appreciated.

Kind Regards

mm


User avatar
Andy
Respected Member
Posts: 279
Joined: Fri Dec 11, 2009 1:34 pm

Post by Andy »

Hi - Welcome to XCore.

I'm not too familiar with I2C, but have you looked at the I2C reference code on the XMOS website? http://www.xmos.com/applications/module/i2c

I guess that would be a good place to start. Feel free to post here if you have problems.
microman
Junior Member
Posts: 7
Joined: Sat Apr 17, 2010 6:22 pm
Contact:

Post by microman »

Hello,

I had a looked at the example but that, unfortunately, is for an I2C master. :cry:.

The bit I can't figure out, is how to reliably detect start and stop bits.
If I assume that the master I2C device (out of my control) is always going to behave, then its not too much of an issue, but I don't generally rely on assumptions.

A start bit is defined as falling edge on SDA when SCL is high and a stop bit is a rising edge on SDA whilst SCL is high. SCL and SDA are the clock and data lines of this two wire protocol.

I will persevere...

Regards

mm
User avatar
Andy
Respected Member
Posts: 279
Joined: Fri Dec 11, 2009 1:34 pm

Post by Andy »

One way of doing it would be using the pinseq() / pinsneq() functions. Have a look at section 2.3 "Waiting for a condition on an Input Pin" in the XC guide - http://www.xmos.com/system/files/xcuser_en.pdf

Essentially, you can tell the hardware port to wait for a certain condition on a pin or set of pins. I think this is how the start bit is detected in the UART example in the documentation. The statement looks like:

Code: Select all

oneBit when pinsneq (a) :> x;
where oneBit is your port and a is the condition. More on page 18 of the XC programming manual.
microman
Junior Member
Posts: 7
Joined: Sat Apr 17, 2010 6:22 pm
Contact:

Post by microman »

Hello,

Thanks for you input, but it hasn't really moved me on. I wanted to be able to detect a start or stop conditions on at any point in my I2C state machine, so it resets my I2C state machine accordingly. I guess to do this I would need to put the start and stop detection routines on a separate thread?

When not looking for START/STOP condition, i.e reading or writing data, I would like to set up the SDA pin, so data is clocked in on the rising edge of SCL. This is pretty easy as the XMOS chip provides this facility. However, if I do this, it messes up my start/stop detection code as as START/STOP conditions transition SDA whilst SCL is high. My head is spinning.

Does anybody have example code on implementing I2C slave.

Kind Regards

MM
User avatar
Andy
Respected Member
Posts: 279
Joined: Fri Dec 11, 2009 1:34 pm

Post by Andy »

Hey,

Could you draw a diagram or provide pseudo code of the protocol? I think that would help us understand what you're trying to achieve.

You might want to try looking at the old Xlinkers site for examples of I2C slave.
microman
Junior Member
Posts: 7
Joined: Sat Apr 17, 2010 6:22 pm
Contact:

Post by microman »

Hello,

Heres my pseudo code.

[STATE]

1) Wait for Start Condition. [IDLE]
2) Clock in 7 bit address and R/!W bit . [ADDRESS]
3) ACK(4) or NACK(1) the address. [ACKADDRESS]
4) Determine if address is for a read(5) or write(7). [ACKADDRESS]
5) Clock out Data0 and check ACK. If ok then (5) else (1). [READ]
6) Clock out Data1. Goto (1) [READ]
7) Clock in Data0 and ACK. [WRITE]
8) Clock in Data1 and ACK. [WRITE]
9) Goto (1)

If a start or stop condition is detected anywhere between 1 and 9 go back to the IDLE state.

The bit I can't figure out is how I detect a start or stop condition at any point in the state machine. I need to be able to do this just in case the master device goes wrong half away through a read or write sequence.


Kind Regards

MM
Post Reply