public static void SPIWriteReadThread(object DevHandleStr) { USB2SPI.SPI_CONFIG SPIConfig = new USB2SPI.SPI_CONFIG(); int ret; int SPIIndex = 0;//使用SPI1 int DevHandle = Convert.ToInt32(DevHandleStr.ToString()); //根据W25Q40配置USB2SPI适配器 Console.WriteLine("{0}配置USB2SPI适配器...", DevHandle); SPIConfig.ClockSpeedHz = 25000000 >> 2; SPIConfig.CPHA = 0; SPIConfig.CPOL = 0; SPIConfig.LSBFirst = USB2SPI.SPI_MSB; SPIConfig.Master = USB2SPI.SPI_MASTER; SPIConfig.Mode = USB2SPI.SPI_MODE_HARD_HDX; SPIConfig.SelPolarity = USB2SPI.SPI_SEL_LOW; ret = USB2SPI.SPI_Init(DevHandle, SPIIndex, ref SPIConfig); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("{0}初始化设备错误!", DevHandle); return; } //写数据 Console.WriteLine("{0}USB2SPI适配器写数据...", DevHandle); Byte[] WriteData = new Byte[10240]; ret = USB2SPI.SPI_WriteBytes(DevHandle, SPIIndex, WriteData, WriteData.Length); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("{0}写数据出错!", SPIIndex); while (true) { ; } } //读数据 Console.WriteLine("{0}USB2SPI适配器读数据...", DevHandle); Byte[] ReadData = new Byte[10240]; ret = USB2SPI.SPI_ReadBytes(DevHandle, SPIIndex, ReadData, ReadData.Length); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("{0}读数据出错!", SPIIndex); while (true) { ; } } }
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 int WriteDataFromHexFile(int DevHandle, int SPIIndex, string FileName) { StreamReader HexReader = new StreamReader(FileName); String szLine = ""; String szHex = ""; int StartAddr = 0; int Addr; while (true) { szLine = HexReader.ReadLine(); //读取一行数据 if (szLine == null) //读完所有行 { break; } if (szLine.Substring(0, 1) == ":") //判断第1字符是否是: { int DataLen = Convert.ToByte(szLine.Substring(1, 2), 16); Byte DataType = Convert.ToByte(szLine.Substring(7, 2), 16); if (DataType == 0x01)//'01' End of File Record: 用来标识文件结束,放在文件的最后,标识HEX文件的结尾 { break; } if (DataType == 0x04)//'04' Extended Linear Address Record: 用来标识扩展线性地址的记录 { StartAddr = Convert.ToInt16(szLine.Substring(9, 4), 16); } if (DataType == 0x00)//'00' Data Rrecord:用来记录数据,HEX文件的大部分记录都是数据记录 { Addr = Convert.ToUInt16(szLine.Substring(3, 4), 16) + (StartAddr << 16); szHex = szLine.Substring(9, DataLen * 2); //读取有效字符:后0和1 Byte[] DataBuffer = new Byte[DataLen]; Int32 i = 0, j = 0; Byte RealCheckSum = 0; //数据校验和 for (i = 0; i < DataLen; i++) //两字符合并成一个16进制字节 { DataBuffer[j] = Convert.ToByte(szHex.Substring(i * 2, 2), 16); RealCheckSum += DataBuffer[j]; j++; } for (i = 0; i < 4; i++) { RealCheckSum += Convert.ToByte(szLine.Substring(1 + i * 2, 2), 16); } Byte CheckSum = Convert.ToByte(szLine.Substring(9 + DataLen * 2, 2), 16);//文件中记录的校验和 if (CheckSum != (Byte)(0x100 - RealCheckSum)) { Console.WriteLine("{0}文件数据校验有误!", DevHandle); break; } int ret = USB2SPI.SPI_FlashWrite(DevHandle, SPIIndex, Addr, DataBuffer, DataLen); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("{0}芯片写数据出错!", DevHandle); return(ret); } } } } HexReader.Close(); //关闭目标文件 return(0); }
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"); }
static void Main(string[] args) { usb_device.DEVICE_INFO DevInfo = new usb_device.DEVICE_INFO(); USB2SPI.SPI_CONFIG SPIConfig = new USB2SPI.SPI_CONFIG(); Int32[] DevHandles = new Int32[20]; Int32 DevHandle = 0; Int32 ADS1256Index = USB2SPI.SPI1_CS0; UInt32 ResetPinMask, DrdyPinMask; 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]; //打开设备 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")); } //配置RESET引脚和DRADY引脚 if (ADS1256Index == 0) { ResetPinMask = 1 << 9; DrdyPinMask = 1 << 8; } else { ResetPinMask = 1 << 1; DrdyPinMask = 1 << 0; } ret = USB2GPIO.GPIO_SetOutput(DevHandle, ResetPinMask, 1); if (ret != USB2GPIO.GPIO_SUCCESS) { Console.WriteLine("Initialize gpio error!"); return; } //配置SPI总线相关参数 SPIConfig.Mode = USB2SPI.SPI_MODE_HARD_HDX; SPIConfig.ClockSpeedHz = 1562500; SPIConfig.CPHA = 1; SPIConfig.CPOL = 0; SPIConfig.LSBFirst = USB2SPI.SPI_MSB; SPIConfig.Master = USB2SPI.SPI_MASTER; SPIConfig.SelPolarity = USB2SPI.SPI_SEL_LOW; ret = USB2SPI.SPI_Init(DevHandle, ADS1256Index, ref SPIConfig); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("Initialize device error!"); return; } //RESET引脚输出低脉冲复位ADS1256 USB2GPIO.GPIO_Write(DevHandle, ResetPinMask, 0); //输出低电平 System.Threading.Thread.Sleep(10); USB2GPIO.GPIO_Write(DevHandle, ResetPinMask, ResetPinMask); //输出高电平 //准备配置寄存器数据 WriteBuffer[0] = 0x50; //ADS1256_CMD_WREG WriteBuffer[1] = 0x04; WriteBuffer[2] = 0x04; WriteBuffer[3] = 0x08; //配置AIN0为单端模式 WriteBuffer[4] = 0; //PGA WriteBuffer[5] = 0xA1; //1000SPS WriteBuffer[6] = 0xFF; ret = USB2SPI.SPI_WriteBytesOfEvent(DevHandle, ADS1256Index, WriteBuffer, 7, (Int32)DrdyPinMask, 0x00, 1000); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("SPI Event Write Data Error!"); return; } //准备读配置寄存器数据,验证配置数据写入是否成功 WriteBuffer[0] = 0x10;//ADS1256_CMD_RREG; WriteBuffer[1] = 4; ret = USB2SPI.SPI_WriteReadBytesOfEvent(DevHandle, ADS1256Index, WriteBuffer, 2, ReadBuffer, 5, 10, (Int32)DrdyPinMask, 0x10, 1000); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("SPI Event Write&Read Data Error!"); return; } //判断写入配置寄存器的值和读出寄存器的值是否一样 if ((WriteBuffer[3] != ReadBuffer[1]) || (WriteBuffer[4] != ReadBuffer[2]) || (WriteBuffer[5] != ReadBuffer[3])) { Console.WriteLine("Config ADS1256 Error!"); return; } //发送连续采集数据命令,该命令只适合连续采集一个通道的情况 WriteBuffer[0] = 0xFC; //ADS1256_CMD_SYNC WriteBuffer[1] = 0x00; //ADS1256_CMD_WAKEUP WriteBuffer[2] = 0x03; //ADS1256_CMD_RDATAC ret = USB2SPI.SPI_WriteReadBytesOfEvent(DevHandle, ADS1256Index, WriteBuffer, 3, ReadBuffer, 3, 10, (Int32)DrdyPinMask, 0x10, 1000); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("SPI Event Write&Read Data Error!"); return; } Console.WriteLine("Continuous Get ADC Data!"); //检测DRADY引脚下降沿之后读回数据 int ReadDataNum = 10; ret = USB2SPI.SPI_BlockReadBytesOfEvent(DevHandle, ADS1256Index, ReadBuffer, 3, ReadDataNum, (Int32)DrdyPinMask, 0x10, 100); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("SPI Event BlockRead Data Error!"); return; } //将读回的数据转换为实际的电压值 for (int i = 0; i < ReadDataNum; i++) { int ADCDataTemp = (ReadBuffer[i * 3 + 0] << 16) | (ReadBuffer[i * 3 + 1] << 8) | ReadBuffer[i * 3 + 2]; if ((ADCDataTemp & 0x800000) != 0x00) { ADCDataTemp = (Int32)(0xFF000000 | ((UInt32)ADCDataTemp)); } double Volutage = (ADCDataTemp * 0.59604644775390625) / 1000000; Console.WriteLine("ADC[{0}] = {1} V", i, Volutage); } //发送停止连续采样数据命令 WriteBuffer[0] = 0x0F;//ADS1256_CMD_SDATAC ret = USB2SPI.SPI_WriteBytesOfEvent(DevHandle, ADS1256Index, WriteBuffer, 1, (Int32)DrdyPinMask, 0x10, 1000); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("SPI Event Write Data Error!"); return; } Console.WriteLine("Circulate Get ADC Data!"); //循环采集每个通道数据 ReadDataNum = 3; byte Channel = 0; WriteBuffer[0] = 0x50 | 1;//ADS1256_CMD_WREG WriteBuffer[1] = 0x00; WriteBuffer[2] = (byte)((Channel << 4) | 0x08); WriteBuffer[3] = 0xFC; //ADS1256_CMD_SYNC WriteBuffer[4] = 0x00; //ADS1256_CMD_WAKEUP WriteBuffer[5] = 0x01; //ADS1256_CMD_RDATA for (int i = 0; i < ReadDataNum; i++) { for (Channel = 0; Channel < 8; Channel++) { WriteBuffer[2] = (byte)((Channel << 4) | 0x08); ret = USB2SPI.SPI_WriteReadBytesOfEvent(DevHandle, ADS1256Index, WriteBuffer, 6, ReadBuffer, 3, 10, (Int32)DrdyPinMask, 0x10, 1000); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("SPI Event Write&Read Data Error!"); return; } else { //丢弃第一次的数据 if ((i == 0) && (Channel == 0)) { continue; } int ADCDataTemp = (ReadBuffer[0] << 16) | (ReadBuffer[1] << 8) | ReadBuffer[2]; if ((ADCDataTemp & 0x800000) != 0x00) { ADCDataTemp = (Int32)(0xFF000000 | ((UInt32)ADCDataTemp)); } double Volutage = (ADCDataTemp * 0.59604644775390625) / 1000000; Console.WriteLine("ADC_CH[{0}][{1}] = {2} V", i, Channel == 0 ? 7 : Channel - 1, Volutage); } } } }
static void Main(string[] args) { usb_device.DEVICE_INFO DevInfo = new usb_device.DEVICE_INFO(); USB2SPI.SPI_CONFIG SPIConfig = new USB2SPI.SPI_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]; //打开设备 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); } //配置SPI总线相关参数 Console.Write("Please input SPI channel(0 or 1):"); SPIIndex = Convert.ToInt32(Console.ReadLine()); Console.Write("Please input CPHA(0 or 1):"); SPIConfig.CPHA = Convert.ToByte(Console.ReadLine()); Console.Write("Please input CPOL(0 or 1):"); SPIConfig.CPOL = Convert.ToByte(Console.ReadLine()); Console.WriteLine("SPIConfig.CPHA = " + SPIConfig.CPHA.ToString()); Console.WriteLine("SPIConfig.CPOL = " + SPIConfig.CPOL.ToString()); //配置SPI总线相关参数(配置为从机模式) SPIConfig.Mode = USB2SPI.SPI_MODE_HARD_FDX; SPIConfig.ClockSpeedHz = 50000000; //SPIConfig.CPHA = 0; //SPIConfig.CPOL = 1; SPIConfig.LSBFirst = USB2SPI.SPI_MSB; SPIConfig.Master = USB2SPI.SPI_SLAVE; SPIConfig.SelPolarity = USB2SPI.SPI_SEL_LOW; ret = USB2SPI.SPI_Init(DevHandle, SPIIndex, ref SPIConfig); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("Initialize device error!"); return; } /* * for (int i = 0; i < WriteBuffer.Length; i++) * { * WriteBuffer[i] = (Byte)i; * } * while(true){ * ret = USB2SPI.SPI_SlaveWriteBytes(DevHandle, SPIIndex, WriteBuffer, 16, 5000); * if (ret != USB2SPI.SPI_SUCCESS) * { * Console.WriteLine("SPI slave write data error!"); * return; * } * Console.ReadLine(); * } */ Console.WriteLine("Press any key to exit the data reception!"); GC.KeepAlive(spi_get_data_callback); USB2SPI.SPI_SlaveContinueRead(DevHandle, SPIIndex, spi_get_data_callback); Console.ReadLine(); USB2SPI.SPI_SlaveContinueReadStop(DevHandle, SPIIndex); Console.WriteLine("Test SPI_SUCCESS!"); return; }
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); } }
static void Main(string[] args) { usb_device.DEVICE_INFO DevInfo = new usb_device.DEVICE_INFO(); USB2SPI.SPI_CONFIG SPIConfig = new USB2SPI.SPI_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]; //打开设备 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")); } //配置SPI总线相关参数 SPIConfig.Mode = USB2SPI.SPI_MODE_HARD_HDX; SPIConfig.ClockSpeedHz = 50000000; SPIConfig.CPHA = 0; SPIConfig.CPOL = 0; SPIConfig.LSBFirst = USB2SPI.SPI_MSB; SPIConfig.Master = USB2SPI.SPI_MASTER; SPIConfig.SelPolarity = USB2SPI.SPI_SEL_LOW; ret = USB2SPI.SPI_Init(DevHandle, SPIIndex, ref SPIConfig); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("Initialize device error!"); return; } //SPI发送数据 for (int i = 0; i < WriteBuffer.Length; i++) { WriteBuffer[i] = (Byte)i; } ret = USB2SPI.SPI_WriteBytes(DevHandle, SPIIndex, WriteBuffer, WriteBuffer.Length); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("SPI write data error!"); return; } //SPI异步发送数据,调用该函数后会立即返回,但是SPI数据不一定发送完毕,但是在下一次发送数据之前会保证数据发送完毕 for (int i = 0; i < 64; i++) { ret = USB2SPI.SPI_WriteBytesAsync(DevHandle, SPIIndex, WriteBuffer, WriteBuffer.Length); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("SPI async write data error!"); return; } } //SPI接收数据 ret = USB2SPI.SPI_ReadBytes(DevHandle, SPIIndex, ReadBuffer, 32); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("SPI read data error!"); return; } else { Console.WriteLine("Read Data:"); for (int i = 0; i < 32; i++) { Console.Write(ReadBuffer[i].ToString("X2") + " "); if (((i + 1) % 16) == 0) { Console.WriteLine(""); } } Console.WriteLine(""); } //SPI先发送数据,再接收数据,整个过程片选信号一直有效 int IntervalTime = 10;//发送和接收数据之间的时间间隔,单位为us ret = USB2SPI.SPI_WriteReadBytes(DevHandle, SPIIndex, WriteBuffer, WriteBuffer.Length, ReadBuffer, 32, IntervalTime); if (ret != USB2SPI.SPI_SUCCESS) { Console.WriteLine("SPI write read data error!"); return; } else { Console.WriteLine("Read Data:"); for (int i = 0; i < 32; i++) { Console.Write(ReadBuffer[i].ToString("X2") + " "); if (((i + 1) % 16) == 0) { Console.WriteLine(""); } } Console.WriteLine(""); } Console.WriteLine("Test SPI_SUCCESS!"); return; }