Skip to content

Commit 78a9f60

Browse files
committed
Use TwiBuffers class for all buffers needed by Wire and twi. Allows
optionally tweaking Wire buffer and twi buffer sizes at compile time. enum TWI_STATE : uint8_t becomes uint8_t. twi_state type changed form uint8_t to TWI_STATE. Added examples for customized buffers. Do println() after all received characters have been printed in master_reader.ino. Corrected setting of transmit buffer size in example sketch. Use template struct for wire WireBuffers instead of namespace. Use only a single macro SET_WIRE_BUFFERS().
1 parent 073cd4c commit 78a9f60

File tree

11 files changed

+530
-67
lines changed

11 files changed

+530
-67
lines changed

libraries/Wire/examples/master_reader/master_reader.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ void loop() {
2424
char c = Wire.read(); // receive a byte as character
2525
Serial.print(c); // print the character
2626
}
27+
Serial.println();
2728

2829
delay(500);
2930
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Wire Master Reader Custom Buffer
2+
3+
// Demonstrates use of the Wire library
4+
// Reads data from an I2C/TWI slave device
5+
// Refer to the "Wire Slave Sender Custom Buffer" example for use with this
6+
7+
// Created 31 Dec 2024
8+
9+
// This example code is in the public domain.
10+
11+
12+
#include <Wire.h>
13+
#include <TwoWireBuffers.h>
14+
#include "Arduino.h"
15+
16+
// request 6 bytes from slave device #8
17+
constexpr size_t REQUESTED_BYTE_COUNT = 6;
18+
19+
constexpr size_t RECEIVE_BUFFER_SIZE = REQUESTED_BYTE_COUNT;
20+
constexpr size_t TRANSMIT_BUFFER_SIZE = 0; // There is no transmit in this sketch.
21+
22+
SET_WIRE_BUFFERS(RECEIVE_BUFFER_SIZE, TRANSMIT_BUFFER_SIZE,
23+
true /* master buffers needed */, false /* no slave buffers needed */ );
24+
25+
void setup() {
26+
Wire.begin(); // join I2C bus (address optional for master)
27+
Serial.begin(9600); // start serial for output
28+
29+
// This is just for curiosity and can be removed
30+
printWireBuffersCapacity(Serial);
31+
}
32+
33+
void loop() {
34+
Wire.requestFrom(8, REQUESTED_BYTE_COUNT);
35+
36+
while (Wire.available()) { // slave may send less than requested
37+
char c = Wire.read(); // receive a byte as character
38+
Serial.print(c); // print the character
39+
}
40+
Serial.println();
41+
42+
delay(500);
43+
}
44+
45+
void printWireBuffersCapacity(Stream& stream) {
46+
const auto& buffers = GET_WIRE_BUFFERS();
47+
48+
stream.print("Wire transmit buffer size is ");
49+
stream.println(buffers.txWireBufferCapacity());
50+
51+
stream.print("Wire receive buffer size is ");
52+
stream.println(buffers.rxWireBufferCapacity());
53+
54+
stream.print("twi_masterBuffer size is ");
55+
stream.println(buffers.twi_masterBufferCapacity());
56+
57+
stream.print("twi_rxBuffer size is ");
58+
stream.println(buffers.twi_rxBufferCapacity());
59+
60+
stream.print("twi_txBuffer size is ");
61+
stream.println(buffers.twi_txBufferCapacity());
62+
}

libraries/Wire/examples/master_writer/master_writer.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void setup() {
1616
Wire.begin(); // join I2C bus (address optional for master)
1717
}
1818

19-
byte x = 0;
19+
static byte x = 0;
2020

2121
void loop() {
2222
Wire.beginTransmission(8); // transmit to device #8
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Wire Master Writer Custom Buffer
2+
3+
// Demonstrates use of the Wire library
4+
// Writes data to an I2C/TWI slave device
5+
// Refer to the "Wire Slave Receiver Custom Buffer" example for use with this
6+
7+
// Created 31 Dec 2024
8+
9+
// This example code is in the public domain.
10+
11+
12+
#include <Wire.h>
13+
#include <TwoWireBuffers.h>
14+
#include "Arduino.h"
15+
16+
// The following text will not fit into the default buffer of 32 bytes.
17+
static const char text[] = "You really won't believe it, but x is ";
18+
19+
size_t constexpr RECEIVE_BUFFER_SIZE = 0; // There is no receive in this sketch.
20+
size_t constexpr TRANSMIT_BUFFER_SIZE = 42; // Enhance the buffer to 42 characters.
21+
22+
SET_WIRE_BUFFERS(RECEIVE_BUFFER_SIZE, TRANSMIT_BUFFER_SIZE,
23+
true /* master buffers needed */, false /* no slave buffers needed */ );
24+
25+
void setup() {
26+
Wire.begin(); // join I2C bus (address optional for master)
27+
28+
// This is just for curiosity and can be removed
29+
Serial.begin(9600); // start serial for output
30+
printWireBuffersCapacity(Serial);
31+
}
32+
33+
static byte x = 0;
34+
35+
void loop() {
36+
Wire.beginTransmission(8); // transmit to device #8
37+
Wire.write(text); // sends five bytes
38+
Wire.write(x); // sends one byte
39+
Wire.endTransmission(); // stop transmitting
40+
41+
x++;
42+
delay(500);
43+
}
44+
45+
void printWireBuffersCapacity(Stream& stream) {
46+
const auto& buffers = GET_WIRE_BUFFERS();
47+
48+
stream.print("Wire transmit buffer size is ");
49+
stream.println(buffers.txWireBufferCapacity());
50+
51+
stream.print("Wire receive buffer size is ");
52+
stream.println(buffers.rxWireBufferCapacity());
53+
54+
stream.print("twi_masterBuffer size is ");
55+
stream.println(buffers.twi_masterBufferCapacity());
56+
57+
stream.print("twi_rxBuffer size is ");
58+
stream.println(buffers.twi_rxBufferCapacity());
59+
60+
stream.print("twi_txBuffer size is ");
61+
stream.println(buffers.twi_txBufferCapacity());
62+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Wire Slave Receiver Custom Buffer
2+
3+
// Demonstrates use of the Wire library
4+
// Receives data as an I2C/TWI slave device
5+
// Refer to the "Wire Master Writer Custom Buffer" example for use with this
6+
7+
// Created 31 Dec 2024
8+
9+
// This example code is in the public domain.
10+
11+
12+
#include <Wire.h>
13+
#include <TwoWireBuffers.h>
14+
#include "Arduino.h"
15+
16+
size_t constexpr RECEIVE_BUFFER_SIZE = 42; // Be able receive up to 42 characters in one message.
17+
size_t constexpr TRANSMIT_BUFFER_SIZE = 0; // There is no transmit in this sketch.
18+
19+
SET_WIRE_BUFFERS(RECEIVE_BUFFER_SIZE, TRANSMIT_BUFFER_SIZE,
20+
true /* master buffers needed */, false /* no slave buffers needed */ );
21+
22+
void setup() {
23+
Wire.begin(8); // join I2C bus with address #8
24+
Wire.onReceive(receiveEvent); // register event
25+
Serial.begin(9600); // start serial for output
26+
27+
// This is just for curiosity and can be removed
28+
printWireBuffersCapacity(Serial);
29+
}
30+
31+
void loop() {
32+
delay(100);
33+
}
34+
35+
// function that executes whenever data is received from master
36+
// this function is registered as an event, see setup()
37+
void receiveEvent(int howMany) {
38+
while (1 < Wire.available()) { // loop through all but the last
39+
char c = Wire.read(); // receive byte as a character
40+
Serial.print(c); // print the character
41+
}
42+
int x = Wire.read(); // receive byte as an integer
43+
Serial.println(x); // print the integer
44+
}
45+
46+
void printWireBuffersCapacity(Stream& stream) {
47+
const auto& buffers = GET_WIRE_BUFFERS();
48+
49+
stream.print("Wire transmit buffer size is ");
50+
stream.println(buffers.txWireBufferCapacity());
51+
52+
stream.print("Wire receive buffer size is ");
53+
stream.println(buffers.rxWireBufferCapacity());
54+
55+
stream.print("twi_masterBuffer size is ");
56+
stream.println(buffers.twi_masterBufferCapacity());
57+
58+
stream.print("twi_rxBuffer size is ");
59+
stream.println(buffers.twi_rxBufferCapacity());
60+
61+
stream.print("twi_txBuffer size is ");
62+
stream.println(buffers.twi_txBufferCapacity());
63+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Wire Slave Sender Custom Buffer
2+
3+
// Demonstrates use of the Wire library
4+
// Sends data as an I2C/TWI slave device
5+
// Refer to the "Wire Master Reader Custom Buffer" example for use with this
6+
7+
// Created 31 Dec 2024
8+
9+
// This example code is in the public domain.
10+
11+
12+
#include <Wire.h>
13+
#include <TwoWireBuffers.h>
14+
#include "Arduino.h"
15+
16+
static const char text[] = "hello "; // respond with message of 6 bytes
17+
18+
size_t constexpr RECEIVE_BUFFER_SIZE = 0; // There is no receive in this sketch.
19+
size_t constexpr TRANSMIT_BUFFER_SIZE = sizeof(text)-1; // Don't need a byte for the \0
20+
21+
SET_WIRE_BUFFERS(RECEIVE_BUFFER_SIZE, TRANSMIT_BUFFER_SIZE,
22+
true /* master buffers needed */, false /* no slave buffers needed */ );
23+
24+
void setup() {
25+
Wire.begin(8); // join I2C bus with address #8
26+
Wire.onRequest(requestEvent); // register event
27+
28+
// This is just for curiosity and can be removed
29+
Serial.begin(9600);
30+
printWireBuffersCapacity(Serial);
31+
}
32+
33+
void loop() {
34+
delay(100);
35+
}
36+
37+
// function that executes whenever data is requested by master
38+
// this function is registered as an event, see setup()
39+
void requestEvent() {
40+
Wire.write(text);
41+
// as expected by master
42+
}
43+
44+
void printWireBuffersCapacity(Stream& stream) {
45+
const auto& buffers = GET_WIRE_BUFFERS();
46+
47+
stream.print("Wire transmit buffer size is ");
48+
stream.println(buffers.txWireBufferCapacity());
49+
50+
stream.print("Wire receive buffer size is ");
51+
stream.println(buffers.rxWireBufferCapacity());
52+
53+
stream.print("twi_masterBuffer size is ");
54+
stream.println(buffers.twi_masterBufferCapacity());
55+
56+
stream.print("twi_rxBuffer size is ");
57+
stream.println(buffers.twi_rxBufferCapacity());
58+
59+
stream.print("twi_txBuffer size is ");
60+
stream.println(buffers.twi_txBufferCapacity());
61+
}

libraries/Wire/src/TwoWireBuffers.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
twi.c - TWI/I2C library for Wiring & Arduino
3+
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include "TwoWireBuffers.h"
21+
22+
constexpr size_t RX_BUFFER_DEFAULT_LENGTH = 32;
23+
constexpr size_t TX_BUFFER_DEFAULT_LENGTH = 32;
24+
25+
// Default buffers for the one and only Wire object
26+
template<> __attribute__((weak)) TwoWireBuffers::Interface& WireBuffers<0>::instance() { \
27+
static TwoWireBuffers::Impl<RX_BUFFER_DEFAULT_LENGTH, TX_BUFFER_DEFAULT_LENGTH, true, true> buffers; \
28+
return buffers; \
29+
}

0 commit comments

Comments
 (0)