Skip to content
This repository has been archived by the owner on Aug 27, 2023. It is now read-only.

Commit

Permalink
use CRC16 to verify heater PID settings in eeprom
Browse files Browse the repository at this point in the history
  • Loading branch information
triffid committed Feb 20, 2011
1 parent 9dc4d54 commit 8540be9
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions crc.c
Original file line number Diff line number Diff line change
@@ -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 changes: 8 additions & 0 deletions crc.h
Original file line number Diff line number Diff line change
@@ -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 */
11 changes: 8 additions & 3 deletions heater.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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;
Expand All @@ -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 */
}
Expand Down

0 comments on commit 8540be9

Please sign in to comment.