Interop questions

Work progress but I encounter a pb again with the following code.
When calls are commented, I receive 1 so tp is correctly initialized (I suppose :wink:).
But when I try to do only delay or delayNoInterrupt or call them with acquire and release, application became unresponsive. As it is very difficult to debug native code (if it can be !), I would like to know if I have forgotten Something obvious ?

TinyCLR_Result Interop_TestInterop_TestInterop_LedInterop::GetResult___I4(const TinyCLR_Interop_MethodData md) {
	auto ip = (const TinyCLR_Interop_Provider*)md.ApiProvider.FindDefault(&md.ApiProvider, TinyCLR_Api_Type::InteropProvider);
	TinyCLR_Interop_ManagedValue self;
	TinyCLR_Interop_ManagedValue ret;

	ret.Type = TinyCLR_Interop_ManagedValueType::I4;
	ip->GetThisObject(ip, md.Stack, self);
	ip->GetReturn(ip, md.Stack, ret);
	auto tp = (const TinyCLR_Time_Provider*)md.ApiProvider.Find(&md.ApiProvider, "GHIElectronics.TinyCLR.NativeApis.STM32F4.TimeProvider", TinyCLR_Api_Type::TimeProvider);
	if (tp != nullptr)
	{
		ret.Data.Numeric->I4 = 1;
                // each of the following call conduct to a "crash"
		//tp->Acquire(tp);
		//tp->Delay(tp,100000ULL);
		//tp->Release(tp);
	}
	else
	{
		ret.Data.Numeric->I4 = 0;
	}
	return TinyCLR_Result::Success;
}

Find returns const TinyCLR_Api_Info*, not const void* like FindBySelector, FindDefault, and FindByIndex. Those last three are helper methods that call Find and extract a specific implementation from the const TinyCLR_Api_Info* using the const void* Implementation member.

Try:

auto timeApi = md.ApiProvider.Find(&md.ApiProvider, "GHIElectronics.TinyCLR.NativeApis.STM32F4.TimeProvider", TinyCLR_Api_Type::TimeProvider);

if (timeApi != nullptr && timeApi->Count == 1) {
    auto tp = (const TinyCLR_Time_Provider*)timeApi->Implementation;

    if (tp != nullptr) {
        //...
    }
}

One note on the Implementation field. If Count is 0, then it’s nullptr as you’d expect. If it’s one, then it’s a pointer directly to the actual implementation. If it’s greater than one, then it’s an array of Count pointers to actual implementations.

1 Like

##To use Time Interop functions##

All the return values can be removed, they are only usefull to debug code.

TinyCLR_Result Interop_TestInterop_TestInterop_LedInterop::Blink___I4(const TinyCLR_Interop_MethodData md) {
	// Prepare return value
	auto ip = (const TinyCLR_Interop_Provider*)md.ApiProvider.FindDefault(&md.ApiProvider, TinyCLR_Api_Type::InteropProvider);
	TinyCLR_Interop_ManagedValue self;
	TinyCLR_Interop_ManagedValue ret;
	ret.Type = TinyCLR_Interop_ManagedValueType::I4;
	ip->GetThisObject(ip, md.Stack, self);
	ip->GetReturn(ip, md.Stack, ret);

        // Get pointer to Gpio Provider
	auto gpio = (const TinyCLR_Gpio_Provider*)md.ApiProvider.FindDefault(&md.ApiProvider, TinyCLR_Api_Type::GpioProvider);
	if (gpio == nullptr) {
		// Gpio ptr is null
		ret.Data.Numeric->I4 = 1;
		return TinyCLR_Result::Success;
	}

        // Get pointer to Time API
	auto timeApi = md.ApiProvider.Find(&md.ApiProvider, "GHIElectronics.TinyCLR.NativeApis.STM32F4.TimeProvider", TinyCLR_Api_Type::TimeProvider);
	if (timeApi != nullptr && timeApi->Count == 1) {

                // Get pointer to Time Provider
		auto tp = (const TinyCLR_Time_Provider*)timeApi->Implementation;
		if (tp != nullptr)
		{
			// Blink led connected to pin 45 4 times with 1 sec delay
                        
			for (int i = 0; i < 4; i++) {
				gpio->Write(gpio, 45, TinyCLR_Gpio_PinValue::High);
				tp->Delay(tp, 1000000ULL);
				gpio->Write(gpio, 45, TinyCLR_Gpio_PinValue::Low);
				tp->Delay(tp, 1000000ULL);
			}
			
			ret.Data.Numeric->I4 = 0;
		}
		else
		{
			// Time provider pointer is null
			ret.Data.Numeric->I4 = 3;
		}
	}
	else
	{
		// Time API pointer is null
		ret.Data.Numeric->I4 = 2;
	}
	return TinyCLR_Result::Success;
}
1 Like

Thanks you a lot again it was what you said ! :grinning:

1 Like

Hi guys i need a little help about interopt

got from samples Interopt sample and put on the folder to create firmware

i can compile this way but questions is how to include in firmware

code on Device.cpp


#include "Device.h"
#include "../../Drivers/DevicesInterop/GHIElectronics_TinyCLR_InteropUtil.h"

#include "InterTest.h"

/*
  how to call this intertest.h
*/

void STM32F4_Startup_OnSoftResetDevice(const TinyCLR_Api_Manager* apiManager, const TinyCLR_Interop_Manager* interopManager) {
    DevicesInterop_Add(interopManager);
	
	/*
	   how to include into firmware
	 */
	
}

finally i find a way to include on firmware …

but now i face another problem when i try to access from managed code …

An unhandled exception of type ‘System.NotSupportedException’ occurred in TinyCLRApplicationXTEA.exe


interopt = InterTest is inside as showed on pictures

finally mission accomplished

but is very tricky to arrive to implement this one …

now i’m going to make documentation step by step how to (because in forum is a little advanced)…

2 Likes

Glad you have it working but you may want to wait for 2.0 before spending time on documenting your findings.

1 Like