diff --git a/mote/v2/avr/ctrl.c b/mote/v2/avr/ctrl.c index 53119fc..fb6fb59 100644 --- a/mote/v2/avr/ctrl.c +++ b/mote/v2/avr/ctrl.c @@ -20,6 +20,7 @@ // $Id$ #include +#include #include "global.h" #include "main.h" @@ -204,9 +205,19 @@ void ctrlRxToTxLoop(void) } } +uint8_t ctrlCalcCrc8(cBuffer* buffer) +{ + uint8_t i, crc = 0; + + for (i = 0; i < buffer->datalength; i++) { + crc = _crc_ibutton_update(crc, bufferGetAtIndex(buffer, i)); + } + return crc; +} + void ctrlDecode(void) { - uint8_t cmd[2]; + uint8_t cmd[2], crc; ctrlFlushTxBuffer(); @@ -231,6 +242,9 @@ void ctrlDecode(void) ctrlAddToTxBuffer('z'); } + crc = ctrlCalcCrc8(&ctrlTxBuffer); + ctrlWriteCharToTxBuffer(crc); + ctrlAddToTxBuffer('.'); } diff --git a/mote/v2/avr/ctrl.h b/mote/v2/avr/ctrl.h index 1e3066c..59666f5 100644 --- a/mote/v2/avr/ctrl.h +++ b/mote/v2/avr/ctrl.h @@ -104,6 +104,14 @@ void ctrlFlushTxBuffer(void); */ void ctrlRxToTxLoop(void); +/** + * Calculate the CRC-8 checksum over the bytes in the buffer. + * + * @param buffer pointer to the buffer containing the data + * @return CRC-8 checksum + */ +uint8_t ctrlCalcCrc8(cBuffer* buffer); + /** * Decode the message in the ctrl Rx buffer and dispatch to either ctrlCmdGet, * ctrlCmdSet or ctrlCmdCommit.