implement i2c-timeout
This commit is contained in:
parent
ad0d837355
commit
a41be6d7ca
4
blmc.c
4
blmc.c
@ -193,9 +193,9 @@ void setpwm(uint8_t pwm)
|
|||||||
|
|
||||||
/* set current-limit flag */
|
/* set current-limit flag */
|
||||||
if (blmc.pwm_limit)
|
if (blmc.pwm_limit)
|
||||||
blmc.flags |= FLAG_CURRENTLIMIT;
|
blmc.flags |= FLAG_CURRENT_LIMIT;
|
||||||
else
|
else
|
||||||
blmc.flags &= ~FLAG_CURRENTLIMIT;
|
blmc.flags &= ~FLAG_CURRENT_LIMIT;
|
||||||
|
|
||||||
/* prevent overflow */
|
/* prevent overflow */
|
||||||
if (blmc.pwm_limit > pwm)
|
if (blmc.pwm_limit > pwm)
|
||||||
|
10
blmc.h
10
blmc.h
@ -27,14 +27,16 @@
|
|||||||
#define FLAG_COM_NORMAL 0x008
|
#define FLAG_COM_NORMAL 0x008
|
||||||
|
|
||||||
#define FLAG_SOFTERR_MASK 0x0F0
|
#define FLAG_SOFTERR_MASK 0x0F0
|
||||||
#define FLAG_CURRENTLIMIT 0x010
|
#define FLAG_CURRENT_LIMIT 0x010
|
||||||
#define FLAG_I2CTIMEOUT 0x020
|
#define FLAG_I2C_TIMEOUT 0x020
|
||||||
|
|
||||||
#define FLAG_HARDERR_MASK 0xF00
|
#define FLAG_HARDERR_MASK 0xF00
|
||||||
#define FLAG_UNDERVOLTAGE 0x100
|
#define FLAG_UNDERVOLTAGE 0x100
|
||||||
#define FLAG_OVERCURRENT 0x200
|
#define FLAG_OVERCURRENT 0x200
|
||||||
#define FLAG_SELFTESTFAILED 0x400
|
#define FLAG_SELFTEST_FAILED 0x400
|
||||||
#define FLAG_INVALIDEEPROM 0x800
|
#define FLAG_INVALID_EEPROM 0x800
|
||||||
|
|
||||||
|
#define FLAG_I2C_ACTIVE 0x1000
|
||||||
|
|
||||||
struct blmc_ {
|
struct blmc_ {
|
||||||
uint16_t flags;
|
uint16_t flags;
|
||||||
|
@ -35,7 +35,7 @@ extern struct ee_param params;
|
|||||||
#define CMD_GET_PARAM 0x24
|
#define CMD_GET_PARAM 0x24
|
||||||
#define CMD_BOOT_LOADER 0x2F
|
#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)
|
ISR(TWI_vect)
|
||||||
{
|
{
|
||||||
@ -72,6 +72,7 @@ ISR(TWI_vect)
|
|||||||
}
|
}
|
||||||
|
|
||||||
} else if (cmd == CMD_SET_PWM) {
|
} else if (cmd == CMD_SET_PWM) {
|
||||||
|
blmc.flags |= FLAG_I2C_ACTIVE;
|
||||||
setpwm(TWDR);
|
setpwm(TWDR);
|
||||||
bcnt = 0;
|
bcnt = 0;
|
||||||
|
|
||||||
|
30
main.c
30
main.c
@ -21,8 +21,8 @@
|
|||||||
* No Error, Motor off ON -
|
* No Error, Motor off ON -
|
||||||
* No Error, Motor spinup FAST -
|
* No Error, Motor spinup FAST -
|
||||||
* No Error, Motor running SLOW -
|
* No Error, Motor running SLOW -
|
||||||
* Current Limit - ON
|
* Current Limit F/S ON
|
||||||
* i2c Timeout - ON (not implemented yet)
|
* i2c Timeout ON ON
|
||||||
* Undervoltage OFF SLOW
|
* Undervoltage OFF SLOW
|
||||||
* Overcurrent (Hard Limit) OFF FAST
|
* Overcurrent (Hard Limit) OFF FAST
|
||||||
* SELFTEST failed FAST FAST (not implemented yet)
|
* SELFTEST failed FAST FAST (not implemented yet)
|
||||||
@ -57,12 +57,28 @@ ISR(TIMER0_OVF_vect)
|
|||||||
uint16_t diff = blmc.rpm_tmp - blmc.rpm_tmp_old;
|
uint16_t diff = blmc.rpm_tmp - blmc.rpm_tmp_old;
|
||||||
blmc.rpm_tmp_old = blmc.rpm_tmp;
|
blmc.rpm_tmp_old = blmc.rpm_tmp;
|
||||||
|
|
||||||
|
if ((blmc.flags & FLAG_RUN_MASK) == (FLAG_PWM_NORMAL | FLAG_COM_NORMAL)) {
|
||||||
/* too few commutations while running -> do a spinup */
|
/* too few commutations while running -> do a spinup */
|
||||||
if (diff < 0x8 && (blmc.flags & FLAG_RUN_MASK) == (FLAG_PWM_NORMAL | FLAG_COM_NORMAL)) {
|
if (diff < 0x08) {
|
||||||
blmc.flags &= ~(FLAG_RUN_MASK);
|
blmc.flags &= ~(FLAG_RUN_MASK);
|
||||||
blmc.flags |= FLAG_PWM_SPINUP | FLAG_COM_SPINUP;
|
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) */
|
/* set pwm again (adjust current limit) */
|
||||||
setpwm(blmc.pwm);
|
setpwm(blmc.pwm);
|
||||||
|
|
||||||
@ -138,7 +154,7 @@ int main(void)
|
|||||||
|
|
||||||
blmc.flags = 0x00;
|
blmc.flags = 0x00;
|
||||||
if (read_parameters())
|
if (read_parameters())
|
||||||
blmc.flags |= FLAG_INVALIDEEPROM;
|
blmc.flags |= FLAG_INVALID_EEPROM;
|
||||||
|
|
||||||
sei();
|
sei();
|
||||||
|
|
||||||
@ -161,7 +177,7 @@ int main(void)
|
|||||||
|
|
||||||
/* hard errors */
|
/* hard errors */
|
||||||
if (blmc.flags & FLAG_HARDERR_MASK) {
|
if (blmc.flags & FLAG_HARDERR_MASK) {
|
||||||
if (blmc.flags & FLAG_CURRENTLIMIT) {
|
if (blmc.flags & FLAG_CURRENT_LIMIT) {
|
||||||
ledX[0] = LED_OFF;
|
ledX[0] = LED_OFF;
|
||||||
ledX[1] = LED_FAST;
|
ledX[1] = LED_FAST;
|
||||||
|
|
||||||
@ -169,11 +185,11 @@ int main(void)
|
|||||||
ledX[0] = LED_OFF;
|
ledX[0] = LED_OFF;
|
||||||
ledX[1] = LED_SLOW;
|
ledX[1] = LED_SLOW;
|
||||||
|
|
||||||
} else if (blmc.flags & FLAG_SELFTESTFAILED) {
|
} else if (blmc.flags & FLAG_SELFTEST_FAILED) {
|
||||||
ledX[0] = LED_FAST;
|
ledX[0] = LED_FAST;
|
||||||
ledX[1] = LED_FAST;
|
ledX[1] = LED_FAST;
|
||||||
|
|
||||||
} else if (blmc.flags & FLAG_INVALIDEEPROM) {
|
} else if (blmc.flags & FLAG_INVALID_EEPROM) {
|
||||||
ledX[0] = LED_SLOW;
|
ledX[0] = LED_SLOW;
|
||||||
ledX[1] = LED_SLOW;
|
ledX[1] = LED_SLOW;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user