Permalink
Browse files

Added CRC8 table-less (#32)

  • Loading branch information...
gioblu committed Apr 23, 2016
1 parent 1a8bccb commit c22a1440c61d3469f01c24f2faefdcab46c87651
Showing with 20 additions and 5 deletions.
  1. +16 −4 PJON.cpp
  2. +3 −0 PJON.h
  3. +1 −1 README.md
@@ -168,6 +168,18 @@ boolean PJON::can_start() {
}
/* Compute CRC8 with a table-less implementation: */
uint8_t PJON::compute_crc_8(char input_byte, uint8_t crc) {
for (uint8_t i = 8; i; i--, input_byte >>= 1) {
uint8_t result = (crc ^ input_byte) & 0x01;
crc >>= 1;
if(result) crc ^= 0x8C;
}
return crc;
};
/* Every byte is prepended with 2 synchronization padding bits. The first
is a longer than standard logic 1 followed by a standard logic 0.
__________ ___________________________
@@ -227,13 +239,13 @@ uint16_t PJON::send_string(uint8_t id, char *string, uint8_t length) {
pinModeFast(_input_pin, OUTPUT);
this->send_byte(id);
CRC ^= id;
CRC = this->compute_crc_8(id, CRC);
this->send_byte(length + 3);
CRC ^= length + 3;
CRC = this->compute_crc_8(length + 3, CRC);
for(uint8_t i = 0; i < length; i++) {
this->send_byte(string[i]);
CRC ^= string[i];
CRC = this->compute_crc_8(string[i], CRC);
}
this->send_byte(CRC);
@@ -445,7 +457,7 @@ uint16_t PJON::receive() {
package_length = data[i];
else return FAIL;
CRC ^= data[i];
CRC = this->compute_crc_8(data[i], CRC);
}
pinModeFast(_input_pin, OUTPUT);
3 PJON.h
@@ -109,6 +109,9 @@ limitations under the License. */
uint8_t device_id();
void acquire_id();
uint8_t device_id();
uint8_t compute_crc_8(char input_byte, uint8_t crc);
void set_id(uint8_t id);
void set_negative_acknowledge(boolean state);
@@ -41,7 +41,7 @@ void loop() {
- Single wire (plus common ground) physical layer with up to 50 meters range.
- Device id implementation to enable univocal communication up to 254 devices.
- Optional auto-addressing with id collision avoidance.
- Lightweight 1 byte XOR based error detection.
- CRC8 table-less cyclic redundancy check implementation.
- Acknowledgement of correct packet sending.
- Collision avoidance to enable multi-master capability.
- Broadcast functionality to contact all connected devices.

0 comments on commit c22a144

Please sign in to comment.