继续前文。
7、IR
根据之前的JTAG框图,下一步就是选择寄存器,也就是IR in。
JNIEXPORT jstring JNICALL Java_copy_FTDI_1copy_runIRcommand
(JNIEnv* env, jobject, jstring line) {
const char* cStr = env->GetStringUTFChars(line, nullptr);
int linelength = strlen(cStr);
int writebytecnt = 999;
int writecnt = 0;
std::string cap = "";
dwNumBytesToSend = 0;
byOutputBuffer[dwNumBytesToSend++] = 0x4B;
byOutputBuffer[dwNumBytesToSend++] = 0x03;
byOutputBuffer[dwNumBytesToSend++] = 3;
if (linelength-1 > 0) {
writebytecnt = ((linelength - 1) / 8);
if (writebytecnt > 0) {
int length = writebytecnt - 1;
byOutputBuffer[dwNumBytesToSend++] = 0x39;
byOutputBuffer[dwNumBytesToSend++] = (length & 0xFF);
byOutputBuffer[dwNumBytesToSend++] = ((length / 256) & 0xFF);
do {
std::string str = std::string(cStr).substr(linelength - ((writecnt + 1) * 8), 8);
BYTE num = std::stoi(str.c_str(), nullptr, 2);
byOutputBuffer[dwNumBytesToSend++] = num;
writecnt++;
} while (writecnt < writebytecnt);
}
}
int remainbitcnt = ((linelength - 1) % 8);
if (remainbitcnt > 0) {
byOutputBuffer[dwNumBytesToSend++] = 0x3B;
byOutputBuffer[dwNumBytesToSend++] = remainbitcnt - 1;
std::string str = std::string(cStr).substr(1, remainbitcnt);
BYTE num = std::stoi(str.c_str(), nullptr, 2);
byOutputBuffer[dwNumBytesToSend++] = num;
}
//get last bit
std::string str = std::string(cStr).substr(0, 1);
BYTE num = std::stoi(str.c_str(), nullptr, 2);
printf("IR last num =%s _ %d\n", str.c_str(), num);
byOutputBuffer[dwNumBytesToSend++] = 0x6E;
byOutputBuffer[dwNumBytesToSend++] = 0x03;
byOutputBuffer[dwNumBytesToSend++] = (num*128 + 3);
byOutputBuffer[dwNumBytesToSend++] = 0x87;
ftStatus |= FT_Write(ftHandle, byOutputBuffer, dwNumBytesToSend, &dwNumBytesSent);
if (ftStatus != FT_OK) {
cap += "device ir write fail!!!!";
}
dwNumBytesToSend = 0;
//read buffer
int byteCnts = (linelength % 8) == 0 ? linelength / 8 : linelength / 8 + 1;
int targetBytes = ((linelength - 1) % 8) == 0 ? (((linelength - 1) / 8) + 1) : (((linelength - 1) / 8) + 2);
int RemainingBits = (byte)(8 - ((linelength - 1) % 8));
//do
//{
// ftStatus = FT_GetQueueStatus(ftHandle, &dwNumBytesToRead);
//} while ((dwNumBytesToRead == 0) && (ftStatus == FT_OK));//or Timeout
ftStatus |= FT_Read(ftHandle, &byInputBuffer, targetBytes, &dwNumBytesRead);
printf("capture1 = %d\n", dwNumBytesRead);
printf("capture3 = %d\n", targetBytes);
std::string binaryStr = "";
if (RemainingBits < 8) {
//binaryStr = std::bitset<8>(byInputBuffer[dwNumBytesRead]).to_string();
binaryStr = std::string(binaryStr).substr(0, remainbitcnt);
//cap += binaryStr;
//printf("capture00_%2d = %s\n", 00, cap.c_str());
binaryStr = std::bitset<8>(byInputBuffer[dwNumBytesRead - 1]).to_string();
binaryStr = std::string(binaryStr).substr(3, 1);
cap += binaryStr;
printf("capture11_%d = %s\n", 11, cap.c_str());
binaryStr = std::bitset<8>(byInputBuffer[dwNumBytesRead - 2]).to_string();
binaryStr = std::string(binaryStr).substr(0, remainbitcnt);
cap += binaryStr;
printf("capture22_%d = %s\n", 22, cap.c_str());
for (int i = dwNumBytesRead - 3; i >= 0; i--) {
binaryStr = std::bitset<8>(byInputBuffer[i]).to_string();
cap += binaryStr;
printf("capture4_%d = %s\n", i, cap.c_str());
}
printf("\ncapture333 = %s\n", cap.c_str());
}
else {
binaryStr = std::bitset<8>(byInputBuffer[dwNumBytesRead - 1]).to_string();
binaryStr = std::string(binaryStr).substr(3, 1);
cap += binaryStr;
printf("capture11_%d = %s\n", 11, cap.c_str());
for (int i = dwNumBytesRead - 2; i >= 0; i--) {
binaryStr = std::bitset<8>(byInputBuffer[i]).to_string();
cap