Skip to content

Commit 1fc6588

Browse files
committed
feat(core): third step of ArduinoCore-API migration – Wire (I2C)
Signed-off-by: Aymane Bahssain <aymane.bahssain@st.com>
1 parent 1b0174e commit 1fc6588

File tree

5 files changed

+83
-31
lines changed

5 files changed

+83
-31
lines changed

CI/build/examples/BareMinimum/BareMinimum.ino

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
#include <SoftwareSerial.h>
8+
#include <Wire.h>
89

910
#ifndef USER_BTN
1011
#define USER_BTN 2
@@ -48,9 +49,35 @@ void setup() {
4849
}
4950
swSerial.end();
5051

52+
// Wire
53+
Wire.setSCL(PIN_WIRE_SCL);
54+
Wire.setSDA(digitalPinToPinName(PIN_WIRE_SDA));
55+
Wire.setClock(400000);
56+
Wire.begin(4);
57+
Wire.onRequest(requestEvent);
58+
Wire.onReceive(receiveEvent);
59+
Wire.beginTransmission(4);
60+
Wire.endTransmission();
61+
Wire.requestFrom(2, 1);
62+
Wire.end();
63+
5164
}
5265

5366
void loop() {
5467
}
5568

69+
// Wire
70+
// function that executes whenever data is received from master
71+
// this function is registered as an event, see setup()
72+
void receiveEvent(int) {
73+
while (1 < Wire.available()) {
74+
Wire.read();
75+
}
76+
}
77+
// function that executes whenever data is requested by master
78+
// this function is registered as an event, see setup()
79+
void requestEvent() {
80+
Wire.write("x");
81+
}
82+
5683

CI/build/examples/BareMinimum/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ cmake_minimum_required(VERSION 3.21)
77
# STEP 1: set up bases of environment
88
# -----------------------------------------------------------------------------
99

10-
add_definitions(-DEXTENDED_PIN_MODE -DARDUINO_CORE_STM32)
1110

1211
file(REAL_PATH "../../../../" CORE_PATH EXPAND_TILDE)
1312
file(TO_CMAKE_PATH "${CORE_PATH}" CORE_PATH)
@@ -49,5 +48,5 @@ build_sketch(TARGET "BareMinimum"
4948
# Servo
5049
SoftwareSerial
5150
# SPI
52-
# Wire
51+
Wire
5352
)

cmake/set_base_arduino_config.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ target_compile_definitions(base_config INTERFACE
2525
USE_HAL_DRIVER
2626
USE_FULL_LL_DRIVER
2727
ARDUINO_ARCH_STM32
28+
EXTENDED_PIN_MODE
2829
)
2930
target_compile_options(base_config INTERFACE
3031
-mthumb

libraries/Wire/src/Wire.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static const uint8_t MASTER_ADDRESS = 0x01;
3333

3434
// Constructors ////////////////////////////////////////////////////////////////
3535

36-
TwoWire::TwoWire(uint32_t sda, uint32_t scl)
36+
TwoWire::TwoWire(pin_size_t sda, pin_size_t scl)
3737
{
3838
memset((void *)&_i2c, 0, sizeof(_i2c));
3939
_i2c.sda = digitalPinToPinName(sda);
@@ -56,7 +56,7 @@ TwoWire::~TwoWire()
5656

5757
// Public Methods //////////////////////////////////////////////////////////////
5858

59-
void TwoWire::begin(uint32_t sda, uint32_t scl)
59+
void TwoWire::begin(pin_size_t sda, pin_size_t scl)
6060
{
6161
_i2c.sda = digitalPinToPinName(sda);
6262
_i2c.scl = digitalPinToPinName(scl);
@@ -125,9 +125,9 @@ void TwoWire::end(void)
125125
rxBufferAllocated = 0;
126126
}
127127

128-
void TwoWire::setClock(uint32_t frequency)
128+
void TwoWire::setClock(uint32_t freq)
129129
{
130-
i2c_setTiming(&_i2c, frequency);
130+
i2c_setTiming(&_i2c, freq);
131131
if (_i2c.isMaster == 0) {
132132
i2c_attachSlaveTxEvent(&_i2c, onRequestService);
133133
i2c_attachSlaveRxEvent(&_i2c, onReceiveService);
@@ -189,9 +189,9 @@ uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop
189189
return requestFrom((uint8_t)address, (uint8_t)quantity, (uint32_t)0, (uint8_t)0, (uint8_t)sendStop);
190190
}
191191

192-
uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool sendStop)
192+
size_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool sendStop)
193193
{
194-
return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)sendStop);
194+
return (size_t)requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)sendStop);
195195
}
196196

197197
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity)
@@ -237,15 +237,15 @@ void TwoWire::beginTransmission(int address)
237237
// no call to endTransmission(true) is made. Some I2C
238238
// devices will behave oddly if they do not see a STOP.
239239
//
240-
uint8_t TwoWire::endTransmission(uint8_t sendStop)
240+
uint8_t TwoWire::endTransmission(bool stopBit)
241241
{
242242
#if !defined(I2C_OTHER_FRAME)
243-
UNUSED(sendStop);
243+
UNUSED(stopBit);
244244
#endif
245245
int8_t ret = 4;
246246
// check transfer options and store it in the I2C handle
247247
#if defined(I2C_OTHER_FRAME)
248-
if (sendStop == 0) {
248+
if (stopBit == 0) {
249249
_i2c.handle.XferOptions = I2C_OTHER_FRAME ;
250250
} else {
251251
_i2c.handle.XferOptions = I2C_OTHER_AND_LAST_FRAME;
@@ -292,7 +292,7 @@ uint8_t TwoWire::endTransmission(uint8_t sendStop)
292292
//
293293
uint8_t TwoWire::endTransmission(void)
294294
{
295-
return endTransmission((uint8_t)true);
295+
return endTransmission(true);
296296
}
297297

298298
// must be called in:
@@ -454,12 +454,22 @@ void TwoWire::onReceive(cb_function_receive_t function)
454454
user_onReceive = function;
455455
}
456456

457+
void TwoWire::onReceive(void(*function)(int))
458+
{
459+
user_onReceive = function;
460+
}
461+
457462
// sets function called on slave read
458463
void TwoWire::onRequest(cb_function_request_t function)
459464
{
460465
user_onRequest = function;
461466
}
462467

468+
void TwoWire::onRequest(void(*function)(void))
469+
{
470+
user_onRequest = function;
471+
}
472+
463473
/**
464474
* @brief Allocate the Rx/Tx buffer to the requested length if needed
465475
* @note Minimum allocated size is BUFFER_LENGTH)

libraries/Wire/src/Wire.h

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
#include <functional>
2626

27-
#include "Stream.h"
2827
#include "Arduino.h"
28+
#include "api/HardwareI2C.h"
2929
extern "C" {
3030
#include "utility/twi.h"
3131
}
@@ -41,7 +41,7 @@ extern "C" {
4141
// WIRE_HAS_END means Wire has end()
4242
#define WIRE_HAS_END 1
4343

44-
class TwoWire : public Stream {
44+
class TwoWire : public arduino::HardwareI2C {
4545
public:
4646
typedef std::function<void(int)> cb_function_receive_t;
4747
typedef std::function<void(void)> cb_function_request_t;
@@ -76,14 +76,14 @@ class TwoWire : public Stream {
7676
void recoverBus(void);
7777

7878
public:
79-
TwoWire(uint32_t sda = SDA, uint32_t scl = SCL);
79+
TwoWire(pin_size_t sda = SDA, pin_size_t scl = SCL);
8080
~TwoWire();
8181
// setSCL/SDA have to be called before begin()
82-
void setSCL(uint32_t scl)
82+
void setSCL(pin_size_t scl)
8383
{
8484
_i2c.scl = digitalPinToPinName(scl);
8585
};
86-
void setSDA(uint32_t sda)
86+
void setSDA(pin_size_t sda)
8787
{
8888
_i2c.sda = digitalPinToPinName(sda);
8989
};
@@ -95,31 +95,46 @@ class TwoWire : public Stream {
9595
{
9696
_i2c.sda = sda;
9797
};
98-
void begin(bool generalCall = false);
99-
void begin(uint32_t, uint32_t);
98+
void begin(bool generalCall);
99+
void begin(pin_size_t, pin_size_t);
100100
void begin(uint8_t, bool generalCall = false, bool NoStretchMode = false);
101101
void begin(int, bool generalCall = false, bool NoStretchMode = false);
102-
void end();
103-
void setClock(uint32_t);
104-
void beginTransmission(uint8_t);
102+
void begin() override
103+
{
104+
begin(false);
105+
}
106+
void begin(uint8_t address) override
107+
{
108+
begin(address, false, false);
109+
}
110+
void end() override;
111+
void setClock(uint32_t freq) override;
112+
void beginTransmission(uint8_t address) override;
105113
void beginTransmission(int);
106-
uint8_t endTransmission(void);
107-
uint8_t endTransmission(uint8_t);
114+
uint8_t endTransmission(void) override;
115+
uint8_t endTransmission(bool stopBit) override;
108116
uint8_t requestFrom(uint8_t, uint8_t);
109117
uint8_t requestFrom(uint8_t, uint8_t, uint8_t);
110-
uint8_t requestFrom(uint8_t, size_t, bool);
118+
size_t requestFrom(uint8_t address, size_t len, bool stopBit) override;
111119
uint8_t requestFrom(uint8_t, uint8_t, uint32_t, uint8_t, uint8_t);
112120
uint8_t requestFrom(int, int);
113121
uint8_t requestFrom(int, int, int);
114-
virtual size_t write(uint8_t);
115-
virtual size_t write(const uint8_t *, size_t);
116-
virtual int available(void);
117-
virtual int read(void);
118-
virtual int peek(void);
119-
virtual void flush(void);
122+
size_t requestFrom(uint8_t address, size_t len) override
123+
{
124+
return (size_t)requestFrom(address, (uint8_t)len);
125+
}
126+
127+
size_t write(uint8_t) override;
128+
size_t write(const uint8_t *, size_t) override;
129+
int available(void) override;
130+
int read(void) override;
131+
int peek(void) override;
132+
void flush(void) override;
120133

121134
void onReceive(cb_function_receive_t callback);
135+
void onReceive(void (*)(int)) override;
122136
void onRequest(cb_function_request_t callback);
137+
void onRequest(void (*)(void)) override;
123138

124139
inline size_t write(unsigned long n)
125140
{

0 commit comments

Comments
 (0)