iMX6 board - ADC problems with use of latest CE libraries and C# .Net wrapper

Hello support!

Using iMX6 board and writing code for ADC in C#. Libraries in use are latest CE libraries (toradexcelibraries_v1_6_3123)

The problem with ADC is:

ADC Init and configuration pass okay (TRUE) however actual Read_ADC request hangs the code without throwing any error.

Please see C# code below with comments

        //Init ADC
        ADC_handle = adc.Adc_Init("ADC1");
        ResultBox.Text += "ADC Init Result: " + ADC_handle.ToString() + "\r\n";

At this point we are good and get actual ADC_handler.

        //Pass init strings
        bool set_conf1 = adc.Adc_SetConfigInt(ADC_handle, "BitResolution", 12, TdxCommon.ParamStorageType.StoreVolatile);
        bool set_conf2 = adc.Adc_SetConfigInt(ADC_handle, "AvgSamples", 8, TdxCommon.ParamStorageType.StoreVolatile);

        //Show result
        ResultBox.Text += "Config results: " + set_conf1.ToString() + "  " + set_conf2.ToString() + "\r\n";

We good here as well and also get both TRUE

        //Assign Colibri pin (pin8)
        gpio.uIo adc_pin;
        adc_pin.GenericDefinition = 0x00200008; //Generic pin assignment and type definition (MSB:type LSB:pin number)

        bool conf_res = adc.Adc_SetConfigInt(ADC_handle, "ioAdc", adc_pin.GenericDefinition, TdxCommon.ParamStorageType.StoreVolatile);

        ResultBox.Text += "ADC Config result: " + conf_res.ToString() + "\r\n";

Probably this pin assignment is not required (as per your demo code) and we tried both ways with and without pin assignment code above. When pin assignment code is in use we have TRUE as a return.

Now we open ADC

        //Open ADC
        bool result = adc.Adc_Open(ADC_handle);
        ResultBox.Text += "ADC Open result: " + result.ToString() + "\r\n";

Here we get TRUE

Now wait some time to be safe.

        //Delay some time

Now performing actual reading of ADC value.
Here our code hangs. We have no more results or information.
Code stays at Adc_Read function and waiting infinite.

        //Read ADC
        UInt32 read_res = adc.Adc_Read(ADC_handle, ADC_result, 4);

We never get here.

        //Show result
        ResultBox.Text += "Pre-result: " + read_res.ToString() + "\r\n";

        //long longValue = Marshal.ReadInt64(ADC_result);

        //Show result
        ResultBox.Text += "ADC Read result: " + ADC_result.ToString() + "\r\n";

        //Close ADC
        bool adc_close1 = adc.Adc_Close(ADC_handle);

        //Deinit ADC
        bool adc_close2 = adc.Adc_Deinit(ADC_handle);

        //Show result
        ResultBox.Text += "Closure and DeInit: " + adc_close1.ToString() + "  " + adc_close2.ToString() + "\r\n";

Please help to resolve this issue. So far it looks like we did everything as per your example and using your wrapper however for some reason there is no result which should be at the end.
Did we do something wrong ?

Thank you,

Correct code for ADC return value conversion in C# would be:

UInt32 microVolts;

//Allocate return value
IntPtr microVoltsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(microVolts));
Marshal.StructureToPtr(microVolts, microVoltsPtr, true);

bytesRead = adc.Adc_Read(ADC_handle, microVoltsPtr, 4);
ResultBox.Text += "Adc_Read returned bytes: " + bytesRead.ToString() + "\r\n";

adcValue = (UInt32)Marshal.ReadInt32(microVoltsPtr);
ResultBox.Text += "adcValue(mV): " + adcValue.ToString() + "\r\n";

Everything works fine now. The problem was at our side.
Since demo code is given in C where data return is different we were missing (Marshal.StructureToPtr)
After fixing the code ADC reading works perfectly !

The only thing noticed is if at the end of ADC read we do:

bool adc_close2 = adc.Adc_Deinit(ADC_handle);

it will deactivate touch controller.

Thank you,

Thank you for sharing your solution, we appreciate that.
Please notice also that on i.MX6 the ADC pins are connected to the external touch controller, so no direct configuration of IOs is required. On Vybrid those pins are connected directly to the SOC, but GPIO-low level configuration is anyway done inside the library.