0

i2c c
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

i2c c

by jfabernathy on Wed Dec 30, 2020 6:03 pm

I have a simple test program in Circuitpython that is an I2C master running on a Xiao talking to another Xiao as a slave written in Arduino code.

I have the test programs where both MCUs are using Arduino but I wanted to see if I could convert the test programs to Circuitpython. The Master was easy, but I have not figured out where to start on the slave program. The master code is:
Code: Select all | TOGGLE FULL SIZE
import time
import board
import busio

i2c = busio.I2C(board.SCL, board.SDA)
cmd = ['A', 'B', 'C', 'D']
i = 0

while True:
    time.sleep(1)
    while not i2c.try_lock():
        pass
        i2c.writeto(0x09, cmd[i])
        print("byte written = ", cmd[i])
        i = i + 1
        if (i > 3): i=0
        result = bytearray(5)
        i2c.readfrom_into(0x09, result)
        print("results sent back = ", result)
        i2c.unlock()

The slave program basically hangs out on the I2C bus and when it gets a one byte "cmd" it replies with "hello". Both programs print to the console what they are doing so I can observe the behavior. I'm looking for the similar routine where the slave establishes it's address and just waits for a request. Not seeing that in Circuitpython.

jfabernathy
 
Posts: 45
Joined: Thu Dec 17, 2020 10:58 pm

Re: i2c c

by danhalbert on Wed Dec 30, 2020 6:14 pm

Look for I2CPeripheral. But I'm not sure if that build includes it.
https://circuitpython.readthedocs.io/en ... index.html

danhalbert
 
Posts: 2262
Joined: Tue Aug 08, 2017 12:37 pm

Re: i2c c

by jfabernathy on Wed Dec 30, 2020 6:30 pm

Thanks, this looks like what I was searching for. All I found was I2CSlave and that was from CP 5.3. I'm using 6.0.

Let me see if I understand your comment about

" But I'm not sure if that build includes it."

Are you referring to the CP build for version 6? or related to the build of CP for my Xiao MCU's??

jfabernathy
 
Posts: 45
Joined: Thu Dec 17, 2020 10:58 pm

Re: i2c c

by danhalbert on Wed Dec 30, 2020 6:52 pm

The build for the Xiao. From https://circuitpython.readthedocs.io/en ... atrix.html, I see that it doesn't, because this is a small build, without external SPI flash. You would have to do a custom build.

danhalbert
 
Posts: 2262
Joined: Tue Aug 08, 2017 12:37 pm

Re: i2c c

by jfabernathy on Wed Dec 30, 2020 7:28 pm

Thank you for that explanation. I'm a newbie at this Arduino/circuitpython stuff and I'm picking up thinks new everyday. I originally picked up a box of 3 Xiao on Amazon for $15 just to hookup to my Raspberry Pi's as a way to learn interaction between different devices.

This is a great hobby and fills my day since I'm quarantined like most everyone else. Retired and stuck at home is very boring, but I've found a lot of fun in getting back into electronics and programming.

Again, thanks for you help. I have an Adafruit Metro ESP32-S2 coming next week and I think it has more memory, but not sure. I was going for the Wifi/bluetooth on that device.

jfabernathy
 
Posts: 45
Joined: Thu Dec 17, 2020 10:58 pm

Re: i2c c

by baxtertidwell on Thu Jan 07, 2021 4:45 pm

JF, I'm about where you are, re: retired and locked down. Playing with these little things is just the ticket!

I see that i2cperipheral page in the documentation, and I got it working with one Feather Express asking it for an address.

But that's as far as I got. I can't figure out how to actually send and receive data. What does the master look like?

Ideally, I want to have a single master and a dozen slaves (can we still say master and slave?), each with its own address all listening for messages meant for them.

I'd also like to have the slaves send messages to the master if that's possible.

baxtertidwell
 
Posts: 41
Joined: Mon Jan 07, 2013 6:16 pm

Re: i2c c

by jfabernathy on Thu Jan 07, 2021 4:59 pm

I never got the working on the Xiao with CP. Had to switch to Arduino

jfabernathy
 
Posts: 45
Joined: Thu Dec 17, 2020 10:58 pm

Re: i2c c

by danhalbert on Thu Jan 07, 2021 5:03 pm

We are using the terminology "main" and "peripheral" now.

danhalbert
 
Posts: 2262
Joined: Tue Aug 08, 2017 12:37 pm

Re: i2c c

by baxtertidwell on Thu Jan 07, 2021 5:15 pm

Thanks, JF.

Dan, do you have any examples of two-way communication over i2c?

baxtertidwell
 
Posts: 41
Joined: Mon Jan 07, 2013 6:16 pm

Re: i2c c

by danhalbert on Thu Jan 07, 2021 5:19 pm

It was a contributed native module. Here is the original pull request, with an example: https://github.com/adafruit/circuitpython/pull/1064. The contributor also points to at least one other example in their github repos.

danhalbert
 
Posts: 2262
Joined: Tue Aug 08, 2017 12:37 pm

Please be positive and constructive with your questions and comments.