32x: a hack for dealing with pwm inaccuracy
authornotaz <notasas@gmail.com>
Tue, 9 Jul 2013 00:05:02 +0000 (03:05 +0300)
committernotaz <notasas@gmail.com>
Tue, 9 Jul 2013 23:10:47 +0000 (02:10 +0300)
pico/32x/pwm.c

index 3adbf21..e3385aa 100644 (file)
@@ -52,10 +52,15 @@ void p32x_timers_do(unsigned int cycles)
 
   cycles *= 3;
 
-  pwm_cycle_counter += cycles;
-  while (pwm_cycle_counter > pwm_cycles) {
-    pwm_cycle_counter -= pwm_cycles;
-    pwm_smp_expect++;
+  // since we run things in async fashion, allow pwm to lag behind
+  // but don't allow our "queue" to be infinite
+  cnt = pwm_smp_expect - pwm_smp_cnt;
+  if (cnt <= 0 || cnt * pwm_cycles < OSC_NTSC/7*3 / 60 / 2) {
+    pwm_cycle_counter += cycles;
+    while (pwm_cycle_counter > pwm_cycles) {
+      pwm_cycle_counter -= pwm_cycles;
+      pwm_smp_expect++;
+    }
   }
 
   // WDT timers