using System; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.InteropServices; using System.Threading; namespace ConsoleApplication9 { internal class Program { private static void Main() { int g, rep; // Set up gpi pointer for direct register access // Switch GPIO 7..11 to output mode /************************************************************************\ * You are about to change the GPIO settings of your computer. * * Mess this up and it will stop working! * * It might be a good idea to 'sync' before running this program * * so at least you still have your code changes written to the SD-card! * \************************************************************************/ // Set GPIO pins 7-11 to output for (g = 7; g <= 11; g++) { Console.WriteLine("Setting up pi {0}",g); IO.INP_GPIO((uint)g); // must use INP_GPIO before we can use OUT_GPIO IO.OUT_GPIO((uint)g); } for (rep = 0; rep < 10; rep++) { Console.WriteLine("Cycle {0} of 10",rep); for (g = 7; g <= 11; g++) { Console.WriteLine("Setting up pin {0}",g); IO.GPIO_SET = (uint)(1 << g); Thread.Sleep(333); } for (g = 7; g <= 11; g++) { Console.WriteLine("Resetting pin {0}",g); IO.GPIO_CLR = (uint)(1 << g); Thread.Sleep(333); } } //dont try it /*Console.WriteLine("Testing max frequency"); var sw = new Stopwatch(); sw.Start(); int cyclesCount = 100000; for(int i=0;i /// sets bits which are 1 ignores bits which are 0 /// public static uint GPIO_SET { set { *(gpio + 7) = value; } } /// /// clears bits which are 1 ignores bits which are 0 /// public static uint GPIO_CLR { set { *(gpio + 10) = value; } } /// /// Get data /// public static uint GPIO_IN0 { get { return *(gpio + 13); } } static IO() { setup_io(); } /// /// Set up a memory regions to access GPIO /// private static void setup_io() { /* open /dev/mem */ if ((mem_fd = open("/dev/mem", O_RDWR | O_SYNC)) < 0) { Console.WriteLine("can't open /dev/mem"); throw new Exception(); } /* mmap GPIO */ // Allocate MAP block if ((gpio_mem = malloc(BLOCK_SIZE + (PAGE_SIZE - 1))) == NULL) { Console.WriteLine("allocation error"); throw new Exception(); } // Make sure pointer is on 4K boundary if ((ulong)gpio_mem % PAGE_SIZE != 0) gpio_mem += PAGE_SIZE - ((ulong)gpio_mem % PAGE_SIZE); // Now map it gpio_map = (byte*)mmap( gpio_mem, BLOCK_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, mem_fd, GPIO_BASE ); if ((long)gpio_map < 0) { Console.WriteLine("mmap error {0}", (int)gpio_map); throw new Exception(); } // Always use volatile pointer! gpio = (uint*)gpio_map; } // setup_io [DllImport("libc.so.6")] static extern void* mmap(void* addr, uint length, int prot, int flags, int fd, uint offset); [DllImport("libc.so.6")] static extern int open(string file, int mode /*, int permissions */); [DllImport("libc.so.6")] static extern byte* malloc(uint size); } }