This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

GPIO access call from C#

Hi,

I'm totally new to hardware development on windows CE and my boss want me to control some pins on a beagle bone black. in the schema linked bellow, i need to turn off pin 23 (GPIO 1_17) so i can reset an Xbee Modem. 

http://mkaczanowski.com/wp-content/uploads/2014/03/resizedimage600667-hwio-beaglebone-ports2.png

So far, what I understood of GPIO development is that I need to access it like a file and that this is working based on registry settings. The prefix of my driver is GIO and the port (index) is 1. So I would call it like "GIO1:". 

I'm using OpenNetCF.IO.StreamInterfaceDriver framework to open the file and trying to set On or Off a few pins. 

Here is the part of the code that open and close the file via a constructor and destructor.

public class Driver : StreamInterfaceDriver
{

public Driver() : base ("GIO1:")
{
Open(System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite);
}

~Driver()
{
Close();

}

.....

So far this is working. Because if I try to open "GIO0:" it fail and give me an error.

To understand which command I need to send to DeviceIoControl (i mean the control code), i've taken a look at the file gpio_ioctls.h (in the BSP) and here are the list of the commands : 


#define IOCTL_GPIO_SETBIT \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x0300, METHOD_BUFFERED, FILE_ANY_ACCESS)

#define IOCTL_GPIO_CLRBIT \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x0301, METHOD_BUFFERED, FILE_ANY_ACCESS)

#define IOCTL_GPIO_UPDATEBIT \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x0302, METHOD_BUFFERED, FILE_ANY_ACCESS)

#define IOCTL_GPIO_GETBIT \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x0303, METHOD_BUFFERED, FILE_ANY_ACCESS)

#define IOCTL_GPIO_SETMODE \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x0304, METHOD_BUFFERED, FILE_ANY_ACCESS)

#define IOCTL_GPIO_GETMODE \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x0305, METHOD_BUFFERED, FILE_ANY_ACCESS)

#define IOCTL_GPIO_GETIRQ \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x0306, METHOD_BUFFERED, FILE_ANY_ACCESS)

#define IOCTL_GPIO_SET_POWER_STATE \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x0307, METHOD_BUFFERED, FILE_ANY_ACCESS)

#define IOCTL_GPIO_GET_POWER_STATE \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x0308, METHOD_BUFFERED, FILE_ANY_ACCESS)

#define IOCTL_GPIO_SET_DEBOUNCE_TIME \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x0309, METHOD_BUFFERED, FILE_ANY_ACCESS)

#define IOCTL_GPIO_GET_DEBOUNCE_TIME \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x0310, METHOD_BUFFERED, FILE_ANY_ACCESS)

#define IOCTL_GPIO_INIT_INTERRUPT \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x0311, METHOD_BUFFERED, FILE_ANY_ACCESS)

#define IOCTL_GPIO_ACK_INTERRUPT \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x0312, METHOD_BUFFERED, FILE_ANY_ACCESS)

#define IOCTL_GPIO_DISABLE_INTERRUPT \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x0313, METHOD_BUFFERED, FILE_ANY_ACCESS)

If I understand this C code well : the command i need to use to set a pin ON or OFF is IOCTL_GPIO_SET_BIT, which is the control code 0x0300.

Now i need to send which pin and which state i want the pin to be (on or off). In the file gpio.h, there is an enum with the list of "banks" and GPIO IDs. So I assumed that GPIO 1_17 Is bank 1 and Id 17. Which is GPIO_48. here is a part of that file : 

//------------------------------------------------------------------------------
// Enum : Gpio Numbers
//
typedef enum {
//GPIOBank0
GPIO_0 = 0,
GPIO_1,
GPIO_2,
GPIO_3,
GPIO_4,
GPIO_5,
GPIO_6,
GPIO_7,
GPIO_8,
GPIO_9,
GPIO_10,
GPIO_11,
GPIO_12,
GPIO_13,
GPIO_14,
GPIO_15,
GPIO_16,
GPIO_17,
GPIO_18,
GPIO_19,
GPIO_20,
GPIO_21,
GPIO_22,
GPIO_23,
GPIO_24,
GPIO_25,
GPIO_26,
GPIO_27,
GPIO_28,
GPIO_29,
GPIO_30,
GPIO_31,

//GPIOBank1
GPIO_32,
GPIO_33,
GPIO_34,
GPIO_35,
GPIO_36,
GPIO_37,
GPIO_38,
GPIO_39,
GPIO_40,
GPIO_41,
GPIO_42,
GPIO_43,
GPIO_44,
GPIO_45,
GPIO_46,
GPIO_47,
GPIO_48,
GPIO_49,
GPIO_50,

..............

Now here is my code, and i know it is completely wrong, but this is where i need help... 

public bool SetPinOn() {
if (this.IsOpen)
{

UInt32 call = 0x31; // GPIO 1_17

DeviceIoControl(0x0300, SerializeToByteArray(call));

return true;
}
else
{
return false;
}
}


private static byte[] SerializeToByteArray(object anything)
{
int rawsize = Marshal.SizeOf(anything);
IntPtr buffer = Marshal.AllocHGlobal(rawsize);
Marshal.StructureToPtr(anything, buffer, false);
byte[] rawdatas = new byte[rawsize];
Marshal.Copy(buffer, rawdatas, 0, rawsize);
Marshal.FreeHGlobal(buffer);
return rawdatas;
}

every time i use this code, i get and IOControl Call Fail. 

I don't know how to pass the control code (command 0x0300) what to do first and how to pass the pin# in parameter and how to pass the bit state. :'(

If you have any sample that would work it would be greatly appreciated. Even with pInvoke i've tried but failed. But if you only have samples with pInvoke i would be happy too :)

Thank you so much.