/* erase memory page */ private async Task EraseSpecial(STEraseMode mode) { /* command word */ var tx = new byte[4]; /* temporary storage for response bytes */ var tmp = new byte[1]; /* command code */ tx[0] = (byte)STCmds.ERASE; /* checksum */ tx[1] = ComputeChecksum(tx, 0, 1); /* erase single page */ tx[2] = (byte)((int)mode); /* checksum */ tx[3] = (byte)~ComputeChecksum(tx, 2, 2); /* 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, 2); /* wait for response code */ await SerialRead(tmp, 0, 1); /* 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 EraseSpecial(STEraseMode mode) { /* 发送数组 */ var tx = new byte[4]; /* 接收缓冲数组 */ var tmp = new byte[1]; /* 赋值 */ tx[0] = (byte)STCmds.ERASE; /* 校验 */ tx[1] = ComputeChecksum(tx, 0, 1); /* 擦除单页 */ tx[2] = (byte)((int)mode); /* 校验 */ tx[3] = (byte)~ComputeChecksum(tx, 2, 2); /* 发送指令并等待回复 */ 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, 2); /* 等待应答 */ await SerialRead(tmp, 0, 1); /* 判断是否为ACK */ if (tmp[0] != (byte)STResps.ACK) { throw new STBootException("Special Code Rejected"); } /* 发生错误 */ } catch (Exception) { /* 释放信号量 */ sem.Release(); /* re-throw */ throw; } /* 释放信号量 */ sem.Release(); }
/* erase memory page */ private async Task EraseSpecial(STEraseMode mode) { /* command word */ var tx = new byte[4]; /* temporary storage for response bytes */ var tmp = new byte[1]; /* command code */ tx[0] = (byte)STCmds.ERASE; /* checksum */ tx[1] = ComputeChecksum(tx, 0, 1); /* erase single page */ tx[2] = (byte)((int)mode); /* checksum */ tx[3] = (byte)~ComputeChecksum(tx, 2, 2); /* 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, 2); /* wait for response code */ await SerialRead(tmp, 0, 1); /* 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(); }