Bug in G30 with PWM and serial

I believe I have found a bug in the G30, the PWM outputs stop when receiving serial data. I have created this test setup to illustrate the problem. This shows the G30 dev board connected to a pc via the serial port with a scope on the Serial port receive and the PWM output used in the sample code I have.

This image shows the scope traces of the serial data and the PWM trace stopping.
The code running on the G30 creates a PWM output, creates a serial port and reads but does nothing wit the data. Every 20 seconds it disposes the PWM and creates and output port to hold the PWM line high for a test. It then disposes the output port and recreates the PWM object. `using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using System.IO.Ports;
using System.Threading;
using GHI.Pins;

namespace GHIPWM
{
public class Program
{
private static PWM PWM;
private static OutputPort PWMPin;
private static SerialPort Com;

    public static void Main()
    {
        PWM = new PWM(GHI.Pins.G30.PwmOutput.PA8, 10000, 5000, PWM.ScaleFactor.Microseconds, true);
        PWM.Start();

        Com = new SerialPort("COM2", 2400, Parity.Even);
        Com.Open();
        Com.DataReceived += new SerialDataReceivedEventHandler(Com_DataReceived);
        while (true)
        {
            Thread.Sleep(20000);
            EnablePwm(false);
            // DoTest();
            EnablePwm(true);
        }
    }

    private static void EnablePwm(bool pwm)
    {
        if (pwm)
        {
            PWMPin.Dispose();
            PWMPin = null;
            PWM = new PWM(GHI.Pins.G30.PwmOutput.PA8, 10000, 5000, PWM.ScaleFactor.Microseconds, true);
            PWM.Start();
        }
        else
        {
            PWM.Stop();
            PWM.Dispose();
            PWM = null;
            PWMPin = new OutputPort(GHI.Pins.G30.Gpio.PA8, true);
        }
    }

    private static void Com_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        try
        {
            int Bytes = Com.BytesToRead;
            byte[] Data = new byte[Bytes];
            Com.Read(Data, 0, Bytes);
        }
        catch (Exception ex)
        {
            Debug.Print(ex.Message);
        }
    }
}

}
`

The code running on the PC to send serial data opens com1 and on any key press will stream 10,000 random bytes at the G30 and will quit if Q is pressed.

`using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;

namespace SerialTest
{
class Program
{
private static SerialPort Com;

    static void Main(string[] args)
    {
        Com = new SerialPort("COM1", 2400, Parity.Even);
        Com.Open();
        Com.DataReceived += new SerialDataReceivedEventHandler(Com_DataReceived);
        ConsoleKeyInfo key =Console.ReadKey();
        
        while (key.Key.ToString() != "Q")
        {
            int i;
            int c;
            Random rnd = new Random();
            for (i = 0; i < 10000; i++) { 
             
             c = rnd.Next(255);
           
            Com.Write(Convert.ToChar(c).ToString());
            System.Threading.Thread.Sleep(4);
            }
            key = Console.ReadKey();
        }
        Com.Close();
    }

    private static void Com_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        try
        {
            int Bytes = Com.BytesToRead;
            byte[] Data = new byte[Bytes];
            Com.Read(Data, 0, Bytes);
        }
        catch (Exception ex)
        {
            
        }
    }
}

}
`

Whenever I run these programs and stream data the PWM output stops and remains low until the restarted every 20 seconds and then it will stop again and so on. This code is cut down from a larger project where I had this problem which I mentioned in a thread on the old forum https://www.ghielectronics.com/community/forum/topic?id=24403 To ship the product we had to add an additional microprocessor to do the PWM. I have created these simplified programs so that you can test them to confirm this problem is repeatable and to see if there’s some way to fix it.

Thanks, David.

What happens if you leave the PWM running while you receive serial data?

The PWM is left running while receiving the data.

Why Dispose PWM? What is the benefit or the use of that?

I need to be able to hold the PWM line high during a test and the PWM object doesn’t support that. Although I believe the hardware in the G30 does.

100% duty cycle?

1 Like

That would probably allow me to do the testing but the PWM would still be stopping without me calling Stop. I’m not really looking for a work around here, we’ve already got around it on one job by putting an additional PCB stacked underneath the G30TH to do the PWM. I’m trying to get a confirmation that the PWM does stop without Stop being called and to have that rectified.

So, the PWM is working properly while the serial data is being tranceived? Or is the PWM signal stopping while the Serial Data is being tranceived?

The PWM operates while the data is being received up to a point and then it stops. This condition only manifests when a significant amount of data is being received. This problem only occurs when both things are happening. It’s PWM hardware, it should continue to operate regardless until Stop is called.

Is this on the latest 4.3.8.1 firmware?

Yes it is:
Firmware (TinyCLR) version information:
4.3.8.1 on this computer.
4.3.8.1 on this device.

The Firmware (TinyCLR) is up to date. <<<

Can you test to see if the behavior is the same using COM1 (the USB to serial connector) and PWM8 (the onboard LED)?

I can’t seem to recreate the problem in that configuration.

Then I’d try just to change one of each. First change just to PWM8 and then separately just change to COM1 and see which if any case fails.

With COM1 PWM8 works.
With COM2 PWM8 stops.
With COM1 PWM0 stops.
Have you managed to recreate this?

1 Like

I was able to reproduce it. Unfortunately I don’t have a workaround at this time.

1 Like

Is someone looking into this or is there some kind of timeline for when this might resolved?

@Daithi, Its perhaps a serious problem thats going to take time. Also they are testing a new release, so it make make it out with that one. That release got delayed do to issues.

We do not have a timeline for a fix at this time.

After some testing I am moving forward with a design in which I use COM1 and PWM’s 3 and 4.

Please let me know if you have found that the bug exists in this combination.

Edit:

I had to change my COM port to COM2 for unrelated reasons. The error showed up in a slightly different form: Starting the COM port after the PWM would cause PWM 4 to stop. I could still get my 200 Hz PWM no problem but I had to either start the port first, or restart the PWM afterwards. To be safe I switched to PWM 13 and 14.

There appears to be no interaction between COM2 and PWMs 13 and 14.

1 Like