Reading OutputPort in use by Fez Spider

Have you read Chapter 10 in the user manual that @ Mike pointed you to?

Read this chapter which will give you an understanding of how to work with the GPIO. Your pin is on port 1 as designated by P1.31

Just to be sure, here is the manual you should be looking at
http://www.nxp.com/documents/user_manual/UM10237.pdf

If you do not come right then I will give you a sample. But I believe that Mike’s approach here is to help you to help yourself, once you work this out for yourself you will have a much better understanding of how to understand and interpret the data sheets and user manuals. It take some practice :slight_smile:

I didn’t knew what that 1 is for :smiley: thank you for that tip! I’ll post the new results asap :wink: thanks

before i give up and ask for the easy way, can you tell me if there is a fast way to convert an int to binary in netmf ? don’t seem to find nothing in the library …

@ thisway.ro - Why do you need to convert an int to binary? Do you just want a visual representation for your own purposes?

Remember, an int is really just a binary number to the MCU shown as a nice decimal number for us humans. There is no need to do any conversion other then for presentation purposes.

But, to answer your question. There is no function that will convert an integer to a string of 1s and 0s, you will need to do this yourself.

NB: You can use the windows Calc.exe and change to “Programmer View” to see how a number is represented in different bases. You can use this to quickly convert between binary->decimal, hexadecimal or even octal.

@ taylorza - yes, only for reading the information as a 1:1 digit to byte representation. much easier to understand than a int summing the binary components.

there are many converters online also, not a tool is the missing link, just the netmf lacking it :slight_smile: not a real problem anyway …

i’ve tried reading lots of addresses before enabling the debug led via the OutputPort.write() method and did not find a change in values. in fact, only one address even returns something different then 0. the address is 0xE002C04C wich is corresponding to 5.16 Pin Mode select register 3 (PINMODE3 - 0xE002 C04C). according to the manual: “This register controls pull-up/pull-down resistor configuration for PORT1 pins 16 to 31”.

am i barking at the wrong tree ? how wrong ? :))

You can also mask out bits that you’re not interested in and simplify the number you’re shown to help your understanding. You can then also shift bits into locations that they represent exactly what you want - André has shown you some of those in his line of code, but just go research bitwise manipulation as it is another good concept to understand.

@ Brett - shifting bits to right or left is not a hard concept to grasp - not new to me, but let’s just say that netmf is very new and limited language for me.

@ taylorza - i still haven’t found the proper registry to read from. it should change when i turn the led on and off, right ? it makes no sense otherwise …

can you please give me the right answer :)) ?

thanks

@ thisway.ro - I will connect my spider up and see if I can get something going for you.

much appreciated :slight_smile:

@ thisway.ro - Sorry for the delay.

So here is an example that works. I control two pins pin 0.6 which is pin 3 on socket 4 of the spider mainboard and then pin 1.31 which is the debug LED. I cannot confirm that the code for the debug led is correct since my spider debug led appears not to work even Mainboard.SetDebugLED() does not work. Which is why connected a led to P0.6 to confirm the code.


Register FIO0DIR = new Register(0x3fffc000);
Register FIO0PIN = new Register(0x3fffc014);
Register FIO0SET = new Register(0x3fffc018);
Register FIO0CLR = new Register(0x3fffc01C);

Register FIO1DIR = new Register(0x3fffc020);
Register FIO1PIN = new Register(0x3fffc034);
Register FIO1SET = new Register(0x3fffc038);
Register FIO1CLR = new Register(0x3fffc03C);

Debug.Print("PIN 0.6 (Socket 4 Pin 3)");
// Set the pin direction to output
FIO0DIR.SetBits(1u << 6);
// Read the state 
Debug.Print(((FIO0DIR.Read() & (1u << 6)) != 0).ToString());

// Set the pin low
FIO0CLR.SetBits(1u << 6);
// Read the state 
Debug.Print(((FIO0PIN.Read() & (1u << 6)) != 0).ToString());

// Set the pin high
FIO0SET.SetBits(1u << 6);
// Read the state 
Debug.Print(((FIO0PIN.Read() & (1u << 6)) != 0).ToString());

Debug.Print("PIN 1.31 (Debug LED)");

// Set the pin direction to output
FIO1DIR.SetBits(1u << 31);
// Read the state 
Debug.Print(((FIO1DIR.Read() & (1u << 31)) != 0).ToString());

// Set the pin low
FIO1CLR.SetBits(1u << 16);
// Read the state 
Debug.Print(((FIO1PIN.Read() & (1u << 31)) != 0).ToString());
      
// Set the pin hight
FIO1SET.SetBits(1u << 31); // Set pin 31      
// Read the state 
Debug.Print(((FIO1PIN.Read() & (1u << 31)) != 0).ToString());

Update: I connected a LED to the spider on the debug LED and can confirm that the code for P1.31 works as expected as well.

Now I wonder what happened to my spiders debug LED, I have never actually switched it on so I do not even know if it ever worked…

@ taylorza - the example is great, there are many things you did there that i did not try. i hadn’t had time to play with the code so far, busy with holidays but just gave it a try and it does work :smiley: thanks again

i’ll post tomorrow i think the outcome of this little learning experiment

with a tiny delay I’ve completed the code that gets by the CLR_E_PIN_UNAVAILABLE System.Exception you get by using the OutputPort class on the DebugLED pin 1.31 | 47 on the Fez Spider mainboard :slight_smile:

here it is:


using System;
using System.Threading;
using Microsoft.SPOT;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.Hardware.LowLevel;
using Gadgeteer.Modules.GHIElectronics;

namespace DebugLED
{
    public partial class Program
    { 
        const int FIO1DIR_ADDRESS = 0x3FFFC000;
        const int FIO1PIN_ADDRESS = 0x3FFFC034;
        const int FIO1SET_ADDRESS = 0x3FFFC038;
        const int FIO1CLR_ADDRESS = 0x3FFFC03C;

        void ProgramStarted()
        {
            // turn the DebugLED ON after boot
            Mainboard.SetDebugLED(true);
            Debug.Print("The DebugLED should be ON now.");

            // create timer to blink the DebugLED
            GT.Timer timer = new GT.Timer(1000);
            timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);

            // wait a few seconds for my eyes to get time to focus on the LED
            Thread.Sleep(3000);
            Debug.Print("Time out! Start blinking!");
            timer.Start();
        }

        void timer_Tick(GT.Timer timer)
        {
            Debug.Print("Debug LED is now " + (toggleDebugLED() ? "ON" : "OFF"));
        }

        bool toggleDebugLED()
        {
            Register FIO1PIN = new Register(FIO1PIN_ADDRESS);
            Register FIO1SET = new Register(FIO1SET_ADDRESS);
            Register FIO1CLR = new Register(FIO1CLR_ADDRESS);

            if ((FIO1PIN.Read() & (1u << 31)) == 0) // if the pin is low - turn LED on
            {
                FIO1SET.SetBits(1u << 31);
            }
            else // the pin is high - turn LED off
            {
                FIO1CLR.SetBits(1u << 31);
            }

            // give feedback as the new LED status on/off
            return (FIO1PIN.Read() & (1u << 31)) != 0;
        }
    }
}

For easier reference and for sharing purposes I’ve created an entry in the Codeshare with the code listed here.

http://www.tinyclr.com/codeshare/entry/623

Thanks again for your help :slight_smile: