public void checkFreqValidity(double fine, ref double freq, int coarse, FTDIdevice thisUSB, Device device, SSLInterface ssl)
 {
     //makes sure freq isn't out of range or a duplicate measurement, using the following general procedure: if it's invalid, measure it again; if it's still invalid, throw error.
     if (freq > 12001400 || freq < 11998000) // if out of range of trimming
     {
         //first, try again
         freq = FreqAtTrim(thisUSB, ssl, coarse, Convert.ToInt32(fine));
         //if freq is wildly off, probably unseated
         if (freq > 12010000 || freq < 11990000) throw new Exception_Yellow("Frequency counter range error (coarse " + coarse.ToString() + " fine " + fine.ToString() + " " + freq.ToString() + "). Please ensure that board is properly seated and try again.  If error persists, testing fails.");
         if (freq > 12001400 || freq < 11998000) throw new Exception_Red("Crystal frequency is outside acceptable range (coarse " + FinalCoarse.ToString() + " fine " + FinalFine.ToString() + "). Testing fails. Insert a new board and press \"Run Test\" or Enter.");  // freqency is too far from 12 MHz to be trimmed
     }    //        (make sure that this catches a null freq)
     //if this frequency is more than twice as close as we'd expect to the previous measurement, probably unseated:
     if (previouscoarseforchecking != -12345) //if this is not the first time we're running a check
     {
         if (coarse == previouscoarseforchecking && Convert.ToInt32(fine) != Convert.ToInt32(previousfineforchecking))  //if fine trims can be compared on their own
         {
             if ((freq - previousfreqforchecking) / (finetrimsizeinHz * (fine - previousfineforchecking)) < .5)
             {
                 freq = FreqAtTrim(thisUSB, ssl, coarse, Convert.ToInt32(fine));
                 if ((freq - previousfreqforchecking) / (finetrimsizeinHz * (Convert.ToInt32(fine) - Convert.ToInt32(previousfineforchecking))) < .5) throw new Exception_Yellow("Frequency counter repeat or anomaly error (coarse " + coarse.ToString() + " fine " + fine.ToString() + " " + freq.ToString() + "). Please ensure that board is properly seated and try again.  If error persists, testing fails.");
             }
         }
         else if (Math.Abs(freq - previousfreqforchecking) < .3 * finetrimsizeinHz)
         {
             freq = FreqAtTrim(thisUSB, ssl, coarse, Convert.ToInt32(fine));
             if (Math.Abs(freq - previousfreqforchecking) < .3 * finetrimsizeinHz) throw new Exception_Yellow("Frequency counter repeat or anomaly error (coarse " + coarse.ToString() + " fine " + fine.ToString() + " " + freq.ToString() + "). Please ensure that board is properly seated and try again.  If error persists, testing fails.");
         }
     }
     previousfreqforchecking = freq;
     previousfineforchecking = fine;
     previouscoarseforchecking = coarse;
 }
 public void EraseSector24(SSLInterface sslinterface)
 {
     try
     {
         sslinterface.SSL_ERASE_Sector(0x18000);  //also write-unprotects
     }
     catch (Exception_Yellow exc)
     {
         if (!exc.Message.Contains("retry failed")) throw new Exception_Yellow(exc.Message); //if it's some other exception, don't catch it
         else sslinterface.SSL_ERASE_Sector(0x18000); //retry once, if the exception comes back again, we won't catch it and it'll go on to be displayed as "retry failed"
     }
 }
 public void LoadHardwareSettings(SSLInterface sslinterface)
 {
     parameters.fillHardwarePars();
     //thisUSB.poll();  //just in case we need to clear junk off the port
     try
     {
         sslinterface.SSL_WRITE(Parameters.HwInfAdr, parameters.HardwareParametersArray, (UInt16)parameters.HardwareParametersArray.Length);
     }
     catch (Exception_Yellow exc)
     {
         if (!exc.Message.Contains("retry failed")) throw new Exception_Yellow(exc.Message); //if it's some other exception, don't catch it
         else sslinterface.SSL_WRITE(Parameters.HwInfAdr, parameters.HardwareParametersArray, (UInt16)parameters.HardwareParametersArray.Length); //retry once, if the exception comes back again, we won't catch it and it'll go on to be displayed as "retry failed"
     }
 }
 public void LoadTrimsOnly(SSLInterface sslinterface)
 {
     //parameters.fillHardwarePars();
     //thisUSB.poll();  //just in case we need to clear junk off the port
     try
     {
         sslinterface.SSL_WRITE(Parameters.TrimAdr, parameters.TwoTrimBytes, (UInt16)parameters.TwoTrimBytes.Length);
     }
     catch (Exception_Yellow exc)
     {
         if (!exc.Message.Contains("retry failed")) throw new Exception_Yellow(exc.Message); //if it's some other exception, don't catch it
         else sslinterface.SSL_WRITE(Parameters.TrimAdr, parameters.TwoTrimBytes, (UInt16)parameters.TwoTrimBytes.Length);
     }
 }
Пример #5
0
        public void Run()
        {
            thisUSB.adjustBaudRate(Parameters.BaudRate_SSL);
            //InsertCustomParameters();

            //load ssl using RAMLoader
            RAMLoader loadSSL = new RAMLoader(parameters, thisUSB, parameters.SSLfilepath, mf_parent);
            loadSSL.Run(true);

            thisUSB.adjustBaudRate(Parameters.BaudRate_SSL);  //increase the baud rate 8x, saves six seconds on loading (goes from 14 to 8)

            mf_parent.UpdateOutputText("Loading production firmware, filename \"" + parameters.FWimagefilename + "\"...");
            SSLInterface sslinterface = new SSLInterface(parameters, thisUSB);
            try
            {
                //Read the firmware image into memory and check its length
                int filesize = 0;
                byte[] fullfilebuffer = readimagefromfile(parameters.FWimagefilepath, ref filesize);
                try
                {
                    sslinterface.SSL_FULLERASE(); //cut this out before giving to controltek, this is here just to clear the hw info pars out while we're debugging.
                }
                catch (Exception_Yellow exc)
                {
                    if (!exc.Message.Contains("retry failed")) throw new Exception_Yellow(exc.Message); //if it's some other exception, don't catch it
                    else sslinterface.SSL_FULLERASE(); //retry once, if the exception comes back again, we won't catch it and it'll go on to be displayed as "retry failed"
                }
                LoadFW(sslinterface, fullfilebuffer, filesize);
                LoadHardwareSettings(sslinterface);
                Commit(sslinterface, filesize);
            }
            catch (Exception_Yellow ey)
            {
                if (ey.Message.Contains("CRC"))
                {
                    mf_parent.UpdateOutputText("Error! Reverting to empty state, do not unplug...");
                    sslinterface.SSL_FULLERASE();
                    throw new Exception_Yellow("SSL throws CRC check error; packet was corrupted over UART. Memory has been erased, please try again.");
                }
                else if (ey.Message.Contains("Write"))
                {
                    mf_parent.UpdateOutputText("Error! Reverting to empty state, do not unplug...");
                    sslinterface.SSL_FULLERASE();
                    throw new Exception_Yellow("SSL throws write error; write operation fails. Memory has been erased, please try again.");
                }
                else throw ey;
            }
            thisUSB.adjustBaudRate(Parameters.BaudRate_UARTandTesting);
        }
        //    newtrimmer(initialcoarse, initialtrim1, initialtrim2)
        public void NewTrimmer(FTDIdevice thisUSB, int coarse, double finetrim, double backupfinetrim)
        {
            SSLInterface ssl = new SSLInterface(parameters, thisUSB);

            thisUSB.poll();  //clear out anything sitting on the port

            double fine1 = finetrim;        //should be the mode of the trimming results
            double fine2 = backupfinetrim;  //if newton's method doesn't get it on the first try we use secant method with this backupfinetrim and finetrim
            bool done = false;
            double temp;
            double freq2 = -12345;  //dummy value to avoid error
            double freq1 = FreqAtTrim(thisUSB, ssl, coarse, Convert.ToInt32(fine1));
            checkFreqValidity(fine1, ref freq1, coarse, thisUSB, FREQ_COUNTER, ssl);
            if (freqWithin8(coarse, fine1, freq1)) done = true;
            TrimSequence += (" | " + coarse.ToString() + " " + Math.Round(fine1, 3).ToString() + " " + freq1.ToString());
            if (!done) // this if statement is here just in case we got it right the first time
            {
                temp = fine1 - (freq1 - targetfrequency) / finetrimsizeinHz;    //use Newton's method to find the second point, so we finish after 2 measurements more often
                if (temp <= 31 && temp >= 0) fine2 = temp;                      //but only use it if it gives a value between 0 and 31, otherwise revert to input fine2
                freq2 = FreqAtTrim(thisUSB, ssl, coarse, Convert.ToInt32(fine2));         // in the future, could adjust this to save a step when adjusting coarse, but we expect to adjust coarse only very rarely
                checkFreqValidity(fine2, ref freq2, coarse, thisUSB, FREQ_COUNTER, ssl);
                if (freqWithin8(coarse, fine2, freq2)) done = true;
                TrimSequence += (" | " + coarse.ToString() + " " + Math.Round(fine2,3).ToString() + " " + freq2.ToString());
            }
            while (!done && Math.Abs(fine2 - fine1) > 1)  //while not done and trims are more than 1 apart
            {

                temp = fine2 - ((freq2 - targetfrequency) * (fine2 - fine1)) / (freq2 - freq1);  //use the secant method
                fine1 = fine2;
                fine2 = temp;
                freq1 = freq2;
                int intfine2 = Convert.ToInt16(fine2);
                if (intfine2 <= 31 && intfine2 >= 0)
                {
                    //temp = fine2 - ((freq2 - targetfrequency) * (fine2 - fine1)) / (freq2 - freq1);  //use the secant method
                    //fine1 = fine2;
                    //fine2 = temp;
                    //freq1 = freq2;
                    freq2 = FreqAtTrim(thisUSB, ssl, coarse, Convert.ToInt32(fine2));
                    checkFreqValidity(fine2, ref freq2, coarse, thisUSB, FREQ_COUNTER, ssl);
                    if (freqWithin8(coarse, fine2, freq2)) done = true;
                    TrimSequence += (" | " + coarse.ToString() + " " + Math.Round(fine2,3).ToString() + " " + freq2.ToString());
                }
                else if (intfine2 > 31)
                {
                    if (intfine2 > 36)
                    {
                        while (freq2 > targetfrequency && coarse < 31)
                        {
                            coarse++; //could replace this with coarse += (intfine2-31)/7 if we ever saw cases of needing to adjust by >1 coarse trim
                            if (coarse == 16) coarse = 28;  //accounting for the overlap in coarse trim values due to the 5th cap being only 4 pf
                            freq2 = FreqAtTrim(thisUSB, ssl, coarse, 31);
                            checkFreqValidity(fine2, ref freq2, coarse, thisUSB, FREQ_COUNTER, ssl);
                            if (freqWithin8(coarse, fine2, freq2)) done = true;
                            TrimSequence += (" | " + coarse.ToString() + " " + Math.Round(fine2,3).ToString() + " " + freq2.ToString());
                        }
                    }
                    else coarse++;
                    NewTrimmer(thisUSB, coarse, 31, 24);
                    done = true;
                }
                else if (intfine2 < 0)
                {
                    if (intfine2 < -5)
                    {
                        while (freq2 < targetfrequency && coarse > 0)
                        {
                            coarse--; //could replace this with coarse += (intfine2)/7 if we ever saw cases of needing to adjust by >1 coarse trim
                            freq2 = FreqAtTrim(thisUSB, ssl, coarse, 0);
                            checkFreqValidity(fine2, ref freq2, coarse, thisUSB, FREQ_COUNTER, ssl);
                            if (freqWithin8(coarse, fine2, freq2)) done = true;
                            TrimSequence += (" | " + coarse.ToString() + " " + Math.Round(fine2,3).ToString() + " " + freq2.ToString());
                        }
                    }
                    else coarse--;
                    NewTrimmer(thisUSB, coarse, 0, 7);
                    done = true;
                }
            }
            if (!done)  //(could have become done from coarse changing / recursive call; this code is only reached if we've finished trimming with fine trims 1 away from each other but freqs not within 8)
            {
                //if we've trimmed as well as possible but no frequency was within 8 Hz, choose which one was closer (and if it was fine1, re-set that trim).
                if (Math.Abs(freq1 - targetfrequency) < Math.Abs(freq2 - targetfrequency))
                {
                    //FTinti.Command(FTinti, "95 0A 02 " + FTinti.TrimPacketPrep(Convert.ToInt32(fine1)) + FTinti.TrimPacketPrep(coarse));
                    FinishTrim(coarse, fine1, freq1);
                }
                else FinishTrim(coarse, fine2, freq2);
            }
        }
        //checks frequency over GPIB at given trim values
        public double FreqAtTrim(FTDIdevice thisUSB, SSLInterface ssl, int coarse, int fine)
        {
            double freq = 0;
            //setting trim when talking to SSL:
            try
            {
                ssl.SSL_SetTrims((byte)fine, (byte)coarse);
            }
            catch (Exception_Yellow exc)
            {
                if (!exc.Message.Contains("retry failed")) throw new Exception_Yellow(exc.Message); //if it's some other exception, don't catch it
                else ssl.SSL_SetTrims((byte)fine, (byte)coarse); //retry once, if the exception comes back again, we won't catch it and it'll go on to be displayed as "retry failed"
            }
            //unused code for setting trim if talking to ZTC:
            //Utils.SendZTCCommand(thisUSB, "95 0A 02 " + TrimPacketPrep(fine) + TrimPacketPrep(coarse));
            //    Utils.WaitForZTCResponse(thisUSB, parameters);
            ////System.Threading.Thread.Sleep(as long as settling takes);

            FREQ_COUNTER.Write(":MEASURE:FREQ? 12, 0.0000001");
            //FREQ_COUNTER.Write(":meas:freq? 12,0.0000001");

            //System.Threading.Thread.Sleep(2000);
            freq = Convert.ToDouble(FREQ_COUNTER.ReadString());
            return freq;
        }
 private void Commit(SSLInterface sslinterface, int filesize)
 {
     //now that we're done loading, commit the image by writing the image header
     try
     {
         sslinterface.SSL_COMMIT((uint)filesize);
     }
     catch (Exception_Yellow exc)
     {
         if (!exc.Message.Contains("retry failed")) throw new Exception_Yellow(exc.Message); //if it's some other exception, don't catch it
         else sslinterface.SSL_COMMIT((uint)filesize); //retry once, if the exception comes back again, we won't catch it and it'll go on to be displayed as "retry failed"
     }
 }
        private void LoadFW(SSLInterface sslinterface, byte[] fullfilebuffer, int filesize)
        {
            int currentReadAddress = 0;
            uint currentWriteAddress = FLASH_IMAGE_HEADER;  //Initialize the write address with the first byte after the FLASH header
            UInt16 bytesToRead;

            //only used in testing to erase partially-cleared dongles:
            if (parameters.reprogramming_oldFW_HW3_SW2 || parameters.reprogramming_newFW)
            {
                try
                {
                    mf_parent.UpdateOutputText("Finalizing erase of old firmware...");
                    sslinterface.SSL_FULLERASE();
                }
                catch (Exception_Yellow exc)
                {
                    if (!exc.Message.Contains("retry failed")) throw new Exception_Yellow(exc.Message); //if it's some other exception, don't catch it
                    else sslinterface.SSL_FULLERASE(); //retry once, if the exception comes back again, we won't catch it and it'll go on to be displayed as "retry failed"
                }
            }

            mf_parent.UpdateOutputText("Loading production firmware, filename \"" + parameters.FWimagefilename + "\"...");

                //now chop it up into chunks the SSL will be able to handle, and write each chunk to the ssl, which then writes it to RAM and sends back a confirm
                while (currentReadAddress != filesize)
                {
                    if (filesize - currentReadAddress > Parameters.SSL_ENG_BUFFER_SIZE) bytesToRead = Parameters.SSL_ENG_BUFFER_SIZE;  //make chunks in the max buffer size the SSL can take (512 bytes)
                    else
                        bytesToRead = (UInt16)(filesize - currentReadAddress); //the last chunk will be smaller than the max buffer size
                    byte[] tempbuffer = new byte[bytesToRead];
                    for (int i = 0; i < bytesToRead; i++)
                    {
                        tempbuffer[i] = fullfilebuffer[currentReadAddress];
                        currentReadAddress++;
                    }
                    try
                    {
                        sslinterface.SSL_WRITE(currentWriteAddress, tempbuffer, bytesToRead);
                    }
                    catch (Exception_Yellow exc)
                    {
                        if (!exc.Message.Contains("retry failed")) throw new Exception_Yellow(exc.Message); //if it's some other exception, don't catch it
                        else sslinterface.SSL_WRITE(currentWriteAddress, tempbuffer, bytesToRead); //retry once, if the exception comes back again, we won't catch it and it'll go on to be displayed as "retry failed"
                    }
                    //catch (Exception e)
                    //{
                    //    if (!e.Message.Contains("reference not set")) throw new Exception(e.Message); //if it's some other exception, don't catch it
                    //    else throw new Exception(e.Message + "  it happened inside SSL_WRITE");
                    //}

                    currentWriteAddress += bytesToRead;
                    mf_parent.UpdateProgressBar_Detail((int)(100 * (currentWriteAddress - FLASH_IMAGE_HEADER) / filesize));
                }
        }
        //THIS IS THE MAIN CODE THAT RUNS WHEN THE BUTTON IS CLICKED
        public void MainProcess()
        {
            int BufferErrorIncrementer = 0;
            GoToForBufferingErrorRetry:
            try
            {
                Setup();
                ReadCounterID();
                CheckRequirements();                                //check that necessary stuff is plugged in
                ReadBarcode(); //come back to this                  //read scanned barcode (MAC address) from text box
                parameters.SetSerialNumber();
                UpdateOutputText("Opening USB port...");
                thisUSB.OpenPort(parameters.USB_under_test_pid, 6000);    //open up the usb port

                enableResetLine();                                  //enable control of the reset line on the dongle by applying new EEPROM settings to the FTDI chip (takes a few sec)

                thisUSB.ChipReset();
                //load ssl using RAMLoader
                thisUSB.adjustBaudRate(Parameters.BaudRate_SSL);
                RAMLoader loadSSL = new RAMLoader(parameters, thisUSB, parameters.SSLfilepath, this);
                loadSSL.Run(true);
                thisUSB.adjustBaudRate(Parameters.BaudRate_SSL);  //increase the baud rate 8x, saves six seconds on loading (goes from 14 to 8)

                SSLInterface ssl = new SSLInterface(parameters, thisUSB);
                FirmwareLoader fwloader = new FirmwareLoader(parameters, thisUSB, this);
                fwloader.EraseSector24(ssl);

                SetCrystalTrim();                                   //program the trim adjustment values for the crystal clock (depending on options set in Settings.txt)

                fwloader.LoadTrimsOnly(ssl);

                thisUSB.Close();
                Finish();

                //items to only do once totally successful:
                Logger();
                UpdateProgressBar_Overall(progressBar_overall.Maximum);
                SaveSerialNumber();
                UpdateColorDisplay("green");
                UpdatePictureDisplay("pass");//add pic @20120402 by Nino Liu
                //add serial number @20120328 by nino
                if (parameters.testing && parameters.loading) UpdateOutputText("Test and load with MAC: " + parameters.MAC + '\n' + "Serial number: " + parameters.SN + '\n' +
                         "Spent time: " + parameters.totaltesttime + " seconds."+ '\n' + "Optimal Frequency: " + parameters.frequency_measured);
                else if (parameters.loading) UpdateOutputText("Loading  with MAC: " + parameters.MAC + '\n' + "Serial number: " + parameters.SN + '\n' +
                        "Spent time: " + parameters.totaltesttime + " seconds." + '\n' + "Optimal Frequency: " + parameters.frequency_measured);
                else
                    UpdateOutputText("Testing with MAC: " + parameters.MAC + '\n' + "Serial number: " + parameters.SN + '\n' +
                        "Spent time: " + parameters.totaltesttime + " seconds." + '\n' + "Optimal Frequency: " + parameters.frequency_measured);
            }
            catch (Exception_Yellow e)
            {
                ExceptionHandler(e, "yellow");
            }
            catch (Exception_Red e)
            {
                ExceptionHandler(e, "red");
            }
            catch (Exception e)
            {
                if (e.Message.Contains("supplied buffer is not big enough"))
                {
                    BufferErrorIncrementer++;
                    if (BufferErrorIncrementer > 3) Output.Text = "Persistent buffering error -- unplug and replug dongle and try again.  If error persists, dongle fails.";
                    else
                    {
                        Output.Text = "Buffering error -- restarting after 2 seconds.";
                        UpdateColorDisplay("orange");
                        System.Windows.Forms.Application.DoEvents();
                        System.Threading.Thread.Sleep(2000);
                        goto GoToForBufferingErrorRetry;
                    }
                }
                ExceptionHandler(e, "generic");
            }
        }