static void Main(string[] args) { usb_device.DEVICE_INFO DevInfo = new usb_device.DEVICE_INFO(); USB2SPI.SPI_FLASH_CONFIG SPIFlashConfig = new USB2SPI.SPI_FLASH_CONFIG(); Int32[] DevHandles = new Int32[20]; Int32 DevHandle = 0; Int32 SPIIndex = 0; bool state; Int32 DevNum, ret; Byte[] WriteBuffer = new Byte[64]; Byte[] ReadBuffer = new Byte[20480]; //扫描查找设备 DevNum = usb_device.USB_ScanDevice(DevHandles); if (DevNum <= 0) { Console.WriteLine("No device connected!"); return; } else { Console.WriteLine("Have {0} device connected!", DevNum); } DevHandle = DevHandles[0];//选择设备0 //打开设备 state = usb_device.USB_OpenDevice(DevHandle); if (!state) { Console.WriteLine("Open device error!"); return; } else { Console.WriteLine("Open device success!"); } //获取固件信息 StringBuilder FuncStr = new StringBuilder(256); state = usb_device.DEV_GetDeviceInfo(DevHandle, ref DevInfo, FuncStr); if (!state) { Console.WriteLine("Get device infomation error!"); return; } else { Console.WriteLine("Firmware Info:"); Console.WriteLine(" Name:" + Encoding.Default.GetString(DevInfo.FirmwareName)); Console.WriteLine(" Build Date:" + Encoding.Default.GetString(DevInfo.BuildDate)); Console.WriteLine(" Firmware Version:v{0}.{1}.{2}", (DevInfo.FirmwareVersion >> 24) & 0xFF, (DevInfo.FirmwareVersion >> 16) & 0xFF, DevInfo.FirmwareVersion & 0xFFFF); Console.WriteLine(" Hardware Version:v{0}.{1}.{2}", (DevInfo.HardwareVersion >> 24) & 0xFF, (DevInfo.HardwareVersion >> 16) & 0xFF, DevInfo.HardwareVersion & 0xFFFF); Console.WriteLine(" Functions:" + DevInfo.Functions.ToString("X8")); Console.WriteLine(" Functions String:" + FuncStr); Console.WriteLine(" Serial Number:" + DevInfo.SerialNumber[0].ToString("X8") + DevInfo.SerialNumber[1].ToString("X8") + DevInfo.SerialNumber[2].ToString("X8")); } //根据W25Q64配置相关参数 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.NumPages = 32768; SPIFlashConfig.PageSize = 256; SPIFlashConfig.ReadDataAddressBytes = 3; SPIFlashConfig.ReadFastAddressBytes = 3; SPIFlashConfig.SectorSize = 4096; SPIFlashConfig.WritePageAddressBytes = 3; ret = USB2SPI.SPI_FlashInit(DevHandle, SPIIndex, 50000000 >> 2, ref SPIFlashConfig); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("Initialize Device Error!"); return; } //读取芯片ID ret = USB2SPI.SPI_FlashReadID(DevHandle, SPIIndex, SPIFlashConfig.ID); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("Get Device ID Error!"); return; } else { Console.Write("ID = "); for (int i = 0; i < SPIFlashConfig.ID_Length; i++) { Console.Write(SPIFlashConfig.ID[i].ToString("X2")); } Console.WriteLine("\n"); if ((SPIFlashConfig.ID[0] == 0xFF) && (SPIFlashConfig.ID[1] == 0xFF)) { return; } } //擦除扇区 ret = USB2SPI.SPI_FlashEraseSector(DevHandle, SPIIndex, 0, 1); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("Erase Sector Error!"); return; } Byte[] TestBuffer = new Byte[20 * 1024]; for (int i = 0; i < TestBuffer.Length; i++) { TestBuffer[i] = (Byte)i; } ret = USB2SPI.SPI_FlashWrite(DevHandle, SPIIndex, 0, TestBuffer, 256); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("Flash Write Error!"); return; } ret = USB2SPI.SPI_FlashReadFast(DevHandle, SPIIndex, 0, TestBuffer, 256); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("Flash Read Error!"); return; } for (int i = 0; i < 256; i++) { if ((i % 16) == 0) { Console.WriteLine(""); } Console.Write(TestBuffer[i].ToString("X2") + " "); } Console.WriteLine("\n"); }
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); } }
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); } }