Skip to content

Commit 7bdc87d

Browse files
fix: support continueSession for I2C ReceiveData (#668)
Simplify I2C transfer completion handling and add result scheduling methods. The Transfer Complete Reload flag is set by hardware when RELOAD bit is set.
1 parent 1cbc080 commit 7bdc87d

File tree

2 files changed

+30
-23
lines changed

2 files changed

+30
-23
lines changed

hal_st/stm32fxxx/I2cStm.cpp

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,7 @@ namespace hal
188188
{
189189
if (sendData.empty() && receiveData.empty())
190190
{
191-
peripheralI2c[instance]->CR1 &= ~(I2C_CR1_TXIE | I2C_CR1_RXIE | I2C_CR1_TCIE | I2C_CR1_NACKIE | I2C_CR1_ERRIE);
192-
if (nextAction == Action::stop)
193-
peripheralI2c[instance]->CR2 = I2C_CR2_STOP;
194-
continuingPrevious = nextAction == Action::continueSession;
195-
infra::EventDispatcher::Instance().Schedule([this]()
196-
{
197-
onSent(hal::Result::complete, sent);
198-
});
191+
FinishTransferComplete();
199192
}
200193
else if (!receiveData.empty())
201194
peripheralI2c[instance]->CR2 = (std::min<uint32_t>(receiveData.size(), 255) << 16) | (receiveData.size() > 255 || nextAction == Action::continueSession ? I2C_CR2_RELOAD : 0);
@@ -208,21 +201,7 @@ namespace hal
208201
ReadReceivedData();
209202
really_assert(sendData.empty());
210203
really_assert(receiveData.empty());
211-
peripheralI2c[instance]->CR1 &= ~(I2C_CR1_TXIE | I2C_CR1_RXIE | I2C_CR1_TCIE | I2C_CR1_NACKIE | I2C_CR1_ERRIE);
212-
if (nextAction == Action::stop)
213-
peripheralI2c[instance]->CR2 = I2C_CR2_STOP;
214-
continuingPrevious = nextAction == Action::continueSession;
215-
if (onSent != nullptr)
216-
infra::EventDispatcher::Instance().Schedule([this]()
217-
{
218-
onSent(hal::Result::complete, std::exchange(sent, 0));
219-
});
220-
else if (onReceived != nullptr)
221-
infra::EventDispatcher::Instance().Schedule([this]()
222-
{
223-
received = 0;
224-
onReceived(hal::Result::complete);
225-
});
204+
FinishTransferComplete();
226205
}
227206
#else
228207
auto sr1 = peripheralI2c[instance]->SR1;
@@ -410,6 +389,32 @@ namespace hal
410389
++received;
411390
}
412391
}
392+
393+
void I2cStm::ScheduleResult(hal::Result result)
394+
{
395+
if (onSent != nullptr)
396+
infra::EventDispatcher::Instance().Schedule([this, result]()
397+
{
398+
onSent(result, std::exchange(sent, 0));
399+
});
400+
else if (onReceived != nullptr)
401+
infra::EventDispatcher::Instance().Schedule([this, result]()
402+
{
403+
received = 0;
404+
onReceived(result);
405+
});
406+
else
407+
std::abort();
408+
}
409+
410+
void I2cStm::FinishTransferComplete()
411+
{
412+
peripheralI2c[instance]->CR1 &= ~(I2C_CR1_TXIE | I2C_CR1_RXIE | I2C_CR1_TCIE | I2C_CR1_NACKIE | I2C_CR1_ERRIE);
413+
if (nextAction == Action::stop)
414+
peripheralI2c[instance]->CR2 = I2C_CR2_STOP;
415+
continuingPrevious = nextAction == Action::continueSession;
416+
ScheduleResult(hal::Result::complete);
417+
}
413418
#endif
414419

415420
void I2cStm::Clear()

hal_st/stm32fxxx/I2cStm.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ namespace hal
4646
void ErrorInterrupt();
4747
#if defined(I2C_ISR_TXE)
4848
void ReadReceivedData();
49+
void ScheduleResult(hal::Result result);
50+
void FinishTransferComplete();
4951
#endif
5052
void Clear();
5153

0 commit comments

Comments
 (0)