use CRC16 to verify heater PID settings in eeprom

This commit is contained in:
Michael Moon 2011-02-20 17:13:09 +11:00
parent 9dc4d54133
commit 8540be950a
4 changed files with 42 additions and 5 deletions

View File

@ -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

24
crc.c Normal file
View File

@ -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;
}

8
crc.h Normal file
View File

@ -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 File

@ -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 */
}