Control a pyboard by another pyboard over UART

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.
zaord
Posts: 96
Joined: Fri Jan 31, 2020 3:56 pm

Control a pyboard by another pyboard over UART

Post by zaord » Sat Jan 22, 2022 9:48 am

Hi here,
For a project, I can't manage PWM control and measurement on a same pyboard.
I would like to split the work into two micropython system connected like the pyboard.py does.

I plan to use I2C protocol. At the moment I wants to control the pwm pyboard by using commands like PWM_1_FC_CONTROL(1,20) from master pyboard.
How could I manage to do this ?
Best Thanks
Last edited by zaord on Mon Jan 24, 2022 11:57 am, edited 1 time in total.

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

Re: Control a pyboard by another pyboard over I2C

Post by pythoncoder » Sat Jan 22, 2022 10:47 am

See this doc which provides a solution.
Peter Hinch
Index to my micropython libraries.

zaord
Posts: 96
Joined: Fri Jan 31, 2020 3:56 pm

Re: Control a pyboard by another pyboard over I2C

Post by zaord » Sat Jan 22, 2022 11:09 am

Hi Peter, Do I need to use uasyncio absolutely ?

smhodge42
Posts: 5
Joined: Sat Nov 13, 2021 6:17 pm

Re: Control a pyboard by another pyboard over I2C

Post by smhodge42 » Sat Jan 22, 2022 3:34 pm

I have done functionally the same thing (create a bi-directional I2C bus) between a pyboard and an AVR Xmega using a separate digital line as a "service request" line. The Xmega pulls SRQ low when it needs to send a packet to the pyboard and the pyboard then issues 2 I2C reads, one to get the length of the packet and then one to get the full packet. Packets from pyboard to Xmega use normal pyboard master mode. The same could be done with two pyboards.

An Xmega can be both master and slave, but in either case I am using it as a "normal" I2C slave device. I do use it in both modes to communicate bi-directionallly between two Xmegas (w/o any SRQ line).

If a pyboard v2 ever comes out I'd have I2C slave second on my list, after more RAM.

zaord
Posts: 96
Joined: Fri Jan 31, 2020 3:56 pm

Re: Control a pyboard by another pyboard over I2C

Post by zaord » Mon Jan 24, 2022 7:26 am

Hi, Can you share you code for the micropython side ?

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

Re: Control a pyboard by another pyboard over I2C

Post by pythoncoder » Mon Jan 24, 2022 10:43 am

zaord wrote:
Sat Jan 22, 2022 11:09 am
Hi Peter, Do I need to use uasyncio absolutely ?
My implementation requires it.

As a general point I would always use a UART in preference to I2C. The reason I developed the I2C link was to emulate UART behaviour for platforms like ESP8266 which has only one functional UART. For linking Pyboards which have plenty of UARTs, using one employs only two GPIO lines. Further, no driver is required. The interface has lower latency, and makes the use of uasyncio optional.

Why do you want to use I2C?
Peter Hinch
Index to my micropython libraries.

zaord
Posts: 96
Joined: Fri Jan 31, 2020 3:56 pm

Re: Control a pyboard by another pyboard over UART

Post by zaord » Mon Jan 24, 2022 11:50 am

Ha, Peter, just becaise I dit not know the possibility of Uart :)
Do you have a code that I could test for connecting 2 pyboard with uart ??

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

Re: Control a pyboard by another pyboard over UART

Post by pythoncoder » Mon Jan 24, 2022 5:01 pm

This demo shows a Pyboard talking to itself via a UART. Run it as described with X1 and X2 connected to get the idea.

It would work equally well with two Pyboards: connect X1 on one board to X2 on the other, and vice-versa. Run a copy of the script on each board.
Peter Hinch
Index to my micropython libraries.

zaord
Posts: 96
Joined: Fri Jan 31, 2020 3:56 pm

Re: Control a pyboard by another pyboard over UART

Post by zaord » Tue Jan 25, 2022 11:08 am

Works fine,
Do you have any idaea how can I execute a program on the pyb2 from the pyb1 and check when the pyb2 is ready (it have finish to execute the program)
I don't know if uart is similar to rshell with Cpython and the follow / no_follow commands ...

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

Re: Control a pyboard by another pyboard over UART

Post by pythoncoder » Tue Jan 25, 2022 11:29 am

See the docs.

Another approach is to have two continuously running programs. The requester sends a message to the responder asking for data. The responder replies with a message when ready. That is how I would do it.
Peter Hinch
Index to my micropython libraries.

Post Reply