From 7044afcb5e8fe364bd468b17afeb41ba3173d4b5 Mon Sep 17 00:00:00 2001 From: neoxic Date: Sun, 24 Nov 2024 20:09:39 -0800 Subject: [PATCH] Firmware revision 12 (patch 3) Fix missing active drag brake while coasting Fix missing UDIS in music playing routine Unify commutation reset --- src/common.h | 1 + src/main.c | 34 ++++++++++++---------------------- src/util.c | 48 ++++++++++++++++++++++++++++-------------------- 3 files changed, 41 insertions(+), 42 deletions(-) diff --git a/src/common.h b/src/common.h index 986063e..cbc904e 100644 --- a/src/common.h +++ b/src/common.h @@ -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;} diff --git a/src/main.c b/src/main.c index dcca25b..ad745c5 100644 --- a/src/main.c +++ b/src/main.c @@ -18,7 +18,7 @@ #include "common.h" #define REVISION 12 -#define REVPATCH 2 +#define REVPATCH 3 const Cfg cfgdata = { .id = 0x32ea, @@ -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; @@ -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; } @@ -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; diff --git a/src/util.c b/src/util.c index b9b84b7..fc22d63 100644 --- a/src/util.c +++ b/src/util.c @@ -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; @@ -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; @@ -428,13 +448,14 @@ 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 @@ -442,8 +463,10 @@ int playmusic(const char *str, int vol) { 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 @@ -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;