Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

I2C slave and master in same chip..?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive
View previous topic :: View next topic  
Author Message
nisse

Bascom Member



Joined: 14 Dec 2006
Posts: 50

sweden.gif
PostPosted: Thu Jan 30, 2014 8:08 pm    Post subject: I2C slave and master in same chip..? Reply with quote

Is this possible?
I'm testing this and can't get it to work so I'm wondering...
After loading the i2c_TWI-slave.LBX, the i2c commands don't work any more.

(BASCOM-AVR version : 2.0.7.6 )
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 6201
Location: Holland

blank.gif
PostPosted: Thu Jan 30, 2014 10:38 pm    Post subject: Reply with quote

did you also use master twi with i2c_TWI.LBX ?
I never tested it. it should be possible. I did not had that in mind when making these libs.
But if it is a good idea? when is master mode, you can not receive any data with the slave since you occupy the bus.
And who is sending this data to the slave ? you want to use multiple masters on the bus?
The xmega has multiple busses, there it works since i did test that.

_________________
Mark
Back to top
View user's profile Visit poster's website
nisse

Bascom Member



Joined: 14 Dec 2006
Posts: 50

sweden.gif
PostPosted: Fri Jan 31, 2014 7:15 am    Post subject: Reply with quote

I tried both with and without the i2c_twi.lbx
Without it, the slave lib works, but if I load it, the slave stops working.


See attached drawing. MCU 2 is continuously collecting data from devices on it's i2c bus though the i2c switch.
When the Main MCU wants information from MCU 2, the Main MCU signals this though the INT line and MCU 2 switches i2c over to the Main MCU and MCU 2 is now is now in slave mode.
Main MCU is a 90CAN128, MCU 2 is a Mega640
[edit] I've also tried with version 2077, same result...
[/img]
Back to top
View user's profile
nisse

Bascom Member



Joined: 14 Dec 2006
Posts: 50

sweden.gif
PostPosted: Sun Feb 02, 2014 9:23 am    Post subject: Reply with quote

Hear's a flow chart of what I'm trying to achieve:
https://docs.google.com/drawings/d/1g25R5pE16PzQ5hEtEEzt6WsKWrP2If-9o0Dfz93cs_g/edit?pli=1
I've spent the better part of 3 days testing and trying to figure out if it's something in the i2c libs that are conflicting.
But I haven't been able to figure it out Sad
In Atmels AVR311 and AVR315 application notes, that covers their Master and Slave drivers in C, it says:
"The master and slave drivers could be merged to one combined master and slave driver."
But I haven't found any examples of how that is done Sad

Any ideas on how to get this to work would be much appreciated...
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 6201
Location: Holland

blank.gif
PostPosted: Sun Feb 02, 2014 7:52 pm    Post subject: Reply with quote

if you switch from slave to master twi, you need to enable the interrupts : twcr.0=1
and if you switch from slave to master you can best disable interrupts with twcr.0=0

that should make it work.

_________________
Mark
Back to top
View user's profile Visit poster's website
nisse

Bascom Member



Joined: 14 Dec 2006
Posts: 50

sweden.gif
PostPosted: Mon Feb 03, 2014 7:01 pm    Post subject: Reply with quote

I've tried this in all possible combinations, with or without twen, twie and twint
twie 0/1 for master and so on.
sub SetToSlave()
twcr.twen = 0
twcr.twie = 0
twcr.twint = 1
Gosub _twi_init
isMaster = 0
twcr.twen = 1
end sub

sub SetToMaster()
twcr.twen = 0
twcr.twie = 1
twcr.twint = 1
Gosub _i2c_init
isMaster = 1
twcr.twen = 1
end sub

My program starts with this (after all the basic stuff...):

$lib "i2c_twi.lbx"
twbr = 72
twsr = 0
'Config Twi = 100000
Config Sda = Portd.1
Config Scl = Portd.0
i2cinit
const i2cAddress = &HA8
call SetToMaster()

After that I send a few bytes on the I2C line and it works fine, see "beforeS.png"

Then I load the slave driver, config it and set it back to Master:
$lib "i2c_twi-slave.LBX"
Config Twislave = &HA8 , BTR = 2 , BITRATE = 100000 , SAVE = save , GENCALL = no ', USERACK = on
call SetToMaster()

Now Master mode doesn't work and the I2C line looks like in AfterS.png

[img]
[/img]


Any ideas?
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 6201
Location: Holland

blank.gif
PostPosted: Mon Feb 03, 2014 10:43 pm    Post subject: Reply with quote

ahem, my comment was confusing.

sub SetToMaster()
twcr.0 = 0 'disable
end sub

sub SetToSlave()
twcr.0=1 'enable
end sub

That way it should work. But you must switch when the bus is free.
Do you use Proteus? if you make me a proteus i will check it.

The slave driver work on interrupt so it need an enabled interrupt.
The master does not work in int mode so that is why you need to disable it.

And other wise you can test this :
after you loaded the master, save the register content of twcr , then do the same after the slave mode, and then write the backed up master value.

_________________
Mark
Back to top
View user's profile Visit poster's website
nisse

Bascom Member



Joined: 14 Dec 2006
Posts: 50

sweden.gif
PostPosted: Tue Feb 04, 2014 11:32 am    Post subject: Reply with quote

Thanks Mark
Reloading TWCR is a brilliant idea Smile
How do you suggest I make sure the bus is free?
Will it work by checking twcr.twint = 1 or do I also need to check the pin states of SCL SDA?
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 6201
Location: Holland

blank.gif
PostPosted: Tue Feb 04, 2014 11:39 am    Post subject: Reply with quote

i think that you can simply use i2cstop for both master/slave
_________________
Mark
Back to top
View user's profile Visit poster's website
nisse

Bascom Member



Joined: 14 Dec 2006
Posts: 50

sweden.gif
PostPosted: Wed Feb 05, 2014 9:29 am    Post subject: Reply with quote

Thanks Mark, got that part working now Smile
Back to top
View user's profile
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum