예제 #1
0
파일: Program.cs 프로젝트: luyang14/TinyG
        private static bool PrepareMemoryBin(MemorySpecification ms)
        {
            bool result = false;

            try
            {
                using (FileStream fs = new FileStream(ms.FileName, FileMode.Open, FileAccess.Read))
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        int    len   = (int)fs.Length;
                        byte[] image = new byte[len];
                        if (len == br.Read(image, 0, len))
                        {
                            ms.Image = image;
                            result   = true;
                        }
                        br.Close();
                        fs.Close();
                    }
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("PrepareMemory: {0}", ex.Message));
            }
            return(result);
        }
예제 #2
0
파일: Program.cs 프로젝트: luyang14/TinyG
 private static bool PrepareMemory(MemorySpecification ms)
 {
     if (!string.IsNullOrEmpty(ms.FileName) && File.Exists(ms.FileName))
     {
         if ((ms.FileFormat == FileFormat.Binary && PrepareMemoryBin(ms)) ||
             (ms.FileFormat == FileFormat.Hex && PrepareMemoryHex(ms)))
         {
             return(true);
         }
         else
         {
             Console.WriteLine(string.Format("Can't prepare memory: {0}", ms.FileName));
         }
     }
     return(false);
 }
예제 #3
0
        private static bool WriteMemory(MemorySpecification ms)
        {
            bool result = false;

            if (SendCommand(XBOOT.CMD_CHIP_ERASE).Status != ResponseStatus.Ack)
            {
                Console.WriteLine("Chip erase error");
                return false;
            }
            if (SendCommand(XBOOT.CMD_CHECK_BLOCK_SUPPORT).Status != ResponseStatus.Yes)
            {
                Console.WriteLine("No block support");
                return false;
            }
            BlockSize = (UInt16)((UInt16)Response.Answer[1] << 8 | (UInt16)Response.Answer[2]);
            if (!SendStartAddress(StartAddress))
                return false;

            Console.WriteLine(" Write pass ----------");
            Console.Write("            ");
            int len = ms.Image.Length;
            int bsize;
            XBOOT.CMD_BLOCK_LOAD.Data = ms.Image;
            XBOOT.CMD_BLOCK_LOAD.DataIndex = 0;
            result = true;
            //! CRC16.Reset();
            int percent = 0, n_percent;
            while (len != 0)
            {
                bsize = (len >= BlockSize) ? BlockSize : len;
                len -= bsize;

                XBOOT.CMD_BLOCK_LOAD.Command[1] = (byte)((bsize >> 8) & 0xFF);
                XBOOT.CMD_BLOCK_LOAD.Command[2] = (byte)((bsize >> 0) & 0xFF);
                XBOOT.CMD_BLOCK_LOAD.DataCount = bsize;
                if (SendCommand(XBOOT.CMD_BLOCK_LOAD).Status != ResponseStatus.Ack)
                {
                    Console.WriteLine("Block load error");
                    result = false;
                    break;
                }

                while (bsize-- != 0)
                {
                    //! CRC16.Write(XBOOT.CMD_BLOCK_LOAD.Data[XBOOT.CMD_BLOCK_LOAD.DataIndex++]);
                    XBOOT.CMD_BLOCK_LOAD.DataIndex++;
                }

                n_percent = 10 * XBOOT.CMD_BLOCK_LOAD.DataIndex / ms.Image.Length;
                if (percent != n_percent)
                {
                    Console.Write("#");
                    percent = n_percent;
                }
            }
            Console.WriteLine("");
            if (!result)
                return false;

            Console.WriteLine("Verify pass ----------");
            Console.Write("            ");
            if (!SendStartAddress(StartAddress))
                return false;

            len = ms.Image.Length;
            result = true;
            XBOOT.CMD_BLOCK_LOAD.DataIndex = 0;
            percent = 0;
            while (len != 0)
            {
                bsize = (len >= BlockSize) ? BlockSize : len;
                len -= bsize;

                XBOOT.CMD_BLOCK_READ.Command[1] = (byte)((bsize >> 8) & 0xFF);
                XBOOT.CMD_BLOCK_READ.Command[2] = (byte)((bsize >> 0) & 0xFF);
                XBOOT.CMD_BLOCK_READ.ResponseLength = bsize;
                if (SendCommand(XBOOT.CMD_BLOCK_READ).Status != ResponseStatus.Unknown)
                {
                    Console.WriteLine(string.Format(" Block read error at {0}", XBOOT.CMD_BLOCK_LOAD.DataIndex));
                    result = false;
                    break;
                }
                int idx = 0;
                while (bsize-- != 0)
                {
                    if (Response.Answer[idx++] != XBOOT.CMD_BLOCK_LOAD.Data[XBOOT.CMD_BLOCK_LOAD.DataIndex])
                    {
                        Console.WriteLine(string.Format(" Data not equal at {0:X} Origin:{1:X} Flash:{2:X}",
                            XBOOT.CMD_BLOCK_LOAD.DataIndex,
                            XBOOT.CMD_BLOCK_LOAD.Data[XBOOT.CMD_BLOCK_LOAD.DataIndex],
                            Response.Answer[idx++]
                            ));
                        result = false;
                        break;
                    }
                    XBOOT.CMD_BLOCK_LOAD.DataIndex++;
                }
                if (!result)
                    break;

                n_percent = 10 * XBOOT.CMD_BLOCK_LOAD.DataIndex / ms.Image.Length;
                if (percent != n_percent)
                {
                    Console.Write("#");
                    percent = n_percent;
                }
            }
            Console.WriteLine("");
            if (!result)
                return false;

            len = ms.Image.Length;

            XBOOT.CMD_CRC_WRITE.Command[1] = XBOOT.CMD_CRC.Command[2] = (byte)((len >> 16) & 0xFF);
            XBOOT.CMD_CRC_WRITE.Command[2] = XBOOT.CMD_CRC.Command[3] = (byte)((len >> 8) & 0xFF);
            XBOOT.CMD_CRC_WRITE.Command[3] = XBOOT.CMD_CRC.Command[4] = (byte)((len >> 0) & 0xFF);

            if (!SendStartAddress(StartAddress << 1))
                return false;

            if (SendCommand(XBOOT.CMD_CRC).Status != ResponseStatus.Ack)
            {
                Console.WriteLine("Get CRC error");
                return false;
            }

            UInt16 csum = (UInt16)((Response.Answer[1] << 8) | Response.Answer[2]);
            //!			if (csum != CRC16.Read())
            //!			{
            //!				Console.WriteLine("CRC not equal");
            //!				return false;
            //!			}
            if (SendCommand(XBOOT.CMD_CRC_WRITE).Status != ResponseStatus.Ack)
            {
                Console.WriteLine("Write CRC error");
                return false;
            }
            if (SendCommand(XBOOT.CMD_EXIT_BOOTLOADER).Status != ResponseStatus.Ack)
            {
                Console.WriteLine("Exit bootloader error");
                return false;
            }

            return true;
        }
예제 #4
0
        private static bool PrepareMemoryHex(MemorySpecification ms)
        {
            bool result = false;
            try
            {
                using (FileStream fs = new FileStream(ms.FileName, FileMode.Open, FileAccess.Read))
                using (TextReader br = new StreamReader(fs))
                {
                    result = true;
                    bool fail = false;
                    INTEL_COMMAND command = INTEL_COMMAND.EOF;
                    string line;
                    int lineNumber = 0;
                    uint count = 0, address = 0;
                    byte data = 0, checksum;
                    UInt32 extendAddress = 0, startAddress = 0, segment_address = 0;
                    int idx = 0;

                    while ((line = br.ReadLine()) != null)
                    {
                        lineNumber++;
                        line = line.Trim();
                        if (line.Length == 0)
                            continue;
                        if (line.StartsWith("S"))
                        {	// Motorola format
                            Console.WriteLine("Motorola format not support");
                            result = false;
                            break;
                        }
                        if (line.StartsWith(":"))
                        {	// Intel format
                            if (line.Length < 11)
                                fail = true;
                            else
                            {
                                fail |= !uint.TryParse(line.Substring(1, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out count);
                                fail |= !uint.TryParse(line.Substring(3, 4), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out address);
                                fail |= !byte.TryParse(line.Substring(7, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out data);
                                command = (INTEL_COMMAND)data;
                                fail |= !byte.TryParse(line.Substring(line.Length - 2, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out checksum);
                            }
                            if (fail)
                            {
                                Console.WriteLine(string.Format("Can't parse line {0}", lineNumber));
                                result = false;
                                break;
                            }
                            switch (command)
                            {
                                case INTEL_COMMAND.EOF: // End of File
                                    break;
                                case INTEL_COMMAND.DATA:
                                    idx = 9;
                                    goto data_loop;
                                case INTEL_COMMAND.DATA_LOOP:
                                data_loop:
                                    for (; !fail && count > 0; --count)
                                    {
                                        if (line.Length < idx + 2)
                                        {
                                            Console.WriteLine(string.Format("Data record too short at line {0}", lineNumber));
                                            fail = true;
                                        }
                                        else
                                            fail = !byte.TryParse(line.Substring(idx, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out data);

                                        if (!fail)
                                            writeToMemory(segment_address + extendAddress + address - StartAddress, data);

                                        address++;
                                        idx += 2;
                                    }
                                    break;
                                case INTEL_COMMAND.EXT_SEGMENT_ADDR: // Extended Segment Address Record
                                    #region Extended segment address record
                                    if (count != 2 || line.Length != 15)
                                    {
                                        Console.WriteLine(string.Format("Bad Extended segment address record line {0}.", lineNumber));
                                    }
                                    else
                                    {
                                        fail |= !uint.TryParse(line.Substring(9, 4), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out segment_address);
                                        if (fail)
                                        {
                                            result = false;
                                            Console.WriteLine(string.Format("Bad Extended segment address record line {0}.", lineNumber));
                                        }
                                        else
                                            segment_address <<= 4;
                                    }
                                    break;
                                    #endregion
                                case INTEL_COMMAND.SEGMENT_ADDR:
                                    #region Start Segment Address Record
                                    if (count != 4)
                                    {
                                        Console.WriteLine(string.Format("Bad Start Segment record line {0}.", lineNumber));
                                    }
                                    else
                                    {
                                        fail |= !uint.TryParse(line.Substring(9, 8), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out startAddress);
                                        if (fail)
                                        {
                                            result = false;
                                            Console.WriteLine(string.Format("Bad Start Segment records line {0}.", lineNumber));
                                        }
                                    }
                                    break;
                                    #endregion
                                case INTEL_COMMAND.EXTEND_ADDR:
                                    #region Extended Linear Address Record
                                    if (line.Length != 15)
                                    {
                                        Console.WriteLine(string.Format("Bad Extended Address record line {0}.", lineNumber));
                                        fail = true;
                                    }
                                    else
                                    {
                                        fail |= !uint.TryParse(line.Substring(9, 4), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out extendAddress);
                                        if (fail)
                                        {
                                            result = false;
                                            Console.WriteLine(string.Format("Bad Extended Address record line {0}.", lineNumber));
                                        }
                                        else
                                            extendAddress = extendAddress << 16;
                                    }
                                    break;
                                    #endregion
                                case INTEL_COMMAND.LINEAR_ADDR:
                                    #region Start Linear Address Record
                                    if (count != 4)
                                    {
                                        Console.WriteLine(string.Format("Bad Start Address record line {0}.", lineNumber));
                                    }
                                    else
                                    {
                                        fail |= !uint.TryParse(line.Substring(9, 8), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out startAddress);
                                        if (fail)
                                        {
                                            result = false;
                                            Console.WriteLine(string.Format("Bad Start Address record line {0}.", lineNumber));
                                        }
                                    }
                                    break;
                                    #endregion
                                default:
                                    Console.WriteLine(string.Format("Bad command {0} at line {1}.", command, lineNumber));
                                    fail = true;
                                    result = false;
                                    break;
                            }
                            if (fail)
                                break;
                        }

                    }
                    br.Close();
                    fs.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("PrepareMemory: {0}", ex.Message));
            }
            if (result && HexMaxAddress >= HexMinAddress)
            {
                StartAddress = HexMinAddress;
                uint i = HexMaxAddress - HexMinAddress + 1;
                byte[] image = new byte[i];
                while (i != 0)
                {
                    --i;
                    image[i] = HexMemory[i];
                }
                ms.Image = image;
            }
            return result;
        }
예제 #5
0
 private static bool PrepareMemoryBin(MemorySpecification ms)
 {
     bool result = false;
     try
     {
         using (FileStream fs = new FileStream(ms.FileName, FileMode.Open, FileAccess.Read))
         using (BinaryReader br = new BinaryReader(fs))
         {
             int len = (int)fs.Length;
             byte[] image = new byte[len];
             if (len == br.Read(image, 0, len))
             {
                 ms.Image = image;
                 result = true;
             }
             br.Close();
             fs.Close();
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(string.Format("PrepareMemory: {0}", ex.Message));
     }
     return result;
 }
예제 #6
0
 private static bool PrepareMemory(MemorySpecification ms)
 {
     if (!string.IsNullOrEmpty(ms.FileName) && File.Exists(ms.FileName))
     {
         if ((ms.FileFormat == FileFormat.Binary && PrepareMemoryBin(ms)) ||
             (ms.FileFormat == FileFormat.Hex && PrepareMemoryHex(ms)))
         {
             return true;
         }
         else
         {
             Console.WriteLine(string.Format("Can't prepare memory: {0}", ms.FileName));
         }
     }
     return false;
 }
예제 #7
0
        private static bool ParseOptions(string[] args)
        {
            string opt = string.Empty;
            bool param = false;
            bool error = false;
            for(int idx = 0; idx < args.Length; idx++)
            {
                string arg = args[idx];
                if (arg.StartsWith("-"))
                {
                    opt = arg;
                    param = false;
                }
                switch (opt)
                {
                    case "-p":
                        #region Set device type
                        if (param)
                        {
                            DeviceName = arg;
                            if (DeviceName == "x256a3")
                            {
                                StartAddress = 0;
                                Size = (256 + 8) * 1024;
                                EndAddress = StartAddress + Size - 1;
                            }
                            param = false;
                        }
                        else
                            param = true;
                        break;
                        #endregion
                    case "-P":
                        #region Set port name
                        if (param)
                        {
                            PortName = arg.ToUpperInvariant();
                            param = false;
                        }
                        else
                            param = true;
                        break;
                        #endregion
                    case "-b":
                        #region Set port speed
                        if (param)
                        {
                            if (!int.TryParse(arg, out PortSpeed))
                            {
                                Console.WriteLine("Bad port speed value");
                                error = true;
                            }
                            param = false;
                        }
                        else
                            param = true;
                        break;
                        #endregion
                    case "-U":
                        #region Memory operation specification
                        if (param)
                        {
                            MemorySpecification ms = new MemorySpecification();

                            if (arg.StartsWith("flash:"))
                                ms.MemoryType = MemoryType.Flash;
                            else if (arg.StartsWith("eeprom:"))
                                ms.MemoryType = MemoryType.EEPROM;
                            else if (arg.StartsWith("usersig:"))
                                ms.MemoryType = MemoryType.UserSig;

                            if (ms.MemoryType == MemoryType.Unknown)
                            {
                                Console.WriteLine(string.Format("Bad memory type {0}", arg));
                                error = true;
                            }
                            else
                            {
                                int i = arg.IndexOf(":");
                                if (i < arg.Length - 1)
                                {
                                    arg = arg.Substring(i + 1);
                                    if (arg.StartsWith("w:"))
                                        ms.MemoryOpearion = MemoryOpearion.Write;
                                    else if (arg.StartsWith("r:"))
                                        ms.MemoryOpearion = MemoryOpearion.Read;
                                    else if (arg.StartsWith("v:"))
                                        ms.MemoryOpearion = MemoryOpearion.Verify;
                                    if (ms.MemoryOpearion == MemoryOpearion.Unknown)
                                    {
                                        Console.WriteLine(string.Format("Bad memory operation {0}", arg));
                                        error = true;
                                    }
                                    else
                                    {
                                        i = arg.IndexOf(":");
                                        if (i < arg.Length - 1)
                                        {
                                            ms.FileName = arg.Substring(i + 1);
                                            if (File.Exists(ms.FileName))
                                            {
                                                FileInfo fi = new FileInfo(ms.FileName);
                                                switch (fi.Extension.ToUpperInvariant())
                                                {
                                                    case ".BIN":
                                                        ms.FileFormat = FileFormat.Binary;
                                                        break;
                                                    case ".HEX":
                                                        ms.FileFormat = FileFormat.Hex;
                                                        break;
                                                }
                                                if (ms.FileFormat == FileFormat.Unknown)
                                                {
                                                    Console.WriteLine(string.Format("Unknown file format {0}", ms.FileName));
                                                    error = true;
                                                }
                                                else
                                                {
                                                    MemorySpecifications.Add(ms);
                                                }
                                            }
                                            else
                                            {
                                                Console.WriteLine(string.Format("File not exist {0}", ms.FileName));
                                                error = true;
                                            }
                                        }
                                        else
                                        {
                                            Console.WriteLine(string.Format("Bad file name {0}", arg));
                                            error = true;
                                        }
                                    }
                                }
                                else
                                {
                                    Console.WriteLine(string.Format("Bad memory opearion {0}", arg));
                                    error = true;
                                }
                            }
                            param = false;
                        }
                        else
                            param = true;
                        break;
                        #endregion
                    case "-start":
                        if (param)
                        {
                            if (!ParseIntValue(arg, out StartAddress))
                            {
                                Console.WriteLine("Bad start address");
                                error = true;
                            }
                        }
                        else
                            param = true;
                        break;
                    case "-end":
                        if (param)
                        {
                            if (!ParseIntValue(arg, out EndAddress))
                            {
                                Console.WriteLine("Bad end address");
                                error = true;
                            }
                        }
                        else
                            param = true;
                        break;
                    case "-size":
                        if (param)
                        {
                            if (!ParseIntValue(arg, out Size))
                            {
                                Console.WriteLine("Bad size");
                                error = true;
                            }
                        }
                        else
                        {
                            param = true;
                        }
                        break;
                    default:
                        Console.WriteLine(string.Format("Unknown option: {0}", opt));
                        break;
                }
                if (!param)
                    opt = string.Empty;
            }
            return !error;
        }
예제 #8
0
파일: Program.cs 프로젝트: luyang14/TinyG
        private static bool ParseOptions(string[] args)
        {
            string opt   = string.Empty;
            bool   param = false;
            bool   error = false;

            for (int idx = 0; idx < args.Length; idx++)
            {
                string arg = args[idx];
                if (arg.StartsWith("-"))
                {
                    opt   = arg;
                    param = false;
                }
                switch (opt)
                {
                case "-p":
                    #region Set device type
                    if (param)
                    {
                        DeviceName = arg;
                        if (DeviceName == "x256a3")
                        {
                            StartAddress = 0;
                            Size         = (256 + 8) * 1024;
                            EndAddress   = StartAddress + Size - 1;
                        }
                        param = false;
                    }
                    else
                    {
                        param = true;
                    }
                    break;

                    #endregion
                case "-P":
                    #region Set port name
                    if (param)
                    {
                        PortName = arg.ToUpperInvariant();
                        param    = false;
                    }
                    else
                    {
                        param = true;
                    }
                    break;

                    #endregion
                case "-b":
                    #region Set port speed
                    if (param)
                    {
                        if (!int.TryParse(arg, out PortSpeed))
                        {
                            Console.WriteLine("Bad port speed value");
                            error = true;
                        }
                        param = false;
                    }
                    else
                    {
                        param = true;
                    }
                    break;

                    #endregion
                case "-U":
                    #region Memory operation specification
                    if (param)
                    {
                        MemorySpecification ms = new MemorySpecification();

                        if (arg.StartsWith("flash:"))
                        {
                            ms.MemoryType = MemoryType.Flash;
                        }
                        else if (arg.StartsWith("eeprom:"))
                        {
                            ms.MemoryType = MemoryType.EEPROM;
                        }
                        else if (arg.StartsWith("usersig:"))
                        {
                            ms.MemoryType = MemoryType.UserSig;
                        }

                        if (ms.MemoryType == MemoryType.Unknown)
                        {
                            Console.WriteLine(string.Format("Bad memory type {0}", arg));
                            error = true;
                        }
                        else
                        {
                            int i = arg.IndexOf(":");
                            if (i < arg.Length - 1)
                            {
                                arg = arg.Substring(i + 1);
                                if (arg.StartsWith("w:"))
                                {
                                    ms.MemoryOpearion = MemoryOpearion.Write;
                                }
                                else if (arg.StartsWith("r:"))
                                {
                                    ms.MemoryOpearion = MemoryOpearion.Read;
                                }
                                else if (arg.StartsWith("v:"))
                                {
                                    ms.MemoryOpearion = MemoryOpearion.Verify;
                                }
                                if (ms.MemoryOpearion == MemoryOpearion.Unknown)
                                {
                                    Console.WriteLine(string.Format("Bad memory operation {0}", arg));
                                    error = true;
                                }
                                else
                                {
                                    i = arg.IndexOf(":");
                                    if (i < arg.Length - 1)
                                    {
                                        ms.FileName = arg.Substring(i + 1);
                                        if (File.Exists(ms.FileName))
                                        {
                                            FileInfo fi = new FileInfo(ms.FileName);
                                            switch (fi.Extension.ToUpperInvariant())
                                            {
                                            case ".BIN":
                                                ms.FileFormat = FileFormat.Binary;
                                                break;

                                            case ".HEX":
                                                ms.FileFormat = FileFormat.Hex;
                                                break;
                                            }
                                            if (ms.FileFormat == FileFormat.Unknown)
                                            {
                                                Console.WriteLine(string.Format("Unknown file format {0}", ms.FileName));
                                                error = true;
                                            }
                                            else
                                            {
                                                MemorySpecifications.Add(ms);
                                            }
                                        }
                                        else
                                        {
                                            Console.WriteLine(string.Format("File not exist {0}", ms.FileName));
                                            error = true;
                                        }
                                    }
                                    else
                                    {
                                        Console.WriteLine(string.Format("Bad file name {0}", arg));
                                        error = true;
                                    }
                                }
                            }
                            else
                            {
                                Console.WriteLine(string.Format("Bad memory opearion {0}", arg));
                                error = true;
                            }
                        }
                        param = false;
                    }
                    else
                    {
                        param = true;
                    }
                    break;

                    #endregion
                case "-start":
                    if (param)
                    {
                        if (!ParseIntValue(arg, out StartAddress))
                        {
                            Console.WriteLine("Bad start address");
                            error = true;
                        }
                    }
                    else
                    {
                        param = true;
                    }
                    break;

                case "-end":
                    if (param)
                    {
                        if (!ParseIntValue(arg, out EndAddress))
                        {
                            Console.WriteLine("Bad end address");
                            error = true;
                        }
                    }
                    else
                    {
                        param = true;
                    }
                    break;

                case "-size":
                    if (param)
                    {
                        if (!ParseIntValue(arg, out Size))
                        {
                            Console.WriteLine("Bad size");
                            error = true;
                        }
                    }
                    else
                    {
                        param = true;
                    }
                    break;

                default:
                    Console.WriteLine(string.Format("Unknown option: {0}", opt));
                    break;
                }
                if (!param)
                {
                    opt = string.Empty;
                }
            }
            return(!error);
        }
예제 #9
0
파일: Program.cs 프로젝트: luyang14/TinyG
        private static bool PrepareMemoryHex(MemorySpecification ms)
        {
            bool result = false;

            try
            {
                using (FileStream fs = new FileStream(ms.FileName, FileMode.Open, FileAccess.Read))
                    using (TextReader br = new StreamReader(fs))
                    {
                        result = true;
                        bool          fail    = false;
                        INTEL_COMMAND command = INTEL_COMMAND.EOF;
                        string        line;
                        int           lineNumber = 0;
                        uint          count = 0, address = 0;
                        byte          data = 0, checksum;
                        UInt32        extendAddress = 0, startAddress = 0, segment_address = 0;
                        int           idx = 0;

                        while ((line = br.ReadLine()) != null)
                        {
                            lineNumber++;
                            line = line.Trim();
                            if (line.Length == 0)
                            {
                                continue;
                            }
                            if (line.StartsWith("S"))
                            {                           // Motorola format
                                Console.WriteLine("Motorola format not support");
                                result = false;
                                break;
                            }
                            if (line.StartsWith(":"))
                            {                           // Intel format
                                if (line.Length < 11)
                                {
                                    fail = true;
                                }
                                else
                                {
                                    fail   |= !uint.TryParse(line.Substring(1, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out count);
                                    fail   |= !uint.TryParse(line.Substring(3, 4), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out address);
                                    fail   |= !byte.TryParse(line.Substring(7, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out data);
                                    command = (INTEL_COMMAND)data;
                                    fail   |= !byte.TryParse(line.Substring(line.Length - 2, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out checksum);
                                }
                                if (fail)
                                {
                                    Console.WriteLine(string.Format("Can't parse line {0}", lineNumber));
                                    result = false;
                                    break;
                                }
                                switch (command)
                                {
                                case INTEL_COMMAND.EOF:                                 // End of File
                                    break;

                                case INTEL_COMMAND.DATA:
                                    idx = 9;
                                    goto data_loop;

                                case INTEL_COMMAND.DATA_LOOP:
data_loop:
                                    for (; !fail && count > 0; --count)
                                    {
                                        if (line.Length < idx + 2)
                                        {
                                            Console.WriteLine(string.Format("Data record too short at line {0}", lineNumber));
                                            fail = true;
                                        }
                                        else
                                        {
                                            fail = !byte.TryParse(line.Substring(idx, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out data);
                                        }

                                        if (!fail)
                                        {
                                            writeToMemory(segment_address + extendAddress + address - StartAddress, data);
                                        }

                                        address++;
                                        idx += 2;
                                    }
                                    break;

                                case INTEL_COMMAND.EXT_SEGMENT_ADDR:                                 // Extended Segment Address Record
                                    #region Extended segment address record
                                    if (count != 2 || line.Length != 15)
                                    {
                                        Console.WriteLine(string.Format("Bad Extended segment address record line {0}.", lineNumber));
                                    }
                                    else
                                    {
                                        fail |= !uint.TryParse(line.Substring(9, 4), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out segment_address);
                                        if (fail)
                                        {
                                            result = false;
                                            Console.WriteLine(string.Format("Bad Extended segment address record line {0}.", lineNumber));
                                        }
                                        else
                                        {
                                            segment_address <<= 4;
                                        }
                                    }
                                    break;

                                    #endregion
                                case INTEL_COMMAND.SEGMENT_ADDR:
                                    #region Start Segment Address Record
                                    if (count != 4)
                                    {
                                        Console.WriteLine(string.Format("Bad Start Segment record line {0}.", lineNumber));
                                    }
                                    else
                                    {
                                        fail |= !uint.TryParse(line.Substring(9, 8), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out startAddress);
                                        if (fail)
                                        {
                                            result = false;
                                            Console.WriteLine(string.Format("Bad Start Segment records line {0}.", lineNumber));
                                        }
                                    }
                                    break;

                                    #endregion
                                case INTEL_COMMAND.EXTEND_ADDR:
                                    #region Extended Linear Address Record
                                    if (line.Length != 15)
                                    {
                                        Console.WriteLine(string.Format("Bad Extended Address record line {0}.", lineNumber));
                                        fail = true;
                                    }
                                    else
                                    {
                                        fail |= !uint.TryParse(line.Substring(9, 4), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out extendAddress);
                                        if (fail)
                                        {
                                            result = false;
                                            Console.WriteLine(string.Format("Bad Extended Address record line {0}.", lineNumber));
                                        }
                                        else
                                        {
                                            extendAddress = extendAddress << 16;
                                        }
                                    }
                                    break;

                                    #endregion
                                case INTEL_COMMAND.LINEAR_ADDR:
                                    #region Start Linear Address Record
                                    if (count != 4)
                                    {
                                        Console.WriteLine(string.Format("Bad Start Address record line {0}.", lineNumber));
                                    }
                                    else
                                    {
                                        fail |= !uint.TryParse(line.Substring(9, 8), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out startAddress);
                                        if (fail)
                                        {
                                            result = false;
                                            Console.WriteLine(string.Format("Bad Start Address record line {0}.", lineNumber));
                                        }
                                    }
                                    break;

                                    #endregion
                                default:
                                    Console.WriteLine(string.Format("Bad command {0} at line {1}.", command, lineNumber));
                                    fail   = true;
                                    result = false;
                                    break;
                                }
                                if (fail)
                                {
                                    break;
                                }
                            }
                        }
                        br.Close();
                        fs.Close();
                    }
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("PrepareMemory: {0}", ex.Message));
            }
            if (result && HexMaxAddress >= HexMinAddress)
            {
                StartAddress = HexMinAddress;
                uint   i     = HexMaxAddress - HexMinAddress + 1;
                byte[] image = new byte[i];
                while (i != 0)
                {
                    --i;
                    image[i] = HexMemory[i];
                }
                ms.Image = image;
            }
            return(result);
        }
예제 #10
0
파일: Program.cs 프로젝트: luyang14/TinyG
        private static bool WriteMemory(MemorySpecification ms)
        {
            bool result = false;

            if (SendCommand(XBOOT.CMD_CHIP_ERASE).Status != ResponseStatus.Ack)
            {
                Console.WriteLine("Chip erase error");
                return(false);
            }
            if (SendCommand(XBOOT.CMD_CHECK_BLOCK_SUPPORT).Status != ResponseStatus.Yes)
            {
                Console.WriteLine("No block support");
                return(false);
            }
            BlockSize = (UInt16)((UInt16)Response.Answer[1] << 8 | (UInt16)Response.Answer[2]);
            if (!SendStartAddress(StartAddress))
            {
                return(false);
            }

            Console.WriteLine(" Write pass ----------");
            Console.Write("            ");
            int len = ms.Image.Length;
            int bsize;

            XBOOT.CMD_BLOCK_LOAD.Data      = ms.Image;
            XBOOT.CMD_BLOCK_LOAD.DataIndex = 0;
            result = true;
            //! CRC16.Reset();
            int percent = 0, n_percent;

            while (len != 0)
            {
                bsize = (len >= BlockSize) ? BlockSize : len;
                len  -= bsize;

                XBOOT.CMD_BLOCK_LOAD.Command[1] = (byte)((bsize >> 8) & 0xFF);
                XBOOT.CMD_BLOCK_LOAD.Command[2] = (byte)((bsize >> 0) & 0xFF);
                XBOOT.CMD_BLOCK_LOAD.DataCount  = bsize;
                if (SendCommand(XBOOT.CMD_BLOCK_LOAD).Status != ResponseStatus.Ack)
                {
                    Console.WriteLine("Block load error");
                    result = false;
                    break;
                }

                while (bsize-- != 0)
                {
                    //! CRC16.Write(XBOOT.CMD_BLOCK_LOAD.Data[XBOOT.CMD_BLOCK_LOAD.DataIndex++]);
                    XBOOT.CMD_BLOCK_LOAD.DataIndex++;
                }

                n_percent = 10 * XBOOT.CMD_BLOCK_LOAD.DataIndex / ms.Image.Length;
                if (percent != n_percent)
                {
                    Console.Write("#");
                    percent = n_percent;
                }
            }
            Console.WriteLine("");
            if (!result)
            {
                return(false);
            }

            Console.WriteLine("Verify pass ----------");
            Console.Write("            ");
            if (!SendStartAddress(StartAddress))
            {
                return(false);
            }

            len    = ms.Image.Length;
            result = true;
            XBOOT.CMD_BLOCK_LOAD.DataIndex = 0;
            percent = 0;
            while (len != 0)
            {
                bsize = (len >= BlockSize) ? BlockSize : len;
                len  -= bsize;

                XBOOT.CMD_BLOCK_READ.Command[1]     = (byte)((bsize >> 8) & 0xFF);
                XBOOT.CMD_BLOCK_READ.Command[2]     = (byte)((bsize >> 0) & 0xFF);
                XBOOT.CMD_BLOCK_READ.ResponseLength = bsize;
                if (SendCommand(XBOOT.CMD_BLOCK_READ).Status != ResponseStatus.Unknown)
                {
                    Console.WriteLine(string.Format(" Block read error at {0}", XBOOT.CMD_BLOCK_LOAD.DataIndex));
                    result = false;
                    break;
                }
                int idx = 0;
                while (bsize-- != 0)
                {
                    if (Response.Answer[idx++] != XBOOT.CMD_BLOCK_LOAD.Data[XBOOT.CMD_BLOCK_LOAD.DataIndex])
                    {
                        Console.WriteLine(string.Format(" Data not equal at {0:X} Origin:{1:X} Flash:{2:X}",
                                                        XBOOT.CMD_BLOCK_LOAD.DataIndex,
                                                        XBOOT.CMD_BLOCK_LOAD.Data[XBOOT.CMD_BLOCK_LOAD.DataIndex],
                                                        Response.Answer[idx++]
                                                        ));
                        result = false;
                        break;
                    }
                    XBOOT.CMD_BLOCK_LOAD.DataIndex++;
                }
                if (!result)
                {
                    break;
                }

                n_percent = 10 * XBOOT.CMD_BLOCK_LOAD.DataIndex / ms.Image.Length;
                if (percent != n_percent)
                {
                    Console.Write("#");
                    percent = n_percent;
                }
            }
            Console.WriteLine("");
            if (!result)
            {
                return(false);
            }

            len = ms.Image.Length;

            XBOOT.CMD_CRC_WRITE.Command[1] = XBOOT.CMD_CRC.Command[2] = (byte)((len >> 16) & 0xFF);
            XBOOT.CMD_CRC_WRITE.Command[2] = XBOOT.CMD_CRC.Command[3] = (byte)((len >> 8) & 0xFF);
            XBOOT.CMD_CRC_WRITE.Command[3] = XBOOT.CMD_CRC.Command[4] = (byte)((len >> 0) & 0xFF);

            if (!SendStartAddress(StartAddress << 1))
            {
                return(false);
            }

            if (SendCommand(XBOOT.CMD_CRC).Status != ResponseStatus.Ack)
            {
                Console.WriteLine("Get CRC error");
                return(false);
            }

            UInt16 csum = (UInt16)((Response.Answer[1] << 8) | Response.Answer[2]);

//!			if (csum != CRC16.Read())
//!			{
//!				Console.WriteLine("CRC not equal");
//!				return false;
//!			}
            if (SendCommand(XBOOT.CMD_CRC_WRITE).Status != ResponseStatus.Ack)
            {
                Console.WriteLine("Write CRC error");
                return(false);
            }
            if (SendCommand(XBOOT.CMD_EXIT_BOOTLOADER).Status != ResponseStatus.Ack)
            {
                Console.WriteLine("Exit bootloader error");
                return(false);
            }

            return(true);
        }