CHGame 0.3.0
Libraries for the CHGame handheld
Loading...
Searching...
No Matches
Wire (I2C)

The Arduino Wire API: I2C master (and slave) on I2C1. More...

Classes

class  TwoWire
 An I2C bus: the Arduino Wire API on one of the chip's I2C peripherals (the global Wire on this board). More...
 

Macros

#define BUFFER_LENGTH   32
 The smallest size of the receive and transmit buffers, in bytes (they grow on the heap when a transfer needs more).
 
#define WIRE_MAX_TX_BUFF_LENGTH   1024U
 The most bytes one transmission (beginTransmission() ... endTransmission()) can hold; write() returns 0 past it.
 
#define WIRE_HAS_END   1
 Defined to 1: this Wire has end().
 

Variables

TwoWire Wire
 The I2C bus: I2C1 on PC18 (SDA, header H1.2) and PC19 (SCL, H1.1).
 

Detailed Description

The Arduino Wire API: I2C master (and slave) on I2C1.

#include <Wire.h>
Wire.begin(); // master, 100 kHz
Wire.write(0x00);
Wire.write(0xAF);
uint8_t err = Wire.endTransmission(); // 0: done
if (Wire.requestFrom(0x3C, 1) == 1) {
int b = Wire.read();
}
Arduino's Wire library (TwoWire), ported to WCH's chips: I2C master and slave on the CH32X035's I2C1.
void begin(bool generalCall=false)
Joins the bus as the master, at 100 kHz.
virtual size_t write(uint8_t)
Adds a byte to the transmission, or, in a slave's onRequest() callback, sends it to the master.
virtual int read(void)
Takes the next received byte.
void beginTransmission(uint8_t)
Starts collecting bytes for a slave; nothing goes on the bus until endTransmission().
uint8_t requestFrom(uint8_t, uint8_t)
Reads bytes from a slave into the receive buffer, for available() and read().
uint8_t endTransmission(void)
Sends the bytes collected since beginTransmission(), then a STOP.
TwoWire Wire
The I2C bus: I2C1 on PC18 (SDA, header H1.2) and PC19 (SCL, H1.1).

On the CHGame board (rev0) the global Wire uses SDA = PC18 (PIN_WIRE_SDA) and SCL = PC19 (PIN_WIRE_SCL), which come out on the expansion header H1: SDA on H1.2, SCL on H1.1. The board has no pull-up resistors on them, so the device or the wiring must provide them. (They are also the chip's two-wire debug pins, DIO and DCK.) The stock CH32X035 defaults, PC17 and PC16, are the board's USB D+ and D-: never pass them to begin(sda, scl) or setSDA() / setSCL().

Transfers are polled (slave mode uses the I2C interrupt) and each step gives up after about 3 ms (I2C_TIMEOUT_TICK). The transmit and receive buffers live on the heap: they start at BUFFER_LENGTH bytes, grow as needed, and a transmission holds at most WIRE_MAX_TX_BUFF_LENGTH bytes.

Macro Definition Documentation

◆ BUFFER_LENGTH

#define BUFFER_LENGTH   32

The smallest size of the receive and transmit buffers, in bytes (they grow on the heap when a transfer needs more).

◆ WIRE_MAX_TX_BUFF_LENGTH

#define WIRE_MAX_TX_BUFF_LENGTH   1024U

The most bytes one transmission (beginTransmission() ... endTransmission()) can hold; write() returns 0 past it.

Default 1024.

◆ WIRE_HAS_END

#define WIRE_HAS_END   1

Defined to 1: this Wire has end().

Variable Documentation

◆ Wire

TwoWire Wire
extern

The I2C bus: I2C1 on PC18 (SDA, header H1.2) and PC19 (SCL, H1.1).