The Stick
Posts: 4
Joined: Sat Jan 05, 2013 10:23 pm

Problems bit-banging an I2C Slave

Fri Mar 27, 2015 1:57 pm

I have a Pi B+ on which I'm trying to bit-bash an I2C Slave in C / C++ (code's pretty much C despite the fact I'm using g++ and .cpp files), utilizing GPIO 18 as my Clock and GPIO 17 as my Data. I'm utilizing the BCM2835 C library from Mike M. My code is a little less featured that a true Slave, if only because I don't expect to be sending information back to the Master.

To test my code, I've used the Pi's built-in I2C Master bus, as well as a RasPi 2's Master Bus. Both utilized the BCM2835 library's native functions to communicate with the PI B+ Slave code. And from both I find that the Pi recognizes that the Master on the bus is trying to communicate with Slave 0x00.

I've checked the clock and data lines with an oscilloscope, and I see for certain that it's sending 11001100 - address 0x66, write. I've coded the Slave to respond to 0x66, but it doesn't seem to see the data at all.

I know for certain that the Pi is seeing the clock - debugging output displays that a message was sent to 0x00 after 8 bits on the clock - but for some reason it doesn't see the data. A separate pin test I've run, wiring the 3v3 pin direct to my Data pin, indicates it's reading as intended.

My question for you all is this: what would I need to do to get my code to recognize information flowing in through my data pin? Here's my code, if that's something you'd like to look over. This isn't the full code - I start a thread in main to initialize this function, and then I listen for a keypress on q to turn alive to false.

(For what it's worth, yes, I am the same person that posted this question to the Pi Stack Exchange.)

****

Edit 1:
Because I've got a B+, I'm currently investigating using the BSC module the BCM2835 has in it. Supposedly it can be used as an I2C Slave. While it's not implemented for access in Mike M's code, I can still use his code to get at the base registers.

rst
Posts: 529
Joined: Sat Apr 20, 2013 6:42 pm
Location: Germany

Re: Problems bit-banging an I2C Slave

Tue Mar 31, 2015 7:41 pm

The Stick wrote:Because I've got a B+, I'm currently investigating using the BSC module the BCM2835 has in it. Supposedly it can be used as an I2C Slave. While it's not implemented for access in Mike M's code, I can still use his code to get at the base registers.
Perhaps this may help to get the hardware I2C slave running. It's a bare metal polling driver for it.

The Stick
Posts: 4
Joined: Sat Jan 05, 2013 10:23 pm

Re: Problems bit-banging an I2C Slave

Tue Mar 31, 2015 9:10 pm

Hi rst,

I've been looking through your code a whole lot trying to get mine to work. I'd say that it's pretty much done, except for the fact that the data and/or clock pins seem to not be paying the bus any attention.

I've set BCM 18 and BCM 19 (physical pins 12 and 35) to 7 (via setting 0x20200000 + 0x4 to itself or'd with (0x3F << 23)) - alternate function 3. I've also set the Control Register (0x20214000 + 0xC) to 0x0305 - TX & RX enabled, I2C mode, device enabled - and set the Slave Address (0x20214000 + 0x8) to 0x66. I made sure that each access to the registers used some DMB of some description.

On the Pi playing as the master, I got the I2C tools, and tried an i2cdetect on it. I got nothing back. Got NACKs when I tried using Mike's code to send it information as well.

I'm incredibly certain that it's not the master-side code that has issues - it's been examined on an oscilloscope. Something on the slave side must be wonky, be it an electrical issue or bad code...

rst
Posts: 529
Joined: Sat Apr 20, 2013 6:42 pm
Location: Germany

Re: Problems bit-banging an I2C Slave

Wed Apr 01, 2015 10:50 am

The Stick wrote:I've set BCM 18 and BCM 19 (physical pins 12 and 35) to 7 (via setting 0x20200000 + 0x4 to itself or'd with (0x3F << 23)) - alternate function 3.
I think it's 0x3F << 24. There are 3 bits for each pin in the GPSEL register and 10 pins per register. So (18-10) * 3 is 24. The other initializations you have done should be right and sufficient from my point of view.

The Stick
Posts: 4
Joined: Sat Jan 05, 2013 10:23 pm

Re: Problems bit-banging an I2C Slave

Wed Apr 01, 2015 2:00 pm

Yup, that was indeed it. Now to just figure out why the slave is getting nothing but 0x01 from the master...

Master creates a char* that equals...

Code: Select all

" Here, have some text.  This is but a sample of my power."
...and then sets the first char to 57, indicating the length of the message. This shows up as a series of 0x01 on the slave - in fact 8 different messages, 1 byte long each, with that byte being 0x01. Gotta figure out what's up with that.

EDIT - Found the issue. I was using && instead of & to mask to just the first 8 bits. Wooo

Return to “Interfacing (DSI, CSI, I2C, etc.)”