diff --git a/blmc.h b/blmc.h index 8d308e1..4074f82 100644 --- a/blmc.h +++ b/blmc.h @@ -34,6 +34,7 @@ #define FLAG_UNDERVOLTAGE 0x100 #define FLAG_OVERCURRENT 0x200 #define FLAG_SELFTESTFAILED 0x400 +#define FLAG_INVALIDEEPROM 0x800 struct blmc_ { uint16_t flags; diff --git a/eeprom.c b/eeprom.c index d958435..9e4fa12 100644 --- a/eeprom.c +++ b/eeprom.c @@ -20,43 +20,42 @@ #include #include "eeprom.h" -#include -#include "main.h" - static const struct ee_param defaults = DEFAULT_PARAMETERS; struct ee_param params; -struct ee_param params_in_eeprom EEMEM; +struct ee_param params_in_eeprom EEMEM = DEFAULT_PARAMETERS; -void write_parameters(struct ee_param *p) +void write_parameters(void) { uint8_t i; uint16_t crc = 0x0000; - uint8_t *tmp = (uint8_t *)p; - for (i = 0; i < sizeof(params) -2; i++) + uint8_t *tmp = (uint8_t *)¶ms; + for (i = 0; i < sizeof(struct ee_param) -2; i++) crc = _crc_ccitt_update(crc, *tmp++); params.crc16 = crc; - eeprom_write_block(&p, ¶ms_in_eeprom, sizeof(params)); + eeprom_write_block(¶ms, ¶ms_in_eeprom, sizeof(struct ee_param)); } -void read_parameters(void) +uint8_t read_parameters(void) { - eeprom_read_block(¶ms, ¶ms_in_eeprom, sizeof(params)); + eeprom_read_block(¶ms, ¶ms_in_eeprom, sizeof(struct ee_param)); uint8_t i; uint16_t crc = 0x0000; uint8_t *tmp = (uint8_t *)¶ms; - for (i = 0; i < sizeof(params); i++) + for (i = 0; i < sizeof(struct ee_param); i++) crc = _crc_ccitt_update(crc, *tmp++); if (crc != 0x0000) { - i = sizeof(params); + i = sizeof(struct ee_param); uint8_t *src = (uint8_t *)&defaults; uint8_t *dst = (uint8_t *)¶ms; while (i--) *dst++ = *src++; - write_parameters((struct ee_param *)¶ms); + write_parameters(); + return 1; } + return 0; } diff --git a/eeprom.h b/eeprom.h index 152610d..5acb97a 100644 --- a/eeprom.h +++ b/eeprom.h @@ -34,7 +34,7 @@ struct ee_param { .voltage_min = 0x000, \ }; -void read_parameters(void); -void write_parameters(struct ee_param *p); +uint8_t read_parameters(void); +void write_parameters(void); #endif diff --git a/main.c b/main.c index 21882c6..9fd8150 100644 --- a/main.c +++ b/main.c @@ -23,9 +23,10 @@ * No Error, Motor running SLOW - * Current Limit - ON * i2c Timeout - ON (not implemented yet) - * Undervoltage OFF SLOW (not implemented yet) - * Overcurrent (Hard Limit) OFF FAST (not implemented yet) - * SELFTEST failed SLOW SLOW (not implemented yet) + * Undervoltage OFF SLOW + * Overcurrent (Hard Limit) OFF FAST + * SELFTEST failed FAST FAST (not implemented yet) + * EEPROM invalid SLOW SLOW */ #include @@ -133,43 +134,54 @@ int main(void) /* I2C Init: keep Address from bootloader, Auto ACKs with Interrupts */ TWCR = (1< 200ms) */ if (blmc.flags & FLAG_COM_SPINUP)