Loading...
Searching...
No Matches
Wire.h
Go to the documentation of this file.
1
15#pragma once
16
17#include "Arduino.h"
18#include "api/HardwareI2C.h"
19#include "api/RingBuffer.h"
20#include "hardware/i2c.h"
21
27class PhyduinoWire : public arduino::HardwareI2C {
28public:
30 static constexpr size_t kBufferSize = 256;
31
38 PhyduinoWire(i2c_inst_t* i2c, pin_size_t sda, pin_size_t scl)
39 : i2c_(i2c), sda_(sda), scl_(scl) {}
40
44 void begin() override;
45
52 void begin(uint8_t address) override;
53
57 void end() override;
58
63 void setClock(uint32_t hz) override;
64
67
72 void beginTransmission(uint8_t address) override;
73
84 uint8_t endTransmission(bool sendStop) override;
85
90 uint8_t endTransmission() override { return endTransmission(true); }
91
99 size_t requestFrom(uint8_t address, size_t length, bool sendStop) override;
100
107 size_t requestFrom(uint8_t address, size_t length) override { return requestFrom(address, length, true); }
108
110
113
119 size_t write(uint8_t data) override;
120
127 size_t write(const uint8_t* data, size_t length) override;
128 using arduino::Print::write;
129
134 int available() override;
135
140 int read() override;
141
146 int peek() override;
147
151 void flush() override {}
152
154
158
163 void onReceive(void (*callback)(int)) override { on_receive_ = callback; }
164
169 void onRequest(void (*callback)()) override { on_request_ = callback; }
170
172
174 void handlePeripheralEvent(int event);
175
176private:
177 void setupPins();
178
179 i2c_inst_t* i2c_;
180 pin_size_t sda_, scl_;
181 uint32_t clock_hz_ = 100000;
182 bool started_ = false;
183 uint8_t tx_address_ = 0;
184 uint8_t tx_buffer_[kBufferSize];
185 size_t tx_length_ = 0;
186 arduino::RingBufferN<kBufferSize> rx_buffer_;
187
188 // Peripheral mode
189 bool peripheral_mode_ = false;
190 void (*on_receive_)(int) = nullptr;
191 void (*on_request_)() = nullptr;
192 size_t tx_sent_ = 0; // bytes of tx_buffer_ already sent to the controller
193 bool request_started_ = false;
194};
195
197extern PhyduinoWire Wire;
Entry header for sketches: Arduino API + board pin map + Phyduino extras.
PhyduinoWire Wire
I2C on the header SDA / SCL pins.
I2C controller or peripheral on one RP2040 I2C peripheral; sketches use Wire.
Definition Wire.h:27
void onRequest(void(*callback)()) override
Sets the function called when a controller reads from this board.
Definition Wire.h:169
size_t requestFrom(uint8_t address, size_t length) override
Reads bytes from a device and ends with a stop.
Definition Wire.h:107
void beginTransmission(uint8_t address) override
Starts a write to a device; queue the bytes with write().
static constexpr size_t kBufferSize
Largest number of bytes in one transmission or request.
Definition Wire.h:30
uint8_t endTransmission() override
Sends the queued bytes and ends with a stop.
Definition Wire.h:90
int available() override
Number of received bytes waiting to be read.
void begin() override
Starts as a controller, at 100 kHz unless setClock() was called.
size_t write(uint8_t data) override
Queues one byte: for endTransmission(), or as the reply in onRequest().
void onReceive(void(*callback)(int)) override
Sets the function called after a controller has written to this board.
Definition Wire.h:163
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().
void end() override
Stops I2C and frees the pins.
int peek() override
Next received byte, left in the buffer.
void begin(uint8_t address) override
Starts as a peripheral that answers at address.
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().
Definition Wire.h:38
void setClock(uint32_t hz) override
Sets the bus clock; applies at once if already started.
uint8_t endTransmission(bool sendStop) override
Sends the queued bytes.
size_t write(const uint8_t *data, size_t length) override
Queues several bytes: for endTransmission(), or as the reply in onRequest().
void flush() override
Does nothing; kept for Arduino compatibility.
Definition Wire.h:151
int read() override
Takes the next received byte.
uint8_t pin_size_t
Type for Arduino pin numbers (uint8_t).
Definition Common.h:112