Bridge protocol frame lenght is now a 16-bit field. Added more efficient Bridge.trasnfer() methods.

This commit is contained in:
Cristian Maglie 2013-06-05 11:50:33 +02:00
parent 9f9314c6bc
commit b464d4848e
2 changed files with 47 additions and 15 deletions

View File

@ -168,9 +168,12 @@ bool BridgeClass::crcCheck(uint16_t _CRC) {
return CRC == _CRC; return CRC == _CRC;
} }
uint8_t BridgeClass::transfer(const uint8_t *buff, uint8_t len, uint8_t BridgeClass::transfer(const uint8_t *buff1, uint16_t len1,
uint8_t *rxbuff, uint8_t rxlen) const uint8_t *buff2, uint16_t len2,
const uint8_t *buff3, uint16_t len3,
uint8_t *rxbuff, uint16_t rxlen)
{ {
uint16_t len = len1 + len2 + len3;
for ( ; ; delay(100), dropAll() /* Delay for retransmission */) { for ( ; ; delay(100), dropAll() /* Delay for retransmission */) {
// Send packet // Send packet
crcReset(); crcReset();
@ -178,11 +181,21 @@ uint8_t BridgeClass::transfer(const uint8_t *buff, uint8_t len,
crcUpdate(0xFF); crcUpdate(0xFF);
write((char)index); // Message index write((char)index); // Message index
crcUpdate(index); crcUpdate(index);
write((char)len); // Message length write((char)((len >> 8) & 0xFF)); // Message length (hi)
crcUpdate(len); crcUpdate((len >> 8) & 0xFF);
for (uint8_t i=0; i<len; i++) { // Payload write((char)(len & 0xFF)); // Message length (lo)
write((char)buff[i]); crcUpdate(len & 0xFF);
crcUpdate(buff[i]); for (uint8_t i=0; i<len1; i++) { // Payload
write((char)buff1[i]);
crcUpdate(buff1[i]);
}
for (uint8_t i=0; i<len2; i++) { // Payload
write((char)buff2[i]);
crcUpdate(buff2[i]);
}
for (uint8_t i=0; i<len3; i++) { // Payload
write((char)buff3[i]);
crcUpdate(buff3[i]);
} }
crcWrite(); // CRC crcWrite(); // CRC
@ -198,13 +211,20 @@ uint8_t BridgeClass::transfer(const uint8_t *buff, uint8_t len,
crcUpdate(index); crcUpdate(index);
// Recv len // Recv len
uint8_t l = timedRead(5); int lh = timedRead(5);
if (l < 0) if (lh < 0)
continue; continue;
crcUpdate(l); crcUpdate(lh);
int ll = timedRead(5);
if (ll < 0)
continue;
crcUpdate(ll);
uint16_t l = lh;
l <<= 8;
l += ll;
// Recv data // Recv data
for (uint8_t i=0; i<l; i++) { for (uint16_t i=0; i<l; i++) {
int c = timedRead(5); int c = timedRead(5);
if (c < 0) if (c < 0)
continue; continue;

View File

@ -65,8 +65,20 @@ public:
void flush() { stream.flush(); } void flush() { stream.flush(); }
// Trasnfer a frame (with error correction and response) // Trasnfer a frame (with error correction and response)
uint8_t transfer(const uint8_t *buff, uint8_t len, uint8_t transfer(const uint8_t *buff1, uint16_t len1,
uint8_t *rxbuff=NULL, uint8_t rxlen=0); const uint8_t *buff2, uint16_t len2,
const uint8_t *buff3, uint16_t len3,
uint8_t *rxbuff, uint16_t rxlen);
// multiple inline versions of the same function to allow efficient frame concatenation
uint8_t transfer(const uint8_t *buff1, uint16_t len1)
{ transfer(buff1, len1, NULL, 0); }
uint8_t transfer(const uint8_t *buff1, uint16_t len1,
uint8_t *rxbuff, uint16_t rxlen)
{ transfer(buff1, len1, NULL, 0, rxbuff, rxlen); }
uint8_t transfer(const uint8_t *buff1, uint16_t len1,
const uint8_t *buff2, uint16_t len2,
uint8_t *rxbuff, uint16_t rxlen)
{ transfer(buff1, len1, buff2, len2, NULL, 0, rxbuff, rxlen); }
private: private:
uint8_t index; uint8_t index;
int timedRead(unsigned int timeout); int timedRead(unsigned int timeout);