/// <summary> /// Initializes a new instance of the <see cref="MemoryGpioConnectionDriver"/> class. /// </summary> public MemoryGpioConnectionDriver() { using (var memoryFile = UnixFile.Open("/dev/mem", UnixFileMode.ReadWrite | UnixFileMode.Synchronized)) { gpioAddress = MemoryMap.Create( IntPtr.Zero, Interop.BCM2835_BLOCK_SIZE, MemoryProtection.ReadWrite, MemoryFlags.Shared, memoryFile.Descriptor, GetProcessorBaseAddress(Board.Current.Processor)); } }
/// <summary> /// Initializes a new instance of the <see cref="GpioConnectionDriver"/> class. /// </summary> public GpioConnectionDriver() { using (var memoryFile = UnixFile.Open("/dev/mem", UnixFileMode.ReadWrite | UnixFileMode.Synchronized)) { gpioAddress = MemoryMap.Create( IntPtr.Zero, Interop.BCM2835_BLOCK_SIZE, MemoryProtection.ReadWrite, MemoryFlags.Shared, memoryFile.Descriptor, Board.Current.Model == '2' ? Interop.BCM2836_GPIO_BASE : Interop.BCM2835_GPIO_BASE); } }
/// <summary> /// Initializes a new instance of the <see cref="GpioConnectionDriver" /> class. /// </summary> /// <param name="threadFactory">The thread factory.</param> public GpioConnectionDriver(IThreadFactory threadFactory = null) { this.thread = ThreadFactory.EnsureThreadFactory(threadFactory).Create(); using (var memoryFile = UnixFile.Open("/dev/mem", UnixFileMode.ReadWrite | UnixFileMode.Synchronized)) { this.gpioAddress = MemoryMap.Create( IntPtr.Zero, Interop.Bcm2835BlockSize, MemoryProtection.ReadWrite, MemoryFlags.Shared, memoryFile.Descriptor, GetProcessorBaseAddress(Board.Current.Processor)); } }
private void InitializePoll(ProcessorPin pin) { lock (polls) { PinPoll poll; if (polls.TryGetValue(pin, out poll)) { return; } var pinPoll = new PinPoll(); pinPoll.PollDescriptor = Interop.epoll_create(1); if (pinPoll.PollDescriptor < 0) { throw new IOException("Call to epoll_create(1) API failed with the following return value: " + pinPoll.PollDescriptor); } var valuePath = Path.Combine(gpioPath, string.Format("gpio{0}/value", (int)pin)); pinPoll.FileDescriptor = UnixFile.OpenFileDescriptor(valuePath, UnixFileMode.ReadOnly | UnixFileMode.NonBlocking); var ev = new Interop.epoll_event { events = (Interop.EPOLLIN | Interop.EPOLLET | Interop.EPOLLPRI), data = new Interop.epoll_data { fd = pinPoll.FileDescriptor } }; pinPoll.InEventPtr = Marshal.AllocHGlobal(64); Marshal.StructureToPtr(ev, pinPoll.InEventPtr, false); var controlResult = Interop.epoll_ctl(pinPoll.PollDescriptor, Interop.EPOLL_CTL_ADD, pinPoll.FileDescriptor, pinPoll.InEventPtr); if (controlResult != 0) { throw new IOException("Call to epoll_ctl(EPOLL_CTL_ADD) API failed with the following return value: " + controlResult); } pinPoll.OutEventPtr = Marshal.AllocHGlobal(64); polls[pin] = pinPoll; } }
private void InitializePoll(ProcessorPin pin) { lock (this.pinPolls) { if (this.pinPolls.TryGetValue(pin, out var poll)) { return; } var pinPoll = default(PinPoll); pinPoll.PollDescriptor = Interop.Epoll_create(1); if (pinPoll.PollDescriptor < 0) { throw new IOException("Call to epoll_create(1) API failed with the following return value: " + pinPoll.PollDescriptor); } var valuePath = Path.Combine(GpioPath, string.Format("gpio{0}/value", (int)pin)); pinPoll.FileDescriptor = UnixFile.OpenFileDescriptor(valuePath, UnixFileMode.ReadOnly | UnixFileMode.NonBlocking); var ev = new Interop.EpollEvent { Events = Interop.Epollin | Interop.Epollet | Interop.Epollpri, Data = new Interop.EpollData { Fd = pinPoll.FileDescriptor }, }; pinPoll.InEventPtr = Marshal.AllocHGlobal(64); Marshal.StructureToPtr(ev, pinPoll.InEventPtr, false); var controlResult = Interop.Epoll_ctl(pinPoll.PollDescriptor, Interop.EpollCtlAdd, pinPoll.FileDescriptor, pinPoll.InEventPtr); if (controlResult != 0) { throw new IOException("Call to epoll_ctl(EPOLL_CTL_ADD) API failed with the following return value: " + controlResult); } pinPoll.OutEventPtr = Marshal.AllocHGlobal(64); this.pinPolls[pin] = pinPoll; } }
private void UninitializePoll(ProcessorPin pin) { if (pinPolls.TryGetValue(pin, out PinPoll poll)) { pinPolls.Remove(pin); var controlResult = poll.InEventPtr != IntPtr.Zero ? OP.epoll_ctl(poll.PollDescriptor, OP.EPOLL_CTL_DEL, poll.FileDescriptor, poll.InEventPtr) : 0; Marshal.FreeHGlobal(poll.InEventPtr); Marshal.FreeHGlobal(poll.OutEventPtr); UnixFile.CloseFileDescriptor(poll.PollDescriptor); UnixFile.CloseFileDescriptor(poll.FileDescriptor); if (controlResult != 0) { throw new IOException("Call to epoll_ctl(EPOLL_CTL_DEL) API failed with the following return value: " + controlResult); } } }