Software I2C Slave

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Software I2C Slave

Post by OutoftheBOTS_ » Wed Jun 27, 2018 4:06 am

I have created a software I2C Slave written in C for a STM32F10x chip but it would require very little modification to be able to run a STM32F4x chip used by a number of the MicroPython boards.

The code uses a hardware interrupt on the SDA line to detect a start condition sent from the master then disables the interrupt and deals with the transmission from the master. After the completion of the transmission it enables the interrupt and returns to your main code.

At the moment it is very basic and doesn't have a timeout watch dog as I haven't yet added it.

This is my very first ever C program so am keen for feedback so that I can learn.

I am not sure how complex it would be to modify it and add it to the MicroPython firmware that run on STMF4x chips but if anyone would like to have a poke at it the code is here https://github.com/OutOfTheBots/I2C_Slave-STM32f10x

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Software I2C Slave

Post by pythoncoder » Wed Jun 27, 2018 4:15 pm

Boards running STM32F4x chips such as the Pyboards can run pyb which supports I2C slave mode. You could look at the source to see how it is integrated into MicroPython.
Peter Hinch
Index to my micropython libraries.

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: Software I2C Slave

Post by OutoftheBOTS_ » Wed Jun 27, 2018 10:19 pm

Boards running STM32F4x chips such as the Pyboards can run pyb which supports I2C slave mode.
Yes it has a Slave mode but I was unable to use it.

Has anyone here managed to make it work??

When I posted here asking about it everyone said the same thing that they couldn't get it to work. I asked the guys at OpenMV cam and they said they were unable to get I2C Slave to work in micropython because of the same reason of master getting a NACK from Micropython Slave.

It didn't seem to have a callback system for when the master sends a start condition this means the master receives a NACK when it tries to start a transmission.

There doesn't seem to be a system where you can make a data_register to place data in for the master to read or write too.

This is why I wrote my I2C Slave driver as it has a n interrupt on the SDA line that detects the master sending a start condition. It also has a bytearray 256 bytes big that both the master and slave can read/write too.

Last night I added that the master will be able to read/write up to 256 bytes in 1 transmission.

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: Software I2C Slave

Post by OutoftheBOTS_ » Wed Jun 27, 2018 10:26 pm

You could look at the source to see how it is integrated into MicroPython.
This is my first C program and am still finding my way around C. It is my aim to learn how to be able to modify/contribute to MicroPython firmware but atm it is still probably a little over my head.

Before it can be added I also need to learn how to add a timeout to my code because at the moment if the tranmission broke half way though the slave will just sit there waiting for the next pulse of the SCL line instead to exiting the interrup and returning to the progrma after a time out period.

Any idea on the best way to implement a timeout watchdog??

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: Software I2C Slave

Post by OutoftheBOTS_ » Thu Jun 28, 2018 4:35 am

I have added a timeout now so that if it gets stuck in a loop for any lenght of time it will exit the interrupt and return to main program.
updated code here https://github.com/OutOfTheBots/I2C_Slave-STM32f10x

Post Reply