diff --git a/blmc.c b/blmc.c index 9f43880..de2cf81 100644 --- a/blmc.c +++ b/blmc.c @@ -145,14 +145,16 @@ void spinup(void) } /* manual spinup complete, analog comperator takes control */ - blmc.flags = FLAG_PWM_SPINUP | FLAG_COM_NORMAL; + blmc.flags &= ~(FLAG_RUN_MASK); + blmc.flags |= FLAG_PWM_SPINUP | FLAG_COM_NORMAL; next_phase(); for (time = 0; time < params.spinup_wait; time++) _delay_ms(20); /* switch to desired pwm value */ - blmc.flags = FLAG_PWM_NORMAL | FLAG_COM_NORMAL; + blmc.flags &= ~(FLAG_RUN_MASK); + blmc.flags |= FLAG_PWM_NORMAL | FLAG_COM_NORMAL; } /* @@ -164,8 +166,10 @@ void setpwm(uint8_t pwm) /* run motor *only* if there are no hard errors */ if (pwm >= params.pwm_min && !(blmc.flags & FLAG_HARDERR_MASK)) { /* do a spinup */ - if (blmc.pwm == 0) - blmc.flags = FLAG_PWM_SPINUP | FLAG_COM_SPINUP; + if (blmc.pwm == 0) { + blmc.flags &= ~(FLAG_RUN_MASK); + blmc.flags |= FLAG_PWM_SPINUP | FLAG_COM_SPINUP; + } } else { blmc.flags &= ~FLAG_RUN_MASK; diff --git a/i2c-slave.c b/i2c-slave.c index 80d804d..a0560d9 100644 --- a/i2c-slave.c +++ b/i2c-slave.c @@ -22,15 +22,17 @@ #include "main.h" #include "blmc.h" +#include "eeprom.h" extern struct blmc_ blmc; +extern struct ee_param params; #define CMD_WAIT 0x00 #define CMD_GET_INFO 0x10 #define CMD_SET_PWM 0x21 #define CMD_GET_STATUS 0x22 -// #define CMD_SET_PARAM 0x23 -// #define CMD_GET_PARAM 0x24 +#define CMD_SET_PARAM 0x23 +#define CMD_GET_PARAM 0x24 #define CMD_BOOT_LOADER 0x2F const static uint8_t info[16] = "blctrl m8-v1.1"; @@ -58,6 +60,8 @@ ISR(TWI_vect) case CMD_GET_INFO: case CMD_SET_PWM: case CMD_GET_STATUS: + case CMD_SET_PARAM: + case CMD_GET_PARAM: bcnt++; break; @@ -67,12 +71,17 @@ ISR(TWI_vect) break; } - } else if (bcnt == 1) { - if (cmd == CMD_SET_PWM) - setpwm(TWDR); - + } else if (cmd == CMD_SET_PWM) { + setpwm(TWDR); bcnt = 0; + } else if (cmd == CMD_SET_PARAM) { + ((uint8_t *)¶ms)[bcnt++ -1] = TWDR; + if (bcnt > sizeof(params)) { + write_parameters(); + bcnt = 0; + } + } else { bcnt = 0; } @@ -124,6 +133,12 @@ ISR(TWI_vect) } break; + case CMD_GET_PARAM: + TWDR = ((uint8_t *)¶ms)[bcnt++]; + if (bcnt > sizeof(params)) + bcnt = 0; + break; + default: TWDR = 0xFF; break; diff --git a/main.c b/main.c index 9fd8150..b1edd5f 100644 --- a/main.c +++ b/main.c @@ -58,8 +58,10 @@ ISR(TIMER0_OVF_vect) 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_PWM_SPINUP | FLAG_COM_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; + } /* set pwm again (adjust current limit) */ setpwm(blmc.pwm);