示例#1
0
        public static void ChipWriteDataThread(object Index)
        {
            USB2SPI.SPI_FLASH_CONFIG SPIFlashConfig = new USB2SPI.SPI_FLASH_CONFIG();
            int ret;
            int SPIIndex  = 0;//使用SPI1
            int DevHandle = Convert.ToInt32(Index.ToString());

            //根据W25Q40配置USB2SPI适配器
            Console.WriteLine("{0}配置USB2SPI适配器...", DevHandle);
            SPIFlashConfig.CMD_EraseSector         = 0x20;
            SPIFlashConfig.CMD_ReadData            = 0x03;
            SPIFlashConfig.CMD_ReadFast            = 0x0B;
            SPIFlashConfig.CMD_ReadID              = 0x9F;
            SPIFlashConfig.CMD_ReadStatus          = 0x05;
            SPIFlashConfig.CMD_WriteEnable         = 0x06;
            SPIFlashConfig.CMD_WritePage           = 0x02;
            SPIFlashConfig.CMD_EraseChip           = 0xC7;
            SPIFlashConfig.EraseSectorAddressBytes = 3;
            SPIFlashConfig.ID_Length             = 3;
            SPIFlashConfig.ID                    = new Byte[16];
            SPIFlashConfig.NumPages              = 4096;
            SPIFlashConfig.PageSize              = 256;
            SPIFlashConfig.ReadDataAddressBytes  = 3;
            SPIFlashConfig.ReadFastAddressBytes  = 3;
            SPIFlashConfig.SectorSize            = 4096;
            SPIFlashConfig.WritePageAddressBytes = 3;
            ret = USB2SPI.SPI_FlashInit(DevHandle, SPIIndex, 50000000 >> 1, ref SPIFlashConfig);
            if (ret != USB2SPI.SPI_SUCCESS)
            {
                Console.WriteLine("{0}初始化设备错误!", DevHandle);
                return;
            }
            //读取芯片ID

            /*Console.WriteLine("{0}读取芯片ID...", SPIIndex);
             * ret = USB2SPI.SPI_FlashReadID(DevHandle, SPIIndex, SPIFlashConfig.ID);
             * if (ret != USB2SPI.SPI_SUCCESS)
             * {
             *  Console.WriteLine("{0}获取芯片ID出错!", SPIIndex);
             *  return;
             * }
             * else
             * {
             *  Console.Write("{0}芯片ID为:", SPIIndex);
             *  for (int i = 0; i < SPIFlashConfig.ID_Length; i++)
             *  {
             *      Console.Write(SPIFlashConfig.ID[i].ToString("X2"));
             *  }
             *  Console.WriteLine("");
             * }*/
            //整片擦除,该操作比较耗时
            Console.WriteLine("{0}开始擦除芯片!", DevHandle);
            ret = USB2SPI.SPI_FlashEraseChip(DevHandle, SPIIndex);
            if (ret != USB2SPI.SPI_SUCCESS)
            {
                Console.WriteLine("擦除芯片失败!");
                return;
            }
            //读取Hex文件并将数据烧入Flash
            Console.WriteLine("开始将Hex文件中的数据写入芯片!");
            ret = WriteDataFromHexFile(DevHandle, SPIIndex, "CAACDS00-001-R02M0.HEX");
            if (ret != USB2SPI.SPI_SUCCESS)
            {
                Console.WriteLine("{0}芯片写数据出错!", DevHandle);
                return;
            }
            //读取Hex文件并校验Flash中的数据
            Console.WriteLine("开始校验Flash中的数据!");
            int ErrorDataCount = VerifyDataFromHexFile(DevHandle, SPIIndex, "CAACDS00-001-R02M0.HEX");

            //统计出错的数据字节数
            if (ErrorDataCount > 0)
            {
                Console.WriteLine("{0}数据校验失败!有{1}字节出错!", DevHandle, ErrorDataCount);
            }
            else if (ErrorDataCount == 0)
            {
                Console.WriteLine("{0}烧写完毕!数据校验无误!", DevHandle);
            }
            else
            {
                Console.WriteLine("{0}读数据出错,数据校验失败!", DevHandle);
            }
        }
示例#2
0
        public static void ChipWriteDataThread(object Handle)
        {
            USB2SPI.SPI_FLASH_CONFIG SPIFlashConfig = new USB2SPI.SPI_FLASH_CONFIG();
            int ret;
            int SPIIndex  = 0;//使用SPI1
            int DevHandle = Convert.ToInt32(Handle.ToString());

            //根据W25Q40配置USB2SPI适配器
            Console.WriteLine("{0}配置USB2SPI适配器...", DevHandle);
            SPIFlashConfig.CMD_EraseSector         = 0x20;
            SPIFlashConfig.CMD_ReadData            = 0x03;
            SPIFlashConfig.CMD_ReadFast            = 0x0B;
            SPIFlashConfig.CMD_ReadID              = 0x9F;
            SPIFlashConfig.CMD_ReadStatus          = 0x05;
            SPIFlashConfig.CMD_WriteEnable         = 0x06;
            SPIFlashConfig.CMD_WritePage           = 0x02;
            SPIFlashConfig.CMD_EraseChip           = 0xC7;
            SPIFlashConfig.EraseSectorAddressBytes = 3;
            SPIFlashConfig.ID_Length             = 3;
            SPIFlashConfig.ID                    = new Byte[16];
            SPIFlashConfig.NumPages              = 4096;
            SPIFlashConfig.PageSize              = 256;
            SPIFlashConfig.ReadDataAddressBytes  = 3;
            SPIFlashConfig.ReadFastAddressBytes  = 3;
            SPIFlashConfig.SectorSize            = 4096;
            SPIFlashConfig.WritePageAddressBytes = 3;
            ret = USB2SPI.SPI_FlashInit(DevHandle, SPIIndex, 50000000 >> 1, ref SPIFlashConfig);
            if (ret != USB2SPI.SPI_SUCCESS)
            {
                Console.WriteLine("{0}初始化设备错误!", DevHandle);
                return;
            }
            //读取芯片ID
            Console.WriteLine("{0}读取芯片ID...", SPIIndex);
            ret = USB2SPI.SPI_FlashReadID(DevHandle, SPIIndex, SPIFlashConfig.ID);
            if (ret != USB2SPI.SPI_SUCCESS)
            {
                Console.WriteLine("{0}获取芯片ID出错!", SPIIndex);
                return;
            }
            else
            {
                Console.Write("{0}芯片ID为:", SPIIndex);
                for (int i = 0; i < SPIFlashConfig.ID_Length; i++)
                {
                    Console.Write(SPIFlashConfig.ID[i].ToString("X2"));
                }
                Console.WriteLine("");
            }
            //整片擦除,该操作比较耗时
            Console.WriteLine("{0}开始擦除芯片!", DevHandle);
            ret = USB2SPI.SPI_FlashEraseChip(DevHandle, SPIIndex);
            if (ret != USB2SPI.SPI_SUCCESS)
            {
                Console.WriteLine("擦除芯片失败!");
                return;
            }
            //将要烧写的文件读到内存
            const string name = "data.bin";
            //打开文件
            FileStream fs = null;

            try
            {
                fs = fs = new FileStream(name, FileMode.Open, FileAccess.Read);
            }
            catch (Exception ex)
            {
                Console.WriteLine("{0}打开文件失败!", DevHandle);
                return;
            }
            // 读取数据
            BinaryReader r            = new BinaryReader(fs);
            long         ReadDataSize = fs.Length < (SPIFlashConfig.PageSize * SPIFlashConfig.NumPages) ? fs.Length : SPIFlashConfig.PageSize * SPIFlashConfig.NumPages;

            byte[] DataBuffer = r.ReadBytes((int)ReadDataSize);
            //循环将数据写入芯片
            Console.WriteLine("{0}开始写数据!", DevHandle);
            int count     = 0;
            int PackSize  = 10240;
            int StartAddr = 0;

            for (count = 0; count < (ReadDataSize / PackSize); count++)
            {
                byte[] DataBufferTemp = new byte[PackSize];
                Array.ConstrainedCopy(DataBuffer, PackSize * count, DataBufferTemp, 0, PackSize);
                ret = USB2SPI.SPI_FlashWrite(DevHandle, SPIIndex, StartAddr, DataBufferTemp, PackSize);
                if (ret != USB2SPI.SPI_SUCCESS)
                {
                    Console.WriteLine("{0}芯片写数据出错!", DevHandle);
                    return;
                }
                StartAddr += PackSize;
            }
            if ((ReadDataSize % PackSize) > 0)
            {
                byte[] DataBufferTemp = new byte[ReadDataSize % PackSize];
                Array.ConstrainedCopy(DataBuffer, PackSize * count, DataBufferTemp, 0, (int)(ReadDataSize % PackSize));
                ret = USB2SPI.SPI_FlashWrite(DevHandle, SPIIndex, StartAddr, DataBufferTemp, (int)(ReadDataSize % PackSize));
                if (ret != USB2SPI.SPI_SUCCESS)
                {
                    Console.WriteLine("{0}芯片写数据出错!", DevHandle);
                    return;
                }
            }
            //校验数据
            Console.WriteLine("{0}开始校验数据!", DevHandle);
            int ErrorDataCount = 0;

            StartAddr = 0;
            for (count = 0; count < (ReadDataSize / PackSize); count++)
            {
                byte[] DataBufferTemp = new byte[PackSize];
                ret = USB2SPI.SPI_FlashRead(DevHandle, SPIIndex, StartAddr, DataBufferTemp, PackSize);
                if (ret != USB2SPI.SPI_SUCCESS)
                {
                    Console.WriteLine("{0}芯片读数据出错!", DevHandle);
                    return;
                }
                else
                {
                    StartAddr += PackSize;
                    for (int i = 0; i < PackSize; i++)
                    {
                        if (DataBufferTemp[i] != DataBuffer[PackSize * count + i])
                        {
                            ErrorDataCount++;
                        }
                    }
                }
            }
            if ((ReadDataSize % PackSize) > 0)
            {
                byte[] DataBufferTemp = new byte[ReadDataSize % PackSize];
                ret = USB2SPI.SPI_FlashRead(DevHandle, SPIIndex, StartAddr, DataBufferTemp, (int)(ReadDataSize % PackSize));
                if (ret != USB2SPI.SPI_SUCCESS)
                {
                    Console.WriteLine("{0}芯片读数据出错!", DevHandle);
                    return;
                }
                else
                {
                    for (int i = 0; i < (ReadDataSize % PackSize); i++)
                    {
                        if (DataBufferTemp[i] != DataBuffer[PackSize * count + i])
                        {
                            ErrorDataCount++;
                        }
                    }
                }
            }
            //统计出错的数据字节数
            if (ErrorDataCount > 0)
            {
                Console.WriteLine("{0}数据校验失败!有{1}字节出错!", DevHandle, ErrorDataCount);
            }
            else
            {
                Console.WriteLine("{0}烧写完毕!数据校验无误!", DevHandle);
            }
        }