DigitalSignal.ReadPulse never calls OnReadPulseFinished handler

My code was working in 2.2.0.6000. I would call “ReadPulse” and later “Abort”, and get the duration and number of counts from my “OnReadPulseFinished” event handler. If I simply update the NuGet packages and the firmware on my SC20100 from 2.2.0.6000 to 2.2.0.7000 (or later), with my code unchanged, the event handler is never called. This is also true if I don’t call “Abort” and wait until long after the specified number of pulses should have been read.

We will check soon.

I figured out how to build Signals and its dependencies GPIO and Native from the sources on GitHub. According to my debugging, it sets the “pulseReadCallback” field when I call “DigitalSignal.OnReadPulseFinished += DigitalSignal_OnReadPulseFinished”, and it calls “NativeAbort” when I call “DigitalSignal.Abort()”. However, I set a breakpoint on line 168 of “Signals.cs” (the first line of “OnInterruptEventHandler”), and it never hit that breakpoint after my call to Abort. So I think the reading of the pulse is not being aborted properly.

Could you show us simple code that we can see the issue quickly?

I will work on it.

1 Like

I’m having a hard time narrowing down what the problem is. It seems I can get it to work in 2.2.1.2000 by moving the instantiation of the DigitalSignal object later in my code, so that less other code runs between creating the object and calling ReadPulse, Abort, etc. But I have so far been unable to identify the problem using the bisection method. It would help if I could log all of the statements that execute between calling the constructor and ReadPulse. Do you know if something like the “Runtime Flow” extension for Visual Studio would work with TinyCLR OS?

Okay, I narrowed it down to something with garbage collection. Here is a simple code that you can try. With the variable “makeItNotWork” set to false, it will output

Reading pulse
	Aborting
		[X] ReadPulseFinished called
Reading pulse
	Aborting
		[X] ReadPulseFinished called
...

With the variable “makeItNotWork” set to true in 2.2.1.2000, but not 2.2.0.6000, it will output

Reading pulse
	Aborting
Reading pulse
	Aborting
...

Here is the code:

using GHIElectronics.TinyCLR.Devices.Gpio;
using GHIElectronics.TinyCLR.Devices.Signals;
using GHIElectronics.TinyCLR.Native;
using GHIElectronics.TinyCLR.Pins;
using System;
using System.Collections;
using System.Diagnostics;
using System.Threading;

namespace TestReadPulse
{
    internal class Program
    {
        static void Main()
        {
            GpioController gpioController = GpioController.GetDefault();
            GpioPin digitalSignalPin = gpioController.OpenPin(SC20100.Timer.DigitalSignal.Controller5.PA0);
            DigitalSignal digitalSignal = new DigitalSignal(digitalSignalPin);

            ArrayList KBs = new ArrayList();
            while (Memory.ManagedMemory.FreeBytes > 100000)
            {
                byte[] tenKB = new byte[10 * 1024];
                KBs.Add(tenKB);
            }

            bool makeItNotWork = false;
            if (makeItNotWork)
            {
                GC.Collect();
            }

            digitalSignal.OnReadPulseFinished += DigitalSignal_OnReadPulseFinished;

            while (true)
            {
                Debug.WriteLine("Reading pulse");
                digitalSignal.ReadPulse(uint.MaxValue, GpioPinEdge.RisingEdge, false);
                Thread.Sleep(100);

                Debug.WriteLine("\tAborting");
                digitalSignal.Abort();
                Thread.Sleep(100);
            }
        }

        private static void DigitalSignal_OnReadPulseFinished(GHIElectronics.TinyCLR.Devices.Signals.DigitalSignal sender, TimeSpan duration, uint count, GpioPinValue initialState)
        {
            Debug.WriteLine("\t\t[X] ReadPulseFinished called");
        }
    }
}

Thanks for the code, we were able to reproduce it. But seems to us it is still happened on 2.2.0.6000, correct?

I didn’t think it did for me. I can double check it if you want.

No needed. We fixed for next release.

Thanks

1 Like

You’re welcome. Thanks for the quick response and fix!

as I see you know how to build the library, if you want to try the fix before new release, apply our fix below:

fixed #1344 DigitalSignal.ReadPulse never calls OnReadPulseFinished h… · ghi-electronics/TinyCLR-Libraries@e31e86e (github.com)

1 Like