@@ -675,117 +675,22 @@ HRESULT Library_sys_dev_i2c_native_System_Device_I2c_I2cDevice::
675675 cacheBufferInvalidate (palI2c->ReadBuffer , palI2c->ReadSize );
676676 }
677677 }
678- }
679-
680- while (eventResult)
681- {
682- if (!isLongRunningOperation)
683- {
684- // this is not a long running operation so nothing to do here
685- break ;
686- }
687-
688- if (palI2c->WorkingThread ->state == CH_STATE_FINAL)
689- {
690- // I2C working thread is now complete
691- break ;
692- }
693-
694- // non-blocking wait allowing other threads to run while we wait for the I2C transaction to complete
695- NANOCLR_CHECK_HRESULT (
696- g_CLR_RT_ExecutionEngine.WaitEvents (stack.m_owningThread , *timeout, Event_I2cMaster, eventResult));
697- }
698-
699- if (isLongRunningOperation)
700- {
701- // pop timeout heap block from stack
702- stack.PopValue ();
703- }
704678
705- if (eventResult || !isLongRunningOperation)
706- {
707- // event occurred
708- // OR this is NOT a long running operation
709-
710- i2cReleaseBus (palI2c->Driver );
711-
712- // create the return object (I2cTransferResult)
713- // only at this point we are sure that there will be a return from this thread so it's OK to use the
714- // managed stack
715- CLR_RT_HeapBlock &top = stack.PushValueAndClear ();
716- NANOCLR_CHECK_HRESULT (
717- g_CLR_RT_ExecutionEngine.NewObjectFromIndex (top, g_CLR_RT_WellKnownTypes.I2cTransferResult ));
718- result = top.Dereference ();
719- FAULT_ON_NULL (result);
720-
721- if (isLongRunningOperation)
722- {
723- // ChibiOS requirement: need to call chThdWait for I2C working thread in order to have it's memory
724- // released to the heap, otherwise it won't be returned
725- transactionResult = chThdWait (palI2c->WorkingThread );
726- }
679+ NANOCLR_CLEANUP ();
727680
728- // get the result from the working thread execution
729- if (transactionResult != MSG_OK)
681+ if (hr != CLR_E_THREAD_WAITING)
730682 {
731- // error in transaction
732- int errors = i2cGetErrors (palI2c->Driver );
733-
734- // figure out what was the error and set the status field
735- switch (errors)
683+ // un-pin the buffers
684+ if (writeBuffer != NULL && writeBuffer->IsPinned ())
736685 {
737- case I2C_ACK_FAILURE:
738- result[I2cTransferResult::FIELD___status].SetInteger (
739- (CLR_UINT32)I2cTransferStatus_SlaveAddressNotAcknowledged);
740- break ;
741-
742- case I2C_TIMEOUT:
743- result[I2cTransferResult::FIELD___status].SetInteger (
744- (CLR_UINT32)I2cTransferStatus_ClockStretchTimeout);
745- break ;
746-
747- default :
748- result[I2cTransferResult::FIELD___status].SetInteger ((CLR_UINT32)I2cTransferStatus_UnknownError);
686+ writeBuffer->Unpin ();
749687 }
750688
751- // set the bytes transferred count to 0 because we don't have a way to know how many bytes were
752- // actually sent/received
753- result[I2cTransferResult::FIELD___bytesTransferred].SetInteger (0 );
754- }
755- else
756- {
757- // successful transaction
758- // set the result field
759- result[I2cTransferResult::FIELD___status].SetInteger ((CLR_UINT32)I2cTransferStatus_FullTransfer);
760-
761- // set the bytes transferred field
762- result[I2cTransferResult::FIELD___bytesTransferred].SetInteger (
763- (CLR_UINT32)(palI2c->WriteSize + palI2c->ReadSize ));
764- }
765-
766- if (palI2c->ReadSize > 0 )
767- {
768- // invalidate cache over read buffer to ensure that content from DMA is read
769- // (only required for Cortex-M7)
770- cacheBufferInvalidate (palI2c->ReadBuffer , palI2c->ReadSize );
771- }
772- }
773-
774- NANOCLR_CLEANUP ();
775-
776- if (hr != CLR_E_THREAD_WAITING)
777- {
778- // un-pin the buffers
779- if (writeBuffer != NULL && writeBuffer->IsPinned ())
780- {
781- writeBuffer->Unpin ();
689+ if (readBuffer != NULL && readBuffer->IsPinned ())
690+ {
691+ readBuffer->Unpin ();
692+ }
782693 }
783694
784- if (readBuffer != NULL && readBuffer->IsPinned ())
785- {
786- readBuffer->Unpin ();
787- }
695+ NANOCLR_CLEANUP_END ();
788696 }
789-
790- NANOCLR_CLEANUP_END ();
791- }
0 commit comments