News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Multi Master I2C

Started by crankshaft, Jan 22, 2022, 10:23 AM

Previous topic - Next topic

crankshaft

I need to emulate / replace an obsolete device that uses I2C Multi Master.

Does anybody have any experience and / or sample code snippit that I could uses as a starting point for this ?


TimB


Can you give more details? EG how many masters? Set up etc

What I use is just software i2c. I can have as many masters as I have pins


Tim

RGV250

#2
Hi Tim,
I am assuming he means multi masters on the same BUS, more than that I cannot help.

I think you would need to monitor the state of the BUS so the other master(s) could take control when it is idle.

Bob

crankshaft

Thanks for helping :-)

Yes, it is a single I2C bus and on this bus, there is another Master which of course is controlling the clock line.

This is a charge station, I need to replace the station and have it communicate with the device.

I have already sniffed the data using buspirate so I know pretty much what data is being sent and received, but O dont know how to go about implementing a Master where there's another Master already on the bus.

RGV250

Hi,
You could try looking at this for ideas, post 6 seems to imply the "Wire" library supports it so might be worth studying.
https://forum.arduino.cc/t/arduino-i2c-multi-master-design/13644/6

Bob


John Drew

I know the protocol supports multiple masters and multiple slaves but not sure if that's only when using the MSSP. Software I2C may or may not work. A question for the maestro.
John

crankshaft

The original microcontroller that was used was a NEC D780022A:

DESCRIPTION
The µPD780021A, 780022A, 780023A, and 780024A are members of the µPD780024A Subseries of the 78K/0 Series. Only selected functions of the existing µPD78054 Subseries are provided, and the serial interface is enhanced. The µPD780021AY, 780022AY, 780023AY, and 780024AY are the µPD780024A Subseries with a multimaster supporting I2C bus interface, which makes them suitable for AV equipment.

This post (https://www.microchip.com/forums/FindPost/1090950) seems to suggest that it can be achieved by using the SSPSTAT register but I don't understand how this could be implemented.


trastikata

I have no experience with multimasters, but when I googled it, came to this topic:

https://www.microchip.com/forums/m913240.aspx

The entire topic is interesting, but note post #9 and #17 as well.

crankshaft

Thanks, by reading the forums and data sheets I have it working, probably not the correct way to do it but it works.

Will post more info later


crankshaft

Hmmmm, is it possible for a PIC to be both a master and a slave ?

Actually in slave mode, it is write only, another master is writing the timestamp to this address.

I am using the 12F1840 as I dont need a lot of functionality and I am using HBUS (hardware I2C) as a master in multi-master mode.


crankshaft

#10
Also, I have been looking at the I2C slave example which is for receiving / sending a single byte.

But in my case, I am receiving multiple bytes and need to send multiple bytes.

How do you go about reading all the bytes from SSPBUF ?? - when you read from it does it automagically empty and load the next byte ?

if I loop can I get all of the bytes i.e.

dim indata[10] as byte
dim index as byte = 0

while SSPBUF > 0
  indata[index] = SSPBUF
  index = index + 1
wend

and... are all of the I2C commands such as I2CRead, I2C Write and HBUSIN and HBUSOUT only used for I2C Master mode ?

I've not been able to find anything in the docs or samples that uses these commands in slave mode.

Stephen Moss

Quote from: crankshaft on Jan 26, 2022, 08:52 AMHmmmm, is it possible for a PIC to be both a master and a slave ?
I have not tried it but I don't see why not, setting the Mode Select bits to the appropriate value should switch between modes.
Although the functions of other registers and their values may change as a result, i.e. maySSP1ADD holds the slave address or the Baud rate clock divider bits. In that situation, ideally if you were switching between modes you would use the same value for both however, if a device on the Bus is already using that Address value you would have to change the SSP1ADD contents to the appropriate value every time you switch between Master and Slave.

However, if you are referring to monitoring the Bus for the purpose of arbitration between masters, then from a quick scan the 12F1840 datasheet it appears that is done in hardware when set as a Master, setting the BCL1IF bit and puts the MSSP module of into an idle state (SEN bit to 0?) of the Master(s) that do not have control of the bus.
Although as you only have two masters you could optionally use a couple of spare pins and set up your own system where the masters set a pin high when they want to use the bus that is monitored by the other master, but it is probably better to use the in built system of the MSSP.

Quote from: crankshaft on Jan 26, 2022, 10:51 AMHow do you go about reading all the bytes from SSPBUF ?? - when you read from it does it automagically empty and load the next byte ?

I don't recall doing any I2C slave stuff but although reading the SSPBUF should clear its contents I don't think it would automatically get the next byte, I would think the slave would need to send an acknowledge signal to the Master to let it know the previous byte was received and it can send the next, otherwise the master would be either waiting indefinitely to send the next byte or send all the data asynchronously in which case you may not get it all if data is still being sent before you have read the existing contents of the SSPBUF.

Quote from: crankshaft on Jan 26, 2022, 10:51 AMare all of the I2C commands such as I2CRead, I2C Write and HBUSIN and HBUSOUT only used for I2C Master mode ?
Les would be the definitive authority on this but I would think they would be Master only as...
A) It makes sense that they were created for easy communication with I2C slaves rather than to create slaves and
b) The I2C Slave example does not appear to use those commands, instead it used the MSSP register bits. If those commands worked for slaves then I would expect them to also be used in the slave example.

Also remember that although they still work for older devices the HBUS commands have been downgraded to legacy status so it would probably be a good idea to move away from using them for new code even on devices that do support them and instead understand how to use the registers associated with the MSSP module, it looks like you will have to if using the PIC as a slave anyway so why not do it for both slave and master.

top204

I have never done any multi-master I2C coms, in fact I try to keep away from I2C whenever possible because it is slow and cumbersome. :-)

I've always been a fan of SPI, because of its speed and versatility. SPI only requires 1 more line than I2C, because both DIN and DOUT can share the same pin.

crankshaft

Thanks so much for the help and the time you've spent in replying.

In this case, I2C is not a choice, I am trying to replace an obsolete charger which uses I2C to communicate with the device being charged, and I therefore am reverse engineering the existing charger.

I am 80% there, The master is sorted and working it basically pings the device.

But I need the slave as the device is transmitting it's date / time to the charger which I want to read (as does the original) and are trying to avoid using 2 PICs, one as a master and another as a slave.

top204

#14
There are quite a few devices now with 2 MSSP or I2C peripherals, so maybe one used for slave and one used for master on the same device? It may work, but I do not know how one would stop interference from each other if they both decide to communicate at the same time, or while one is already communicating.

I know multi-master I2C was something that was thought of later in its evolution, and so has problems, because I2C was developed for the TV industry, so the TV board's controller could communicate with the peripherals that were starting to come into play on TV chassis, so there was only ever one master and multiple slaves.

Multi-Master looks for coms happening before it initialises coms itself, but if a slave unit is not using its Acks and NAcks correctly, they "will" interfere with each other and cause chaos, because one of them is not looking for busy lines. :-)

tumbleweed

Have you seen the mchip app note on using the MSSP as an I2C slave?  http://ww1.microchip.com/downloads/en/Appnotes/90003251A.pdf

The example code is in C, but it's pretty straight forward.
You could skip using interrupts, convert the ISR() routine to a regular subroutine and call it/poll when SSP1IF = 1

It shows how to read the data into a buffer instead of just a single byte.

crankshaft

Thank you all, after a lot of trial and error I managed to both achieve multimaster mode and a single device as a master and slave.

Master/Slave Switch

setmaster:
    If SLAVE == 0 Then Return
    SLAVE = 0
    SSPADD  = $4F  ;
    SSPCON2 = $0   ;0
    SSPCON  = $28  ;40:%00101000
    Return
     
setslave:
    If SLAVE == 1 Then Return
    SLAVE = 1
    SSPADD  = $A0
    SSPCON2 = $0
    SSPCON  = $36            ' Set to I2C slave with 7-bit address

Multimaster Wait for Bus
waitbus:
    GoSub setmaster
    Clear DOSTOP
    While SBIT == 1
        DOSTOP = 1
        Toggle LED
    Wend
    If DOSTOP == 1 Then HBStop
    Return

This may not be the best way of doing this, but it works :-)