Skip to content
Browse files

use CRC16 to verify heater PID settings in eeprom

  • Loading branch information...
1 parent 9dc4d54 commit 8540be950a63bc7dddec120893e939ef9afef178 @triffid triffid committed Feb 20, 2011
Showing with 41 additions and 4 deletions.
  1. +1 −1 Makefile
  2. +24 −0 crc.c
  3. +8 −0 crc.h
  4. +8 −3 heater.c
View
2 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
View
24 crc.c
@@ -0,0 +1,24 @@
+#include "crc.h"
+
+#include <util/crc16.h>
+
+// 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;
+}
View
8 crc.h
@@ -0,0 +1,8 @@
+#ifndef _CRC_H
+#define _CRC_H
+
+#include <stdint.h>
+
+uint16_t crc_block(void *data, uint16_t len);
+
+#endif /* _CRC_H */
View
11 heater.c
@@ -6,10 +6,12 @@
#include "arduino.h"
#include "debug.h"
+#include "temp.h"
+#include "crc.h"
+
#ifndef EXTRUDER
-#include "sersendf.h"
+ #include "sersendf.h"
#endif
-#include "temp.h"
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 */
}

0 comments on commit 8540be9

Please sign in to comment.
Something went wrong with that request. Please try again.