fix eeprom writing & LED codes
This commit is contained in:
parent
31ded19772
commit
447c7ae484
1
blmc.h
1
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;
|
||||
|
25
eeprom.c
25
eeprom.c
@ -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 *)¶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;
|
||||
}
|
||||
|
4
eeprom.h
4
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
|
||||
|
40
main.c
40
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 <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…
Reference in New Issue
Block a user