private int _EraseOb() { IAP_Protocol_TypeDef cmd; int sizeCmd; byte[] buf; IntPtr ptr; if (i2cWriteRawCB == null) { return(-1); } tbLog.AppendText("Erase Option ... "); cmd = new IAP_Protocol_TypeDef(); cmd.Code = IAP_CODE_Enum.IAP_CMD_ERASE; cmd.uLength = 12; cmd.uPara = 0; cmd.uStAddr = 0x1FF00000; cmd.uEndAddr = cmd.uStAddr + 1024 - 1; cmd.Check = ~(cmd.Code); cmd.uCRC = 0; sizeCmd = Marshal.SizeOf(typeof(IAP_Protocol_TypeDef)); buf = new byte[sizeCmd]; ptr = Marshal.AllocHGlobal(sizeCmd); Marshal.StructureToPtr(cmd, ptr, false); Marshal.Copy(ptr, buf, 0, sizeCmd); cmd.uCRC = _CalcCRC16(buf, 4, sizeCmd); Marshal.StructureToPtr(cmd, ptr, true); Marshal.Copy(ptr, buf, 0, sizeCmd); Marshal.FreeHGlobal(ptr); if (i2cWriteRawCB(0x70, (byte)sizeCmd, buf) < 0) { return(-1); } tbLog.AppendText("\r\nTx buf[" + sizeCmd + "]"); _BufDumpLog(buf, sizeCmd); Thread.Sleep(2); _CheckDeviceReady(); Thread.Sleep(10); tbLog.AppendText("Done.\r\n"); return(0); }
private int _StartAp() { IAP_Protocol_TypeDef cmd; int sizeCmd; byte[] buf; IntPtr ptr; if (i2cWriteRawCB == null) { return(-1); } tbLog.AppendText("Reset ... "); cmd = new IAP_Protocol_TypeDef(); cmd.Code = IAP_CODE_Enum.IAP_CMD_START_APP; cmd.uLength = 0; cmd.Check = ~(cmd.Code); cmd.uCRC = 0; sizeCmd = Marshal.SizeOf(typeof(IAP_Protocol_TypeDef)); buf = new byte[sizeCmd]; ptr = Marshal.AllocHGlobal(sizeCmd); Marshal.StructureToPtr(cmd, ptr, true); Marshal.Copy(ptr, buf, 0, sizeCmd); Marshal.FreeHGlobal(ptr); if (i2cWriteRawCB(0x70, 4, buf) < 0) { return(-1); } //tbLog.AppendText("\r\nTx buf[4]"); //_BufDumpLog(buf, sizeCmd); Thread.Sleep(2); _CheckDeviceReady(); Thread.Sleep(10); tbLog.AppendText("Done.\r\n"); return(0); }
private int _CrcAp() { IAP_Protocol_TypeDef cmd; int sizeCmd; byte[] buf, fileBuf; IntPtr ptr; ushort crc16; if (i2cWriteRawCB == null) { return(-1); } if (tbApFilePath.Text.Length == 0) { tbLog.AppendText("Please select AP file!!!\r\n"); return(-2); } tbLog.AppendText("CRC AP ... "); fileBuf = File.ReadAllBytes(tbApFilePath.Text); cmd = new IAP_Protocol_TypeDef(); cmd.Code = IAP_CODE_Enum.IAP_CMD_CRC; cmd.uLength = 12; cmd.uPara = 0; cmd.uStAddr = (uint)IAP_Enum.IAP_SKIP_VECTOR; cmd.uEndAddr = (uint)fileBuf.Length - 1; cmd.Check = ~(cmd.Code); cmd.uCRC = 0; sizeCmd = Marshal.SizeOf(typeof(IAP_Protocol_TypeDef)); buf = new byte[sizeCmd]; ptr = Marshal.AllocHGlobal(sizeCmd); Marshal.StructureToPtr(cmd, ptr, false); Marshal.Copy(ptr, buf, 0, sizeCmd); cmd.uCRC = _CalcCRC16(buf, 4, sizeCmd); Marshal.StructureToPtr(cmd, ptr, true); Marshal.Copy(ptr, buf, 0, sizeCmd); Marshal.FreeHGlobal(ptr); if (i2cWriteRawCB(0x70, (byte)sizeCmd, buf) < 0) { return(-1); } //tbLog.AppendText("\r\nTx buf[" + sizeCmd + "]"); //_BufDumpLog(buf, sizeCmd); Thread.Sleep(2); _CheckDeviceReady(); crc16 = _CalcCRC16(fileBuf, (int)IAP_Enum.IAP_SKIP_VECTOR, fileBuf.Length); buf = BitConverter.GetBytes(crc16); if (i2cWriteRawCB(0x70, 2, buf) < 0) { return(-1); } //tbLog.AppendText("Tx buf[" + 2 + "]"); //_BufDumpLog(buf, 2); Thread.Sleep(2); _CheckDeviceReady(); Thread.Sleep(10); tbLog.AppendText("Done.\r\n"); return(0); }
private int _FlashAp(IAP_PARA_Enum type) { IAP_Protocol_TypeDef cmd; int sizeCmd, index; byte[] buf, fileBuf, tmp; IntPtr ptr; ushort crc16; if (i2cWriteRawCB == null) { return(-1); } if (tbApFilePath.Text.Length == 0) { tbLog.AppendText("Please select AP file!!!\r\n"); return(-2); } if (type == IAP_PARA_Enum.IAP_PARA_PROGRAM) { tbLog.AppendText("Program AP ... "); } else { tbLog.AppendText("Verify AP ... "); } fileBuf = File.ReadAllBytes(tbApFilePath.Text); cmd = new IAP_Protocol_TypeDef(); cmd.Code = IAP_CODE_Enum.IAP_CMD_FLASH; cmd.uLength = 12; cmd.uPara = (byte)type; cmd.uStAddr = 0; cmd.uEndAddr = (uint)fileBuf.Length - 1; cmd.Check = ~(cmd.Code); cmd.uCRC = 0; sizeCmd = Marshal.SizeOf(typeof(IAP_Protocol_TypeDef)); buf = new byte[sizeCmd]; ptr = Marshal.AllocHGlobal(sizeCmd); Marshal.StructureToPtr(cmd, ptr, false); Marshal.Copy(ptr, buf, 0, sizeCmd); cmd.uCRC = _CalcCRC16(buf, 4, sizeCmd); Marshal.StructureToPtr(cmd, ptr, true); Marshal.Copy(ptr, buf, 0, sizeCmd); Marshal.FreeHGlobal(ptr); if (i2cWriteRawCB(0x70, (byte)sizeCmd, buf) < 0) { return(-1); } //tbLog.AppendText("Tx buf[" + sizeCmd + "]"); //_BufDumpLog(buf, sizeCmd); Thread.Sleep(2); _CheckDeviceReady(); buf = new byte[130]; index = 0; tbLog.AppendText(fileBuf.Length.ToString() + "\r\n"); while (index < fileBuf.Length) { tbLog.AppendText(index.ToString() + " "); if (index + 128 < fileBuf.Length) { Buffer.BlockCopy(fileBuf, index, buf, 0, 128); index += 128; } else { Buffer.BlockCopy(fileBuf, index, buf, 0, fileBuf.Length - index); index += fileBuf.Length - index; } crc16 = _CalcCRC16(buf, 0, 128); tmp = BitConverter.GetBytes(crc16); Buffer.BlockCopy(tmp, 0, buf, 128, 2); if (i2cWriteRawCB(0x70, 130, buf) < 0) { return(-1); } //tbLog.AppendText("Tx buf[" + 130 + "]"); //_BufDumpLog(buf, 130); Thread.Sleep(2); _CheckDeviceReady(); Thread.Sleep(2); } tbLog.AppendText("Done.\r\n"); Thread.Sleep(10); return(0); }