Browse Source

fix eeprom writing & LED codes

tags/v1.2
Olaf Rempel 12 years ago
parent
commit
447c7ae484
4 changed files with 41 additions and 29 deletions
  1. +1
    -0
      blmc.h
  2. +12
    -13
      eeprom.c
  3. +2
    -2
      eeprom.h
  4. +26
    -14
      main.c

+ 1
- 0
blmc.h View File

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


+ 12
- 13
eeprom.c View File

@@ -20,43 +20,42 @@
#include <util/crc16.h>
#include "eeprom.h"

#include <avr/io.h>
#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 *)&params;
for (i = 0; i < sizeof(struct ee_param) -2; i++)
crc = _crc_ccitt_update(crc, *tmp++);

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;
uint16_t crc = 0x0000;
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++);

if (crc != 0x0000) {
i = sizeof(params);
i = sizeof(struct ee_param);
uint8_t *src = (uint8_t *)&defaults;
uint8_t *dst = (uint8_t *)&params;
while (i--)
*dst++ = *src++;

write_parameters((struct ee_param *)&params);
write_parameters();
return 1;
}
return 0;
}

+ 2
- 2
eeprom.h View File

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

+ 26
- 14
main.c View File

@@ -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 <avr/io.h>
@@ -133,43 +134,54 @@ int main(void)
/* I2C Init: keep Address from bootloader, Auto ACKs with Interrupts */
TWCR = (1<<TWEA) | (1<<TWEN) | (1<<TWIE);

read_parameters();
blmc.flags = 0x00;
if (read_parameters())
blmc.flags |= FLAG_INVALIDEEPROM;

sei();

while (1) {
uint8_t ledX[2] = { 0x00, 0x00 };

/* get motor status: spinup, running or off */
if (blmc.flags & FLAG_RUN_MASK) {
if (blmc.flags & (FLAG_COM_SPINUP | FLAG_PWM_SPINUP))
led[0] = LED_FAST;
ledX[0] = LED_FAST;
else
led[0] = LED_SLOW;
ledX[0] = LED_SLOW;
} else {
led[0] = LED_ON;
ledX[0] = LED_ON;
}

/* soft errors (current limit, i2c timeout) */
if (blmc.flags & FLAG_SOFTERR_MASK)
led[1] = LED_ON;
ledX[1] = LED_ON;

/* hard errors */
if (blmc.flags & FLAG_HARDERR_MASK) {
led[0] = LED_OFF;
if (blmc.flags & FLAG_CURRENTLIMIT) {
led[1] = LED_FAST;
ledX[0] = LED_OFF;
ledX[1] = LED_FAST;

} else if (blmc.flags & FLAG_UNDERVOLTAGE) {
led[1] = LED_SLOW;
ledX[0] = LED_OFF;
ledX[1] = LED_SLOW;

} else if (blmc.flags & FLAG_SELFTESTFAILED) {
led[0] = LED_SLOW;
led[1] = LED_SLOW;
ledX[0] = LED_FAST;
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)))
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) */
if (blmc.flags & FLAG_COM_SPINUP)


Loading…
Cancel
Save