@@ -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 ()
0 commit comments