Doh! Use the right constants when you talk to a Netduino

(c) Bertrand Le Roy 2010When programming a Netduino microcontroller, you are going to use constants from the SecretLabs.NETMF.Hardware.Netduino namespace. In particular, there are constants defined for each of the microcontroller’s output and input ports, enabling you to write for example:

var button = new InputPort(
Pins.ONBOARD_SW1,
false,
Port.ResistorMode.Disabled);

The type of those constants is Microsoft.Spot.Hardware.Cpu.Pin. Here is the definition of that enumeration:

public enum Pin {
    GPIO_NONE = -1,
    GPIO_Pin0 = 0,
    GPIO_Pin1 = 1,
    GPIO_Pin2 = 2,
    GPIO_Pin3 = 3,
    GPIO_Pin4 = 4,
    GPIO_Pin5 = 5,
    GPIO_Pin6 = 6,
    GPIO_Pin7 = 7,
    GPIO_Pin8 = 8,
    GPIO_Pin9 = 9,
    GPIO_Pin10 = 10,
    GPIO_Pin11 = 11,
    GPIO_Pin12 = 12,
    GPIO_Pin13 = 13,
    GPIO_Pin14 = 14,
    GPIO_Pin15 = 15,
}

Now it’s easy to confuse those constants from the Microsoft.Spot.Hardware namespace for the following, which are defined in the Netduino library:

namespace SecretLabs.NETMF.Hardware.Netduino {
    public static class Pins {
        public const Cpu.Pin GPIO_PIN_D2 = Pin.GPIO_Pin0;
        public const Cpu.Pin GPIO_PIN_D3 = Pin.GPIO_Pin1;
        public const Cpu.Pin GPIO_PIN_D7 = Pin.GPIO_Pin3;
        public const Cpu.Pin GPIO_PIN_D8 = Pin.GPIO_Pin4;
        public const Cpu.Pin GPIO_PIN_A4 = Pin.GPIO_Pin10;
        public const Cpu.Pin GPIO_PIN_A5 = Pin.GPIO_Pin11;
        public const Cpu.Pin GPIO_PIN_D4 = Pin.GPIO_Pin12;
        public const Cpu.Pin GPIO_PIN_D12 = 16;
        public const Cpu.Pin GPIO_PIN_D11 = 17;
        public const Cpu.Pin GPIO_PIN_D13 = 18;
        public const Cpu.Pin GPIO_PIN_D0 = 27;
        public const Cpu.Pin GPIO_PIN_D1 = 28;
        public const Cpu.Pin ONBOARD_SW1 = 29;
        public const Cpu.Pin GPIO_PIN_D5 = 51;
        public const Cpu.Pin GPIO_PIN_D6 = 52;
        public const Cpu.Pin GPIO_PIN_D9 = 53;
        public const Cpu.Pin GPIO_PIN_D10 = 54;
        public const Cpu.Pin ONBOARD_LED = 55;
        public const Cpu.Pin GPIO_PIN_A0 = 59;
        public const Cpu.Pin GPIO_PIN_A1 = 60;
        public const Cpu.Pin GPIO_PIN_A2 = 61;
        public const Cpu.Pin GPIO_PIN_A3 = 62;
        public const Cpu.Pin GPIO_NONE = Pin.GPIO_NONE;
    }
}

See the big difference? If you use the first ones, thinking that the numbers are mapped to your ports, then you are going to get the signal for D0 on D2, D1 on D3 and so on.

As the type is Cpu.Pin, the confusion is very easy and you won’t even see the real constants you should be using if you didn’t include the SecretLabs.NETMF.Hardware.Netduino namespace.

I’ve seen it happen, and Fabien and I scratched our heads for a while before we understood that basic mistake.

Moral of the story:

Include the SecretLabs.NETMF.Hardware.Netduino namespace in all the classes you write against the Netduino, always use the Pins.* constants from that namespace, and never use the ones from Microsoft.SPOT.Hardware.

Checking that the pin number is not just a number but rather a letter (A or D for analog or digital) and a number is a good way to make sure you have the right ones.

No Comments