implement i2c-timeout

This commit is contained in:
Olaf Rempel 2008-03-05 17:08:33 +01:00
parent ad0d837355
commit a41be6d7ca
4 changed files with 36 additions and 17 deletions

4
blmc.c
View File

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

10
blmc.h
View File

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

View File

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

36
main.c
View File

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