MAIX BIT I2C Slave_Mode not working, can anyone help?

Hello,
I am quite new to Sipeed and have purchased a Maix Bit v2.

I need to configure the board as a slave device but the example on the Sipeed website doesn’t seem to work. The board works okay in the master mode but just hangs the I2C lines when configured as a slave using the following routine.

from machine import I2C

count = 0

def on_receive (data):
    print ("on_receive:", data)

def on_transmit ():
    count = count + 1
    print ("on_transmit, send:", count)
    return count

def on_event (event):
    print ("on_event:", event)

i2c = I2C (I2C.I2C0, mode = I2C.MODE_SLAVE, scl = 28, sda = 29, addr = 0x09, addr_size = 7, on_receive = on_receive, on_transmit = on_transmit, on_event = on_event)

If I hold down the Maix BIT reset button the rest of the i2c bus works okay.  it would appear that the SCL is clocking okay on pin 28 but the SDA on pin 29 is being held high.

The master device is an arduino nano and I am using a voltage shifter to manage the different logic levels.  The hardware works okay when the Maix Bit is the Master and the nano is the Slave.
Can anyone help me on this please?.

Thanks

ikna

Hello,

I am quite new to Sipeed and have purchased a Maix Bit v2.

I need to configure the board as a slave device but the example on the Sipeed website doesn’t seem to work. The board works okay in the master mode but just hangs the I2C lines when configured as a slave using the routine below.

If I hold down the Maix BIT reset button the rest of the i2c bus works okay. it would appear that the SCL is clocking okay on pin 28 but the SDA on pin 29 is being held high.

The master device is an arduino nano and I am using a voltage shifter to manage the different logic levels. The hardware works okay when the Maix Bit is the Master and the nano is the Slave.
Can anyone help me on this please?.

Thanks

ikna

from machine import I2C

count = 0

def on_receive (data):
    print ("on_receive:", data)

def on_transmit ():
    count = count + 1
    print ("on_transmit, send:", count)
    return count

def on_event (event):
    print ("on_event:", event)

i2c = I2C (I2C.I2C0, mode = I2C.MODE_SLAVE, scl = 28, sda = 29, addr = 0x09, addr_size = 7, on_receive = on_receive, on_transmit = on_transmit, on_event = on_event)

Hello,

I should have mentioned that I have included an endless while loop at the end of the routine that does nothing.

I got it working.

Hi. I had the same problem. How did you solve it? Thanks.

Hello,

I am not particularly familiar with Python so the coding solution may not be ideal and the lack of information on how the ‘object functions’ work makes it difficult to de-bug, however I have managed to solve the problem.

I suspect that the on_receive, on_transmit and on_event functions are called by an interupt generated by the i2c signal, and is not being called by the main routine, as such there is nowhere to pass the received i2c data back too.

My solution was to create an array, ‘message’ and the variable ‘c’ as global variables. According to information on the internet, Python doesn’t like global variables so they need to be specifically identified as global array variables using ‘Global’.

I use the ‘c’ variable as a general counter with the transmitted data being stored in the global ‘message’ array. I don’t actually receive any messages in my routine, but you could apply the same principle to the ‘data’ variable.

When the Master sends a request to the Slave, the on_event function resets the counter ‘c’ to -1. The contents of the ‘message’ array is built in the main routine, and is transmitted to the Master device after a valid ‘on_event’ has been detected.

Global Variables

Global c
c = -1
Global message
message = [0,1,2,3,4]

def on_receive (data):
print (“on_receive:”, data)

def on_transmit ():
c = c + 1
print (“on_transmit:”, c, " : ", message[c])
return message[c]

def on_event (event):
print (“on_event:”, event)
c = -1

i2c = I2C(I2C.I2C0, mode = I2C.MODE_SLAVE, scl = 28, sda = 29, addr = 0x24, addr_size = 7, on_receive = on_receive, on_transmit = on_transmit, on_event = on_event)

One other issue I found was on the Arduino. When I was using a Mega as the Master, the salve address ‘24’ was being compiled as a hexidecimal number, but when the same code was used on a Nano the Slave address ‘24’ was being compiled as a decimal number. Make sure that you declare the Slave address in a hexidecimal format on the Arduino.

I hope this helps, and you manage to solve your issue.

Regards

ikna

sorry, I forgot to include the following at the start of the routine:

from machine import I2C

Regards

ikna