Skip to content

Commit 1dfb54b

Browse files
committed
chore(i2c): add HAL v2 support
Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
1 parent c4e9703 commit 1dfb54b

5 files changed

Lines changed: 320 additions & 42 deletions

File tree

‎cores/arduino/stm32/stm32yyxx_hal_conf.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#define USE_HAL_I2C_MODULE 0U
4242
#else
4343
#define USE_HAL_I2C_MODULE 1U
44+
#define USE_HAL_I2C_GET_LAST_ERRORS 1U
4445
#endif
4546

4647
#if defined(HAL_I2S_MODULE_DISABLED)

‎libraries/Wire/src/Wire.cpp‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,19 @@ void TwoWire::begin(uint8_t address, bool generalCall, bool NoStretchMode)
8484

8585
ownAddress = address << 1;
8686

87-
_i2c.isMaster = (address == MASTER_ADDRESS) ? 1 : 0;
87+
_i2c.isMaster = (address == MASTER_ADDRESS);
8888

89-
_i2c.generalCall = (generalCall == true) ? 1 : 0;
89+
_i2c.generalCall = generalCall;
9090

91-
_i2c.NoStretchMode = (NoStretchMode == true) ? 1 : 0;
91+
_i2c.NoStretchMode = NoStretchMode;
9292

93-
if (_i2c.isMaster == 1) {
93+
if (_i2c.isMaster) {
9494
recoverBus(); // in case I2C bus (device) is stuck after a reset for example
9595
}
9696

9797
i2c_init(&_i2c, 100000, ownAddress);
9898

99-
if (_i2c.isMaster == 0) {
99+
if (!_i2c.isMaster) {
100100
// i2c_attachSlaveTxEvent(&_i2c, reinterpret_cast<void(*)(i2c_t*)>(&TwoWire::onRequestService));
101101
// i2c_attachSlaveRxEvent(&_i2c, reinterpret_cast<void(*)(i2c_t*, uint8_t*, int)>(&TwoWire::onReceiveService));
102102

@@ -128,7 +128,7 @@ void TwoWire::end(void)
128128
void TwoWire::setClock(uint32_t freq)
129129
{
130130
i2c_setTiming(&_i2c, freq);
131-
if (_i2c.isMaster == 0) {
131+
if (!_i2c.isMaster) {
132132
i2c_attachSlaveTxEvent(&_i2c, onRequestService);
133133
i2c_attachSlaveRxEvent(&_i2c, onReceiveService);
134134
}
@@ -151,7 +151,7 @@ size_t TwoWire::requestFrom(uint8_t address, size_t len, uint32_t iaddress, uint
151151
#endif
152152
size_t read = 0;
153153

154-
if (_i2c.isMaster == 1) {
154+
if (_i2c.isMaster) {
155155
allocateRxBuffer(len);
156156

157157
if (isize > 0) {
@@ -181,7 +181,13 @@ size_t TwoWire::requestFrom(uint8_t address, size_t len, uint32_t iaddress, uint
181181
_i2c.handle.XferOptions = I2C_OTHER_AND_LAST_FRAME;
182182
}
183183
#endif
184-
184+
#if defined(USE_HALV2_DRIVER)
185+
if (stopBit == false) {
186+
_i2c.handle.xfer_opt = HAL_I2C_XFER_OTHER_FRAME ;
187+
} else {
188+
_i2c.handle.xfer_opt = HAL_I2C_XFER_OTHER_AND_LAST_FRAME;
189+
}
190+
#endif
185191
if (I2C_OK == i2c_master_read(&_i2c, address << 1, rxBuffer, (uint16_t)len)) {
186192
read = len;
187193
}
@@ -236,8 +242,15 @@ uint8_t TwoWire::endTransmission(bool stopBit)
236242
_i2c.handle.XferOptions = I2C_OTHER_AND_LAST_FRAME;
237243
}
238244
#endif
245+
#if defined(USE_HALV2_DRIVER)
246+
if (stopBit == false) {
247+
_i2c.handle.xfer_opt = HAL_I2C_XFER_OTHER_FRAME ;
248+
} else {
249+
_i2c.handle.xfer_opt = HAL_I2C_XFER_OTHER_AND_LAST_FRAME;
250+
}
251+
#endif
239252

240-
if (_i2c.isMaster == 1) {
253+
if (_i2c.isMaster) {
241254
// transmit buffer (blocking)
242255
switch (i2c_master_write(&_i2c, txAddress, txBuffer, txDataSize)) {
243256
case I2C_OK :

‎libraries/Wire/src/Wire.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ class TwoWire : public arduino::HardwareI2C {
150150
using Print::write;
151151

152152
// Could be used to mix Arduino API and STM32Cube HAL API (ex: DMA). Use at your own risk.
153+
#if defined(USE_HALV2_DRIVER)
154+
hal_i2c_handle_t *getHandle(void)
155+
#else
153156
I2C_HandleTypeDef *getHandle(void)
157+
#endif
154158
{
155159
return &(_i2c.handle);
156160
}

0 commit comments

Comments
 (0)