aboutsummaryrefslogtreecommitdiff
path: root/spaghetti-monster/fsm-ramping.c
diff options
context:
space:
mode:
authorGabriel Hart2021-08-20 10:06:00 -0500
committerGabriel Hart2021-08-20 10:06:00 -0500
commitf92e07dc18e4eaf9e84f36ef3d476af688f62b5b (patch)
treea2e5860dea9a49d2298c0766d1716d7102f2f947 /spaghetti-monster/fsm-ramping.c
parentfrom Tom E, support PB4 for any of the 3 PWM channels (diff)
parentmerged digitalcircuit's branch to make autolock timer configurable at build time (diff)
downloadanduril-f92e07dc18e4eaf9e84f36ef3d476af688f62b5b.tar.gz
anduril-f92e07dc18e4eaf9e84f36ef3d476af688f62b5b.tar.bz2
anduril-f92e07dc18e4eaf9e84f36ef3d476af688f62b5b.zip
Merge in TK's repo thru revision 613
Diffstat (limited to 'spaghetti-monster/fsm-ramping.c')
-rw-r--r--spaghetti-monster/fsm-ramping.c41
1 files changed, 22 insertions, 19 deletions
diff --git a/spaghetti-monster/fsm-ramping.c b/spaghetti-monster/fsm-ramping.c
index 503f90f..1a08149 100644
--- a/spaghetti-monster/fsm-ramping.c
+++ b/spaghetti-monster/fsm-ramping.c
@@ -153,30 +153,33 @@ void set_level(uint8_t level) {
#endif // ifdef USE_TINT_RAMPING
#ifdef USE_DYN_PWM
+ uint16_t top = PWM_GET(pwm_tops, level);
+ #ifdef PWM1_CNT
+ // wait to ensure compare match won't be missed
+ // (causes visible flickering when missed, because the counter
+ // goes all the way to 65535 before returning)
+ // (see attiny1634 reference manual page 103 for a warning about
+ // the timing of changing the TOP value (section 12.8.4))
+ // to be safe, allow at least 64 cycles to update TOP
+ while(PWM1_CNT > (top - 64)) {}
+ #endif
// pulse frequency modulation, a.k.a. dynamic PWM
- PWM1_TOP = PWM_GET(pwm_tops, level);
+ PWM1_TOP = top;
+
+ // repeat for other channels if necessary
#ifdef PMW2_TOP
- PWM2_TOP = PWM_GET(pwm_tops, level);
+ #ifdef PWM2_CNT
+ while(PWM2_CNT > (top - 64)) {}
+ #endif
+ PWM2_TOP = top;
#endif
#ifdef PMW3_TOP
- PWM3_TOP = PWM_GET(pwm_tops, level);
- #endif
-
- // reset the phase, to avoid random long pulses
- // see attiny1634 reference manual page 103 for a warning about
- // the timing of changing the TOP value (section 12.8.4)
- // (we don't care about being phase-correct, so the brute-force
- // approach can be used to reset it here)
- #ifdef PWM1_CNT
- PWM1_CNT = 0;
- #endif
- #ifdef PWM2_CNT
- PWM2_CNT = 0;
- #endif
- #ifdef PWM3_CNT
- PWM3_CNT = 0;
+ #ifdef PWM3_CNT
+ while(PWM3_CNT > (top - 64)) {}
+ #endif
+ PWM3_TOP = top;
#endif
- #endif
+ #endif // ifdef USE_DYN_PWM
}
#endif // ifdef OVERRIDE_SET_LEVEL
#ifdef USE_DYNAMIC_UNDERCLOCKING