diff --git a/blmc.c b/blmc.c index de2cf81..14b3304 100644 --- a/blmc.c +++ b/blmc.c @@ -193,9 +193,9 @@ void setpwm(uint8_t pwm) /* set current-limit flag */ if (blmc.pwm_limit) - blmc.flags |= FLAG_CURRENTLIMIT; + blmc.flags |= FLAG_CURRENT_LIMIT; else - blmc.flags &= ~FLAG_CURRENTLIMIT; + blmc.flags &= ~FLAG_CURRENT_LIMIT; /* prevent overflow */ if (blmc.pwm_limit > pwm) diff --git a/blmc.h b/blmc.h index 4074f82..11fd765 100644 --- a/blmc.h +++ b/blmc.h @@ -27,14 +27,16 @@ #define FLAG_COM_NORMAL 0x008 #define FLAG_SOFTERR_MASK 0x0F0 -#define FLAG_CURRENTLIMIT 0x010 -#define FLAG_I2CTIMEOUT 0x020 +#define FLAG_CURRENT_LIMIT 0x010 +#define FLAG_I2C_TIMEOUT 0x020 #define FLAG_HARDERR_MASK 0xF00 #define FLAG_UNDERVOLTAGE 0x100 #define FLAG_OVERCURRENT 0x200 -#define FLAG_SELFTESTFAILED 0x400 -#define FLAG_INVALIDEEPROM 0x800 +#define FLAG_SELFTEST_FAILED 0x400 +#define FLAG_INVALID_EEPROM 0x800 + +#define FLAG_I2C_ACTIVE 0x1000 struct blmc_ { uint16_t flags; diff --git a/i2c-slave.c b/i2c-slave.c index a0560d9..3cbff05 100644 --- a/i2c-slave.c +++ b/i2c-slave.c @@ -35,7 +35,7 @@ extern struct ee_param params; #define CMD_GET_PARAM 0x24 #define CMD_BOOT_LOADER 0x2F -const static uint8_t info[16] = "blctrl m8-v1.1"; +const static uint8_t info[16] = "blctrl m8-v1.2"; ISR(TWI_vect) { @@ -72,6 +72,7 @@ ISR(TWI_vect) } } else if (cmd == CMD_SET_PWM) { + blmc.flags |= FLAG_I2C_ACTIVE; setpwm(TWDR); bcnt = 0; diff --git a/main.c b/main.c index b1edd5f..f639ada 100644 --- a/main.c +++ b/main.c @@ -21,8 +21,8 @@ * No Error, Motor off ON - * No Error, Motor spinup FAST - * No Error, Motor running SLOW - - * Current Limit - ON - * i2c Timeout - ON (not implemented yet) + * Current Limit F/S ON + * i2c Timeout ON ON * Undervoltage OFF SLOW * Overcurrent (Hard Limit) OFF FAST * SELFTEST failed FAST FAST (not implemented yet) @@ -57,10 +57,26 @@ ISR(TIMER0_OVF_vect) uint16_t diff = blmc.rpm_tmp - blmc.rpm_tmp_old; blmc.rpm_tmp_old = blmc.rpm_tmp; - /* too few commutations while running -> do a spinup */ - if (diff < 0x8 && (blmc.flags & FLAG_RUN_MASK) == (FLAG_PWM_NORMAL | FLAG_COM_NORMAL)) { - blmc.flags &= ~(FLAG_RUN_MASK); - blmc.flags |= FLAG_PWM_SPINUP | FLAG_COM_SPINUP; + if ((blmc.flags & FLAG_RUN_MASK) == (FLAG_PWM_NORMAL | FLAG_COM_NORMAL)) { + /* too few commutations while running -> do a spinup */ + if (diff < 0x08) { + blmc.flags &= ~(FLAG_RUN_MASK); + blmc.flags |= FLAG_PWM_SPINUP | FLAG_COM_SPINUP; + } + + /* no i2c cmd in the last 20ms */ + if (!(blmc.flags & FLAG_I2C_ACTIVE)) { + /* already in i2c timeout, turn off motor */ + if (blmc.flags & FLAG_I2C_TIMEOUT) + blmc.pwm = 0; + + blmc.flags |= FLAG_I2C_TIMEOUT; + + } else { + blmc.flags &= ~FLAG_I2C_TIMEOUT; + } + + blmc.flags &= ~FLAG_I2C_ACTIVE; } /* set pwm again (adjust current limit) */ @@ -138,7 +154,7 @@ int main(void) blmc.flags = 0x00; if (read_parameters()) - blmc.flags |= FLAG_INVALIDEEPROM; + blmc.flags |= FLAG_INVALID_EEPROM; sei(); @@ -161,7 +177,7 @@ int main(void) /* hard errors */ if (blmc.flags & FLAG_HARDERR_MASK) { - if (blmc.flags & FLAG_CURRENTLIMIT) { + if (blmc.flags & FLAG_CURRENT_LIMIT) { ledX[0] = LED_OFF; ledX[1] = LED_FAST; @@ -169,11 +185,11 @@ int main(void) ledX[0] = LED_OFF; ledX[1] = LED_SLOW; - } else if (blmc.flags & FLAG_SELFTESTFAILED) { + } else if (blmc.flags & FLAG_SELFTEST_FAILED) { ledX[0] = LED_FAST; ledX[1] = LED_FAST; - } else if (blmc.flags & FLAG_INVALIDEEPROM) { + } else if (blmc.flags & FLAG_INVALID_EEPROM) { ledX[0] = LED_SLOW; ledX[1] = LED_SLOW; }