Plain pin control on Cerberus

In the past few days, I tried to control an individual pin on my new Cerberus board. I followed some posts on the forum. I also took a look at the “Plain NETMF on Gadgeteer”. But I was not able to output the signal I want (toggle the pin every 0.5 second). Following is the code that I used to test the individual pin (Socket 6, pin 9). I have no idea of how this can be accomplished. Any help ??

James Tsai


using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Hardware;
//using Microsoft.SPOT.Touch;
using System.IO.Ports;
using System.Text;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using GI = Gadgeteer.Interfaces;
using GHIElectronics.Gadgeteer;


namespace GadgeteerApp1
{
    public partial class Program
    {
        public static OutputPort DataOut { get; set; }
        static OutputPort m_Direction;
        bool f=false;


        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            GT.Socket socket = GT.Socket.GetSocket(6, true, null, null);
            m_Direction = new OutputPort(socket.CpuPins[9], true);

            Gadgeteer.Timer timer1 = new Gadgeteer.Timer(500);
            timer1.Tick += new Gadgeteer.Timer.TickEventHandler(timer_Tick);

            timer1.Start();
            Debug.Print("Program Started");
        }

        void timer_Tick(Gadgeteer.Timer timer)
        {
            if (f == false)
            {
                Debug.Print("LED on");
                f = true;
                m_Direction.Write(true);
                
            }
            else
            {
                Debug.Print("LED off");
                f = false;
                m_Direction.Write(false);
            }
        }
    }
}

You have to declare your timer object at the class scope. The way it is now it is Garbage Collected after ProgramStarted is done.

@ Architect -

Thanks for the quick reply. I forgot to mention that the output window shows the “LED on” and “LED off” alternatively. So, I think the timer works as expected. Anyway, I moved the timer1 declaration to the class level and got the same result. I have noticed that the LED did turn on and off once. After that, the LED remains off.

@ User_11850 - It is possible that it will run for some time, but eventually it is going to be collected. Please show what your code looks like after the change. Another suggestion is to use Timer constructor with the second parameter to set the timer to run continuously. Although it is the default value, but try anyways.

@ User_11850 - It is possible that after the change your new version didn’t deployed properly. I have just tried this on the latest firmware on my Cerberus and it worked as it should:


using Microsoft.SPOT;

using GT = Gadgeteer;
using Microsoft.SPOT.Hardware;

namespace GadgeteerApp2
{
    public partial class Program
    {
        public static OutputPort DataOut { get; set; }
        static OutputPort m_Direction;
        bool f = false;
        Gadgeteer.Timer timer1;

        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            GT.Socket socket = GT.Socket.GetSocket(6, true, null, null);
            m_Direction = new OutputPort(socket.CpuPins[9], true);

            timer1 = new Gadgeteer.Timer(500);
            timer1.Tick += new Gadgeteer.Timer.TickEventHandler(timer_Tick);

            timer1.Start();
            Debug.Print("Program Started");
        }

        void timer_Tick(Gadgeteer.Timer timer)
        {
            f = !f;
            m_Direction.Write(f);
        }
    }
}


@ Architect -

I changed the socket number from 6 to 2. And it works now. I am not sure why it works. It seems that not every socket can be used for this purpose. I also changed the socket number to 3. And I got “An unhandled exception of type ‘System.Exception’ occurred in MicrosoftSPOT.Hardware.dll” and following messages.

James Tsai


Found debugger!

Create TS.

 Loading start at 8073a14, end 809c630

   Assembly: mscorlib (4.2.0.0)     Assembly: Microsoft.SPOT.Native (4.2.0.0)     Assembly: Microsoft.SPOT.Hardware (4.2.0.0)  
   Assembly: Microsoft.SPOT.Graphics (4.2.0.0)     Assembly: Microsoft.SPOT.TinyCore (4.2.0.0)  
   Assembly: Microsoft.SPOT.Hardware.SerialPort (4.2.0.0)     Assembly: Microsoft.SPOT.IO (4.2.0.0)  
   Assembly: System.IO (4.2.0.0)     Assembly: Microsoft.SPOT.Hardware.OneWire (4.2.0.0)  
   Assembly: Microsoft.SPOT.Hardware.Usb (4.2.0.0)     Assembly: Microsoft.SPOT.Hardware.PWM (4.2.0.1)  
   Assembly: Microsoft.SPOT.Net (4.2.0.0)     Assembly: System (4.2.0.0)  Loading Deployment Assemblies.

Attaching deployed file.

   Assembly: GHI.OSHW.Hardware (4.2.4.0)  Attaching deployed file.

   Assembly: Gadgeteer (2.42.0.0)  Attaching deployed file.

   Assembly: System.Http (4.2.0.0)  Attaching deployed file.

   Assembly: Microsoft.SPOT.Net.Security (4.2.0.0)  Attaching deployed file.

   Assembly: GHIElectronics.Gadgeteer.FEZCerberus (1.1.2.0)  Attaching deployed file.

   Assembly: Microsoft.SPOT.Touch (4.2.0.0)  Attaching deployed file.

   Assembly: GTM.GHIElectronics.LED7R (1.1.1.0)  Attaching deployed file.

   Assembly: GadgeteerApp_LED_6_9 (1.0.0.0)  Attaching deployed file.

   Assembly: System.Net.Security (4.2.0.0)  Resolving.

The debugging target runtime is loading the application assemblies and starting execution.
Ready.

'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\mscorlib.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Native.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Hardware.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Graphics.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.TinyCore.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Hardware.SerialPort.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.IO.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\System.IO.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Hardware.OneWire.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Hardware.Usb.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Hardware.PWM.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Net.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\System.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\GHI Electronics\GHI OSHW NETMF v4.2 SDK\Assemblies\le\GHI.OSHW.Hardware.dll'
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Gadgeteer\Core\Assemblies\.NET Micro Framework 4.2\le\Gadgeteer.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Net.Security.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\System.Net.Security.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\System.Http.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\GHI Electronics\GHI .NET Gadgeteer SDK\Mainboards\FEZCerberus\NETMF 4.2\le\GHIElectronics.Gadgeteer.FEZCerberus.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Touch.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files\GHI Electronics\GHI .NET Gadgeteer SDK\Modules\LED7R\NETMF 4.2\le\GTM.GHIElectronics.LED7R.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'd:\mydoc\visual studio 2010\Projects\GadgeteerApp_LED_6_9\GadgeteerApp_LED_6_9\bin\Debug\le\GadgeteerApp_LED_6_9.exe', Symbols loaded.
The thread '<No Name>' (0x2) has exited with code 0 (0x0).
Using mainboard GHI Electronics FEZCerberus version 1.1
A first chance exception of type 'System.Exception' occurred in Microsoft.SPOT.Hardware.dll
An unhandled exception of type 'System.Exception' occurred in Microsoft.SPOT.Hardware.dll

Same exact code just the socket number 3?

@ Architect -

Yes.

I have tried several combinations.


            GT.Socket socket = GT.Socket.GetSocket(6, true, null, null);
            m_Direction = new OutputPort(socket.CpuPins[9], true);

wont work, i.e., nothing came out from the pin.


GT.Socket socket = GT.Socket.GetSocket(2, true, null, null);
            m_Direction = new OutputPort(socket.CpuPins[9], true);

works as expected.


GT.Socket socket = GT.Socket.GetSocket(3, true, null, null);
            m_Direction = new OutputPort(socket.CpuPins[9], true);

popups an error dialog box that shows some error messages.

I’ll check when I get home.

@ Architect -

My fault. I forgot that I have connected an LED7R to socket 3 in Program.gadgeteer. Now it works as expected on sockets 1, 2, 3 and 7. But I still do not get anything out from sockets 4, 5 and 6.

The DeviceInfo from .NET MF Depolyment Tool shows:


DeviceInfo:
  HAL build info: 4.2.0.0, Copyright GHI Electronics, LLC
  OEM Product codes (vendor, model, SKU): 255, 0, 65535
  Serial Numbers (module, system):
    FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
    FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  Solution Build Info: 4.2.4.0, Copyright (C) GHI Electronics, LLC
  AppDomains:
    default, id=1
  Assemblies:
    mscorlib,4.2.0.0
    Microsoft.SPOT.Native,4.2.0.0
    Microsoft.SPOT.Hardware,4.2.0.0
    Microsoft.SPOT.Graphics,4.2.0.0
    Microsoft.SPOT.TinyCore,4.2.0.0
    Microsoft.SPOT.Hardware.SerialPort,4.2.0.0
    Microsoft.SPOT.IO,4.2.0.0
    System.IO,4.2.0.0
    Microsoft.SPOT.Hardware.OneWire,4.2.0.0
    Microsoft.SPOT.Hardware.Usb,4.2.0.0
    Microsoft.SPOT.Hardware.PWM,4.2.0.1
    Microsoft.SPOT.Net,4.2.0.0
    System,4.2.0.0
    GHI.OSHW.Hardware,4.2.4.0
    Gadgeteer,2.42.0.0
    System.Http,4.2.0.0
    Microsoft.SPOT.Net.Security,4.2.0.0
    GHIElectronics.Gadgeteer.FEZCerberus,1.1.2.0
    Microsoft.SPOT.Touch,4.2.0.0
    GadgeteerApp_LED_6_9,1.0.0.0
    System.Net.Security,4.2.0.0

Am I missing anything??

James Tsai

@ User_11850 - Cool! I will check your code on sockets in question on my Cerberus later

@ User_11850 - I have tested all sockets you have mentioned and everything works great and no exceptions on my Cerbebrus (Rev 1.1)

@ Architect -

So, there must be something wrong on my cerberus, .NET setup, etc.
I will start from register level. Or, do you have any suggestion?

Your Visual Studio solution has only one project, right? No any other modules are connected?

@ User_11850 -

I reloaded the bootloader (4.2.4.0) and firmware (non ethernet) and everything worked as expected.
I was using ethernet firmware before this experiment. I will verify this later.

Ok, that explains the difference in our results. Usually if pin is reserved (and it is possible with internet requiring some of the pins) you wouldn’t be able to use that pin.

1 Like

@ Architect -
Thanks for your help.

No problem. :smiley: