예제 #1
0
        protected bool BindSocket(GoSocket socket, System.Guid uniqueId)
        {
            // make sure that the socket is not in use.
            if (socket.BoundToModule)
            {
                return(false);
            }

            // if we were passed a uniqueId, make sure that the module matches.
            if (!uniqueId.Equals(Guid.Empty))
            {
                // validate the module's GUID
                if (GoBus.GoHub.RootHub.GetModuleUniqueId(socket).Equals(uniqueId) == false)
                {
                    return(false);
                }
            }

            GoBus.GoHub.RootHub.SetSocketLedState((int)socket, true);
            socket.BoundToModule = true;
            _socket        = socket;
            _boundToSocket = true;

            return(true);
        }
예제 #2
0
 public void Initialize(GoBus.GoSocket socket)
 {
     Cpu.Pin        chipSelect;
     Cpu.Pin        gpio;
     SPI.SPI_module spi;
     socket.GetPhysicalResources(out gpio, out spi, out chipSelect);
     Spi = new SPI(new SPI.Configuration(chipSelect, false, 1, 0, false, false, 1000, spi));
     BindSocket(socket, new Guid(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }));
 }
예제 #3
0
 protected void UnbindSocket()
 {
     if (_socket != null)
     {
         GoBus.GoHub.RootHub.SetSocketLedState((int)_socket, false);
         _socket.BoundToModule = false;
     }
     _socket        = null;
     _boundToSocket = false;
 }
예제 #4
0
        public void Initialize(GoBus.GoSocket socket, NativeEventHandler irqHandler, uint speedKHz = 2500)
        {
            if (speedKHz < 500 || speedKHz > 10000)
            {
                throw new ArgumentOutOfRangeException("speedKHz");
            }
            if (irqHandler == null)
            {
                throw new ArgumentNullException("irqHandler");
            }
            Cpu.Pin        chipSelect;
            Cpu.Pin        gpio;
            SPI.SPI_module spiModule;
            socket.GetPhysicalResources(out gpio, out spiModule, out chipSelect);

            SetSocketPowerState(false);
            Thread.Sleep(2000);
            SetSocketPowerState(true);
            Thread.Sleep(250);

            Spi = new SPI(new SPI.Configuration(
                              SPI_mod: spiModule,
                              ChipSelect_Port: chipSelect,
                              ChipSelect_ActiveState: false,
                              ChipSelect_SetupTime: 0,
                              ChipSelect_HoldTime: 0,
                              Clock_IdleState: false,
                              Clock_Edge: false,
                              Clock_RateKHz: speedKHz));

            BindSocket(socket);

            while (true)
            {
                Spi.WriteRead(spiTxBuffer, spiRxBuffer);
                if (spiRxBuffer[1] == '[' &&
                    spiRxBuffer[2] == 'n' &&
                    spiRxBuffer[3] == 'w' &&
                    spiRxBuffer[4] == 'a' &&
                    spiRxBuffer[5] == 'z' &&
                    spiRxBuffer[6] == 'e' &&
                    spiRxBuffer[7] == 't' &&
                    spiRxBuffer[8] == '.' &&
                    spiRxBuffer[9] == 'j' &&
                    spiRxBuffer[10] == 's' &&
                    spiRxBuffer[11] == 't' &&
                    spiRxBuffer[12] == 'k')
                {
                    break;
                }
                Thread.Sleep(100);
            }
            Irq              = new InterruptPort(gpio, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
            Irq.OnInterrupt += new NativeEventHandler(irqHandler);
        }
예제 #5
0
파일: SD.cs 프로젝트: Larouex/herdrider
 public void Initialize(GoSocket socket, string mountPoint = "SD")
 {
     _mountPoint = mountPoint;
     SPI.SPI_module sdCardSpi;
     Cpu.Pin sdCardChipSelect;
     Cpu.Pin sdCardGPIO;
     socket.GetPhysicalResources(out sdCardGPIO, out sdCardSpi, out sdCardChipSelect);
     if (!BindSocket(socket)) throw new ApplicationException("socket already bound");
     SetSocketPowerState(true);
     StorageDevice.MountSD(_mountPoint, sdCardSpi, sdCardChipSelect);
 }
예제 #6
0
 public void Initialize(GoBus.GoSocket socket)
 {
     SPI.SPI_module spi;
     Cpu.Pin        chipSelect;
     Cpu.Pin        gpio;
     socket.GetPhysicalResources(out gpio, out spi, out chipSelect);
     if (!BindSocket(socket, new Guid(new byte[] { 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 })))
     {
         throw new ApplicationException("not a relay");
     }
     SetSocketPowerState(true);
     _relay = new OutputPort(gpio, false);
 }
예제 #7
0
        protected bool BindSocket(GoSocket socket, System.Guid uniqueId)
        {
            // make sure that the socket is not in use.
            if (socket.BoundToModule)
                return false;

            // if we were passed a uniqueId, make sure that the module matches.
            if (!uniqueId.Equals(Guid.Empty))
            {
                // validate the module's GUID
                if (GoBus.GoHub.RootHub.GetModuleUniqueId(socket).Equals(uniqueId) == false)
                    return false;
            }

            GoBus.GoHub.RootHub.SetSocketLedState((int)socket, true);
            socket.BoundToModule = true;
            _socket = socket;
            _boundToSocket = true;

            return true;
        }
예제 #8
0
        internal GoHub()
        {
            // initialize our sockets
            _sockets    = new GoSocket[_socketCount];
            _socketLeds = new OutputPort[_socketCount];
            for (int iSocket = 0; iSocket < _socketCount; iSocket++)
            {
                _sockets[iSocket]    = new GoSocket(iSocket + 1);
                _socketLeds[iSocket] = new OutputPort(GetSocketLedPin(iSocket + 1), false);
                _sockets[iSocket].SetHub(this);
                _sockets[iSocket].SetPowerState(true);
            }

            // TODO: do this in native code during clr startup, to ensure the pullup state.
            // put uart RX pins in input, pull-up mode by creating and then disposing of them.
            InputPort uart1rx = new InputPort((Cpu.Pin) 0x01, false, Port.ResistorMode.PullUp);
            InputPort uart2rx = new InputPort((Cpu.Pin) 0x03, false, Port.ResistorMode.PullUp);

            uart1rx.Dispose();
            uart2rx.Dispose();

            // enable our power output
            _powerEnable = new OutputPort((Cpu.Pin) 0x0A, false);
        }
예제 #9
0
 protected bool BindSocket(GoSocket socket)
 {
     return(BindSocket(socket, Guid.Empty));
 }
예제 #10
0
 protected void UnbindSocket()
 {
     if (_socket != null)
     {
         GoBus.GoHub.RootHub.SetSocketLedState((int)_socket, false);
         _socket.BoundToModule = false;
     }
     _socket = null;
     _boundToSocket = false;
 }
예제 #11
0
 protected bool BindSocket(GoSocket socket)
 {
     return BindSocket(socket, Guid.Empty);
 }
예제 #12
0
        internal Guid GetModuleUniqueId(GoSocket socket)
        {
            int frameLength = 1 + 16 + 1;

            byte[] writeFrameBuffer = new byte[frameLength];
            byte[] readFrameBuffer  = new byte[frameLength];

            // get socket's physical pins and SPI bus
            Cpu.Pin        socketGpioPin;
            SPI.SPI_module socketSpiModule;
            Cpu.Pin        socketSpiSlaveSelectPin;
            //
            socket.GetPhysicalResources(out socketGpioPin, out socketSpiModule, out socketSpiSlaveSelectPin);

            SPI.Configuration spiConfig;
            SPI spi;

            try
            {
                spiConfig = new SPI.Configuration(socketSpiSlaveSelectPin, false, 0, 0, false, false, 500, socketSpiModule);
                spi       = new SPI(spiConfig);
            }
            catch
            {
                return(Guid.Empty);
            }

            // TODO: move the socket4ChipSelectDeassert logic to native code (this suppresses the MCO)
            OutputPort socket4ChipSelectDeassert = null;

            if (Port.ReservePin((Cpu.Pin) 0x08, true))
            {
                socket4ChipSelectDeassert = new OutputPort((Cpu.Pin) 0x08, true);
            }

            int iEnumAttempt    = 0;
            int emptyFrameCount = 0;

            byte basicDeviceType = 0x00;

            try
            {
                while (iEnumAttempt < 72)       // NOTE: 72 attempts allow our frame to rotate four full times in case of frame mis-alignment; this should be unnecessary in time.
                {
                    writeFrameBuffer[0] = 0xFE; // 'Enumerate' command
                    for (int i = 1; i < writeFrameBuffer.Length - 1; i++)
                    {
                        writeFrameBuffer[i] = 0xFF; // filler -- so that module sends ID
                    }
                    writeFrameBuffer[writeFrameBuffer.Length - 1] = CRC8.Compute8(writeFrameBuffer, 0, writeFrameBuffer.Length - 1);
                    spi.WriteRead(writeFrameBuffer, readFrameBuffer);

                    // validate the response...
                    bool replyIsBlank = true;
                    for (int i = 1; i < readFrameBuffer.Length - 1; i++)
                    {
                        if (readFrameBuffer[i] != 0)
                        {
                            replyIsBlank = false;
                        }
                    }

                    if (replyIsBlank)
                    {
                        // no response; try again (up to 4 times total)
                        emptyFrameCount++;
                        if (emptyFrameCount > 4)
                        {
                            return(Guid.Empty);
                        }
                    }
                    else if (CRC8.Compute8(readFrameBuffer) == 0)
                    {
                        return(new Guid(Microsoft.SPOT.Hardware.Utility.ExtractRangeFromArray(readFrameBuffer, 1, readFrameBuffer.Length - 2)));
                    }
                    else if (readFrameBuffer[1] != 0)
                    {
                        bool replyIsBasicDevice = true;
                        // did not pass CRC; see if it's a basic device type (first byte is id, then all other bytes including CRC will be zeros)
                        for (int i = 2; i < readFrameBuffer.Length; i++)
                        {
                            if (readFrameBuffer[i] != 0)
                            {
                                replyIsBasicDevice = false;
                            }
                        }

                        if (replyIsBasicDevice)
                        {
                            if (basicDeviceType == 0)
                            {
                                // first successful query; store this id and we'll verify by second enum
                                basicDeviceType = readFrameBuffer[1];
                            }
                            else
                            {
                                if (basicDeviceType == readFrameBuffer[1])
                                {
                                    return(new Guid(Microsoft.SPOT.Hardware.Utility.ExtractRangeFromArray(readFrameBuffer, 1, readFrameBuffer.Length - 2)));
                                }
                                else
                                {
                                    // mismatch; could not enumerate successfully
                                    return(Guid.Empty);
                                }
                            }
                        }
                    }
                    else
                    {
                        // corrupt message; try again
                    }

                    iEnumAttempt++;
                }
            }
            finally
            {
                if (socket4ChipSelectDeassert != null)
                {
                    socket4ChipSelectDeassert.Dispose();
                    Port.ReservePin((Cpu.Pin) 0x08, false);
                }
            }

            // if we reach here, we could not retrieve a GUID.
            return(Guid.Empty);
        }
예제 #13
0
        public void Initialize(GoSocket socket, uint speedKHz = 25000)
        {
            if (speedKHz < 5000 || speedKHz > 40000) throw new ArgumentException("speedKHz");
            if (socket == null) throw new ArgumentNullException("socket");

            SPI.SPI_module displaySpi;
            Cpu.Pin displayChipSelect;
            Cpu.Pin displayGPIO;
            socket.GetPhysicalResources(out displayGPIO, out displaySpi, out displayChipSelect);
            if (!BindSocket(socket)) throw new InvalidOperationException("socket already bound");

            SetSocketPowerState(false);
            Thread.Sleep(2000);
            SetSocketPowerState(true);
            Thread.Sleep(250);

            Spi = new SPI(new SPI.Configuration(
                SPI_mod: displaySpi,
                ChipSelect_Port: displayChipSelect,
                ChipSelect_ActiveState: false,
                ChipSelect_SetupTime: 0,
                ChipSelect_HoldTime: 0,
                Clock_IdleState: false,
                Clock_Edge: false,
                Clock_RateKHz: speedKHz));

            GoBusIrqPort = new InterruptPort(displayGPIO, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
            GoBusIrqPort.OnInterrupt += OnGoBusIrq;

            WaitUntilModuleIsInitialized();
        }