Loading...
Searching...
No Matches
PhyduinoWire Class Reference

I2C controller or peripheral on one RP2040 I2C peripheral; sketches use Wire. More...

#include <Wire.h>

Inheritance diagram for PhyduinoWire:

Public Member Functions

 PhyduinoWire (i2c_inst_t *i2c, pin_size_t sda, pin_size_t scl)
 Binds Wire to an I2C peripheral and its pins; nothing is set up until begin().
void begin () override
 Starts as a controller, at 100 kHz unless setClock() was called.
void begin (uint8_t address) override
 Starts as a peripheral that answers at address.
void end () override
 Stops I2C and frees the pins.
void setClock (uint32_t hz) override
 Sets the bus clock; applies at once if already started.
Controller
void beginTransmission (uint8_t address) override
 Starts a write to a device; queue the bytes with write().
uint8_t endTransmission (bool sendStop) override
 Sends the queued bytes.
uint8_t endTransmission () override
 Sends the queued bytes and ends with a stop.
size_t requestFrom (uint8_t address, size_t length, bool sendStop) override
 Reads bytes from a device into the receive buffer; take them with read().
size_t requestFrom (uint8_t address, size_t length) override
 Reads bytes from a device and ends with a stop.
Data (controller and peripheral)
size_t write (uint8_t data) override
 Queues one byte: for endTransmission(), or as the reply in onRequest().
size_t write (const uint8_t *data, size_t length) override
 Queues several bytes: for endTransmission(), or as the reply in onRequest().
int available () override
 Number of received bytes waiting to be read.
int read () override
 Takes the next received byte.
int peek () override
 Next received byte, left in the buffer.
void flush () override
 Does nothing; kept for Arduino compatibility.
Peripheral

The callbacks run inside the I2C interrupt: keep them short.

void onReceive (void(*callback)(int)) override
 Sets the function called after a controller has written to this board.
void onRequest (void(*callback)()) override
 Sets the function called when a controller reads from this board.

Static Public Attributes

static constexpr size_t kBufferSize = 256
 Largest number of bytes in one transmission or request.

Detailed Description

I2C controller or peripheral on one RP2040 I2C peripheral; sketches use Wire.

Every transfer gives up after 50 ms if the bus is stuck.

Constructor & Destructor Documentation

◆ PhyduinoWire()

PhyduinoWire::PhyduinoWire ( i2c_inst_t * i2c,
pin_size_t sda,
pin_size_t scl )
inline

Binds Wire to an I2C peripheral and its pins; nothing is set up until begin().

Parameters
i2cpico-sdk I2C (i2c0 or i2c1).
sdaArduino pin for SDA.
sclArduino pin for SCL.

Member Function Documentation

◆ available()

int PhyduinoWire::available ( )
override

Number of received bytes waiting to be read.

Returns
Byte count.

◆ begin()

void PhyduinoWire::begin ( uint8_t address)
override

Starts as a peripheral that answers at address.

Set onReceive() and onRequest() first.

Parameters
address7-bit address, 0x08-0x77.

◆ beginTransmission()

void PhyduinoWire::beginTransmission ( uint8_t address)
override

Starts a write to a device; queue the bytes with write().

Parameters
address7-bit device address.

◆ endTransmission() [1/2]

uint8_t PhyduinoWire::endTransmission ( )
inlineoverride

Sends the queued bytes and ends with a stop.

Returns
As endTransmission(bool).

◆ endTransmission() [2/2]

uint8_t PhyduinoWire::endTransmission ( bool sendStop)
override

Sends the queued bytes.

With no bytes queued it only checks that the device answers, which is what I2C scanners use.

Parameters
sendStoptrue to end with a stop; false to keep the bus for a following requestFrom() (repeated start).
Returns
0 on success, 2 if the device did not answer, 5 on timeout, 4 on any other error.

◆ onReceive()

void PhyduinoWire::onReceive ( void(* callback )(int))
inlineoverride

Sets the function called after a controller has written to this board.

Parameters
callbackFunction taking the number of bytes received; read them with read().

◆ onRequest()

void PhyduinoWire::onRequest ( void(* callback )())
inlineoverride

Sets the function called when a controller reads from this board.

Parameters
callbackFunction that queues the reply with write().

◆ peek()

int PhyduinoWire::peek ( )
override

Next received byte, left in the buffer.

Returns
0-255, or -1 if there is none.

◆ read()

int PhyduinoWire::read ( )
override

Takes the next received byte.

Returns
0-255, or -1 if there is none.

◆ requestFrom() [1/2]

size_t PhyduinoWire::requestFrom ( uint8_t address,
size_t length )
inlineoverride

Reads bytes from a device and ends with a stop.

Parameters
address7-bit device address.
lengthNumber of bytes, at most kBufferSize.
Returns
Number of bytes received, 0 on error.

◆ requestFrom() [2/2]

size_t PhyduinoWire::requestFrom ( uint8_t address,
size_t length,
bool sendStop )
override

Reads bytes from a device into the receive buffer; take them with read().

Parameters
address7-bit device address.
lengthNumber of bytes, at most kBufferSize.
sendStoptrue to end with a stop; false to keep the bus.
Returns
Number of bytes received, 0 on error.

◆ setClock()

void PhyduinoWire::setClock ( uint32_t hz)
override

Sets the bus clock; applies at once if already started.

Parameters
hzClock in Hz, usually 100000 or 400000.

◆ write() [1/2]

size_t PhyduinoWire::write ( const uint8_t * data,
size_t length )
override

Queues several bytes: for endTransmission(), or as the reply in onRequest().

Parameters
dataBytes to queue.
lengthNumber of bytes.
Returns
Number of bytes queued; fewer than length if the buffer is full.

◆ write() [2/2]

size_t PhyduinoWire::write ( uint8_t data)
override

Queues one byte: for endTransmission(), or as the reply in onRequest().

Parameters
dataByte to queue.
Returns
1, or 0 if the buffer is full.

The documentation for this class was generated from the following file: