Skip to content

Commit

Permalink
Firmware revision 12 (patch 3)
Browse files Browse the repository at this point in the history
Fix missing active drag brake while coasting
Fix missing UDIS in music playing routine
Unify commutation reset
  • Loading branch information
neoxic committed Nov 25, 2024
1 parent 8458887 commit 7044afc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 42 deletions.
1 change: 1 addition & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ int calcpid(PID *pid, int x, int y);
void checkcfg(void);
int savecfg(void);
int resetcfg(void);
void resetcom(void);
int playmusic(const char *str, int vol);

static inline int min(int a, int b) {return a < b ? a : b;}
Expand Down
34 changes: 12 additions & 22 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "common.h"

#define REVISION 12
#define REVPATCH 2
#define REVPATCH 3

const Cfg cfgdata = {
.id = 0x32ea,
Expand Down Expand Up @@ -333,30 +333,15 @@ static void nextstep(void) {
}

static void laststep(void) {
#ifdef PWM_ENABLE
TIM1_CCMR1 = TIM_CCMR1_OC1M_FORCE_HIGH | TIM_CCMR1_OC2M_FORCE_HIGH;
TIM1_CCMR2 = TIM_CCMR2_OC3M_FORCE_HIGH;
#else
TIM1_CCMR1 = TIM_CCMR1_OC1M_FORCE_LOW | TIM_CCMR1_OC2M_FORCE_LOW;
TIM1_CCMR2 = TIM_CCMR2_OC3M_FORCE_LOW;
#endif
int er = TIM_CCER_CC1NE | TIM_CCER_CC2NE | TIM_CCER_CC3NE;
#ifdef INVERTED_HIGH
er |= TIM_CCER_CC1P | TIM_CCER_CC2P | TIM_CCER_CC3P;
#endif
#ifdef PWM_ENABLE
er |= TIM_CCER_CC1NP | TIM_CCER_CC2NP | TIM_CCER_CC3NP;
#endif
TIM1_CCER = er;
TIM1_EGR = TIM_EGR_COMG;
resetcom();
if (sine && prep) { // Switch over to 6-step
step = (step + 29) / 60 + 1;
if (step > 6) step = 1;
}
sine = 0;
prep = 0;
if (lock) nextstep(); // Active drag brake
else { // Passive drag brake
if (lock) nextstep();
else {
#ifdef PWM_ENABLE
TIM1_CCMR1 = TIM_CCMR1_OC1PE | TIM_CCMR1_OC1M_PWM2 | TIM_CCMR1_OC2PE | TIM_CCMR1_OC2M_PWM2;
TIM1_CCMR2 = TIM_CCMR2_OC3PE | TIM_CCMR2_OC3M_PWM2;
Expand Down Expand Up @@ -628,8 +613,13 @@ void main(void) {
running = 1;
} else { // Neutral
if (braking == 1) braking = 2; // Reverse after braking
if (sync < 6) { // Drag brake
curduty = lock ? min(cfg.duty_drag, 100 - cutback) : cfg.duty_drag * 20; // 60% cutback at 15C above prot_temp
if (lock) { // Active drag brake
curduty = min(cfg.duty_drag, 100 - cutback); // 60% cutback at 15C above prot_temp
running = 0;
goto setduty;
}
if (sync < 6) { // Passive drag brake
curduty = cfg.duty_drag * 20;
running = 0;
goto setduty;
}
Expand Down Expand Up @@ -726,7 +716,7 @@ void main(void) {
TIM1_CCMR1 = m1;
TIM1_CCMR2 = m2;
TIM1_CCER = er;
TIM1_EGR = TIM_EGR_COMG;
TIM1_EGR = TIM_EGR_UG | TIM_EGR_COMG;
step = reverse + 1;
ertm = 600; // 100K ERPM (freq_min/duty_spup/duty_ramp have no effect)
continue;
Expand Down
48 changes: 28 additions & 20 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,25 @@ int resetcfg(void) {
return savecfg();
}

void resetcom(void) {
#ifdef PWM_ENABLE
TIM1_CCMR1 = TIM_CCMR1_OC1M_FORCE_HIGH | TIM_CCMR1_OC2M_FORCE_HIGH;
TIM1_CCMR2 = TIM_CCMR2_OC3M_FORCE_HIGH;
#else
TIM1_CCMR1 = TIM_CCMR1_OC1M_FORCE_LOW | TIM_CCMR1_OC2M_FORCE_LOW;
TIM1_CCMR2 = TIM_CCMR2_OC3M_FORCE_LOW;
#endif
int er = TIM_CCER_CC1NE | TIM_CCER_CC2NE | TIM_CCER_CC3NE;
#ifdef INVERTED_HIGH
er |= TIM_CCER_CC1P | TIM_CCER_CC2P | TIM_CCER_CC3P;
#endif
#ifdef PWM_ENABLE
er |= TIM_CCER_CC1NP | TIM_CCER_CC2NP | TIM_CCER_CC3NP;
#endif
TIM1_CCER = er;
TIM1_EGR = TIM_EGR_UG | TIM_EGR_COMG;
}

int playmusic(const char *str, int vol) {
static const uint16_t arr[] = {30575, 28859, 27240, 25713, 24268, 22906, 21621, 20407, 19261, 18181, 17160, 16196, 15287};
static char flag;
Expand All @@ -412,6 +431,7 @@ int playmusic(const char *str, int vol) {
if (!vol || ertm || flag) return 0;
flag = 1;
vol <<= 1;
resetcom();
#ifdef PWM_ENABLE
TIM1_CCMR1 = TIM_CCMR1_OC1M_FORCE_LOW | TIM_CCMR1_OC2PE | TIM_CCMR1_OC2M_PWM1;
TIM1_CCMR2 = TIM_CCMR2_OC3M_FORCE_LOW;
Expand All @@ -428,22 +448,25 @@ int playmusic(const char *str, int vol) {
er |= TIM_CCER_CC1NP | TIM_CCER_CC2NP | TIM_CCER_CC3NP;
#endif
TIM1_CCER = er;
TIM1_CCR2 = 0; // Preload silence
TIM1_PSC = CLK_MHZ / 8 - 1; // 125ns resolution
for (int a, b, c = 0; (a = *str++);) {
if (a >= 'a' && a <= 'g') a -= 'c', b = 0; // Low note
else if (a >= 'A' && a <= 'G') a -= 'C', b = 1; // High note
else if (a == '_') goto update; // Pause
else {
else if (a == '_') { // Pause
TIM1_CCR2 = 0;
goto update;
} else {
if (a == '+' && !c++) continue; // Octave up
if (a == '-' && c--) continue; // Octave down
break; // Invalid specifier
}
a = (a + 7) % 7 << 1;
if (a > 4) --a;
if (*str == '#') ++a, ++str;
TIM1_CCR2 = vol; // Volume
TIM1_CR1 = TIM_CR1_CEN | TIM_CR1_ARPE | TIM_CR1_UDIS;
TIM1_ARR = arr[a] >> (b + c); // Frequency
TIM1_CCR2 = vol; // Volume
TIM1_CR1 = TIM_CR1_CEN | TIM_CR1_ARPE;
update:
TIM1_EGR = TIM_EGR_UG | TIM_EGR_COMG;
a = strtol(str, &end, 10); // Duration
Expand All @@ -453,23 +476,8 @@ int playmusic(const char *str, int vol) {
str = end;
}
delay(tmp * a);
TIM1_CCR2 = 0; // Preload silence
}
#ifdef PWM_ENABLE
TIM1_CCMR1 = TIM_CCMR1_OC1M_FORCE_HIGH | TIM_CCMR1_OC2M_FORCE_HIGH;
TIM1_CCMR2 = TIM_CCMR2_OC3M_FORCE_HIGH;
#else
TIM1_CCMR1 = TIM_CCMR1_OC1M_FORCE_LOW | TIM_CCMR1_OC2M_FORCE_LOW;
TIM1_CCMR2 = TIM_CCMR2_OC3M_FORCE_LOW;
#endif
er = TIM_CCER_CC1NE | TIM_CCER_CC2NE | TIM_CCER_CC3NE;
#ifdef INVERTED_HIGH
er |= TIM_CCER_CC1P | TIM_CCER_CC2P | TIM_CCER_CC3P;
#endif
#ifdef PWM_ENABLE
er |= TIM_CCER_CC1NP | TIM_CCER_CC2NP | TIM_CCER_CC3NP;
#endif
TIM1_CCER = er;
resetcom();
TIM1_PSC = 0;
TIM1_ARR = CLK_KHZ / 24 - 1;
TIM1_EGR = TIM_EGR_UG | TIM_EGR_COMG;
Expand Down

0 comments on commit 7044afc

Please sign in to comment.