From 8540be950a63bc7dddec120893e939ef9afef178 Mon Sep 17 00:00:00 2001 From: Michael Moon Date: Sun, 20 Feb 2011 17:13:09 +1100 Subject: [PATCH] use CRC16 to verify heater PID settings in eeprom --- Makefile | 2 +- crc.c | 24 ++++++++++++++++++++++++ crc.h | 8 ++++++++ heater.c | 13 +++++++++---- 4 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 crc.c create mode 100644 crc.h diff --git a/Makefile b/Makefile index 9bd9a90..9bc1430 100644 --- a/Makefile +++ b/Makefile @@ -96,7 +96,7 @@ PROGBAUD = 57600 PROGRAM = mendel -SOURCES = $(PROGRAM).c serial.c dda.c gcode_parse.c gcode_process.c timer.c temp.c sermsg.c dda_queue.c watchdog.c debug.c sersendf.c heater.c analog.c delay.c intercom.c pinio.c clock.c home.c +SOURCES = $(PROGRAM).c serial.c dda.c gcode_parse.c gcode_process.c timer.c temp.c sermsg.c dda_queue.c watchdog.c debug.c sersendf.c heater.c analog.c delay.c intercom.c pinio.c clock.c home.c crc.c ARCH = avr- CC = $(ARCH)gcc diff --git a/crc.c b/crc.c new file mode 100644 index 0000000..2a8a10c --- /dev/null +++ b/crc.c @@ -0,0 +1,24 @@ +#include "crc.h" + +#include + +// uint16_t _crc16_update(uint16_t crc, uint8_t a) { +// int i; +// crc ^= a; +// for (i = 0; i < 8; ++i) +// { +// if (crc & 1) +// crc = (crc >> 1) ^ 0xA001; +// else +// crc = (crc >> 1); +// } +// return crc; +// } + +uint16_t crc_block(void *data, uint16_t len) { + uint16_t crc = 0; + for (; len; data++, len--) { + crc = _crc16_update(crc, *((uint8_t *) data)); + } + return crc; +} diff --git a/crc.h b/crc.h new file mode 100644 index 0000000..7cd9852 --- /dev/null +++ b/crc.h @@ -0,0 +1,8 @@ +#ifndef _CRC_H +#define _CRC_H + +#include + +uint16_t crc_block(void *data, uint16_t len); + +#endif /* _CRC_H */ diff --git a/heater.c b/heater.c index 35288ca..3e50f8f 100644 --- a/heater.c +++ b/heater.c @@ -6,10 +6,12 @@ #include "arduino.h" #include "debug.h" -#ifndef EXTRUDER -#include "sersendf.h" -#endif #include "temp.h" +#include "crc.h" + +#ifndef EXTRUDER + #include "sersendf.h" +#endif typedef struct { volatile uint8_t *heater_port; @@ -59,6 +61,7 @@ typedef struct { int32_t EE_i_factor; int32_t EE_d_factor; int16_t EE_i_limit; + uint16_t crc; } EE_factor; EE_factor EEMEM EE_factors[NUM_HEATERS]; @@ -101,7 +104,8 @@ void heater_init() { heaters_pid[i].d_factor = eeprom_read_dword((uint32_t *) &EE_factors[i].EE_d_factor); heaters_pid[i].i_limit = eeprom_read_word((uint16_t *) &EE_factors[i].EE_i_limit); - if ((heaters_pid[i].p_factor == 0) && (heaters_pid[i].i_factor == 0) && (heaters_pid[i].d_factor == 0) && (heaters_pid[i].i_limit == 0)) { +// if ((heaters_pid[i].p_factor == 0) && (heaters_pid[i].i_factor == 0) && (heaters_pid[i].d_factor == 0) && (heaters_pid[i].i_limit == 0)) { + if (crc_block(&heaters_pid[i].p_factor, 14) != eeprom_read_word((uint16_t *) &EE_factors[i].crc)) { heaters_pid[i].p_factor = DEFAULT_P; heaters_pid[i].i_factor = DEFAULT_I; heaters_pid[i].d_factor = DEFAULT_D; @@ -119,6 +123,7 @@ void heater_save_settings() { eeprom_write_dword((uint32_t *) &EE_factors[i].EE_i_factor, heaters_pid[i].i_factor); eeprom_write_dword((uint32_t *) &EE_factors[i].EE_d_factor, heaters_pid[i].d_factor); eeprom_write_word((uint16_t *) &EE_factors[i].EE_i_limit, heaters_pid[i].i_limit); + eeprom_write_word((uint16_t *) &EE_factors[i].crc, crc_block(&heaters_pid[i].p_factor, 14)); } #endif /* BANG_BANG */ }