7 releases (breaking)

0.8.0 Mar 16, 2024
0.7.0 Sep 1, 2023
0.6.0 Feb 18, 2023
0.5.0 Dec 11, 2022
0.1.0 Jan 3, 2022

#2 in #pio

Download history 25/week @ 2023-12-25 86/week @ 2024-02-19 27/week @ 2024-02-26 18/week @ 2024-03-04 258/week @ 2024-03-11 177/week @ 2024-03-18 50/week @ 2024-03-25 206/week @ 2024-04-01 172/week @ 2024-04-08

658 downloads per month
Used in rp-pico

Apache-2.0

33KB
780 lines

I2C PIO

Implements the I2C hal using the RP2040 PIO peripheral.

Refer to the rp-hal for examples.


lib.rs:

Implements an I2C Controller using the RP2040 PIO block.

This implementation is based on the c-sdk's example with the following variation:

  • It uses WAIT on GPIO to handle clock stretching without requiring SCL to be SDA+1.
  • It keeps autopush enabled a flushes the RX FIFO during write operations.

General command word description

TX Encoding: | 15:10 | 9 | 8:1 | 0 | | Instr | Final | Data | NAK |

If Instr has a value n > 0, then this FIFO word has no data payload, and the next n + 1 words will be executed as instructions. Otherwise, shift out the 8 data bits, followed by the ACK bit.

The Instr mechanism allows stop/start/repstart sequences to be programmed by the processor, and then carried out by the state machine at defined points in the datastream.

The "Final" field should be set for the final byte in a transfer. This tells the state machine to ignore a NAK: if this field is not set, then any NAK will cause the state machine to halt and interrupt.

Autopull should be enabled, with a threshold of 16. Autopush should be enabled, with a threshold of 8. The TX FIFO should be accessed with halfword writes, to ensure the data is immediately available in the OSR.

Pin mapping:

  • Input pin 0 is SDA
  • Jump pin is SDA
  • Side-set pin 0 is SCL
  • Set pin 0 is SDA
  • OUT pin 0 is SDA

The OE outputs should be inverted in the system IO controls! (It's possible for the inversion to be done in this program, but costs 2 instructions: 1 for inversion, and one to cope with the side effect of the MOV on TX shift counter.)

Dependencies

~14MB
~250K SLoC