Example #1
0
        public bool I2CReadBytes(int busID, byte[] data)
        {
            //DiscardNextBlock = true;
            //AddAccess(eDirection.Read, busID, data.Length, null);

            int retries = I2CRetries;

            lock (Lock)
            {
                do
                {
                    //Thread.Sleep(5);
                    if (USBRXDeviceNative.UsbI2CReadBytes(DevNum, busID, (ushort)data.Length, data))
                    {
                        return(true);
                    }

                    if (retries == 0)
                    {
                        DeviceLost = true;
                        return(false);
                    }
                    Thread.Sleep(I2CSleep);
                } while (retries-- > 0);
            }
            return(false);
        }
Example #2
0
 public bool I2CDeviceAck(int busID)
 {
     lock (Lock)
     {
         return(USBRXDeviceNative.UsbI2CWriteBytes(DevNum, busID, 0, null));
     }
 }
Example #3
0
 public bool Read(byte[] data)
 {
     lock (Lock)
     {
         return(USBRXDeviceNative.UsbParIn(DevNum, data, (uint)data.Length));
     }
 }
Example #4
0
 public void I2CSetTimeout(ushort timeout, ushort retries)
 {
     lock (Lock)
     {
         USBRXDeviceNative.UsbI2CSetTimeout(DevNum, (ushort)(((retries & 0xFF) << 8) | (timeout & 0xFF)));
     }
 }
Example #5
0
 public bool SPIReset(bool state)
 {
     lock (Lock)
     {
         return(USBRXDeviceNative.UsbSetIOState(DevNum, USBRXDeviceNative.PIN_SPI_RESET, state ? USBRXDeviceNative.PIN_STATE_LOW : USBRXDeviceNative.PIN_STATE_HIGH));
     }
 }
Example #6
0
 public bool SPITransfer(byte[] dataWrite, byte[] dataRead)
 {
     lock (Lock)
     {
         return(USBRXDeviceNative.UsbSpiTransfer(DevNum, dataWrite, dataRead, (ushort)dataWrite.Length));
     }
 }
Example #7
0
        public bool I2CReadByte(int busID, byte[] data)
        {
            //AddAccess(eDirection.Write, busID, 1, null);

            int retries = I2CRetries;

            lock (Lock)
            {
                do
                {
                    //Thread.Sleep(5);
                    if (USBRXDeviceNative.UsbI2CReadByte(DevNum, busID, data))
                    {
                        return(true);
                    }

                    if (retries == 0)
                    {
                        DeviceLost = true;
                        return(false);
                    }
                    Thread.Sleep(I2CSleep);
                } while (retries-- > 0);
            }
            return(false);
        }
Example #8
0
        public void ReadBlockReceived()
        {
            lock (ReadTimerLock)
            {
                TimeoutsHappened = 0;
                DeviceLost       = false;

                FIFOReset(true);
                USBRXDeviceNative.ResetEpFifo(DevNum);

                if (PreQueueTransfer)
                {
                    //PreQueueTransfer = true;
                    //Log.AddMessage("ReadTrigger [new transfer]");
                    lock (Lock)
                    {
                        USBRXDeviceNative.UsbSetControlledTransfer(DevNum, ReadBlockSize, ReadFragmentSize);
                    }
                    FIFOReset(false);
                }


                ReadTimerLocked = false;
                Monitor.Pulse(ReadTimerLock);
            }
        }
Example #9
0
        private bool FIFOReset(bool state)
        {
            if (UseAtmelFIFO)
            {
                return(Atmel.FIFOReset(state));
            }

            lock (Lock)
            {
                return(USBRXDeviceNative.UsbSetIOState(DevNum, FIFOResetPortPin, state ? USBRXDeviceNative.PIN_STATE_HIGH : USBRXDeviceNative.PIN_STATE_LOW));
            }
        }
Example #10
0
        private void StopStreamRead()
        {
            FIFOReset(true);

            lock (Lock)
            {
                USBRXDeviceNative.UsbSetControlledTransfer(DevNum, ReadBlockSize, ReadFragmentSize);
                USBRXDeviceNative.UsbSetGPIFMode(DevNum);
            }

            StopThreads();
        }
Example #11
0
        private void StartStreamRead()
        {
            StartThreads();

            lock (Lock)
            {
                USBRXDeviceNative.ResetEpFifo(DevNum);
                USBRXDeviceNative.UsbSetGPIFMode(DevNum);
                USBRXDeviceNative.UsbSetControlledTransfer(DevNum, 0, ReadBlockSize);
            }

            FIFOReset(false);
        }
Example #12
0
        public void SPIInit()
        {
            lock (Lock)
            {
                USBRXDeviceNative.UsbSetIODir(DevNum, USBRXDeviceNative.PIN_SPI_RESET, USBRXDeviceNative.PIN_DIR_OUT);      // RESET
                USBRXDeviceNative.UsbSetIODir(DevNum, USBRXDeviceNative.PIN_SPI_SDO, USBRXDeviceNative.PIN_DIR_OUT);        // SDO
                USBRXDeviceNative.UsbSetIODir(DevNum, USBRXDeviceNative.PIN_SPI_CLK, USBRXDeviceNative.PIN_DIR_OUT);        // CLK
                USBRXDeviceNative.UsbSetIODir(DevNum, USBRXDeviceNative.PIN_SPI_SDI, USBRXDeviceNative.PIN_DIR_IN);         // SDI
                USBRXDeviceNative.UsbSetIODir(DevNum, USBRXDeviceNative.PIN_SPI_LED_IN, USBRXDeviceNative.PIN_DIR_IN);      // LED pin

                USBRXDeviceNative.UsbSetIOState(DevNum, USBRXDeviceNative.PIN_SPI_RESET, USBRXDeviceNative.PIN_STATE_HIGH); // RESET inactive
            }
        }
Example #13
0
        public void ShowConsole(bool show)
        {
            ushort mode = (ushort)(USBRXDeviceNative.MODE_NORMAL | USBRXDeviceNative.MODE_FASTI2C);

            if (show)
            {
                mode |= USBRXDeviceNative.MODE_CONSOLE;
            }

            lock (Lock)
            {
                USBRXDeviceNative.UsbSetTimeout(0x80 | DevNum, mode);
            }
        }
Example #14
0
        private void StartRead()
        {
            StartThreads();

            lock (Lock)
            {
                USBRXDeviceNative.ResetEpFifo(DevNum);
                USBRXDeviceNative.UsbSetGPIFMode(DevNum);
            }
            ReadTimerLocked = false;
            ReadTimer.Start();

            lock (ReadTimerLock)
            {
                Monitor.Pulse(ReadTimerLock);
            }
        }
Example #15
0
        private void ReadThreadMain()
        {
            try
            {
                lock (ReadTrigger)
                {
                    while (true)
                    {
                        /* when read timer fires */
                        while (!ReadTriggered)
                        {
                            Monitor.Wait(ReadTrigger, 50);
                        }
                        //Log.AddMessage("ReadTrigger [was fired]");

                        ReadTriggered = false;

                        /* start a new transfer */

                        if (!PreQueueTransfer)
                        {
                            //PreQueueTransfer = true;
                            //Log.AddMessage("ReadTrigger [new transfer]");
                            lock (Lock)
                            {
                                USBRXDeviceNative.ResetEpFifo(DevNum);
                                USBRXDeviceNative.UsbSetControlledTransfer(DevNum, ReadBlockSize, ReadFragmentSize);
                            }
                            FIFOReset(false);
                        }

                        ExpectedReadDuration = SamplesPerBlock / (double)Tuner.SamplingRate;

                        /* dont fire next read until data was processed */
                        ReadTimerLocked = true;
                    }
                }
            }
            catch (ThreadAbortException ex)
            {
                return;
            }
        }
Example #16
0
        public void Close()
        {
            if (Tuner != null)
            {
                Tuner.CloseTuner();
            }

            /* stop read timer */
            ReadTimer.Stop();

            /* stop read trigger thread */
            StopThreads();

            /* close driver handle */
            lock (Lock)
            {
                USBRXDeviceNative.UsbClose(DevNum);
                ReleaseDeviceNum(DevNum);
            }
        }
Example #17
0
        private int GetFreeDeviceNum()
        {
            int dev = 0;

            /* allocate a free device id */
            lock (UsedDevNums)
            {
                while (UsedDevNums.Contains(dev) || !USBRXDeviceNative.UsbDevicePresent(dev))
                {
                    dev++;
                    if (dev > MaxDevices)
                    {
                        return(-1);
                    }
                }

                UsedDevNums.Add(dev);
            }

            return(dev);
        }
Example #18
0
 public void I2CSetSpeed(bool fast)
 {
     USBRXDeviceNative.UsbI2CSetSpeed(DevNum, fast ? 1 : 0);
 }
Example #19
0
        public bool Init()
        {
            try
            {
                //ShowConsole(true);

                Tuner mainTuner = null;
                long  stepSize  = 0;

                bool       success = false;
                BO35       bo35    = null;
                AR5000N    ar5000  = null;
                MT2131     mt2131  = null;
                USBRX_R820 r820    = null;
                VUHF_RX    vuhfrx  = null;

                if (mainTuner == null && (TunerCombination == eCombinationType.BO35 || TunerCombination == eCombinationType.Automatic))
                {
                    /* try to open BO-35 */
                    bo35 = new BO35(true);

                    try
                    {
                        success = bo35.OpenTuner();
                    }
                    catch (Exception e)
                    {
                    }

                    if (success)
                    {
                        stepSize  = 0;
                        mainTuner = bo35;
                    }
                    else if (TunerCombination == eCombinationType.BO35)
                    {
                        return(false);
                    }
                }
                if (mainTuner == null && (TunerCombination == eCombinationType.AR5000 || TunerCombination == eCombinationType.Automatic))
                {
                    /* try to open BO-35 */
                    ar5000 = new AR5000N("AR5000");

                    try
                    {
                        success = ar5000.OpenTuner();
                    }
                    catch (Exception e)
                    {
                    }

                    if (success)
                    {
                        stepSize  = 0;
                        mainTuner = ar5000;
                    }
                    else if (TunerCombination == eCombinationType.AR5000)
                    {
                        return(false);
                    }
                }


                lock (Lock)
                {
                    DevNum = GetFreeDeviceNum();

                    if (USBRXDeviceNative.UsbInit(DevNum))
                    {
                        if (!UseAtmelFIFO)
                        {
                            USBRXDeviceNative.UsbSetIODir(DevNum, FIFOResetPortPin, USBRXDeviceNative.PIN_DIR_OUT);
                        }

                        /* we will handle ext fifo flushing ourselves */
                        USBRXDeviceNative.SetFifoFlushing(false);

                        /* set maximum I2C speed */
                        USBRXDeviceNative.UsbI2CSetSpeed(DevNum, 1);

                        /* init low level interface to atmel */
                        AtmelProgrammer = new AtmelProgrammer(this);
                        AtmelProgrammer.ResetAtmel();

                        Atmel = new Atmel(this);
                        if (Atmel.Exists)
                        {
                            AD6636 = new AD6636(Atmel, Atmel.TCXOFreq);
                            Tuner  = AD6636;

                            /* detect I2C tuners */
                            if (mainTuner == null && (TunerCombination == eCombinationType.MT2131 || TunerCombination == eCombinationType.Automatic))
                            {
                                mt2131 = new MT2131(this);

                                try
                                {
                                    success = mt2131.OpenTuner();
                                }
                                catch (Exception e)
                                {
                                }

                                if (success)
                                {
                                    stepSize  = mt2131.IFStepSize;
                                    mainTuner = mt2131;
                                }
                                else if (TunerCombination == eCombinationType.MT2131)
                                {
                                    ReleaseDeviceNum(DevNum);
                                    return(false);
                                }
                            }

                            if (mainTuner == null && (TunerCombination == eCombinationType.R820 || TunerCombination == eCombinationType.Automatic))
                            {
                                r820 = new USBRX_R820(this);
                                try
                                {
                                    success = r820.OpenTuner();
                                }
                                catch (Exception e)
                                {
                                }

                                if (success)
                                {
                                    stepSize  = r820.IFStepSize;
                                    mainTuner = r820;
                                }
                                else if (TunerCombination == eCombinationType.MT2131)
                                {
                                    ReleaseDeviceNum(DevNum);
                                    return(false);
                                }
                            }



                            if (mainTuner == null && (TunerCombination == eCombinationType.VUHF_RX || TunerCombination == eCombinationType.Automatic))
                            {
                                vuhfrx = new VUHF_RX(this);

                                try
                                {
                                    success = vuhfrx.OpenTuner();
                                }
                                catch (Exception e)
                                {
                                }

                                if (success)
                                {
                                    stepSize  = vuhfrx.IFStepSize;
                                    mainTuner = vuhfrx;
                                }
                                else if (TunerCombination == eCombinationType.VUHF_RX)
                                {
                                    ReleaseDeviceNum(DevNum);
                                    return(false);
                                }
                            }

                            if (mainTuner != null)
                            {
                                Tuner = new TunerStack(mainTuner, Tuner, stepSize);
                            }

                            Tuner.OpenTuner();
                        }

                        //SetAgc(eAgcType.Slow);

                        CurrentMode = eTransferMode.Stopped;

                        StartThreads();

                        SetAtt(0);

                        return(true);
                    }
                    else
                    {
                        ReleaseDeviceNum(DevNum);
                    }
                }
            }
            catch (DllNotFoundException e)
            {
                MessageBox.Show("Was not able to load the driver. The driver DLL was not found." + Environment.NewLine + Environment.NewLine + e.Message);
            }
            catch (Exception e)
            {
                MessageBox.Show("Was not able to load the driver:" + Environment.NewLine + Environment.NewLine + e.Message);
            }

            return(false);
        }
Example #20
0
        private void HandleTimeout()
        {
            return;

            new Thread(() =>
            {
                switch (TimeoutsHappened - 4)
                {
                case 1:
                case 2:
                    Log.AddMessage("USBRXDevice: Timeout " + TimeoutsHappened + ". Retrigger Transfer");
                    lock (Lock)
                    {
                        USBRXDeviceNative.UsbSetGPIFMode(DevNum);
                    }
                    lock (ReadTimerLock)
                    {
                        Monitor.Pulse(ReadTimerLock);
                    }

                    break;

                case 6:
                    Log.AddMessage("USBRXDevice: Timeout " + TimeoutsHappened + ". Reinit AD6636");

                    AD6636.ReInit();
                    break;

                case 3:
                case 7:
                    Log.AddMessage("USBRXDevice: Timeout " + TimeoutsHappened + ". Reset AD6636");

                    Atmel.AD6636Reset();
                    AD6636.ReInit();
                    AD6636.SoftSync();
                    FIFOReset(false);
                    break;

                case 4:
                case 8:
                    Log.AddMessage("USBRXDevice: Timeout " + TimeoutsHappened + ". Resync AD6636, reset Atmel");

                    AD6636.SoftSync();
                    SPIReset(true);
                    SPIReset(false);
                    Thread.Sleep(50);
                    FIFOReset(false);
                    break;

                case 9:
                    Log.AddMessage("USBRXDevice: Timeout " + TimeoutsHappened + ". Reset Threads");

                    new Thread(() =>
                    {
                        CurrentMode = CurrentMode;
                        FIFOReset(false);
                    }).Start();

                    /*
                     * FIFOReset(true);
                     * USBRXDeviceNative.UsbSetIdleMode(DevNum);
                     * USBRXDeviceNative.UsbSetGPIFMode(DevNum);
                     * USBRXDeviceNative.SetSlaveFifoParams(true, DevNum, 0);
                     * FIFOReset(false);
                     */
                    break;


                case 12:
                    DeviceLost = true;
                    break;
                }
            }).Start();
        }