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

#1936 in Hardware support

Download history 3/week @ 2024-07-21 42/week @ 2024-07-28 43/week @ 2024-08-04 16/week @ 2024-08-11 26/week @ 2024-08-18 30/week @ 2024-08-25 14/week @ 2024-09-01 21/week @ 2024-09-08 5/week @ 2024-09-15 39/week @ 2024-09-22 23/week @ 2024-09-29 8/week @ 2024-10-06 17/week @ 2024-10-13 29/week @ 2024-10-20 11/week @ 2024-10-27 11/week @ 2024-11-03

69 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

~12–21MB
~305K SLoC