public byte[] TPI_SLD_3bytes() { Bitbuffer rcv1 = new Bitbuffer(new byte[2], 16); Bitbuffer rcv2 = new Bitbuffer(new byte[2], 16); Bitbuffer rcv3 = new Bitbuffer(new byte[2], 16); sendNreceive(TPI_constructsendmessage(0x24), rcv1, TPI_constructsendmessage(0x24), rcv2, TPI_constructsendmessage(0x24), rcv3); _TPIaddress += 3; rcv1.discardUntil0(); if (rcv1.length < 9) { // ooops } rcv1.dequeuebit(); // ST rcv2.discardUntil0(); if (rcv2.length < 9) { // ooops } rcv2.dequeuebit(); // ST rcv3.discardUntil0(); if (rcv3.length < 9) { // ooops } rcv3.dequeuebit(); // ST return(new byte[] { rcv1.dequeuebyte_LSBfirst(), rcv2.dequeuebyte_LSBfirst(), rcv3.dequeuebyte_LSBfirst() }); }
public void sendNreceive(Bitbuffer snd1, Bitbuffer rcv1, Bitbuffer snd2, Bitbuffer rcv2) { byte[] snd1msg = composesendmsg(snd1); byte[] snd2msg = composesendmsg(snd2); byte[] rcv1msg = composereceivemsg(rcv1); byte[] rcv2msg = composereceivemsg(rcv2); int allbytes = snd1msg.Length + rcv1msg.Length + snd2msg.Length + rcv2msg.Length; if (m_outFeatureReportBuffer.Length - m_count < allbytes) { flush(); } send(snd1msg); int ofsrcv1 = m_count; send(rcv1msg); send(snd2msg); int ofsrcv2 = m_count; send(rcv2msg); flush(); read(); rcv1.setFromSubArray(m_inFeatureReportBuffer, ofsrcv1 + 1, rcv1.length); rcv2.setFromSubArray(m_inFeatureReportBuffer, ofsrcv2 + 1, rcv2.length); }
public byte[] batchreceive(int numBytes) { List <byte> data = new List <byte>(); #if false while (numBytes > 0) { data.Add(receivedata(1)[0]); ACK(); SDA_HIGH(); numBytes--; } #else flush(); async_clear(); for (int i = 0; i < numBytes; i++) { async_receive(1); async_C_LOW(); // ACK async_D_LOW(); async_C_HIGH(); async_C_LOW(); async_D_HIGH(); // SDA_HIGH } for (int i = 0; i < numBytes; i++) { Bitbuffer tmp = async_getBuffer(); data.Add(tmp.dequeuebyte_MSBfirst()); } #endif return(data.ToArray()); }
public void concat(Bitbuffer b2) { while (b2.length > 0) { enqueuebit(b2.dequeuebit()); } }
public byte TPI_receivebyte_skipIDLE() { D_HIGH(); //flush(); Bitbuffer buf = receivebits(_TPIidlebits + 12); buf.discardUntil0(); int countbits = 0; while (buf.length < 12) { System.Diagnostics.Debug.Print("TPI_receivebyte_skipIDLE: Have to read more bits..."); countbits += 12 - buf.length; Bitbuffer buf2 = receivebits(12 - buf.length); for (int i = 0; i < buf2.length; i++) { buf.enqueuebit(buf2[i]); } buf.discardUntil0(); if (countbits > 128 + 2 + 50) { System.Diagnostics.Debug.Print("TPI_receivebyte_skipIDLE: Too many IDLE bits"); throw new System.Exception("Target: Too many IDLE bits. Target is not responding."); } } buf.dequeuebit(); // ST byte result = buf.dequeuebyte_LSBfirst(); //buf.dequeuebit(); // P //buf.dequeuebit(); // SP1 //buf.dequeuebit(); // SP2 return(result); }
public void async_clear() { m_asyncReceiveOffsets = new List <int>(); m_asyncReceiveLengths = new List <int>(); m_asyncReceiveBuffs = new List <Bitbuffer>(); m_asyncCurrentBuff = new Bitbuffer(); m_asyncRequestLength = new List <int>(); }
public byte[] composereceivemsg(Bitbuffer buf) { List <byte> bytes = new List <byte>(); int bits = buf.length; bytes.Add((byte)(0x00 + bits - 1)); bytes.AddRange(buf.ToByteArray()); return(bytes.ToArray()); }
public void TPI_IDLE(int numidlebits) { Bitbuffer buf = new Bitbuffer(); for (int i = 0; i < numidlebits; i++) { buf.enqueuebit(1); } sendbits(buf); }
public void TPI_BREAK() { Bitbuffer buf = new Bitbuffer(); for (int i = 0; i < 12; i++) { buf.enqueuebit(1); } sendbits(buf); }
public Bitbuffer TPI_constructsendmessage(byte b) { Bitbuffer buf = new Bitbuffer(); buf.enqueuebit(0); // ST buf.enqueuebyte_LSBfirst(b); // D0..D7 buf.enqueuebit(buf.parity(b)); // P buf.enqueuebit(1); // SP1 buf.enqueuebit(2); // SP2 return(buf); }
public void async_flush() { if (m_count <= 1) { return; } bool success = m_OutFeatureReport.Write(m_outFeatureReportBuffer, m_hidHandle); if (success) { //Debug.WriteLine("Send(OK) " + m_count + "/" + m_outFeatureReportBuffer.Length + " bytes"); } else { Debug.WriteLine("Send(fl) " + m_count + "/" + m_outFeatureReportBuffer.Length + " bytes"); } if (m_asyncReceiveLengths.Count > 0) { read(); } while (m_asyncReceiveLengths.Count > 0) { if (m_asyncCurrentBuff.length == m_asyncRequestLength[0]) { m_asyncRequestLength.RemoveAt(0); m_asyncReceiveBuffs.Add(m_asyncCurrentBuff); m_asyncCurrentBuff = new Bitbuffer(); } int dataofs = m_asyncReceiveOffsets[0]; m_asyncReceiveOffsets.RemoveAt(0); int datasiz = m_asyncReceiveLengths[0]; m_asyncReceiveLengths.RemoveAt(0); m_asyncCurrentBuff.enqueueFromSubArray(m_inFeatureReportBuffer, dataofs + 1, datasiz); if (m_asyncCurrentBuff.length == m_asyncRequestLength[0]) { m_asyncRequestLength.RemoveAt(0); m_asyncReceiveBuffs.Add(m_asyncCurrentBuff); m_asyncCurrentBuff = new Bitbuffer(); } } m_count = 0; newOutBuffer(); // Store the report ID in the buffer: m_outFeatureReportBuffer[m_count++] = 0; }
public Bitbuffer async_getBuffer() { if (m_asyncReceiveBuffs.Count > 0) { Bitbuffer b = m_asyncReceiveBuffs[0]; m_asyncReceiveBuffs.RemoveAt(0); return(b); } async_flush(); if (m_asyncReceiveBuffs.Count > 0) { Bitbuffer b = m_asyncReceiveBuffs[0]; m_asyncReceiveBuffs.Remove(b); return(b); } throw new System.Exception("There is not enough data received to fill a buffer"); }
public Bitbuffer receivebits(int numbits) { #if true int bytes = (int)Math.Ceiling(numbits / 8.0); byte[] rawdat = receivedata(new byte[bytes], numbits); return(new Bitbuffer(rawdat, numbits)); #else Bitbuffer data = new Bitbuffer(); for (int i = 0; i < len; i++) { C_LOW(); flush(); C_HIGH(); flush(); data.enqueuebit(Q_READ()); } return(data); #endif }
public void sendbits(Bitbuffer buf) { #if true senddata(buf.ToByteArray(), buf.length); #else for (int i = 0; i < buf.length; i++) { C_LOW(); if (buf[i] > 0) { D_HIGH(); } else { D_LOW(); } flush(); C_HIGH(); flush(); } #endif }
public byte TPI_receivebyte() { D_HIGH(); //flush(); Bitbuffer buf = receivebits(12); buf.discardUntil0(); while (buf.length < 12) { Bitbuffer buf2 = receivebits(12 - buf.length); for (int i = 0; i < buf2.length; i++) { buf.enqueuebit(buf2[i]); } buf.discardUntil0(); } buf.dequeuebit(); // ST byte result = buf.dequeuebyte_LSBfirst(); //buf.dequeuebit(); // P //buf.dequeuebit(); // SP1 //buf.dequeuebit(); // SP2 return(result); }
public override void PageWrite(int address, byte[] bs, bool fastWrite) // 64 bytes in a page { condition_START(); WRITE(); bool isACK = true; if (fastWrite) { skipACK(); } else { isACK = isAcknowledged(); // recv ACK } senddata(new byte[] { (byte)(address >> 8) }); if (fastWrite) { skipACK(); } else { isACK = isAcknowledged(); // recv ACK } senddata(new byte[] { (byte)(address & 255) }); if (fastWrite) { skipACK(); } else { isACK = isAcknowledged(); // recv ACK } if (fastWrite) { Bitbuffer buf = new Bitbuffer(); foreach (byte b in bs) { buf.enqueuebyte_MSBfirst(b); // send byte buf.enqueuebit(1); // skip ACK } sendbits(buf); } else { foreach (byte b in bs) { senddata(new byte[] { b }); if (fastWrite) { skipACK(); } else { isACK = isAcknowledged(); // recv ACK } } } condition_STOP(); if (fastWrite) { System.Threading.Thread.Sleep(4); // write time = 5ms } else { WaitBusy(); } condition_STOP(); }
public void TPI_skipIDLEbits() { D_HIGH(); //flush(); Bitbuffer buf = receivebits(_TPIidlebits); }