bool SoftwareI2CPort::sendByte(uint8_t byte, bool holdLow) { uint32_t mask = 0x80; for (mask = 0x80; mask != 0; mask >>= 1) { delay(m_timing.xmit_count); if (byte & mask) { m_sda.setDirection(GpioPin::kInput); } else { m_sda.setDirection(GpioPin::kOutput); } delay(m_timing.low_count); m_scl.setDirection(GpioPin::kInput); while (!m_scl); // wait for scl to go high, in case the slave is holding it low for clock stretching delay(m_timing.high_count + m_timing.rcv_count); m_scl.setDirection(GpioPin::kOutput); } return readACK(); }
TResult CKKMApi::exec_command(const QByteArray& baCmd, QByteArray& baAnswer, int nTimeOut) { TResult nRet = kResult_Success; LOG_DBG("exec_command ==>"); do { sendENQ(); readACK(); //sendENQ_readACK(); QByteArray baRawCommand = makeCommand(baCmd); LOG_DBG("command : '"<<ba2hex(baRawCommand)<<"'"); m_pSerial->write(baRawCommand); readACK(); sendEOT(); readENQ(nTimeOut); sendACK(); nRet = loadKKMData(baAnswer); if (nRet == kResult_Success) { if (baAnswer.size() > 2) { baAnswer = baAnswer.mid(1, baAnswer.size()-2); } else { nRet = kResult_ReadError; LOG_ERR("incorrect answer size"); break; } } sendACK(); readEOT(); } while (false); LOG_DBG("exec_command <=="); return nRet; }
TResult CKKMApi::sendENQ_readACK() { LOG_DBG("sendENQ_readACK ==>"); TResult nRet = kResult_Success; sendENQ(); nRet = readACK(); int nRepeatCount = 2; while (nRet != kResult_Success && nRepeatCount <= 5) { LOG_DBG("repeat sendENQ_readACK i = " << nRepeatCount); sendENQ(); nRet = readACK(); ++nRepeatCount; } LOG_DBG("sendENQ_readACK <=="); return nRet; }
char TWI_Read(uint8_t reply[], uint8_t n_Byte) { //TWI Read Data for (unsigned char i = 0; i < n_Byte; i++) { reply[i] = readACK(); // uart_writeInt8(reply[i]); // uart_writeString("-"); //_delay_ms(5); if (getStatus() != TW_MR_DATA_ACK) { //Error return 0; } } reply[n_Byte] = readNACK(); // uart_writeInt8(reply[n_Byte]); // uart_writeAbsatz(); if (getStatus() != TW_MR_DATA_NACK) { //Error return 0; } else { return 1; } }
bool TOCAnalyzer::shutdownTOCAnalyzer() { struct TOCData allocation; struct TOCData allocation2; memset(&allocation,0,30); memset(&allocation2,0,30); struct TOCData readData; int phase = 0; allocation.write[0] = ACKStruct.write[0]; allocation.write[1] = ACKStruct.write[1]; allocation.write[2] = ACKStruct.write[2]; allocation.write[3] = 0x0fa5; allocation.write[4] = 0x05f0; allocation.write[5] = 0x5104; allocation.write[6] = 0; allocation.write[7] = 0; allocation.write[8] = 0; allocation.write[9] = 0x0100; allocation.write[10] = 0; allocation.write[11]=0x5a00; allocation.size = 12; allocation2.write[0] = 0x0ba5; allocation2.write[1] = 0x06f4; allocation2.write[2] = 0x0204; allocation2.write[3] = 0; allocation2.write[4] = 0; allocation2.write[5] = 0; allocation2.write[6] = 0x0b00; allocation2.size = 7; LPOVERLAPPED send; LPOVERLAPPED read; while(true) { memset(&readData,0,20); if(phase==0) { send = sendToTOC(serialPortConfiguration,allocation); phase++; } else if(phase==1) { send = sendToTOC(serialPortConfiguration,allocation2); phase++; } else if(phase==2) { send = sendToTOC(serialPortConfiguration,ACKStruct); } Sleep(100); if(phase!=0) read = readFromTOC(serialPortConfiguration,9,readData.write); else read = readACK(); while((!HasOverlappedIoCompleted(send) || !HasOverlappedIoCompleted(read))||writing); delete send; delete read; if(readData.write[2]==11) { break; } Sleep(150); } sendToTOC(serialPortConfiguration,ACKStruct); CloseHandle(serialPortConfiguration); return true; }
bool TOCAnalyzer::performTOCLab() { LPOVERLAPPED readOverlapped; LPOVERLAPPED writeOverlapped; struct TOCData allocation; struct TOCData vialInformation; struct TOCData sampleInformation; struct TOCData sampleInformation2; for(int index=0; index<3; index++) vialInformation.write[index] = ACKStruct.write[index]; vialInformation.write[4] = 0X05ab; vialInformation.write[5] = 0x07f4; vialInformation.write[6] = 0x2804; vialInformation.write[7] = 0; vialInformation.write[8] = 0; vialInformation.write[9] = 0; vialInformation.write[10] = 0; vialInformation.write[11] = 0x3200; vialInformation.size = 12; initLab(sampleInformation,sampleData.getSampleInformation().getVialPosition()); for(int shift=0; shift<59; shift++) sampleInformation2.write[shift] = sampleInformation.write[shift+3]; sampleInformation2.write[1] +=(1<<8); sampleInformation2.write[38]+=(1<<8); sampleInformation2.write[58] +=(2<<8); sampleInformation2.size = 59; Sleep(10); memset(&vialInformation,0,sizeof(struct TOCData)); memset(&secWriteBuffer,0,sizeof(struct TOCData)); memset(&secReadBuffer,0,sizeof(struct TOCData)); memset(&writeBuffer,0,sizeof(struct TOCData)); memset(&readBuffer,0,sizeof(struct TOCData)); initLab(vialInformation,sampleData.getSampleInformation().getVialPosition()); phase(secWriteBuffer); phase2(writeBuffer); Sleep(300); writeOverlapped = sendToTOC(serialPortConfiguration,secWriteBuffer); Sleep(250); readOverlapped = readFromTOC(serialPortConfiguration,10,secReadBuffer.write); Sleep(400); while((!HasOverlappedIoCompleted(writeOverlapped) || !HasOverlappedIoCompleted(readOverlapped))||writing); delete writeOverlapped; delete readOverlapped; Sleep(400); writeOverlapped=sendToTOC(serialPortConfiguration,writeBuffer); Sleep(650); readOverlapped=readACK(); while((!HasOverlappedIoCompleted(writeOverlapped) || !HasOverlappedIoCompleted(readOverlapped))||writing); delete writeOverlapped; delete readOverlapped; Sleep(4000); int index=0; cout<<"phase 1 done"<<endl; while(1) { //allocation= new TOCData; memset(&allocation,0,sizeof(struct TOCData)); writeOverlapped=sendToTOC(serialPortConfiguration,ACKStruct); Sleep(50); readOverlapped=readFromTOC(serialPortConfiguration,12,allocation.write); while((!HasOverlappedIoCompleted(writeOverlapped) || !HasOverlappedIoCompleted(readOverlapped))||writing); if(allocation.write[1]==19 && allocation.write[13] ==0) break; allocation.size = 6; delete writeOverlapped; delete readOverlapped; // delete allocation; Sleep(250); } while((!HasOverlappedIoCompleted(writeOverlapped) || !HasOverlappedIoCompleted(readOverlapped))||writing); delete writeOverlapped; delete readOverlapped; cout<<"phase 2 done"<<endl; writeOverlapped=sendToTOC(serialPortConfiguration,sampleInformation); Sleep(700); readOverlapped=readFromTOC(serialPortConfiguration,8,readBuffer.write); while((!HasOverlappedIoCompleted(writeOverlapped) || !HasOverlappedIoCompleted(readOverlapped))||writing); delete writeOverlapped; delete readOverlapped; while(true) { memset(&allocation.write,0,sizeof(WORD)*BUFFERSIZE); writeOverlapped=sendToTOC(serialPortConfiguration,ACKStruct); Sleep(900); while(!HasOverlappedIoCompleted(writeOverlapped) &&writing); delete writeOverlapped; do { readOverlapped=readFromTOC(serialPortConfiguration,19,allocation.write); allocation.size = 7; while(!HasOverlappedIoCompleted(readOverlapped)&&writing); delete readOverlapped; Sleep(200); } while(inQueue(serialPortConfiguration,1)); if(allocation.write[1]==23 && allocation.write[13] ==0) { break; } Sleep(400); } //while((!HasOverlappedIoCompleted(writeOverlapped) || !HasOverlappedIoCompleted(readOverlapped))||writing); //delete writeOverlapped; //delete readOverlapped; writeOverlapped=sendToTOC(serialPortConfiguration,sampleInformation2); Sleep(500); readOverlapped=readFromTOC(serialPortConfiguration,8,readBuffer.write); while((!HasOverlappedIoCompleted(writeOverlapped) || !HasOverlappedIoCompleted(readOverlapped))||writing); delete writeOverlapped; delete readOverlapped; cout<<"phase 3 done"<<endl; int byteID = 0; struct TOCData escapeSequence; escapeSequence.write[0] = 0x0BA5; escapeSequence.write[1] = 0x0AF4; escapeSequence.write[2] = 0x2A04; escapeSequence.write[3] = 0x00; escapeSequence.write[4] = 0x00; escapeSequence.write[5] = 0x00; escapeSequence.write[6] = 0x3700; escapeSequence.write[7] = 0x03A5; escapeSequence.write[8] = 0x00FC; escapeSequence.write[9] = 0x01; escapeSequence.size = 19; bool escape = false; while(true) { memset(&allocation.write,0,sizeof(WORD)*40); if(escape) { writeOverlapped=sendToTOC(serialPortConfiguration,escapeSequence); escape = false; } else { writeOverlapped=sendToTOC(serialPortConfiguration,ACKStruct); } Sleep(900); readOverlapped=readFromTOC(serialPortConfiguration,19,allocation.write); allocation.size = 19; while(!HasOverlappedIoCompleted(readOverlapped) &&reading); delete readOverlapped; if(allocation.write[1]==79) { sampleData.getSampleResults().addToPeak(allocation.write[14],allocation.write[15]); } if(allocation.write[1]==23) { escape = true; } if(allocation.write[1]==43) { break; } cout<<"concentration so far"<<sampleData.getSampleResults().getAverageConcentration()<<endl; Sleep(250); Sleep(400); } while(true) { memset(&allocation.write,0,sizeof(WORD)*40); if(escape) { writeOverlapped=sendToTOC(serialPortConfiguration,escapeSequence); break; } else { writeOverlapped=sendToTOC(serialPortConfiguration,ACKStruct); } Sleep(900); readOverlapped=readFromTOC(serialPortConfiguration,19,allocation.write); allocation.size = 19; while(!HasOverlappedIoCompleted(readOverlapped) &&reading); delete readOverlapped; if(allocation.write[1]==79) { sampleData.getSampleResults().addToPeak(allocation.write[14],allocation.write[15]); } if(allocation.write[2]==232 && allocation.write[1]==23) { escape = true; } Sleep(250); Sleep(400); } Sleep(900); readOverlapped=readFromTOC(serialPortConfiguration,19,allocation.write); cout<<"Area Calculated" <<sampleData.getSampleResults().currentArea()<<endl; cout<<"Concentration Calculated"<<sampleData.getSampleResults().currentConcentration()<<endl; }