Problem connecting to the G120E

Hello
I have encountered a very strange problem today. I am using the G120E on my custom board, its purpose is to switch some relays, log data to server and to sd card and to measure current via ADCs and Hall effect current sensors, plus control of an inverter via PWMs.

The problem occured when i was programming the current measurement. I initialized all PWMs and Analog channels, just in case of any signal noise, and proceeded with sampling of the measured analog signal and some calculations. I then wanted to set a PWM dutycycle to change according to the RMS of the measured signal, so I initialized a PWM according to the sample code. Upon building the code and deploying, I got an deployment error that the device is unable to connect and since then, the G120E keeps rebooting every ± 2 minutes, unable to reset via FEZconfig nor MFDeploy tool.

using System;
using System.Net;
using System.IO;
using System.Threading;
using System.Resources;
using System.Text;
using System.Collections;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Net.NetworkInformation;
using Microsoft.SPOT.IO;

using GHI.IO;
using GHI.IO.Storage;
using GHI.Pins;
using GHI.Networking;
using GHI.Processor;

namespace AnalogInputTest
{
    public class Program
    {

        // possible problematic line??????????????????????
        public static PWM pwm3 = new PWM((Cpu.PWMChannel)G120E.Gpio.P3_27, 50, 0.1, false);

        public static void Main()
        {

            AnalogInput ain0 = new AnalogInput((Cpu.AnalogChannel)Cpu.AnalogChannel.ANALOG_0);
            AnalogInput ain1 = new AnalogInput((Cpu.AnalogChannel)Cpu.AnalogChannel.ANALOG_1);
            AnalogInput ain2 = new AnalogInput((Cpu.AnalogChannel)Cpu.AnalogChannel.ANALOG_2);
            AnalogInput ain3 = new AnalogInput((Cpu.AnalogChannel)Cpu.AnalogChannel.ANALOG_3);
            AnalogInput ain4 = new AnalogInput((Cpu.AnalogChannel)Cpu.AnalogChannel.ANALOG_4);
            AnalogInput ain5 = new AnalogInput((Cpu.AnalogChannel)Cpu.AnalogChannel.ANALOG_5);
            AnalogInput ain6 = new AnalogInput((Cpu.AnalogChannel)Cpu.AnalogChannel.ANALOG_6);
            AnalogInput ain7 = new AnalogInput((Cpu.AnalogChannel)Cpu.AnalogChannel.ANALOG_7);

            double[] data1 = new double[200];
            double[] data2 = new double[200];
            double[] data3 = new double[200];

            double[][] waves = new double[5][];
            double[] power = new double[20];
            double[] rms_array = new double[20];

            waves[0] = new double[20];
            waves[1] = new double[20];
            waves[2] = new double[20];
            waves[3] = new double[20];
            waves[4] = new double[20];

            int zero = 0;
            int it = 1;
            int is_wave = 0;
            int wave_no = 1;
            int sample = 2;
            int wave_count = 5;
            double sum = 0;
            double integ = 0;
            double average = 0;
            double voltage_meas;
            double current_meas;
            double current_true;
            int resistance_meas = 68;
            int period;
            double pwm_max = 0.95;
            double pwm_min = 0.05;
            double max_current = 10;
            double duty_cycle = 0.5;

            Thread.Sleep(2000);

            pwm3.Start();

            while (true)
            {
                zero = 0;
                it = 1;
                is_wave = 0;
                wave_no = 1;
                sample = 0;
                wave_count = 5;
                sum = 0;
                integ = 0;
                average = 0;
                period = 0;

                // acquire data from ADC in Volts
                for (int i = 0; i < data1.Length; i++)
                {
                    data1[i] = ain1.Read()*3.3;
                    data2[i] = ain2.Read()*3.3;
                    data3[i] = ain3.Read()*3.3;
                //    Thread.Sleep(1);
                }

                // find beginning of first period
                while (zero == 0)
                {
                    if (data1[it] == 0)
                    {
                        zero = it;
                    }
                    else
                    {
                        it++;
                    }
                }

               // it++;

                //extraction of 5 half-periods from measured data
                while (wave_no < wave_count)
                {
                    if (data1[it] == 0)
                    {
                        is_wave = 0;
                        if (data1[it - 1] != 0)
                        {
                            wave_no++;
                            if (period < sample)
                            {
                                period = sample;
                            }
                            sample = 0;
                        }
                    }
                    else
                    {
                        is_wave = 1;
                        waves[wave_no][sample] = data1[it];
                        sample = sample + 1;
                    }
                    it++;
                }

                

                //calculation of RMS of 5 periods of sine wave
                for (int i = 0; i < wave_count; i++)
                {
                    for (int j = 0; j < 20; j++)
			        {
                        power[j] = waves[i][j] * waves[i][j];
                        sum = sum + power[j];
			        }

                    // if too little samples (no current flowing, only some background noise) then dont calculate
                    if (period < 5)
                    {
                        sum = 0;
                    }

                    integ = sum/period;
                    rms_array[i] = System.Math.Sqrt(integ);
                }

                //sum of 5 waves
                for (int i = 0; i < wave_count; i++)
                {
                    average = average + rms_array[i];
                }

                //average value of RMS of 5 half-waves
                average = average / wave_count;

                voltage_meas = average;
                current_meas = voltage_meas / resistance_meas;
                current_true = current_meas * 1000;

                //pwm duty cycle calculation
                duty_cycle = (current_true / max_current) * (pwm_max - pwm_min) + pwm_min;
                pwm3.DutyCycle = duty_cycle;

                Debug.Print("Voltage: " + voltage_meas +" V, Current: " + current_meas +" A, True current: " + current_true +" A");
                Thread.Sleep(500);

            }



            
        }
    }
}

Everything went well until I added the line of code, which initialized the PWM object ‘‘pwm3’’. Would anyone have any suggestions on how to solve the problem? When the board is powered, it is unable to connect to the host pc and reboots every ± 2 minutes. I think it could be resolved, if I could erase the managed application, but I dont know how to do that, since the G120E is unable to connect to the host pc.

@ andre.m - Thanks for the prompt reply. I feel that the choice of PWM is not the problem, because I’m using the G120E, which (according to datasheet pinout) has all PWMs on their own pins except for PWM2.

As for the Bootloader mode, I will try the reset as mentioned in the FEZConfig, however I am not very experienced with hardware, so this may be a silly question: assuming that the reset procedure is to set LDR0/LDR1 pins HIGH, then reset, then set LDR0/LDR1 LOW, can I basically connect the pins to a supply of 3.3 V for HIGH (e.g. from a GPIO on the module or on another module) directly without any resistor, reboot the module and then disconnect the LDR0/LDR1 pins from the 3.3V source? I’d be grateful for any reply.

@ nezvek_fel - In the code you provide, you initialize the PWM object using “new PWM((Cpu.PWMChannel)G120E.Gpio.P3_27, 50, 0.1, false)”. That’s incorrect and could be causing the reboot issue you have. You want “new PWM(G120E.PwmOutput.P3_27, 50, 0.1, false)”.

You likely don’t need to reflash the whole firmware. Just connect LDR1 to ground, reset the board, then disconnect LDR1 from ground after a moment. The board should enter TinyBooter mode. Then using MFDeploy or FEZ Config, you can erase the application, reboot the board, and redeploy your fixed application.

@ John - Thank you for your support, I managed to erase the incorrect code in the TinyBooter mode and now it works fine.