fix eeprom writing & LED codes

This commit is contained in:
Olaf Rempel 2008-02-19 14:52:02 +01:00
parent 31ded19772
commit 447c7ae484
4 changed files with 41 additions and 29 deletions

1
blmc.h
View File

@ -34,6 +34,7 @@
#define FLAG_UNDERVOLTAGE 0x100 #define FLAG_UNDERVOLTAGE 0x100
#define FLAG_OVERCURRENT 0x200 #define FLAG_OVERCURRENT 0x200
#define FLAG_SELFTESTFAILED 0x400 #define FLAG_SELFTESTFAILED 0x400
#define FLAG_INVALIDEEPROM 0x800
struct blmc_ { struct blmc_ {
uint16_t flags; uint16_t flags;

View File

@ -20,43 +20,42 @@
#include <util/crc16.h> #include <util/crc16.h>
#include "eeprom.h" #include "eeprom.h"
#include <avr/io.h>
#include "main.h"
static const struct ee_param defaults = DEFAULT_PARAMETERS; static const struct ee_param defaults = DEFAULT_PARAMETERS;
struct ee_param params; 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; uint8_t i;
uint16_t crc = 0x0000; uint16_t crc = 0x0000;
uint8_t *tmp = (uint8_t *)p; uint8_t *tmp = (uint8_t *)&params;
for (i = 0; i < sizeof(params) -2; i++) for (i = 0; i < sizeof(struct ee_param) -2; i++)
crc = _crc_ccitt_update(crc, *tmp++); crc = _crc_ccitt_update(crc, *tmp++);
params.crc16 = crc; params.crc16 = crc;
eeprom_write_block(&p, &params_in_eeprom, sizeof(params)); eeprom_write_block(&params, &params_in_eeprom, sizeof(struct ee_param));
} }
void read_parameters(void) uint8_t read_parameters(void)
{ {
eeprom_read_block(&params, &params_in_eeprom, sizeof(params)); eeprom_read_block(&params, &params_in_eeprom, sizeof(struct ee_param));
uint8_t i; uint8_t i;
uint16_t crc = 0x0000; uint16_t crc = 0x0000;
uint8_t *tmp = (uint8_t *)&params; uint8_t *tmp = (uint8_t *)&params;
for (i = 0; i < sizeof(params); i++) for (i = 0; i < sizeof(struct ee_param); i++)
crc = _crc_ccitt_update(crc, *tmp++); crc = _crc_ccitt_update(crc, *tmp++);
if (crc != 0x0000) { if (crc != 0x0000) {
i = sizeof(params); i = sizeof(struct ee_param);
uint8_t *src = (uint8_t *)&defaults; uint8_t *src = (uint8_t *)&defaults;
uint8_t *dst = (uint8_t *)&params; uint8_t *dst = (uint8_t *)&params;
while (i--) while (i--)
*dst++ = *src++; *dst++ = *src++;
write_parameters((struct ee_param *)&params); write_parameters();
return 1;
} }
return 0;
} }

View File

@ -34,7 +34,7 @@ struct ee_param {
.voltage_min = 0x000, \ .voltage_min = 0x000, \
}; };
void read_parameters(void); uint8_t read_parameters(void);
void write_parameters(struct ee_param *p); void write_parameters(void);
#endif #endif

40
main.c
View File

@ -23,9 +23,10 @@
* No Error, Motor running SLOW - * No Error, Motor running SLOW -
* Current Limit - ON * Current Limit - ON
* i2c Timeout - ON (not implemented yet) * i2c Timeout - ON (not implemented yet)
* Undervoltage OFF SLOW (not implemented yet) * Undervoltage OFF SLOW
* Overcurrent (Hard Limit) OFF FAST (not implemented yet) * Overcurrent (Hard Limit) OFF FAST
* SELFTEST failed SLOW SLOW (not implemented yet) * SELFTEST failed FAST FAST (not implemented yet)
* EEPROM invalid SLOW SLOW
*/ */
#include <avr/io.h> #include <avr/io.h>
@ -133,43 +134,54 @@ int main(void)
/* I2C Init: keep Address from bootloader, Auto ACKs with Interrupts */ /* I2C Init: keep Address from bootloader, Auto ACKs with Interrupts */
TWCR = (1<<TWEA) | (1<<TWEN) | (1<<TWIE); TWCR = (1<<TWEA) | (1<<TWEN) | (1<<TWIE);
read_parameters();
blmc.flags = 0x00; blmc.flags = 0x00;
if (read_parameters())
blmc.flags |= FLAG_INVALIDEEPROM;
sei(); sei();
while (1) { while (1) {
uint8_t ledX[2] = { 0x00, 0x00 };
/* get motor status: spinup, running or off */ /* get motor status: spinup, running or off */
if (blmc.flags & FLAG_RUN_MASK) { if (blmc.flags & FLAG_RUN_MASK) {
if (blmc.flags & (FLAG_COM_SPINUP | FLAG_PWM_SPINUP)) if (blmc.flags & (FLAG_COM_SPINUP | FLAG_PWM_SPINUP))
led[0] = LED_FAST; ledX[0] = LED_FAST;
else else
led[0] = LED_SLOW; ledX[0] = LED_SLOW;
} else { } else {
led[0] = LED_ON; ledX[0] = LED_ON;
} }
/* soft errors (current limit, i2c timeout) */ /* soft errors (current limit, i2c timeout) */
if (blmc.flags & FLAG_SOFTERR_MASK) if (blmc.flags & FLAG_SOFTERR_MASK)
led[1] = LED_ON; ledX[1] = LED_ON;
/* hard errors */ /* hard errors */
if (blmc.flags & FLAG_HARDERR_MASK) { if (blmc.flags & FLAG_HARDERR_MASK) {
led[0] = LED_OFF;
if (blmc.flags & FLAG_CURRENTLIMIT) { if (blmc.flags & FLAG_CURRENTLIMIT) {
led[1] = LED_FAST; ledX[0] = LED_OFF;
ledX[1] = LED_FAST;
} else if (blmc.flags & FLAG_UNDERVOLTAGE) { } else if (blmc.flags & FLAG_UNDERVOLTAGE) {
led[1] = LED_SLOW; ledX[0] = LED_OFF;
ledX[1] = LED_SLOW;
} else if (blmc.flags & FLAG_SELFTESTFAILED) { } else if (blmc.flags & FLAG_SELFTESTFAILED) {
led[0] = LED_SLOW; ledX[0] = LED_FAST;
led[1] = LED_SLOW; ledX[1] = LED_FAST;
} else if (blmc.flags & FLAG_INVALIDEEPROM) {
ledX[0] = LED_SLOW;
ledX[1] = LED_SLOW;
} }
} }
if (!(blmc.flags & (FLAG_SOFTERR_MASK | FLAG_HARDERR_MASK))) if (!(blmc.flags & (FLAG_SOFTERR_MASK | FLAG_HARDERR_MASK)))
led[1] = LED_OFF; ledX[1] = LED_OFF;
led[0] = ledX[0];
led[1] = ledX[1];
/* do a spinup from main loop (blocking for > 200ms) */ /* do a spinup from main loop (blocking for > 200ms) */
if (blmc.flags & FLAG_COM_SPINUP) if (blmc.flags & FLAG_COM_SPINUP)