From 2f60068f2c03084942a6965a58960f27d23d32af Mon Sep 17 00:00:00 2001 From: Jérémy Dufour Date: Sat, 16 May 2009 01:23:09 +0200 Subject: * digital/avr/io, digital/avr/asserv: - use CRC over TWI communication between the asserv and io boards. --- digital/asserv/src/asserv/twi_proto.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'digital/asserv') diff --git a/digital/asserv/src/asserv/twi_proto.c b/digital/asserv/src/asserv/twi_proto.c index 6168f245..df41efaa 100644 --- a/digital/asserv/src/asserv/twi_proto.c +++ b/digital/asserv/src/asserv/twi_proto.c @@ -27,6 +27,7 @@ #include "modules/utils/utils.h" #include "modules/utils/byte.h" +#include "modules/utils/crc.h" #include "modules/twi/twi.h" #include "io.h" @@ -70,11 +71,13 @@ void twi_proto_update (void) { u8 buf[AC_TWI_SL_RECV_BUFFER_SIZE]; + u8 read_data; /* Handle incoming command. */ - while (twi_sl_poll (buf, sizeof (buf))) - twi_proto_callback (buf, sizeof (buf)); + while ((read_data = twi_sl_poll (buf, sizeof (buf)))) + twi_proto_callback (buf, read_data); /* Update status. */ - u8 status[15]; + u8 status_with_crc[16]; + u8 *status = &status_with_crc[1]; status[0] = 0 | (state_aux[1].blocked << 7) | (state_aux[1].finished << 6) @@ -98,13 +101,26 @@ twi_proto_update (void) status[12] = v16_to_v8 (aux[0].pos, 0); status[13] = v16_to_v8 (aux[1].pos, 1); status[14] = v16_to_v8 (aux[1].pos, 0); - twi_sl_update (status, sizeof (status)); + /* Compute CRC. */ + status_with_crc[0] = crc_compute (&status_with_crc[1], + sizeof (status_with_crc) - 1); + twi_sl_update (status_with_crc, sizeof (status_with_crc)); } /** Handle one command. */ static void twi_proto_callback (u8 *buf, u8 size) { + /* Check CRC. */ + if (crc_compute (buf + 1, size - 1) != buf[0]) + return; + else + { + /* Remove the CRC of the buffer. */ + buf += 1; + size -= 1; + } + if (buf[0] == twi_proto.seq) return; #define c(cmd, size) (cmd) -- cgit v1.2.3