ilaam
Posts: 30
Joined: Fri Mar 28, 2014 9:20 am

I2C slave software driver

Wed Apr 16, 2014 8:29 am

Hello guys,
As RPi doesn't work as slave due to the linux kernel, does any one know an I2C slave driver (linux independent) or is that also impossible?

hampi
Posts: 223
Joined: Fri May 31, 2013 11:29 am
Contact: Website

Re: I2C slave software driver

Wed Apr 16, 2014 5:26 pm

Bit banging could be used for the i2c slave. May I ask where do you need Raspberry Pi as a i2c slave? This question pops up here from time to time.

jdb
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2393
Joined: Thu Jul 11, 2013 2:37 pm

Re: I2C slave software driver

Wed Apr 16, 2014 7:59 pm

There is actually an I2C slave peripheral interface on BCM2835.

The pins for this are only accessible on the compute module, though.

Bit-bash for i2c slave is going to be error prone - for bit-bash you must take an interrupt on an SCL edge and drive the correct data on SDA - any interrupt latency will cause corrupted packets.

You could use the ARM FIQ to get reasonable realtime performance: ~200ns resolution is possible.
Rockets are loud.
https://astro-pi.org

ilaam
Posts: 30
Joined: Fri Mar 28, 2014 9:20 am

Re: I2C slave software driver

Thu Apr 17, 2014 7:32 am

That seems hard while I'm nothing but a beginner :D
hampi : you're right I saw some similar questions but there are no answers neither tutorials for making an RPi function as a slave. It is true that a ship with a powerful processor isn't made to be a slave but in some cases (the mine for example) that is needed. In fact, I "wish" to use RPi to simulate many sensors and really many. So making all simulated sensors work together isn't easy for a microcontroller with modest capabilities.
I never developped a driver but I'll give it a try :)

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: I2C slave software driver

Thu Apr 17, 2014 7:54 am

ilaam wrote:That seems hard while I'm nothing but a beginner :D
hampi : you're right I saw some similar questions but there are no answers neither tutorials for making an RPi function as a slave. It is true that a ship with a powerful processor isn't made to be a slave but in some cases (the mine for example) that is needed. In fact, I "wish" to use RPi to simulate many sensors and really many. So making all simulated sensors work together isn't easy for a microcontroller with modest capabilities.
I never developped a driver but I'll give it a try :)
As jdb points out the problem will be in reacting fast enough to the bus signals.

Let's assume you are using a 100 kbps bus where a bit may be transferred every 10 microseconds. In effect you have 5μs to react to a level change and place data on the line. I'm not convinced you can do that with Linux, even with a kernel module. If there is a Linux solution (without hardware support) I'd guess it would involve clock stretching. Do your proposed bus masters handle clock stretching properly? The Pi does not.

You might have to use bare-metal.

ilaam
Posts: 30
Joined: Fri Mar 28, 2014 9:20 am

Re: I2C slave software driver

Thu Apr 17, 2014 8:53 am

Yes my masters handle clock stretching properly and I'll be working on 100Kbs.. Thanks for the tip I'll see what I can do with bare metal programming :?

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: I2C slave software driver

Thu Apr 17, 2014 9:07 am

ilaam wrote:Yes my masters handle clock stretching properly and I'll be working on 100Kbs.. Thanks for the tip I'll see what I can do with bare metal programming :?
Just because some of us think it might be impossible from Linux doesn't make it true. I'd certainly try the kernel module approach first.

A Linux kernel module would be easier to develop and use. If the worst came to the worst you could just disable interrupts during portions of your processing within the kernel.

hampi
Posts: 223
Joined: Fri May 31, 2013 11:29 am
Contact: Website

Re: I2C slave software driver

Thu Apr 17, 2014 9:24 am

ilaam wrote:In fact, I "wish" to use RPi to simulate many sensors and really many. So making all simulated sensors work together isn't easy for a microcontroller with modest capabilities.
I think the present i2c implementation in linux support only 7-bit addressing and if you need 10-bits you will probably need to write something new.

I did bit-banging of i2c slave with very basic PIC microcontroller if that is any use here

https://github.com/oh7bf/PiPIC/blob/mas ... 12si2c.asm

It might work with 100 kHz clock frequency if you add a 20 MHz crystal to the PIC12F675, but then these two pins can not be used for I/O anymore.

ilaam
Posts: 30
Joined: Fri Mar 28, 2014 9:20 am

Re: I2C slave software driver

Thu Apr 17, 2014 1:01 pm

joan : ok I need to do more research to find how to do it with the linux kernel.
Hampi : I really appreciate your help however my driver needs to be written in C.

Return to “Beginners”