Пример #1
0
        private void EraseSPI64kBlock(UInt32 address)
        {
            WaitUntilIdle();

            byte[] cmd = new byte[5];
            cmd[0] = 0x03; // SPIFLASHWRITE_NEW

            // address
            cmd[1] = (byte)(address & 0xFF);
            cmd[2] = (byte)((address >> 8) & 0xFF);
            cmd[3] = (byte)((address >> 16) & 0xFF);
            cmd[4] = (byte)((address >> 24) & 0xFF);

            STDFU.STDFU_Dnload(ref hDevice, cmd, (uint)cmd.Length, 1);

            STDFU.DFU_Status dfuStatus = new STDFU.DFU_Status();
            STDFU.STDFU_GetStatus(ref hDevice, ref dfuStatus);
            Thread.Sleep(50);
            STDFU.STDFU_GetStatus(ref hDevice, ref dfuStatus);
            // WaitUntilIdle();

            Console.WriteLine(address);

            byte[] bytes = new byte[1024];
            STDFU.STDFU_Upload(ref hDevice, bytes, 1024, 1);
        }
Пример #2
0
        private void WriteSPIFlash(UInt32 address, UInt32 size, byte[] data)
        {
            //Console.WriteLine("Flashin " + data.Length);
            byte[] cmd = new byte[9 + data.Length];
            cmd[0] = 0x04; // SPIFLASHWRITE_NEW

            // address
            cmd[1] = (byte)(address & 0xFF);
            cmd[2] = (byte)((address >> 8) & 0xFF);
            cmd[3] = (byte)((address >> 16) & 0xFF);
            cmd[4] = (byte)((address >> 24) & 0xFF);

            // size
            cmd[5] = (byte)(size & 0xFF);
            cmd[6] = (byte)((size >> 8) & 0xFF);
            cmd[7] = (byte)((size >> 16) & 0xFF);
            cmd[8] = (byte)((size >> 24) & 0xFF);

            // copy
            Array.Copy(data, 0, cmd, 9, data.Length);

            uint res = STDFU.STDFU_Dnload(ref hDevice, cmd, (uint)cmd.Length, 1);

            STDFU.DFU_Status dfuStatus = new STDFU.DFU_Status();
            STDFU.STDFU_GetStatus(ref hDevice, ref dfuStatus);
            //Thread.Sleep(50);
            STDFU.STDFU_GetStatus(ref hDevice, ref dfuStatus);
            WaitUntilIdle();

            byte[] bytes = new byte[data.Length];
            res = STDFU.STDFU_Upload(ref hDevice, bytes, (uint)data.Length, 1);
            //Console.WriteLine("WriteSPIFlash: " + res);
        }
Пример #3
0
        public void TickleTickle()
        {
            STDFU.DFU_Status dfuStatus = new STDFU.DFU_Status();
            UInt32           Result    = STDFU.STDFU_SelectCurrentConfiguration(ref hDevice, 0, 0, 0);

            Console.WriteLine("SelectConfig Res: " + Result);
            STDFU.STDFU_GetStatus(ref hDevice, ref dfuStatus);
            STDFU.STDFU_ClrStatus(ref hDevice);
            // Console.WriteLine("DFU iString: " + dfuStatus.iString);
            // Console.WriteLine("DFU State: " + dfuStatus.bState);
            // Console.WriteLine("DFU STATUS: " + dfuStatus.bStatus);
            IntPtr strinng = Marshal.AllocHGlobal(256);

            Result = STDFU.STDFU_GetStringDescriptor(ref hDevice, 1, strinng, 256);
            // Console.WriteLine("StringDesc Res: " + Result);

            STDFU.STDFU_GetStatus(ref hDevice, ref dfuStatus);
            STDFU.STDFU_ClrStatus(ref hDevice);
            // Console.WriteLine("DFU iString: " + dfuStatus.iString);
            //Console.WriteLine("DFU State: " + dfuStatus.bState);
            //Console.WriteLine("DFU STATUS: " + dfuStatus.bStatus);
            byte[] bytes = new byte[256];
            Marshal.Copy(strinng, bytes, 0, 256);
            Marshal.FreeHGlobal(strinng);
            Console.WriteLine(Encoding.ASCII.GetString(bytes));
        }
Пример #4
0
        private void CustomCommand(byte a, byte b)
        {
            WaitUntilIdle();

            STDFU.STDFU_Dnload(ref hDevice, new byte[] { a, b }, 2, 0);

            WaitUntilIdle();
        }
Пример #5
0
 public void Reboot()
 {
     progress = 0;
     WaitUntilIdle();
     STDFU.STDFU_Dnload(ref hDevice, new byte[] { 0x91, 0x05 }, 2, 0);
     STDFU.DFU_Status dfuStatus = new STDFU.DFU_Status();
     STDFU.STDFU_GetStatus(ref hDevice, ref dfuStatus);
 }
Пример #6
0
        /*public bool DrawText(string msg, int a, int b)
         * {
         *
         *  byte[] cmd = new byte[msg.Length + 3];
         *  cmd[0] = 0x80;
         *
         *  a &= 0xFF;
         *  b &= 0xFF;
         *
         *  WaitUntilIdle();
         *
         *  cmd[1] = (byte)(address & 0xFF);
         *  cmd[2] = (byte)((address >> 8) & 0xFF);
         *  cmd[3] = (byte)((address >> 16) & 0xFF);
         *  cmd[4] = (byte)((address >> 24) & 0xFF);
         *
         *  STDFU.STDFU_Dnload(ref hDevice, cmd, 5, 0);
         *  STDFU.DFU_Status dfuStatus = new STDFU.DFU_Status();
         *  STDFU.STDFU_GetStatus(ref hDevice, ref dfuStatus);
         *
         *  WaitUntilIdle();
         *
         *  STDFU.STDFU_Dnload(ref hDevice, new byte[] { 0x84, 0,  }, 2, 0);
         *
         *  WaitUntilIdle();
         *
         *
         *
         *
         *  return true;
         * }*/

        public byte[] ReadFrameLine(byte y)
        {
            int majorTries = 0;

retryStart:

            WaitUntilIdle();

            for (int i = 0; i < 2; i++)
            {
                byte[] cmd = new byte[5];
                cmd[0] = 0x84; // SPIFLASHWRITE_NEW

                // address
                cmd[1] = (byte)(0);
                cmd[2] = (byte)(y & 0xFF);
                cmd[3] = (byte)(159 & 0xFF);
                cmd[4] = (byte)(y & 0xFF);

                STDFU.STDFU_Dnload(ref hDevice, cmd, (uint)cmd.Length, 1);

                STDFU.DFU_Status dfuStatus = new STDFU.DFU_Status();
                STDFU.STDFU_GetStatus(ref hDevice, ref dfuStatus);
                //Thread.Sleep(40);
                STDFU.STDFU_GetStatus(ref hDevice, ref dfuStatus);
                // WaitUntilIdle();

                byte[] bytes = new byte[(160 * 3) + 5];
                STDFU.STDFU_Upload(ref hDevice, bytes, (160 * 3) + 5, 1);
                if (bytes[4] == y)
                {
                    byte[] ret = new byte[160 * 3];
                    Array.Copy(bytes, 5, ret, 0, 160 * 3);
                    return(ret);
                }
                else
                {
                    Thread.Sleep(3);
                }
                //Thread.Sleep(25);
            }

            return(null);

            /*
             * majorTries++;
             * Console.WriteLine("ERR: Frame line " + y);
             *
             * if (majorTries > 100) return null;
             * else
             * {
             *
             *  TickleTickle();
             *  GetSpiID();
             *
             *  goto retryStart;
             * }*/
        }
Пример #7
0
        private byte[] ReadBlock(UInt32 BlockNumber, UInt32 BlockSize)
        {
            WaitUntilIdle();

            byte[] data = new byte[BlockSize];
            STDFU.STDFU_Upload(ref hDevice, data, BlockSize, (UInt16)BlockNumber);

            WaitUntilIdle();

            return(data);
        }
Пример #8
0
        private void WaitUntilIdle()
        {
            // wait 5 seconds max
            long start = DateTime.Now.Millisecond;

            STDFU.DFU_Status dfuStatus = new STDFU.DFU_Status();
            STDFU.STDFU_GetStatus(ref hDevice, ref dfuStatus);
            while (dfuStatus.bState != STDFU.STATE_DFU_IDLE && ((DateTime.Now.Millisecond - start) < 5000))
            {
                STDFU.STDFU_ClrStatus(ref hDevice);
                STDFU.STDFU_GetStatus(ref hDevice, ref dfuStatus);
            }
        }
Пример #9
0
        private bool WriteBlock(byte[] data, UInt32 BlockNumber)
        {
            if (this.hDevice == INVALID_HANDLE_VALUE || data.Length > MAX_WRITE_BLOCK_SIZE)
            {
                return(false);
            }

            WaitUntilIdle();

            STDFU.STDFU_Dnload(ref hDevice, data, (UInt32)data.Length, (UInt16)BlockNumber);

            WaitUntilIdle();

            return(true);
        }
Пример #10
0
        private void SetAddressPointer(UInt32 address)
        {
            WaitUntilIdle();

            byte[] cmd = new byte[5];
            cmd[0] = 0x21;
            cmd[1] = (byte)(address & 0xFF);
            cmd[2] = (byte)((address >> 8) & 0xFF);
            cmd[3] = (byte)((address >> 16) & 0xFF);
            cmd[4] = (byte)((address >> 24) & 0xFF);

            STDFU.STDFU_Dnload(ref hDevice, cmd, 5, 0);

            WaitUntilIdle();
        }
Пример #11
0
        public byte[] ReadSPIFlash(UInt32 address, UInt32 size)
        {
            byte[] retBytes = new byte[size];
            for (int i = 0; i < size; i++)
            {
                retBytes[i] = 0;
            }

            byte[] cmd = new byte[5];

            byte[] bytes = new byte[MAX_WRITE_BLOCK_SIZE];
            for (int i = 0; i < MAX_WRITE_BLOCK_SIZE; i++)
            {
                bytes[i] = 0;
            }

            for (uint offset = 0; offset < size; offset += MAX_WRITE_BLOCK_SIZE)
            {
                progress = (int)(((float)offset / (float)size) * 100.0) + 1;

                cmd[0] = 0x01; // SPIFLASHWRITE_NEW

                // address
                cmd[1] = (byte)(address & 0xFF);
                cmd[2] = (byte)((address >> 8) & 0xFF);
                cmd[3] = (byte)((address >> 16) & 0xFF);
                cmd[4] = (byte)((address >> 24) & 0xFF);

                uint             res       = STDFU.STDFU_Dnload(ref hDevice, cmd, (uint)cmd.Length, 1);
                STDFU.DFU_Status dfuStatus = new STDFU.DFU_Status();
                STDFU.STDFU_GetStatus(ref hDevice, ref dfuStatus);
                //Thread.Sleep(3);
                STDFU.STDFU_GetStatus(ref hDevice, ref dfuStatus);
                WaitUntilIdle();

                uint lenn = (uint)(size - offset < MAX_WRITE_BLOCK_SIZE ? (size - offset) : MAX_WRITE_BLOCK_SIZE);
                res = STDFU.STDFU_Upload(ref hDevice, bytes, lenn, 1);
                Array.Copy(bytes, 0, retBytes, offset, lenn);
                address += MAX_WRITE_BLOCK_SIZE;
            }

            return(retBytes);
        }
Пример #12
0
        private void EraseSector(UInt32 address)
        {
            UInt32 result = 0;

            byte[] cmd = { 0x41, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };

            cmd[1] = (byte)(address & 0xFF);
            cmd[2] = (byte)((address >> 8) & 0xFF);
            cmd[3] = (byte)((address >> 16) & 0xFF);
            cmd[4] = (byte)((address >> 24) & 0xFF);

            if ((result = STDFU.STDFU_SelectCurrentConfiguration(ref hDevice, 0, 0, 0)) == STDFU.STDFU_NOERROR)
            {
                WaitUntilIdle();

                STDFU.STDFU_Dnload(ref hDevice, cmd, 5, 0);

                WaitUntilIdle();
            }
        }
Пример #13
0
        public string getDeviceDescriptor(uint index = 1)
        {
            STDFU.DFU_Status dfuStatus = new STDFU.DFU_Status();
            UInt32           Result    = STDFU.STDFU_SelectCurrentConfiguration(ref hDevice, 0, 0, 0);

            STDFU.STDFU_GetStatus(ref hDevice, ref dfuStatus);
            STDFU.STDFU_ClrStatus(ref hDevice);

            IntPtr strinng = Marshal.AllocHGlobal(256);

            STDFU.STDFU_GetStringDescriptor(ref hDevice, index, strinng, 256);

            STDFU.STDFU_GetStatus(ref hDevice, ref dfuStatus);
            STDFU.STDFU_ClrStatus(ref hDevice);

            byte[] bytes = new byte[256];
            Marshal.Copy(strinng, bytes, 0, 256);
            Marshal.FreeHGlobal(strinng);
            return(Encoding.ASCII.GetString(bytes));
        }
Пример #14
0
        public UInt32 GetSpiID()
        {
            WaitUntilIdle();

            byte[] cmd = new byte[1];
            cmd[0] = 0x05;


            STDFU.STDFU_Dnload(ref hDevice, cmd, (uint)1, 1);

            STDFU.DFU_Status dfuStatus = new STDFU.DFU_Status();
            STDFU.STDFU_GetStatus(ref hDevice, ref dfuStatus);
            //Console.WriteLine("DFU iString: " + dfuStatus.iString);
            //Console.WriteLine("DFU State: " + dfuStatus.bState);
            //Console.WriteLine("DFU STATUS: " + dfuStatus.bStatus);

            WaitUntilIdle();

            byte[] bytes = new byte[4];
            STDFU.STDFU_Upload(ref hDevice, bytes, 4, 1);

            return(BitConverter.ToUInt32(bytes, 0));
        }
Пример #15
0
        private IntPtr OpenDFUDevice()
        {
            Guid GUID = GUID_DFU;
            DeviceInterfaceData ifData = new DeviceInterfaceData();

            ifData.Size = Marshal.SizeOf(ifData);
            DeviceInterfaceDetailData ifDetail = new DeviceInterfaceDetailData();

            UInt32 Size     = 0;
            IntPtr hInfoSet = SetupDiGetClassDevs(ref GUID, null, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);  // this gets a list of all DFU devices currently connected to the computer (InfoSet)
            IntPtr hDevice  = IntPtr.Zero;

            if (hInfoSet == INVALID_HANDLE_VALUE)
            {
                throw new Exception("Could not open DFU device!");
                //throw new Exception("SetupDiGetClassDevs returned error=" + Marshal.GetLastWin32Error().ToString());
            }

            // Loop ten times hoping to find exactly one DFU device
            int  i     = 10;
            uint Index = 0;

            while (i-- > 0)
            {
                Index = 0;
                while (SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref GUID, Index, ref ifData))
                {
                    Index++;
                }
                if (0 == Index)
                {
                    Thread.Sleep(500);
                }
                else
                {
                    break;
                }
            }

            if (1 == Index)
            {
                SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref GUID, 0, ref ifData);
                SetupDiGetDeviceInterfaceDetail(hInfoSet, ref ifData, IntPtr.Zero, 0, ref Size, IntPtr.Zero);

                if (IntPtr.Size == 8)   // If we are compiled as 64bit
                {
                    ifDetail.Size = 8;
                }
                else if (IntPtr.Size == 4) // If we are compiled as 32 bit
                {
                    ifDetail.Size = 5;
                }

                if (Marshal.SizeOf(ifDetail) < Size)
                {
                    throw new Exception("Could not open DFU device!");
                    //throw new Exception("ifDetail too small");
                }

                if (true == SetupDiGetDeviceInterfaceDetail(hInfoSet, ref ifData, ref ifDetail, Size, ref Size, IntPtr.Zero))
                {
                    string DevicePath = ifDetail.DevicePath.ToUpper();
                    if (STDFU.STDFU_Open(DevicePath, out hDevice) != STDFU.STDFU_NOERROR)
                    {
                        throw new Exception("Could not open DFU device!");
                    }
                }
            }
            else
            {
                throw new Exception("There must be exactly one DFU device attached to the computer!");
            }

            SetupDiDestroyDeviceInfoList(hInfoSet);

            return(hDevice);
        }
Пример #16
0
        public void WriteFirmware_2017(byte[] data)
        {
            IntPtr strinng = Marshal.AllocHGlobal(255);

            STDFU.STDFU_GetStringDescriptor(ref hDevice, 1, strinng, 255);

            byte[] bytes = new byte[255];
            Marshal.Copy(strinng, bytes, 0, 255);


            String device = Encoding.ASCII.GetString(bytes).Trim();

            Console.WriteLine("Device: " + device);

            if (device.Equals("AnyRoad Technology"))
            {
                Console.WriteLine("Radio not in DFU Mode!");
                return;
            }

            byte[] tmp = new byte[14];
            Array.Copy(data, 0, tmp, 0, 14);
            if (Encoding.GetEncoding("ASCII").GetString(tmp) == "OutSecurityBin")
            {
                byte[] newdata = new byte[data.Length - 0x100];
                Array.Copy(data, 0x100, newdata, 0, newdata.Length);
                data = newdata;
            }

            //byte[] unkData = File.ReadAllBytes("C:\\TYT\\FW_2017_f.bin");

            uint[] addresses1 =
            {
                0x00060000,
                0x00070000,
                0x00080000,
                0x00090000,
                0x000a0000,
                0x000b0000,
                0x000c0000,
                0x000d0000,
                0x000e0000,
                0x000f0000,

                0x0800c000,
                0x08010000,
                0x08020000,
                0x08040000,
                0x08060000,
                0x08080000,
                0x080a0000,
                0x080c0000,
                0x080e0000
            };

            uint[] block_ends = { 0x11, 0x41, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81 };

            WaitUntilIdle();

            CustomCommand(0x91, 0x01);
            CustomCommand(0x91, 0x31);

            //CustomCommand(0xA2, 0x01);
            //CustomCommand(0xA2, 0x31);
            //CustomCommand(0xA2, 0x02);
            //CustomCommand(0xA2, 0x03);
            //CustomCommand(0xA2, 0x04);
            //CustomCommand(0xA2, 0x07);
            //CustomCommand(0x91, 0x31);



            int jj = 0;

            foreach (uint addr in addresses1)
            {
                progress = (int)((float)jj++ / (float)0.0) + 1;
                EraseSector(addr);
            }

            SetAddressPointer(0);

            uint block_size  = 1024;
            uint block_start = 2;
            uint address_idx = 0;
            uint datawritten = 0;

            for (; datawritten < data.Length;)
            {
                Console.WriteLine("\r" + "% complete");
                SetAddressPointer(addresses1[i]);


                uint   block_number = block_start;
                byte[] buffa        = new byte[block_size];

                uint blk_data_len = 0;

                if (block_number == 0x42)
                {
                }
                else
                {
                    for (int b = 0; b < buffa.Length; b++)
                    {
                        buffa[b] = 0xFF;
                    }

                    uint dat_siz = (uint)data.Length - datawritten;
                    Array.Copy(data, datawritten, buffa, 0, (dat_siz < block_size ? dat_siz : block_size));
                    datawritten  += (dat_siz < block_size ? dat_siz : block_size);
                    blk_data_len += (dat_siz < block_size ? dat_siz : block_size);

                    WriteBlock(buffa, block_number);
                    WaitUntilIdle();

                    block_number++;
                    int stuff = (int)((float)datawritten / (float)(data.Length) * 50.0) + 50;
                    progress = (stuff == 0 ? 1 : stuff);
                }
                address_idx += 1;
            }
        }
Пример #17
0
        public void WriteFirmware(byte[] data)
        {
            IntPtr strinng = Marshal.AllocHGlobal(255);

            STDFU.STDFU_GetStringDescriptor(ref hDevice, 1, strinng, 255);

            byte[] bytes = new byte[255];
            Marshal.Copy(strinng, bytes, 0, 255);


            String device = Encoding.ASCII.GetString(bytes).Trim();

            Console.WriteLine("Device: " + device);

            if (device.Equals("AnyRoad Technology"))
            {
                Console.WriteLine("Radio not in DFU Mode!");
                return;
            }

            byte[] tmp = new byte[14];
            Array.Copy(data, 0, tmp, 0, 14);
            if (Encoding.GetEncoding("ASCII").GetString(tmp) == "OutSecurityBin")
            {
                byte[] newdata = new byte[data.Length - 0x100];
                Array.Copy(data, 0x100, newdata, 0, newdata.Length);
                data = newdata;
            }

            uint[] addresses =
            {
                0x0800c000,
                0x08010000,
                0x08020000,
                0x08040000,
                0x08060000,
                0x08080000,
                0x080a0000,
                0x080c0000,
                0x080e0000
            };

            uint[] sizes =
            {
                0x4000,    // 0c
                0x10000,   // 1
                0x20000,   // 2
                0x20000,   // 4
                0x20000,   // 6
                0x20000,   // 8
                0x20000,   // a
                0x20000,   // c
                0x20000    // e
            };

            uint[] block_ends = { 0x11, 0x41, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81 };

            WaitUntilIdle();

            CustomCommand(0x91, 0x01);
            CustomCommand(0x91, 0x31);

            int jj = 0;

            foreach (uint addr in addresses)
            {
                progress = (int)((float)jj++ / (addresses.Length + 1) * 50.0) + 1;
                EraseSector(addr);
            }

            uint block_size  = 1024;
            uint block_start = 2;
            uint address_idx = 0;
            uint datawritten = 0;

            for (uint i = 0; i < addresses.Length && datawritten < data.Length; i++)
            {
                Console.WriteLine("\r" + (address_idx * 100 / addresses.Length) + "% complete");
                SetAddressPointer(addresses[i]);


                uint   block_number = block_start;
                byte[] buffa        = new byte[block_size];

                uint blk_data_len = 0;

                while (blk_data_len < sizes[i] && datawritten < data.Length)
                {
                    if (block_number > block_ends[address_idx])
                    {
                        // Console.WriteLine("Something bad happenin!");
                        return;
                    }

                    for (int b = 0; b < buffa.Length; b++)
                    {
                        buffa[b] = 0xFF;
                    }

                    uint dat_siz = (uint)data.Length - datawritten;
                    Array.Copy(data, datawritten, buffa, 0, (dat_siz < block_size ? dat_siz : block_size));
                    datawritten  += (dat_siz < block_size ? dat_siz : block_size);
                    blk_data_len += (dat_siz < block_size ? dat_siz : block_size);

                    WriteBlock(buffa, block_number);
                    WaitUntilIdle();

                    block_number++;
                    int stuff = (int)((float)datawritten / (float)(data.Length) * 50.0) + 50;
                    progress = (stuff == 0 ? 1 : stuff);
                }
                address_idx += 1;
            }
        }