/* extended erase memory page */ private async Task ExtendedEraseSpecial(STExtendedEraseMode mode) { /* command word */ var tx = new byte[5]; /* temporary storage for response bytes */ var tmp = new byte[1]; /* command code */ tx[0] = (byte)STCmds.EXT_ERASE; /* checksum */ tx[1] = ComputeChecksum(tx, 0, 1); /* erase single page */ tx[2] = (byte)((int)mode >> 8); tx[3] = (byte)((int)mode >> 0); /* checksum */ tx[4] = (byte)~ComputeChecksum(tx, 2, 3); /* try to send command and wait for response */ try { /* send bytes */ await SerialWrite(tx, 0, 2); /* wait for response code */ await SerialRead(tmp, 0, 1); /* check response code */ if (tmp[0] != (byte)STResps.ACK) { throw new STBootException("Command Rejected"); } /* send address */ await SerialWrite(tx, 2, 3); /* wait for response code. use longer timeout, erase might * take a while or two. */ await SerialRead(tmp, 0, 1, 10000); /* check response code */ if (tmp[0] != (byte)STResps.ACK) { throw new STBootException("Special code Rejected"); } /* oops, something baaad happened! */ } catch (Exception) { /* release semaphore */ sem.Release(); /* re-throw */ throw; } /* release semaphore */ sem.Release(); }
/****************************************************************************** * 擦除制定页特殊模式 指令 * async 是.NET 4.5之后才用的关键字,用于方便地处理异步操作 * ******************************************************************************/ private async Task ExtendedEraseSpecial(STExtendedEraseMode mode) { /* 发送数组 */ var tx = new byte[5]; /* 接收缓冲数组 */ var tmp = new byte[1]; /* 赋值 */ tx[0] = (byte)STCmds.EXT_ERASE; /* 校验 */ tx[1] = ComputeChecksum(tx, 0, 1); /* 拆分 */ tx[2] = (byte)((int)mode >> 8); tx[3] = (byte)((int)mode >> 0); /* 校验 */ tx[4] = (byte)~ComputeChecksum(tx, 2, 3); /* 发送命令并等待应答 */ try { /* 发送指令 */ await SerialWrite(tx, 0, 2); /* 等待应答 */ await SerialRead(tmp, 0, 1); /* 判断是否为ACK */ if (tmp[0] != (byte)STResps.ACK) { throw new STBootException("Command Rejected"); } /* 发送模式字节 */ await SerialWrite(tx, 2, 3); /* 等待,设置超时时间为1000ms */ await SerialRead(tmp, 0, 1, 10000); /* 是否为ACK */ if (tmp[0] != (byte)STResps.ACK) { throw new STBootException("Special code Rejected"); } /* 出错 */ } catch (Exception) { /* 释放信号量 */ sem.Release(); /* */ throw; } /* 释放信号量 */ sem.Release(); }
/* extended erase memory page */ private async Task ExtendedEraseSpecial(STExtendedEraseMode mode) { /* command word */ var tx = new byte[5]; /* temporary storage for response bytes */ var tmp = new byte[1]; /* command code */ tx[0] = (byte)STCmds.EXT_ERASE; /* checksum */ tx[1] = ComputeChecksum(tx, 0, 1); /* erase single page */ tx[2] = (byte)((int)mode >> 8); tx[3] = (byte)((int)mode >> 0); /* checksum */ tx[4] = (byte)~ComputeChecksum(tx, 2, 3); /* try to send command and wait for response */ try { /* send bytes */ await SerialWrite(tx, 0, 2); /* wait for response code */ await SerialRead(tmp, 0, 1); /* check response code */ if (tmp[0] != (byte)STResps.ACK) throw new STBootException("Command Rejected"); /* send address */ await SerialWrite(tx, 2, 3); /* wait for response code. use longer timeout, erase might * take a while or two. */ await SerialRead(tmp, 0, 1, 10000); /* check response code */ if (tmp[0] != (byte)STResps.ACK) throw new STBootException("Special code Rejected"); /* oops, something baaad happened! */ } catch (Exception) { /* release semaphore */ sem.Release(); /* re-throw */ throw; } /* release semaphore */ sem.Release(); }