# RP2040: I2C on PIO statemachines?!

**URL:** https://community.simplefoc.com/t/rp2040-i2c-on-pio-statemachines/4046
**Category:** hardware support
**Tags:** as5600, rp2040
**Created:** [November 2, 2023, 8:10am UTC](https://community.simplefoc.com/t/rp2040-i2c-on-pio-statemachines/4046 "2023-11-02T08:10:17Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![o\_lampe](https://community.simplefoc.com/letter_avatar_proxy/v4/letter/o/85e7bf/32.png) [@o\_lampe](https://community.simplefoc.com/u/o_lampe)
#### Post date: [November 2, 2023, 8:10am UTC](https://community.simplefoc.com/t/rp2040-i2c-on-pio-statemachines/4046/1 "2023-11-02T08:10:17Z")

</div>

Hi, I’ve seen in a [Github repo](https://github.com/raspberrypi/pico-examples/tree/master/pio/i2c), that there is a PIO based I2C implementation for the Earle\_Philhower Arduino core. That would greatly reduce MCU-interrupt hassle.  
I tried to understand how to use it, but it’s way over my head.  
Is there a way to implement it in sFOC for AS5600 sensors?

---

<div class="post-metadata">

### Author: ![VIPQualityPost](https://community.simplefoc.com/user_avatar/community.simplefoc.com/vipqualitypost/32/4420_2.png) [@VIPQualityPost](https://community.simplefoc.com/u/VIPQualityPost)
#### Post date: [November 2, 2023, 1:26pm UTC](https://community.simplefoc.com/t/rp2040-i2c-on-pio-statemachines/4046/2 "2023-11-02T13:26:39Z")

</div>

The RP2040 already has an I2C peripheral, so there is not really much reason to implement this in PIO (e.g. it’s not bitbanging, it’s handled by a specific on-chip peripheral). I think it’s provided in this repo as an example of how to use the PIO.

The problem with I2C is the low speed, which this does not solve, the transactions will still be the bottleneck for the FOC loop.

---

<div class="post-metadata">

### Author: ![Anthony\_Douglas](https://community.simplefoc.com/user_avatar/community.simplefoc.com/anthony_douglas/32/2300_2.png) [@Anthony\_Douglas](https://community.simplefoc.com/u/Anthony_Douglas)
#### Post date: [November 3, 2023, 8:22pm UTC](https://community.simplefoc.com/t/rp2040-i2c-on-pio-statemachines/4046/3 "2023-11-03T20:22:14Z")

</div>

The problem is not so much the low speed but the latency and the way it’s used in arduino. Others have posted code although it’s kind of hard to find, that did some stuff where they separated the sending and receiving. The problem is arduino simply sends a message and blocks while waiting for a reply, but you don’t have to do that. You can send the message and then get the reply at 1 million baud or something and time for actual communication is not a problem. Also the peripheral buffers the info, and handles everything, the cpu doesn’t actually have to wait for anything. That’s just the way the arduino library is written.

You can get a \>1 khz update rate in any case which is enough for a lot of situations, but it makes a buzzing noise because the system stops modulating the sine wave etc for that period.

---

<div class="post-metadata">

### Author: ![runger](https://community.simplefoc.com/user_avatar/community.simplefoc.com/runger/32/395_2.png) [@runger](https://community.simplefoc.com/u/runger)
#### Post date: [November 5, 2023, 4:52pm UTC](https://community.simplefoc.com/t/rp2040-i2c-on-pio-statemachines/4046/4 "2023-11-05T16:52:16Z")

</div>

> [@o\_lampe](#):
>
> Is there a way to implement it in sFOC for AS5600 sensors?

I have pushed a new driver for the AS5600 to the drivers library, dev branch:

> **[Arduino-FOC-drivers/src/encoders/as5600 at dev · simplefoc/Arduino-FOC-drivers](https://github.com/simplefoc/Arduino-FOC-drivers/tree/dev/src/encoders/as5600)**
>
> Drivers and support code for SimpleFOC. Contribute to simplefoc/Arduino-FOC-drivers development by creating an account on GitHub.

It is not yet tested, its on my list to test it out…

But in theory it should support the “ultra-fast mode” as discovered by one of our users here, with up to 9kHz update rates by not closing the I2C transactions properly between reads.

```auto
#include <Arduino.h>
#include <SimpleFOC.h>
#include <SimpleFOCDrivers.h>
#include "encoders/as5600/MagneticSensorAS5600.h"

MagneticSensorAS5600 sensor;

void setup() {
    sensor.closeTransactions = false;
    sensor.init();
}

```

---

<div class="post-metadata">

### Author: ![o\_lampe](https://community.simplefoc.com/letter_avatar_proxy/v4/letter/o/85e7bf/32.png) [@o\_lampe](https://community.simplefoc.com/u/o_lampe)
#### Post date: [November 6, 2023, 12:51pm UTC](https://community.simplefoc.com/t/rp2040-i2c-on-pio-statemachines/4046/5 "2023-11-06T12:51:30Z")

</div>

I can confirm that it compiles and runs without problems.  
But the loop time is still 1kHz, except I use motor.motion\_downsample.

I also tried to set sensor.closeTransactions to true, but that ended in a screaming motor.  
ATM, it would be better to make this variable static.

I only replaced the _driver_ folder, maybe that’s the problem?

---

<div class="post-metadata">

### Author: ![runger](https://community.simplefoc.com/user_avatar/community.simplefoc.com/runger/32/395_2.png) [@runger](https://community.simplefoc.com/u/runger)
#### Post date: [November 6, 2023, 1:33pm UTC](https://community.simplefoc.com/t/rp2040-i2c-on-pio-statemachines/4046/6 "2023-11-06T13:33:38Z")

</div>

> [@o\_lampe](#):
>
> but that ended in a screaming motor.

Oh no! Well if it’s working ok in normal mode, then that’s at least a partial success. In this mode you would not expect more than the 1kHz you normally get, so that’s fine.  
I’ll investigate the fast mode and get it working eventually…

May I ask which MCU you tested this on?

> [@o\_lampe](#):
>
> I only replaced the _driver_ folder, maybe that’s the problem?

no, that should be fine, if it finds the code and it compiled fine then that should work.

---

<div class="post-metadata">

### Author: ![o\_lampe](https://community.simplefoc.com/letter_avatar_proxy/v4/letter/o/85e7bf/32.png) [@o\_lampe](https://community.simplefoc.com/u/o_lampe)
#### Post date: [November 7, 2023, 8:48am UTC](https://community.simplefoc.com/t/rp2040-i2c-on-pio-statemachines/4046/7 "2023-11-07T08:48:20Z")

</div>

> [@runger](#):
>
> if it’s working ok in normal mode, then that’s at least a partial success. In this mode you would not expect more than the 1kHz you normally get, so that’s fine.

I have a different understanding of _closeTransactions_.  
I thought, when set to `false` it would be the fast mode, because it wouldn’t waste time with closing and re-establishing the connection?

I’ve tested it on a rp2040 with mbed\_i2c. Should I try Earl\_philhowers library instead?

---

<div class="post-metadata">

### Author: ![runger](https://community.simplefoc.com/user_avatar/community.simplefoc.com/runger/32/395_2.png) [@runger](https://community.simplefoc.com/u/runger)
#### Post date: [November 8, 2023, 1:30pm UTC](https://community.simplefoc.com/t/rp2040-i2c-on-pio-statemachines/4046/8 "2023-11-08T13:30:51Z")

</div>

It’s probably still buggy - I didn’t have a chance to test it yet…

I thought the way I implemented it, `closeTransactions = true` is the normal, default behaviour, and `closeTransactions = false` is the fast version.  
But you’re indicating that doesn’t work.

> [@o\_lampe](#):
>
> I’ve tested it on a rp2040 with mbed\_i2c. Should I try Earl\_philhowers library instead?

TBH the Arduino Support for RP2040 isn’t the greatest, it would not be my first choice for testing this. Especially not with mbed, so, yes, maybe EarleHillPower will work better 🙂

Don’t spend too much time on it though, like I said I could not yet test so its probably still buggy.
